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

Range bar timer

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

    Range bar timer

    I'm looking for a way to determine how fast a range bar is created. The idea is that if I get 2 or 3 range bars in less than a few seconds, I want to jump into the trade for a scalp. I saw in another post that someone recommended using

    int barsAgo = (CurrentBar - Bars.GetBar(Time[0].AddSeconds(-30)))+1;

    This seems to return the number of bars that happened in the last 30 seconds, by that time the opportunity has passed. I'd like to take it bar by bar and say something like "bar1 was created in 2 seconds, bar 2 was created in 1 second, open trade in the same direction" and scalp a couple of points.

    Is there a way to do this?

    #2
    Hello sonnygrapples,

    To know how long a bar takes to close you would need to run the script OnEachTick in realtime and then use a Stopwatch to find a elapsed time. Stopwatch is part of the C# language: https://learn.microsoft.com/en-us/do...tframework-4.8

    You could do something like the following to count each bars time:

    Code:
            
            private System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
    
            protected override void OnBarUpdate()
            {
                if(IsFirstTickOfBar)
                {
                    TimeSpan previousBarsLength = stopWatch.Elapsed;
                    Print(previousBarsLength.TotalSeconds);
                    stopWatch.Reset();
                    stopWatch.Start();
                }
            }

    This would start a stopwatch on the first tick of the bar. On the following bar it will see the elapsed time and then reset the stopwatch. It would repeat this for each bar.

    If you wanted to know more than 1 bars time you would have to store the previousBarsLength value to a series so that you have an amount of time for each bar saved.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you I will look into this!

      Comment


        #4
        So I tried this, but it's giving me strange results, according to this code, every single bar is less than a second. There's no way that's the case.

        Code:
        if(IsFirstTickOfBar)
                    {
                        TimeSpan previousBarsLength = stopWatch.Elapsed;
                        Print("Total elapsed seconds " + (decimal)previousBarsLength.TotalSeconds);
                        stopWatch.Reset();
                        stopWatch.Start();
                    }​
        Attached Files

        Comment


          #5
          Hello sonnygrapples,

          Are you using this in realtime? This wont work for historical bars.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Ahh, yeah I was backtesting...

            Comment


              #7
              Hello sonnygrapples

              In backtesting you process bars OnBarClose so you would only know each bars close time. If we assume you are on bar 0 and it took 10 minutes to generate the bar we would only see the time that bar closed so we wouldn't be able to say it took 10 minutes. To be able to judge how long a bar took in historical you would have to use a multi series script. A secondary tick series could be used to leverage the timestamps of each tick.

              When the primary series closes a bar you could storer the last ticks time so you would have an estimate of the bar timestamp. When the primary series closes again you could compare the fist tick you had observed and its time vs the time of the primary close to get an estimate of the time between the first tick and bar close.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by pibrew, Today, 06:37 AM
              0 responses
              1 view
              0 likes
              Last Post pibrew
              by pibrew
               
              Started by rbeckmann05, Yesterday, 06:48 PM
              1 response
              14 views
              0 likes
              Last Post bltdavid  
              Started by llanqui, Today, 03:53 AM
              0 responses
              6 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by burtoninlondon, Today, 12:38 AM
              0 responses
              11 views
              0 likes
              Last Post burtoninlondon  
              Started by AaronKoRn, Yesterday, 09:49 PM
              0 responses
              16 views
              0 likes
              Last Post AaronKoRn  
              Working...
              X