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 Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    558 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    324 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    545 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    547 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X