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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    571 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    331 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    549 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    549 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X