Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Seconds between bars calculation difference

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

    Seconds between bars calculation difference

    Hi NT support,

    Attached is an indicator that calculates seconds per bar. It does it using 2 methods:
    1. Using a secondary (PeriodType.Second,1) period and a counter which is reset in the primary period
    2. Calculating total seconds between Time[0] and Time[1] bars.

    I expect seconds count in both methods to be at least close if not equal but instead there are huge differences (I tested on a sim connection). Could you explain why the seconds count in method 1 would be so off?

    Thanks,
    Tim
    Attached Files

    #2
    Hello Tim,

    Your logic seems to be correct. A second bar will only generate after a Last Price is received from the Broker which, does makes sense during the Market that it would be grater and outside of Market Hours it would be less.

    You may open up a 1 second chart and verify this.
    JCNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_JC View Post
      Hello Tim,

      Your logic seems to be correct. A second bar will only generate after a Last Price is received from the Broker which, does makes sense during the Market that it would be grater and outside of Market Hours it would be less.

      You may open up a 1 second chart and verify this.
      I changed the code to account for that:
      Code:
      	if(BarsInProgress!=0){
      		valup+=(int)(Time[0].Subtract(Time[1]).TotalSeconds);
      		return;
      	}
      The second count is now much more consistent, however it's not perfect and there are areas where there is a visible difference. Please see screenshot.
      Attached Files

      Comment


        #4
        Hello nailz420,

        I am not sure I know exactly what you are looking for based on your changes that you have made. Could you may be clarify what you are looking for Plot0 to be?

        Are you looking for the number of seconds in the 3 Minute bar or are you looking when the last second bar is generated before another 3 Minute bar appears?
        JCNinjaTrader Customer Service

        Comment


          #5
          This indicator is meant to be used on non-time based period types, such as range. I am looking for the number of seconds in a bar and with this test I am trying to show and understand why there is a discrepancy in the results.

          Comment


            #6
            Hello nailz420,

            Thanks for the clarification. If that is the case you should not need to add a secondary Data Series as this will complicate your indicator. You can just use the current Time minus the previous Bars Time for this.

            For example.
            protected override void Initialize()
            {
            Add(new Plot(Color.FromKnownColor(KnownColor.Blue), PlotStyle.Bar, "Plot0"));
            CalculateOnBarClose = true;
            Overlay = false;
            }

            protected override void OnBarUpdate()
            {
            if(CurrentBar < 1) return;

            Plot0.Set(Time[0].Subtract(Time[1]).TotalSeconds);
            }
            JCNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by CaptainJack, 05-29-2026, 05:09 AM
            0 responses
            172 views
            0 likes
            Last Post CaptainJack  
            Started by CaptainJack, 05-29-2026, 12:02 AM
            0 responses
            88 views
            0 likes
            Last Post CaptainJack  
            Started by charlesugo_1, 05-26-2026, 05:03 PM
            0 responses
            129 views
            0 likes
            Last Post charlesugo_1  
            Started by DannyP96, 05-18-2026, 02:38 PM
            1 response
            208 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 05-11-2026, 05:56 AM
            0 responses
            185 views
            0 likes
            Last Post CarlTrading  
            Working...
            X