Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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'.
    BertrandNinjaTrader Customer Service

    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?
        BertrandNinjaTrader Customer Service

        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.
              BertrandNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by arvidvanstaey, Today, 02:19 PM
              4 responses
              11 views
              0 likes
              Last Post arvidvanstaey  
              Started by samish18, 04-17-2024, 08:57 AM
              16 responses
              61 views
              0 likes
              Last Post samish18  
              Started by jordanq2, Today, 03:10 PM
              2 responses
              9 views
              0 likes
              Last Post jordanq2  
              Started by traderqz, Today, 12:06 AM
              10 responses
              18 views
              0 likes
              Last Post traderqz  
              Started by algospoke, 04-17-2024, 06:40 PM
              5 responses
              48 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X