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 cmoran13, Yesterday, 01:02 PM
    0 responses
    29 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    21 views
    0 likes
    Last Post PaulMohn  
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    160 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    95 views
    1 like
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    148 views
    2 likes
    Last Post CaptainJack  
    Working...
    X