Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CalculateOnBarClose

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

    CalculateOnBarClose

    Hello,

    My backtests are coming up with different trades than when trading live. Upon analysis, i realized that during live trading my criteria were being triggered within a bar, and i suspect that the backtest uses OnBarClose values.

    I have CalculateOnBarClose = true;

    However, it does not seem to be the case when trading live. How can i ensure that the strategy follows the same rules as it did in the backtest; specifically, waiting until end of bar to execute trades?
    Last edited by dargente; 01-14-2009, 04:16 AM.

    #2
    Hi dargente, please remove the CalculateOnBarClose call in all indicators your strategy is calling. Then just use one in the strategy itself set to 'true'.

    Comment


      #3
      Bertrand,

      I believe that my code is as you recommend. Here is what i have, please confirm:

      Code:
      [FONT=Fixedsys]        #region Variables
              // Wizard generated variables
              // User defined variables (add any user defined variables below)
              #endregion
      
              /// <summary>
              /// This method is used to configure the strategy and is called once before any strategy method is called.
              /// </summary>
              protected override void Initialize()
              {
      
                  SetStopLoss("", CalculationMode.Ticks, 100, false);
                  SetProfitTarget("", CalculationMode.Ticks, 100);
      
                  CalculateOnBarClose = true;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (CrossAbove(RSI(14, 3).Avg, 50, 1)
                  {
                      EnterLong(DefaultQuantity, "Long Entry");
                      ExitShort("", "Exit Short (Reversal)");
                  }[/FONT]
      Should i remove OnBarUpdate() since it is called on each incoming tick? or replace it with something else?

      Also, can you confirm that the backtest uses end of bar values and does not execute trades based on intra-bar values (except stop losses - those are probably executed intra-bar if triggered, right?)

      Otherwise what is the reason for backtests not executing the same trades as live trading?

      thanks

      Comment


        #4
        Thanks dargente, no the code is good. Please see this link for a review why real time performance maybe different from backtesting - http://www.ninjatrader-support.com/H...sBacktest.html

        In backtesting strategies will always processed at the close of each bar.

        Stop Loss and Profit Target orders will execute intrabar, correct.

        When you start your strategy, are you sure you have selected CalculateOnBarClose = true from the 'General' section?

        Comment


          #5
          When you start your strategy, are you sure you have selected CalculateOnBarClose = true from the 'General' section?


          not sure. it looks like the default is set to 'false', so that could very possibly be the reason. i will test it out again and update you.

          thanks for your help

          Comment


            #6
            CalculateOnBarClose = true from the 'General' section

            ok, so this was the reason. great, one problem solved.

            now, some other issues i am having:

            -- when paper trading on the sim account, my stop loss is triggered when the following occur:
            (if long) when the bid = my stop loss.
            (if short) when the ask = my stop loss

            however, when paper trading on a live IB account, my stop loss is triggered when the following occur:
            (if long) when the ask = my stop loss
            (if short) when the bid = my stop loss

            the difference between the two scenarios is equal to the spread. after many trades, this makes a big difference.

            so, questions are:

            1. how does the backtest execute the stop losses? for example, if long, will the backtest execute the stoploss if the stoploss price equals the current bid, or ask?

            2. why is the sim account different than trading live (regarding stoploss execution), and how can i configure my strategy so that it performs similar to the backtest?

            3. does the backtest results even take into consideration the spreads? or does it just use one price, for example the bid price only? again this would make a huge difference in performance results, especially with lots of trades being executed.

            thanks

            Comment


              #7
              Hi dargente, great! The stoploss will be triggered by the last traded price and then become a market order, so it makes sense to be executed at the ask for longs. The spreads are not figured in per se, but you can add a tick under 'Slippage' when testing the strategy or use higher comissions to account for this.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              580 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              335 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              102 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              554 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              552 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X