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

Bars.BarsSinceNewTradingDay failed on live strategy

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

    Bars.BarsSinceNewTradingDay failed on live strategy

    I ran a strategy live all last year on NT 8.0.27.1 and never had the following problem: This code executed a buy for YM on the market open at 8:30 (Template US Equitities RTH), when the following code should have made it impossible (just showing relevant variables):

    TimBars is set to 60 as a parameter

    protected override void OnBarUpdate()
    {
    if (Bars.BarsSinceNewTradingDay < TimBars)
    {
    clip = 0;
    }
    else if (Bars.BarsSinceNewTradingDay == TimBars)
    {
    clip = 1;
    }
    else { // Code should not have reached here until 9:30, and clip should have been reset at the end of the first bar at 8:31
    if (clip > 1e-10)
    EnterLong(1);
    }
    return;
    ​}
    Is there a bug in the return of Bars.BarsSinceNewTradingDay when the template starts? It looks like it thought it was still the end of the previous day...
    The strategy ran correctly the day before, did something not get cleared? E.g. could the first OnBarUpdate of the session not have reset BarsSinceNewTradingDay yet? The strategy automatically closes at the end of the day - could that screw up resetting the time?
    There is nothing in the trace log prior to the order submission that would indicate anything amiss.
    If NT thinks the opening of the first bar of the day it should execute the last bar of the previous day, this would explain buggy behavior I had seen in live execution of the SampleMACrossOver years ago, but was supposedly fixed.

    #2
    [Originally in Strategy development, but received no reply, so I am trying here.]

    I ran a strategy live all last year on NT 8.0.27.1 and never had the following problem: This code executed a buy for YM on the market open at 8:30 (Template US Equitities RTH), when the following code should have made it impossible (just showing relevant variables):

    TimBars is set to 60 as a parameter

    protected override void OnBarUpdate()
    {
    if (Bars.BarsSinceNewTradingDay < TimBars)
    {
    clip = 0;
    }
    else if (Bars.BarsSinceNewTradingDay == TimBars)
    { /// Logic occurs, but outcome previous day was
    clip = 1;
    }
    else { // Code should not have reached here until 9:30, and clip should have been reset at the end of the first bar at 8:31
    if (clip > 1e-10)
    EnterLong(1);
    }
    return;
    ​}
    Is there a bug in the return of Bars.BarsSinceNewTradingDay when the template starts? It looks like it thought it was still the end of the previous day...
    The strategy ran correctly the day before, did something not get cleared? E.g. could the first OnBarUpdate of the session not have reset BarsSinceNewTradingDay yet? The strategy automatically closes at the end of the day - could that screw up resetting the time?
    There is nothing in the trace log prior to the order submission that would indicate anything amiss.
    If NT thinks the opening of the first bar of the day it should execute the last bar of the previous day, this would explain buggy behavior I had seen in live execution of the SampleMACrossOver years ago, but was supposedly fixed.


    Comment


      #3
      Hello zstheorist,

      Apologies for the delay.

      Above (and outside) of the conditions print the time of the bar and print Bars.IsFirstBarOfSession and also print Bars.BarsSinceNewTradingDay.

      Below is a link to a forum post on using prints to understand behavior and includes videos.


      Save the output to a text file (right-click the output window > select Save as) and attach the txt file to your next post.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        I had anticipated your suggestion, and so made a test strategy that did that before the open this morning. Both IsFirstBarOfSession and BarsSinceNewTradingDay executed today at 8:31 (close of the 8:30 bar) with the correct counting.

        So in isolation, nothing happened, whereas before something executed at 8:30. Here is the original trace:
        2024-01-19 08:30:04:107 (InteractiveBrokers) Cbi.Account.CreateOrder: orderId='<removed>' account='<removed>' name='Buy' orderState=Initialized instrument='YM 03-24' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=2 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=xxxx time='2024-01-19 08:30:04' gtd='2099-12-01' statementDate='2024-01-19' id=-1 comment=''

        What is different was that my original strategy had executed an order the day before that was auto-closed 30 sec before the end of the day, and at 8:30 the variable allowing for execution had not been updated. But it should have been caught by the BarsSinceNewTradingDay < 60 line. i.e. clip = 1 at 8:30 - it is not set to 0 until the end of the bar, and the buy executed, but the "else" with the EnterLong statement should have never been reached until 9:32 (end of 9:31 bar). I just can't figure out how anything could have run at 8:30 when the strategy runs on bar close.

        In order to test the exact conditions that I would have to reproduce the circumstances that led to that order. Hmm. Let me think how I can force a fake buy historically and try again. History tells me this may not be enough - several years ago (NT 6.5/7?) I experienced a similar error that only appeared when an actual trade had taken place. We never could find the source, so I just kept adding guards until I avoided it. For now I guess I can try the same thing.

        Comment


          #5
          Hello zstheorist,

          We need more information, and you would need to reproduce the behavior.

          The order is being submitted from a condition's action block correct?

          Print the time of the bar and all values in the condition along with labels for each value and comparison operator.
          Enable TraceOrders.
          Save the output to a text file and attach this to your next post.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            OK, Bars are definitely mis-numbered in the live scenario - but only when a trade should have occurred the day before.
            I ran the strategy again today and it executed at 8:30 again on YM with the NYSE template, despite adding some debug code and extra checks. I wrote out BarsSInceSession and it returned 389 - that is the last bar of yesterday. The difference between this run and my earlier test is that the strategy should also have traded yesterday and been closed. So it matters whether a trade should have executed yesterday.
            New code:
            if (Bars.IsFirstBarOfSession || Bars.BarsSinceNewTradingDay < TimBars)
            {
            clip = 0
            }
            else if (Bars.BarsSinceNewTradingDay == TimBars)
            {​ // if logic then
            clip = 1
            // This would have been set yesterday to 1
            }
            else
            {
            if (clip> 1e-7 && !Bars.IsFirstBarOfSession && ToTime(Time[0]) > 83000)
            {
            Print("Going with: " + Bars.BarsSinceNewTradingDay);
            EnterLong(1);
            }
            So several things failed here, but it is clear there is a bug in NT that it is executing at 8:30 the closing bar of the previous day. That means it officially closing that bar at 8:30 the next morning.
            Clearly at the 8:30 open it thinks FirstBarInSession is false, BarsSinceSession is that last bar, and ToTime(Time[0]) must be reading yesterday's bar because it should not be > 83000.
            In the past (NT 6.5/7?) there was a bug where if close on exit was checked you got different behavior live the next day when a trade had been closed out or not that did not show up in backtests - could that be influencing this?

            Regardless, how do I stop things running until after 8:30 of the current day? There must be some kind of guard I could add to force it.
            Last edited by zstheorist; 01-30-2024, 08:58 AM.

            Comment


              #7
              Hello zstheorist,

              From my understanding the last bar doesn't close until there is a tick of the next bar to open a new bar, which could be the first bar of a new session.

              The issue is that the last bar of the previous session is closing when the new session opens? (Is the print of the bar time showing the time of the last bar from the previous session?)
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Yes it is returning the last bar of the previous day at 8:30.
                This is partially consistent behavior in sense that if you try to trade at 15:00:00 it will execute on the next open in backtests, but that is not quite what is happening - it is not just waiting to execute, but not even running the logic for the last bar of the previous day until the new open.
                That leaves my question: How do I ensure I do not trade at the open today, when what opened the trade was a condition that was only valid at the end of the day before, but is not today?
                How can I be sure I will stop it trading at 8:30, but force it to wait until the end of the bar (8:31) to consider the logic for the current day?

                Comment


                  #9
                  Hello zstheorist,

                  While you wouldn't be able to prevent the last bar of a session from updating OnBarUpdate(), you could check if the time of the bar is not at the end of the session to proceed with the logic.

                  A sessionIterator will provide the session start and end time.


                  The PreventEntryAfterExitOnCloseExample example linked below demonstrates using a sessionIterator.
                  NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Just before I saw your message I thought of trying IsLastBarOfSession. I see that worked for someone else in the thread you mentioned as well. I will give it a try the next time a trade triggered the day before.
                    I've looked at all SessionIterator functions, but they consistently return a result form the previous day, so they give no more info than the other Bars functions I am using. The only other thought I had that might work is to compare Time[0] vs system time within some threshold.

                    Comment


                      #11
                      Hello zstheorist,

                      The sessionIterator.GetNextSession() gets the information of the session from the date supplied to the method. If the date and time are within the current session, the SessionIterator will be for the current session.


                      Below is a link to an example that uses a sessionIterator.
                      NinjaTrader Community, A common inquiry is that the Exit on close didn't work in a NinjaScript Strategy because there is a position after the exit on close should have occurred. When viewing the log we often find that the Exit on close does indeed exit the position shortly before the end of the session (based on the Exit on
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Haiasi, 04-25-2024, 06:53 PM
                      2 responses
                      17 views
                      0 likes
                      Last Post Massinisa  
                      Started by Creamers, Today, 05:32 AM
                      0 responses
                      5 views
                      0 likes
                      Last Post Creamers  
                      Started by Segwin, 05-07-2018, 02:15 PM
                      12 responses
                      1,786 views
                      0 likes
                      Last Post Leafcutter  
                      Started by poplagelu, Today, 05:00 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post poplagelu  
                      Started by fx.practic, 10-15-2013, 12:53 AM
                      5 responses
                      5,408 views
                      0 likes
                      Last Post Bidder
                      by Bidder
                       
                      Working...
                      X