Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to calculate time between two bars

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

    How to calculate time between two bars

    I am trying to write a strategy to compare the closing time between two bars. Say for instance bar 1 closes at 10:00:00, and bar two closes at 10:00:5, and I want to draw a white arrow under bar 0 if it closed in 5 seconds or less than the previous bar. Is this the best way to calculate it? I created a timespan data type, and it seems that it can only be set to a bar's time by using the NEW command and then assigning it the total ticks of the bar.

    Why can't Time[0] or Time[1] have a function like .TotalMilliseconds?

    Code:
     protected override void Initialize()
            {
               CalculateOnBarClose    = true;
            }
    protected override void OnBarUpdate()    // To be used on 233 tick chart
            {
                if(FirstTickOfBar)
                {
                    TimeSpan0 = new TimeSpan(Time[0].Ticks);
                    TimeSpan1 = new TimeSpan(Time[1].Ticks);
                    
                }
            
               
                 
                if( TimeSpan0.TotalMilliseconds - TimeSpan1.TotalMilliseconds >= 2000)
                    {
                           //Do stuff here    // Drawarrowup....
                    }
    Last edited by dennho; 04-11-2011, 09:28 AM.

    #2
    Hello denho,

    There is a similar indicator available on our forums. I would take a look at this to see if it has what you're looking for.

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I will take a look at that.

      In the mean time, I have an issue w/ the following code that I posted. In a backtest it works, however in forward testing with live data, it is drawing a white arrow on every bar, even if one bar closes 30 seconds the last bar. I set it to only draw a white arrow if the bar closes in 2 seconds or less. Why does it only work for historical data?
      Last edited by dennho; 04-11-2011, 09:06 AM.

      Comment


        #4
        I'm sorry, but this indicator you linked to me is nothing that I need.


        My indicator only works on historical data, but in any forward testing, the white arrows draw on every arrow, regardless of time.
        Last edited by dennho; 04-11-2011, 09:20 AM.

        Comment


          #5
          This is more C# related and unfortunately outside our scope of support. We're willing to help where we can but most of this will be on your end to implement and debug.

          You may want to instead create a time span object based on the difference between two date time objects with date time subtract method.

          NinjaTrader uses the standard C# structure for all the date / time. You can check into the Date / Time methods available on the MSDN site to see all that is available.

          Represents an instant in time, typically expressed as a date and time of day.


          General help for working with date / time objects in NinjaScript is available here:
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Ok I will look into it further and I'll try to see how the indicator you posted above manages to keep working through historical data and forward testing.

            Comment


              #7
              I am having a problem finding the link to the "Average Time between Bar indicator"
              Would appreciate help with that.
              Thanks
              Bob

              Comment


                #8
                Bob, it should be listed in our sharing section here - http://www.ninjatrader.com/support/f...ge+time&desc=1

                Comment


                  #9
                  thanks got it

                  Comment


                    #10
                    I got it working by using the subtract method:

                    Code:
                            protected override void OnBarUpdate()    // To be used on 233 tick chart
                            {
                                if(FirstTickOfBar)
                                {
                                    TimeSpan0 =  Time[0].Subtract(Time[1]);
                     
                                }
                      
                                if(TimeSpan0.TotalMilliseconds < 5000)    
                                {
                                        DrawArrowUp(".", true, 0, Low[0] - .25, Color.White);
                    
                                }
                    This was working, drawing white arrows all across a 233tick ES chart. Then all of a sudden , it stopped working. I don't see any white arrows, even though the bars are of less than 5 seconds old. Why are the white arrows not being drawn anymore ? This was working for weeks!

                    Comment


                      #11
                      dennho, are there any errors in the log tab specific to this indicator? If you print the TimeSpan value would it still return what you expect? What broker or connection are you working with, have you considered syncing the PC clock and then rechecking on a fresh chart on live data?

                      Comment


                        #12
                        Am am using Zen-Fire as my data provider. I shutdown ninjatrader, sync'ed the PC clock from time.windows.com, then after the DrawArrowUp function, I put in a line of code:

                        Code:
                          Print(TimeSpan0.TotalMilliseconds);
                        In
                        Code:
                        if(TimeSpan0.TotalMilliseconds < 5000)    
                                    {
                                        DrawArrowUp(".", true, 0, Low[0] - .25, Color.White);
                                        Print(TimeSpan0.TotalMilliseconds);
                                    //        DrawDot("Less than 5 seconds",true,0,Low[0] - TickSize,Color.White);
                                    }
                        In the output, I see
                        Code:
                        4000
                        2000
                        3000
                        0
                        3000
                        0
                        0
                        4000
                        etc....
                        My problem when the Arrows stopped drawing was when I tried to replace the arrow with a dot instead, and then the dots didn't draw, so I commented out that line, and went back to the original arrow line of code, and thats when things stopped working at all.

                        The DrawArrowUp function only works on the very last bar of the chart (Bar[0]) if the condition is true, then it gets erased, and on to the next bar that is less than 5 seconds old. It used to draw arrows and leave them on the chart, then draw subsequent arrows as the chart continued. Now its not doing that anymore, its only drawing on the very last bar and then it clears the arrow when the next bar comes in.

                        The only yellow lines in my log tab are Strategies failing to initialize, ones that are not even loaded into my chart, and I want to delete later, but those errors were always present even during the time when the DrawArrowUP was working.

                        Code:
                        if (Close[0] < Close[1])// go short condition
                                        {
                                            if (
                                                Close[1] < Close[2]
                                                && Close[2] < Close[3]
                                                && (AvgTimePerBar(5,2)[0] <= .2)
                                                && TimeSpan0.TotalMilliseconds < 5000
                                                )
                                            {
                                                
                                                {
                                                    DrawArrowDown("Go short?" + CurrentBar, false, 0, High[0] + .70, Color.Black);
                        
                                                }
                                            }
                                        }
                        This is the rest of my code, surprisingly enough these Black Arrows down actually draw and stay on the chart when the condition is true, which also involves TimeSpan0.TotalMilliseconds < 5000.

                        Comment


                          #13
                          dennho,

                          Your draw objects need a unique tag or else they are replaced.

                          Your arrow down has this with + CurrentBar that you added to the tag, but your DrawArrowUp does not.

                          DrawArrowDown("Go short?" + CurrentBar, false, 0, High[0] + .70, Color.Black)
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Oh crap I did it again, I totally missed the obvious. Thanks! I got it now! I had it correct the first time, but I deleted the line and "rewrote" that line of code without the +CurrentBar and messed myself up.

                            I should have left the old line of code as a //comment
                            Last edited by dennho; 05-07-2011, 03:07 AM.

                            Comment


                              #15
                              Originally posted by dennho View Post
                              Code:
                                      protected override void OnBarUpdate()    // To be used on 233 tick chart
                                      {
                                          if(FirstTickOfBar)
                                          {
                                              TimeSpan0 =  Time[0].Subtract(Time[1]);
                                          }
                              Before you can reference Time[1], you need to make sure you have a second bar.

                              This would be done by inserting the following statement at the beginning of OnBarUpdate:

                              Code:
                              if (CurrentBar < 1) return;
                              I'm not sure why this code would ever work without this, but that's another question.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              668 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              377 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X