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 cmoran13, 04-16-2026, 01:02 PM
    0 responses
    43 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    26 views
    0 likes
    Last Post PaulMohn  
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    163 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    98 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    158 views
    2 likes
    Last Post CaptainJack  
    Working...
    X