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 SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    30 views
    0 likes
    Last Post SalmaTrader  
    Started by CarlTrading, 07-05-2026, 01:16 PM
    0 responses
    17 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 06-17-2026, 10:32 AM
    0 responses
    9 views
    0 likes
    Last Post CaptainJack  
    Started by kinfxhk, 06-17-2026, 04:15 AM
    0 responses
    16 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 06-17-2026, 04:06 AM
    0 responses
    19 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X