Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trading Range for past 2 hours

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

    Trading Range for past 2 hours

    Hi

    I would like to measure the trading range for the past 2 hours. Now i'm struggling with the DateTime constructor to get the start time from now (last candle in chart, no historical stuff), 2 hours back.

    DateTime startDateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, (DateTime.Now.Hour - 2) , DateTime.Now.Minute, 0);

    DateTime endDateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);

    int startBarsAgo = Bars.GetBar(startDateTime);
    int endBarsAgo = Bars.GetBar(endDateTime);

    Thanks for your help...

    #2
    Hello Sweet&Sour,

    Thanks for your question.

    I would advise to use the Time object to get the time of the close of the last bar so you can easily subtract 2 hours from that time to get the bar number from 2 hours ago. The barsAgo reference would be CurrentBar minus this value.

    For example:
    Code:
    protected override void OnBarUpdate()
    {
    	Print(Bars.GetBar(Time[0].Subtract(new TimeSpan(2,0,0)))); // Bar number from 2 hours ago
    	Print(CurrentBar); // Current Bar number
    	Print(""); // Extra line
    }
    The Time object is documented here: https://ninjatrader.com/support/help...eries_time.htm

    Please let me know if there is anything else I can do to assist.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CaptainJack, 05-29-2026, 05:09 AM
    0 responses
    252 views
    0 likes
    Last Post CaptainJack  
    Started by CaptainJack, 05-29-2026, 12:02 AM
    0 responses
    161 views
    0 likes
    Last Post CaptainJack  
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    165 views
    1 like
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    251 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    203 views
    0 likes
    Last Post CarlTrading  
    Working...
    X