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

IsFirstBarOfSession Broken for Historical Bars

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

    IsFirstBarOfSession Broken for Historical Bars

    I am using Bars.IsFirstBarOfSession to detect when the opening bar when processing in OnBarUpdate. When used on a one minute chart I discovered that IsFirstBarOfSession is not triggering for for historical data more than 2 days older then the current trading day.

    I have attached a sample indicator that illustrates the problem. To reproduce.
    1. Install the attached indicator.
    2. Create a 1 minute chart of AAPL.
    3. Set the # of days back to be 20 (anything more then 2).
    4. Connect to a data source, like Kinetick.
    5. Notice that the very first bar has IsFirstBarOfSession set to true. This is expected.
    6. Notice that only the past 2 days (if the current day is Tuesday through Friday) have a bar set to 1 on the first bar of the day at 9:31. Anything older than this and the IsFirstBarOfSession is being set to 0.
    The indicator drops out the information to the output window as well.

    This seems like it was something that used to work? I recall it working in previous versions.

    NinjaTrader Version 8.0.26
    Attached Files
    Last edited by ntbone; 06-15-2022, 11:01 PM.

    #2
    Hello ntbone,

    I tried this on my end and it seems to be working. I see that your sample has a date condition around the print, try removing that to simplify the problem further.

    Code:
    protected override void OnBarUpdate()
    {
        if(Bars.IsFirstBarOfSession)
            Print(Time[0] + " " + Bars.IsFirstBarOfSession);
    }


    5/26/2022 4:01:00 PM True
    5/29/2022 4:01:00 PM True
    5/30/2022 4:01:00 PM True
    6/8/2022 12:01:00 AM True
    6/8/2022 4:01:00 PM True
    6/9/2022 4:01:00 PM True
    6/12/2022 4:01:00 PM True
    6/13/2022 4:01:00 PM True
    6/14/2022 4:01:00 PM True
    6/15/2022 4:01:00 PM True
    JesseNinjaTrader Customer Service

    Comment


      #3
      The code I wrote was specifically to verify whether IsFirstBarOfSession is working. Bars.IsFirstBarOfSession was reporting false when it should report true. This caused any code that checks Bars.IsFirstBarOfSession to not run.

      Code:
      protected override void OnBarUpdate()
      {
          if(Bars.IsFirstBarOfSession)
          {
              IsFirstBarOfSession[0] = 1;
          }
          else
          {
              IsFirstBarOfSession[0] = 0;
          }
      
          var date = Time[0].Date;
      
          // Without this line the code below would run for every one minute bar in the chart making it harder to see the
          // places where IsFirstBarOfSession is false but should be true.
          if(date != currentDate)
          {
              Print(string.Format("CurrentBar = {0}, IsFirstBarOfSession = {1}, Time = {2}", CurrentBar, Bars.IsFirstBarOfSession, Time[0]));
              currentDate = date;
          }
      }
      I have run the code today on my system and now I am unable to reproduce the problem. Prior to this post it was reproducing consistently.
      Last edited by ntbone; 06-16-2022, 06:23 PM.

      Comment


        #4
        Originally posted by ntbone View Post
        I have run the code today on my system and now I am unable to reproduce the problem. Prior to this post it was reproducing consistently.
        Any holidays in those historical days?

        Perhaps the non-traditional trading hours on
        such days is affecting what you see?

        Comment


          #5
          No. At the time the bug was reproducing it was clearly doing the first bar of the range picked, and then last 2 trading days. I adjusted the range, tried several symbols and even restarted NinjaTrader at the time. It apparently only seemed an issue when it was connected to Kinetick. Prior to connecting it seemed to be working properly.

          I have updated the code that initially reported me the problem. It now uses the date to track a new session and if it finds the date changes but the IsFirstBarInSession is not true it will log so I can keep an eye on the issue.

          I thought that maybe clearing the cache, or the market data in the db folder would correct things but when I restored the original folders it still would not reproduce.

          Comment


            #6
            Hello ntbone,

            Please try to avoid making a complex test if you are trying to check if a bool property is working or not, you just need to print that one property to test that:

            Code:
            protected override void OnBarUpdate()
            {
                if(Bars.IsFirstBarOfSession)  
                    Print(Time[0] + " " + Bars.IsFirstBarOfSession);
            }
            If you want to explore the sample you provided then you need to add more prints to check how your outer condition is evaluating in that use case:

            Code:
            Print(date + " " + currentDate); 
            if(date != currentDate)
            If you can see that the IsFirstBarOfSession is not working correctly and you have removed all your extra logic then we could use the steps + that sample to make a bug report. To submit a report we need to be able to recreate the problem which entails that you first remove any extra code which may be at fault.



            JesseNinjaTrader Customer Service

            Comment


              #7
              The code I wrote is the simplest code that tests IsFirstBarOfSession. It uses the date to determine when the value should be true, and then prints out the value at that time. If I wanted to go one step further I would make it only print out the value when isFirstBarOfSession is wrong. AAPL should always have IsFirstBarSession == true on the first bar of a new day.

              The code you proposed is a debug dump of IsFirstBarOfSession for every bar on the chart. The code isn't testing anything. The tester needs to manually scroll through, determine when the value should be true and find those moments among 100's of print statements.

              As a software engineer I will provide very clear and explicit reproduction steps with the simplest code that reproduces the issue.

              Comment


                #8
                Hello ntbone,

                The code I wrote is the simplest code that tests IsFirstBarOfSession. It uses the date to determine when the value should be true, and then prints out the value at that time. If I wanted to go one step further I would make it only print out the value when isFirstBarOfSession is wrong. AAPL should always have IsFirstBarSession == true on the first bar of a new day.
                Using the date is not a requirement to check the IsFirstBarOfSession property and just complicates the report. We would have to remove that before making a bug report because its not relevant to the question: "IsFirstBarOfSession Broken for Historical Bars".

                The code you proposed is a debug dump of IsFirstBarOfSession for every bar on the chart. The code isn't testing anything. The tester needs to manually scroll through, determine when the value should be true and find those moments among 100's of print statements.
                That does not print for every bar, it uses "IsFirstBarOfSession" to limit prints and is a very simple way of observing a correct output from that bool. That is exactly the kind of way to test a problem that a bug report would expect, a clear and simple print that easily shows the issue. There are no other conditions or unrelated logic required.

                The code at the bottom of this post is a sample that we can use for a report if it shows a problem, you just need to try it and observe the problem using only that print. If you see a problem then we just need the specific steps you followed to test that.

                If you are having problem with the date part of your logic please make a new post with that as the question instead of reporting a bug with a NinjaScript property.


                Code:
                protected override void OnBarUpdate()
                {
                    if(Bars.IsFirstBarOfSession)
                        Print(Time[0] + " " + Bars.IsFirstBarOfSession);
                }
                JesseNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by reynoldsn, Today, 07:23 AM
                4 responses
                9 views
                1 like
                Last Post reynoldsn  
                Started by bee1943, Today, 09:55 AM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by FAQtrader, 04-25-2024, 12:00 PM
                7 responses
                101 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by giulyko00, Yesterday, 11:49 AM
                4 responses
                28 views
                0 likes
                Last Post giulyko00  
                Started by Sebastian - TwinPeaks, Yesterday, 01:31 PM
                4 responses
                26 views
                0 likes
                Last Post Sebastian - TwinPeaks  
                Working...
                X