Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    40 views
    0 likes
    Last Post jeronymite  
    Started by bill2023, Today, 08:51 AM
    2 responses
    16 views
    0 likes
    Last Post bill2023  
    Started by sidlercom80, 10-28-2023, 08:49 AM
    167 responses
    2,260 views
    0 likes
    Last Post jeronymite  
    Started by warreng86, 11-10-2020, 02:04 PM
    7 responses
    1,362 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Perr0Grande, Today, 08:16 PM
    0 responses
    5 views
    0 likes
    Last Post Perr0Grande  
    Working...
    X