Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How does OnCloseBar() calculations works in backtest?

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

    How does OnCloseBar() calculations works in backtest?

    Hello. Well, I'm trying to code a system and my backtests are almost complete. The buy signals work greatly, but short signals give me errors. So I found that I really dont know exactly how the codes are processed in a bar by bar base.

    So, let me try to clarify things. Lets use this example:


    Code:
    #Variables
    
    BuyPrice = 0;
    ShortPrice = 0;
    
    OnBarUpdate()
    
    if (close[0] > open[0])
    {
      BuyPrice = Open[0];
      EnterLongLimit( 1, BuyPrice, "LimitBuy")
    }
    So, if the bar is positive, I want to place a BuyLimitOrder on the Open of this same bar, so if the next bar touchs it, it buys.

    And my question is: when you backtest this code, which of the two actions will be the right:

    1 - At the close of Bar[0], the backtester will place a LimitOrder to be processed in the NEXT bar? I mean, lets say it is in the default mode (expires in every bar), if the next bar touchs the LimitPrice, will it trigger?

    2 - At the close of Bar[0], the backtester will place a LimitOrder and calculates if it is achieved at this same bar and, since it is, go long?


    In other words: the settings you set when a condition is true are used in the bar that met the conditions or only start to work only in the next bar?


    I hope I made my doubts clear. Sorry for the bad english, it is not my mother language.

    Thanks in advance!
    Last edited by Cadelao; 08-01-2014, 09:13 AM.

    #2
    Hello Cadelao,

    Thank you for your post.

    Number 1 will be the correct idea.

    In a backtest, the CalculateOnBarClose will always be true. This means the strategy will run one calculation per bar.

    When a bar hits your conditions and they are true to place an order, the FillType is then called which looks at the next bars price and if it will fill the order or not.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, Cal.

      Comment


        #4
        I came across this and was wondering if in the example that Cadelao gives would cause a future leak. The way I understand it is that if the close is greater than the open in the current bar, then go long.

        In a backtest wouldn't the system look at the current bar already knowing the close is greater and submit the order? If I made the limit price the open[0] and ran that example, would it fill a long order at the open and profit from the open to close on the current bar?

        Thanks in advance!

        Comment


          #5
          Hello,

          using the [0] bars ago would equate to the bar that is evaluated. This means that if some logic becomes true on this bar, the [0] bars ago value would reflect the same bar as the condition had become true in.

          In the case you submit an order based on the condition, the backtest would evaluate the next bar to determine if a fill would occur.

          For this type of question, the most simple way to answer this would be to generate a sample strategy that submits an order in this way to visually see the bars/data used.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            A big flaw with NT is that during Backtesting, CalculateOnBarClose = false is ignored.

            So a trade trigger like this...

            Code:
            bool trigger = Close[1] > Open[1];  // look back one bar for Live Trading
            if (trigger) EnterLong();
            where you'd expect NT would evaluate the conditional statement as true one bar ago and enter on the open of the current bar. But instead, NT Backtesting will trigger a trade on the Open of the following bar, or one bar too late.

            In Live Trading, with CalculateOnBarClose = false, the conditional statement would be evaluated true by the first tick and trade would be triggered at or near the opening price of the current bar.

            For Backtesting, to emulate what would typically happen in real time, you'd need to rewrite the above code like ....

            Code:
            bool trigger = Close[0] > Open[0];  // current bar for Backtesting
            if (trigger) EnterLong();
            Aside from your typical trade logic where you'll want to wait until completion of the current bar, there are way too many reasons for not waiting for the bar to close to execute a trade. So structurally, it makes sense to have such trade logic be base on one bar ago in Live Trading.

            So with NT strategy, you need one set of code written exclusively for Backtesting, and another set of code rewritten exclusively for Live Trading. Not very convenient.
            Last edited by borland; 08-05-2016, 08:27 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GussJ, 03-04-2020, 03:11 PM
            11 responses
            3,229 views
            0 likes
            Last Post xiinteractive  
            Started by andrewtrades, Today, 04:57 PM
            1 response
            14 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by chbruno, Today, 04:10 PM
            0 responses
            7 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Started by josh18955, 03-25-2023, 11:16 AM
            6 responses
            441 views
            0 likes
            Last Post Delerium  
            Started by FAQtrader, Today, 03:35 PM
            0 responses
            12 views
            0 likes
            Last Post FAQtrader  
            Working...
            X