Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problems with testing for DonchianChannel breakouts

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

    Problems with testing for DonchianChannel breakouts

    Hi,

    I am wrestling with this problem now since a few days and feel stuck ...

    I don't quite understand how the DonchianChannel Indicator works for tick based strategies (even so I understand the simple code in the DonchianChannel.cs script!). It is not clear to me which OnBarUpdate method is called first: the one of the Indiciatorsadded toa Strategyvia Add()or theone of Strategy itself - presuming one has CalculateOnBarClose= false in the Strategy's Initialize() method?

    For the DonchianChannel indicator (which by definition is NEVER crossed by the price)it makes a BIG difference which one is called first when one wants to trigger aDonchianChannel Breakoutsignal based on

    1) Bars.CurrentBid > DonchianChannel(20).Upper[0] or

    2) Bars.CurrentBid > DonchianChannel(20).Upper[1].

    Neither one seems to work as expected.

    The case 1) would work if the current bid (= Close[0] in historical backtesting) could WITHIN the current (unfinished) bar temporarily for the duration of one tick exceed the DonchianChannel(20).Upper[0]. This could be the case if the OnBarUpdate() method of the DonchianChannel indicator would be calledAFTER the one of the strategy oronly when the current bar is finished (either by time or # of ticks or volume). In other words, that would work if the Strategy would be updated at every tick and the DonchainChannel at the end of every finished bar.

    But several tests showed me that 1) NEVER triggers an Entry order. So the next best thing I can do is 2), but that triggers an Entry orderTWO bars later than expected!

    For greater clarity, I attached 2 files (in .zip) to explain this problem graphically- a screenshot of one of my test results andthe producing test script done with the Wizzard implementing 2) with either a CrossAbove() call (which doesn't trigger at all) or directly with the Bars.CurrentBid > DonchianChannel(20).Upper[1] condition.

    No matter what I try, I can't ever trigger a signal like: Buy-at-Market when current bid or last pricepushesthe upper DonchianChannelup byat least one tick (== classic definition of aDonchian channel breakout).

    It is clear to me (having programmed Donchian breakouts many times beforeon other platforms) that theMIN/MAX nature of Donchian is somewhat peculiar and great attention has to bepaidtoWHENEXACTLY the Channel and the bid/priceticks are updated. But in NT6beta11 I can't figure out how to do this correctly.

    Any help or advice is deeply appreciated. TIA.


    PS: I read and understood the previous relatedpost http://ninjatrader.mywowbb.com/forum16/1828.html but my problemseems different.
    Attached Files

    #2
    imported post

    Hi plipa,

    I think I boil this down very easy...

    Your code and image make sense to me.

    You enter on signal LongEntry1 at the correct bar. Your code says, if bid > upper DC 1 bar ago. Historically, bid/ask are subsituted with the close of the bar. At the end of the bar that you want to have been triggered, the close is < than DC. The next bar the the condion is true and therefore you enter the following bar.

    You can't use a CrossAbove() since this will only check for the condition on the current bar, not 1 bar ago.

    On historical data, bid/ask will always == Close[0]. In real-time, its the real bid/ask data. If you run with CalculateOnBarClose = false, you would have entered on the bar that you wanted to.

    Ray
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      Thanks Ray, but I DO run with CalculateOnBarClose = false (see the script fragment below). And I think I understood the substituion Bars.CurrentBid == Close[0] on historical data. With CalculateOnBarClose = false the Close[0] is updated at every tick, right? Thats what I am doing (and several variations thereof with Close[0] etc):



      protectedoverridevoid Initialize()

      {

      Add(DonchianChannel(DonchianPeriod));

      SetTrailStop(
      "", CalculationMode.Ticks, 20, false);

      CalculateOnBarClose =
      false;

      }



      protectedoverridevoid OnBarUpdate()

      {


      // Condition set 3

      if (Bars.CurrentBid > DonchianChannel(DonchianPeriod).Upper[1])

      { EnterLong(DefaultQuantity,
      "LongEntry1"); }

      }

      Here are my questions:

      1) So far I have not found ANY way to triggera signal at the desired blue point I marked in the chart. Could you give me a codeexample?

      2) Can I ever trigger an order INSIDE a bar (with CalculateOnBarClose = false, of course) or are orders alwayssubmitted at the open of a new bar, no matter what? I mean, is an EntryLong() call converted into live orders at the tick it was triggered or are the live orders delayed till the end of the bar?

      3) Also, you didn't tell me which OnBarUpdate method is called first: the Strategy'w or the Indicators? My guess is, after some experimentations, that itis the latter - right?

      Thanks again.




      Comment


        #4
        imported post

        plipa wrote:
        Here are my questions:

        1) So far I have not found ANY way to triggera signal at the desired blue point I marked in the chart. Could you give me a codeexample?

        2) Can I ever trigger an order INSIDE a bar (with CalculateOnBarClose = false, of course) or are orders alwayssubmitted at the open of a new bar, no matter what? I mean, is an EntryLong() call converted into live orders at the tick it was triggered or are the live orders delayed till the end of the bar?

        3) Also, you didn't tell me which OnBarUpdate method is called first: the Strategy'w or the Indicators? My guess is, after some experimentations, that itis the latter - right?

        Thanks again.

        1) Your code will trigger where you want, in real-time only. Historically, that will never happen since historical calculations are always on bar close since we only know of the OHLC at the close of bar.

        2) At the tick it was triggered when running in real-time and CalculateOnBarClose = false;

        3) Indicators are always going to be updated first so when they are called from the strategy you are referencing the correct data.

        Ray

        RayNinjaTrader Customer Service

        Comment


          #5
          imported post

          Thanks, Ray. That explains a lot andnow I start to understand how historical backtesting works.

          MEMO to myself and other newbies here:

          "CalculateOnBarClose = false;" is effective ONLY in real-time (live) mode. In historical backtesting (even whenbased on tick data!) the Strategy's OnBarUpdate() is called always at the Close of a completed bar and AFTER all the Indicators received their OnBarUpdate() calls. Presumably, "CalculateOnBarClose = false;" might also be effective in MarketReplay mode (to be tested)?

          Would you mind addinga Tip (2.)likethat to theCalculateOnBarClose documention in the Ninjascript Language Reference?

          Many thanks,

          Peter





          Comment


            #6
            imported post

            Yes, will work in replay since this behaves as real-time. Yes, will ammend the DOC, thanks for pointing this out.

            Ray
            RayNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            597 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            343 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            103 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            556 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            555 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X