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

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 kevinenergy, 02-17-2023, 12:42 PM
            115 responses
            2,698 views
            1 like
            Last Post kevinenergy  
            Started by prdecast, Today, 06:07 AM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by Christopher_R, Today, 12:29 AM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by chartchart, 05-19-2021, 04:14 PM
            3 responses
            577 views
            1 like
            Last Post NinjaTrader_Gaby  
            Started by bsbisme, Yesterday, 02:08 PM
            1 response
            15 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Working...
            X