Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Donchian Channel Displacement / Offset

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Donchian Channel Displacement / Offset

    Hello,

    I am trying to create a simple Donchian Channel strategy in order to backtest. The problem I keep running into is that I can't get the channel to displace or offset (move to the right one bar).

    In condition builder I have the following:

    Left side:
    Close
    Bars Ago: 0
    Offset: 0

    Middle:
    CrossAbove
    Look back period: 1

    Right:
    Donchian Channel
    Input Series: DefaultInput
    Period: Period (defaults to 1 but I can change on strategy tester)
    Bars ago: 1 (this is what I thought should displace or offset the Donchian Channels but it does not)
    Offset type: Ticks
    Offset: 0
    Plot on chart: True (this plots the donchian channel with no displacement)

    Any help you can provide will be much appreciated!

    #2
    Hello CheezePuff,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    To calculate on the offset one bar to the right you were correct, use BarsAgo 1. To offset the visual plot you use the Offset value.

    Please let me know if you have any questions.

    Comment


      #3
      Hi Patrick,

      Unfortunately, this is still not producing any results. It is showing the chart with the Donchian Channels lined up with the same day not displaced one over like it needs to be. So, it is initiating 0 trades since my trade criteria is based on the price braking the Donchian Channel.

      Here is the code for it...maybe that will help:

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class BasicDonchian : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int period = 1; // Default setting for Period
      // User defined variables (add any user defined variables below)
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      Add(DonchianChannel(Period));
      Add(DonchianChannel(Period));

      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CrossAbove(Close, DonchianChannel(Period).Upper, 1))
      {
      EnterLong(DefaultQuantity, "");
      }

      // Condition set 2
      if (CrossBelow(Close, DonchianChannel(Period).Lower, 1))
      {
      ExitLong("", "");
      }
      }

      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      public int Period
      {
      get { return period; }
      set { period = Math.Max(1, value); }
      }
      #endregion
      }
      }

      Comment


        #4
        Hello CheezePuff,

        Thank you for your response.

        Can you produce what you would like to see on the chart? Can you send us a screenshot of what you are looking for?

        Comment


          #5
          Hi Patrick,

          I am not sure If I am posting this image right or not so there are a few links below. Sorry, I am not really forum savvy.

          I included a chart with a 1 period Donchian Channel shifted over one bar and arrows for the buy and close bars.

          Please note that this is not my whole strategy but it is an important part. The rest of my strategy is working fine but this is the only part that wont initiate and plot correctly. So in order to make troubleshooting easier I tried again to only program this portion of my strategy with no luck.

          Thumbnail:



          Link:



          Hotlink:


          upload pic
          Last edited by CheezePuff; 02-10-2016, 12:29 PM.

          Comment


            #6
            Hello CheezePuff,

            Thank you for your patience.

            You would use the following in OnBarUpdate():
            Code:
            			// Condition set 1
            			double priorUp = DonchianChannel(Period).Upper[1];
            			if (CrossAbove(Close, priorUp, 1))
            			{
            				EnterLong(DefaultQuantity, "");
            			}
            
            			// Condition set 2
            			double priorLw = DonchianChannel(Period).Lower[1];
            			if (CrossBelow(Close, priorLw, 1))
            			{
            				ExitLong("", "");
            			}

            Comment


              #7
              Hi Patrick,

              Sorry to take up so much of your time, but this is still not working. I pasted the new code into the strategy and it is still providing the same results when tested (no buys and the Donchian Channel is not shifted over on the chart). Here is the new code so maybe I pasted it wrong...I don't know any coding at all:

              #region Using declarations
              using System;
              using System.ComponentModel;
              using System.Diagnostics;
              using System.Drawing;
              using System.Drawing.Drawing2D;
              using System.Xml.Serialization;
              using NinjaTrader.Cbi;
              using NinjaTrader.Data;
              using NinjaTrader.Indicator;
              using NinjaTrader.Gui.Chart;
              using NinjaTrader.Strategy;
              #endregion

              // This namespace holds all strategies and is required. Do not change it.
              namespace NinjaTrader.Strategy
              {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class BasicDonchian : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int period = 1; // Default setting for Period
              // User defined variables (add any user defined variables below)
              #endregion

              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
              Add(DonchianChannel(Period));
              Add(DonchianChannel(Period));

              CalculateOnBarClose = true;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              // Condition set 1
              double priorUp = DonchianChannel(Period).Upper[1];
              if (CrossAbove(Close, priorUp, 1))
              {
              EnterLong(DefaultQuantity, "");
              }

              // Condition set 2
              double priorLw = DonchianChannel(Period).Lower[1];
              if (CrossBelow(Close, priorLw, 1))
              {
              ExitLong("", "");
              }
              }

              #region Properties
              [Description("")]
              [GridCategory("Parameters")]
              public int Period
              {
              get { return period; }
              set { period = Math.Max(1, value); }
              }
              #endregion
              }
              }

              Comment


                #8
                Hello CheezePuff,

                Thank you for your response.

                Can you provide a screenshot of the results you see and what needs to occur?

                Comment


                  #9
                  Ok, so you're wanting to test the Close against the DC high or low of the bar before it?

                  As Patrick suggested,
                  The '1' in code below retrieves the DC high/low of the bar previous to the last closed bar:

                  Code:
                  double priorUp = DonchianChannel(Period).Upper[[B][COLOR=Red]1[/COLOR][/B]];
                  double priorLw = DonchianChannel(Period).Lower[[COLOR=Red][B]1[/B][/COLOR]];
                  (If you wanted the DC high/low of the same bar as the closed bar, you'd use 0 instead of 1.)

                  Out of curiosity, I copied & pasted the code into a file and compiled it.

                  I backtested using ES on a 5 Minute bar series. The strategy took a lot of trades.

                  It appears to be doing what you say you want.

                  See attached.
                  Attached Files

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DayTradingDEMON, Today, 09:28 AM
                  3 responses
                  19 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by Stanfillirenfro, Today, 07:23 AM
                  9 responses
                  23 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by George21, Today, 10:07 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post George21  
                  Started by navyguy06, Today, 09:28 AM
                  1 response
                  7 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by cmtjoancolmenero, Yesterday, 03:58 PM
                  8 responses
                  32 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Working...
                  X