Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Comparing today's open to yesterday's close on EOD data

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

    Comparing today's open to yesterday's close on EOD data

    I'm trying to get more familiar with NinjaScript and I was trying to write a simple strategy that looked at yesterday's close and today's open.

    I've imported end of day data into NinjaTrader, and with the code below, am not getting the results I expected.


    Code:
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
                ExitOnClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                // Is Close[-1] yesterday's open? Is Open[0] today's open?
                if (Open[0] < Close[-1])
                {
                    EnterLong(DefaultQuantity, "MyBuy");
                }
            }

    When I look at the chart in the Backtest summary, it seems that I buy at today's open and sell at tomorrow's open. How can I ensure to buy at today's open and sell at today's close?

    #2
    el_spudo,

    You are using the [] indexes wrong. 0 refers to the most recent bar, 1 refers to the previous bar. To get yesterday's values you want to do [1] provided you are using daily charts.
    Josh P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by charlesugo_1, 05-26-2026, 05:03 PM
    0 responses
    61 views
    0 likes
    Last Post charlesugo_1  
    Started by DannyP96, 05-18-2026, 02:38 PM
    1 response
    148 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 05-11-2026, 05:56 AM
    0 responses
    162 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 05-10-2026, 08:12 PM
    0 responses
    98 views
    0 likes
    Last Post CarlTrading  
    Started by Hwop38, 05-04-2026, 07:02 PM
    0 responses
    286 views
    0 likes
    Last Post Hwop38
    by Hwop38
     
    Working...
    X