Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars.IsFirstBarOfSession is returning Last bar of session, not first

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

    Bars.IsFirstBarOfSession is returning Last bar of session, not first

    Hello,

    Below is a snippet of the piece of code in question:

    Code:
    OnStateChange()
    {
    Calculate = Calculate.OnBarClose
    }
    
    OnBarUpdate()
    {
         if (CurrentBars[0] < 1)
           return
    
         if (Bars.IsFirstBarOfSession)
         {
             Print (string.Format("{0}-, {1}", Times[0][0], Bars.BarsSinceNewTradingDay));
         }
     }
    This code is running on a 30 minute time frame.

    When I run it in a backtest, the print returns twice, say I run it from 1/03/2023 - 1/05/2023, the prints will read:

    1/3/2023 4:00:00 PM-0
    1/3/2023 5:30:00 PM-22
    1/4/2023 4:00:00 PM-0
    1/4/2023 5:30:00 PM-22
    1/5/2023 4:00:00 PM-0
    1/5/2023 5:30:00 PM-22

    it will print twice, once when the market closes at 4:00 pm and then when it opens at 5:30 pm. Why would it be printing on the last bar of the session as 0 and printing "22" on the real first bar of the session?

    #2
    Hello Don22Trader1,

    Thank you for your post.

    So I may accurately assist you, please answer all of the following questions:
    • What version of NinjaTrader are you using? Please provide the entire version number. This can be found under Help -> About (Example: 8.?.?.?)
    • Who are you connected to? This is displayed in green on the lower-left corner of the Control Center window.
    • Who is your broker?
    • What instrument symbol (and expiry if applicable) have you selected? For example, ES 03-22, AAPL, EURUSD, etc.
    • What Data Series Type have you selected? Example: Tick, Minute, Day
      • You mentioned 30 minute, correct?
    • Is your strategy a multi-instrument or multi-time frame strategy?
    • Do you receive an error on the screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?
    I look forward to your reply.

    Comment


      #3
      Thanks for responding NinjaTrader_Emily,

      Version of NT: 8.0.27.1 64-bit
      Connection: Rithmic for NinjaTrader Brokerage and NinjaTrader Continuum. Both connections produce the same results in the strategy's prints.
      Broker: Ninjatrader
      Instrument Symbol: MNQ 06-23
      Data Series: 30 Minute
      Multi Instrument & Time Frame: Not Multi Instrument, I have added an additional Data Series (1 Tick) but I have not used it anywhere in my script yet.
      Errors: no errors on screen or in the log were found.
      Last edited by Don22Trader1; 04-13-2023, 09:44 PM.

      Comment


        #4
        NinjaTrader_Emily,

        I managed to stop the print from printing twice, it now only prints on the first bar of the session which is the 5:30 pm bar. However, the Bars.BarsSinceNewTrading Day are still printing wrong. Attached below is the output window that shows the prints. Sometimes it prints correctly as 0 but most of the time it prints numbers such as "22", "14", & "8". I am not sure as to why it prints correctly sometimes and wrong in others.
        Attached Files

        Comment


          #5
          Hello Don22Trader1,

          Thank you for your reply.

          I suggest printing additional diagnostic information, such as the BarsInProgress and the CurrentBar index so you can understand which series is producing which value and which bar from that series is being considered as the first bar of the session. The results of the output should help to better understand what is being printed and why. You could also consider only evaluating the condition for the primary data series by adding if (BarsInProgress ==0) to your condition. For additional information regarding working with multi-instrument and multi-time frame scripts, please see the following overview page:


          Please let me know if I may be of further assistance.

          Comment


            #6
            NinjaTrader_Emily,

            Problem solved,

            I followed your advice by adding more prints to check what was going on and everything seemed to be working fine but the Bars.IsFirstBarOfSession print was still coming out wrong! I decided to turn off the computer for the night. I woke up this morning to test the script again without tampering with the code and all of a sudden the Bars.IsFirstBarOfSession Print is now coming out as it should by showing "0".

            I guess Ninjatrader just needed to be restarted. I did just recently update to NT's newest version.

            Anyways, thanks for the help as always.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            43 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            48 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X