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