Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to execute a trade at the next bar open if Calculate = Calculate.OnBarClose

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

    How to execute a trade at the next bar open if Calculate = Calculate.OnBarClose

    How to execute a trade at the next bar open if Calculate = Calculate.OnBarClose

    Could this snippet of code work if i try to open a trade at the next bar open when Calculate = Calculate.OnBarClose ?

    If this works, can we assume it will process much faster using this technique than using Calculate = Calculate.OnEachTick and
    checking if IsFirstTickOfBar is true if we do a backtest on a large amount of data like a 10 years backtest on a daily chart ?

    Code:
    private bool isOkToProcess == false;
    
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Calculate = Calculate.OnBarClose;
        }
    }
    
    protected override void OnBarUpdate()
    {
        isOkToProcess == true;
    }
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
    {
        // if MarketDataType == MarketDataType.Last is true and isOkToProcess is true
        // then we can theorically assume we are dealing with the next bar open, right ?
    
        if (isOkToProcess && marketDataUpdate.MarketDataType == MarketDataType.Last)
            // enter long if the last bar closed higher than the bar before it
            if (Close[1] > High[2])
            {
                EnterLong();
                isOkToProcess = false;
            }
    }​
    Last edited by trendisyourfriend; 08-26-2023, 10:11 AM.

    #2
    Hello trendisyourfriend,

    Thanks for your post.

    If your strategy is set to run in the mode of Calculate.OnBarClose which means that the strategy code executes once at the end of each bar and will perform calculations on completed bars. If your strategy determines to enter an order, the order would be placed on the next bar. In this mode, your strategy will operate the same historically as it does when connected to real-time (or replay) data.

    You would need to use Calculate.OnEachTick and IsFirstTickOfBar if you would like to submit an order at the Open of the next bar as you have mentioned. This would be the recommended way to submit an order at the Open of a bar.

    See the help guide documentation below for more information.

    Calculate: https://ninjatrader.com/support/help.../calculate.htm
    IsFirstTickOfBar: https://ninjatrader.com/support/help...ttickofbar.htm

    Note that OnMarketData() is a real-time data stream and can be CPU intensive if your program code is compute intensive (not optimal). By default, this method is not called on historical data (backtest), however, it can be called historically by using TickReplay.

    OnMarketData(): https://ninjatrader.com/support/help...ta.htm?​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      I have a strategy where I need to check if there is a gap between the previous bar's close and the current open before deciding if entering a trade is okay. Since my strategy runs on a daily interval and uses the `OnBarClose` settings to conduct a backtest spanning over 10 years, What would you suggest instead of using `OnEachTick` or `OnPriceChange`.?

      Originally posted by NinjaTrader_BrandonH View Post
      Hello trendisyourfriend,

      Thanks for your post.

      If your strategy is set to run in the mode of Calculate.OnBarClose which means that the strategy code executes once at the end of each bar and will perform calculations on completed bars. If your strategy determines to enter an order, the order would be placed on the next bar. In this mode, your strategy will operate the same historically as it does when connected to real-time (or replay) data.

      You would need to use Calculate.OnEachTick and IsFirstTickOfBar if you would like to submit an order at the Open of the next bar as you have mentioned. This would be the recommended way to submit an order at the Open of a bar.

      See the help guide documentation below for more information.

      Calculate: https://ninjatrader.com/support/help.../calculate.htm
      IsFirstTickOfBar: https://ninjatrader.com/support/help...ttickofbar.htm

      Note that OnMarketData() is a real-time data stream and can be CPU intensive if your program code is compute intensive (not optimal). By default, this method is not called on historical data (backtest), however, it can be called historically by using TickReplay.

      OnMarketData(): https://ninjatrader.com/support/help...ta.htm?​
      Last edited by trendisyourfriend; 08-27-2023, 03:17 PM.

      Comment


        #4
        Hello trendisyourfriend,

        Thanks for your notes.

        When using Calculate.OnBarClose, the OnBarUpdate() logic in the script will only process at the close of each bar and logic cannot process intrabar.

        Further, when in historical data (backtesting), only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

        Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

        This means orders will only be submitted when the bar closes if the condition to place the order is true.

        ​Please review the help guide document on the differences on real-time vs backtest (historical).


        If you want to place an order at the next bar's Open then Calculate.OnPriceChange or Calculate.OnEachTick would need to be used along with TickReplay. Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

        Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

        SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity': https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

        TickReplay: https://ninjatrader.com/support/help...ick_replay.htm

        Developing for Tick Replay:
        https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm

        Additional information may be found in this NinjaTrader Forum post:
        https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504​​
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, 03-13-2026, 05:17 AM
        0 responses
        87 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        151 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        80 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        53 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        62 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X