Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time-based Strategy and Market Replay

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

    Time-based Strategy and Market Replay

    I would like to develop a strategy which has time-based dependencies such as "...if 2 consecutive open bars within 6 seconds, then do this..."

    My concern is that the strategy will be invalid if I run market replay at anything other than 1x. But obviously I would like to replay much faster for back-testing. Can anyone confirm this will not be possible or is there something I'm missing here?

    Thank you.

    #2
    Hello scottreynolds,
    Thanks for your post.

    It would depend how you are referencing the time. If you are taking the bar time into consideration then it would be fine. However if you are taking the PC time (via the code DateTime.Now) then in market replay you have to make the adjustment to getting the correct time value.

    I would suggest referring to the BarTimer indicator that comes with NinjaTrader in this regard.
    Code:
    private DateTime Now
    {
    	get 
    	{ 
    		DateTime now = (Bars.MarketData.Connection.Options.Provider == Cbi.Provider.Replay ? Bars.MarketData.Connection.Now : DateTime.Now); 
    
    		if (now.Millisecond > 0)
    			now = Cbi.Globals.MinDate.AddSeconds((long) System.Math.Floor(now.Subtract(Cbi.Globals.MinDate).TotalSeconds));
    
    		return now;
    	}
    }
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      Thanks for the code. There's something that is still not working quite right. I'm using the following code:
      Code:
      DateTime current_time = Now;      //joydeep's function
      TimeSpan span = current_time - start_time;
      double ms = (double)span.TotalMilliseconds;
      But variable, ms, is always a multiple of 1000 - it's as if Now will not return anything but seconds. Also, this code is never run in your function:
      Code:
      if (now.Millisecond > 0)
      	now = Cbi.Globals.MinDate.AddSeconds((long) System.Math.Floor(now.Subtract(Cbi.Globals.MinDate).TotalSeconds));
      now.Millisecond is always 0.

      Any ideas?

      Comment


        #4
        Hello scottreynolds,
        The maximum bar granularity returned by NinjaTrader is one second.

        To assist you further may I know
        • What is the value of the variable "start_time".
        • What values you are getting and what values do you expect.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          I need time granularity to milliseconds. Any time I capture, "start_time", "current_time", anything, needs to be accurate to milliseconds. start_time would be the time recorded when the start of a signal forms. As far as I can tell, your Now function works but only to accuracy of seconds.

          Comment


            #6
            DateTime.Now will work down to milliseconds, but the bar time is limited to seconds.

            Code:
            int rightNow =  DateTime.Now.Millisecond;
            Keep in mind that this is taken from yoru PC clock, so as Joydeep mentioned, would cause problems if you replayed faster than 1x.

            The Time[x] of a bar will only be down to 1 second.
            MatthewNinjaTrader Product Management

            Comment


              #7
              Originally posted by scottreynolds View Post
              I need time granularity to milliseconds. Any time I capture, "start_time", "current_time", anything, needs to be accurate to milliseconds. start_time would be the time recorded when the start of a signal forms. As far as I can tell, your Now function works but only to accuracy of seconds.
              "Least Common Denominator"/"HIghest Common Factor" means that you cannot have a granularity finer than the smallest granularity of your data.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              647 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              369 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              108 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              572 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X