Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

IntraBar BackTest OnExecution

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

    IntraBar BackTest OnExecution

    I have searched the forums trying to fix a strategy to get more accurate backtest results but have not found an example that uses OnExecution in the code. I have based my code on the reference samples below but am having a problem when trying to put them together.

    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


    You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


    In the Initialize section I add a 1 minute time frame and have CalculateOnBarClose = false because I then use FirstTickOfBar in the OnBarUpdate section.

    Then I use if(BarsInProgress == 0) in the onBarUpdate section along with this entry order: entryOrder = EnterLongStop(1, false, 30000, Math.Max((Close[0]+(5*TickSize)),GetCurrentAsk(0)),"LongEntry");
    I have this set to false because I want the code to check each bar whether to resubmit the order or not.

    In the onExecution section the stop order is: stopOrder = ExitLongStop(1, true, execution.Order.Filled, execution.Order.AvgFillPrice - (StopPips*TickSize), "LongStop", "LongEntry");

    The problem I now have is that this code will not produce any orders. Without trying to add the extra time series the code works fine. Any ideas as to where I am going wrong? Thanks!

    GT

    #2
    GT,

    Please use TraceOrders = true. I suspect your orders are expired as they reach a different BarsInProgress context.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thank for the quick reply! Trace orders show the order only working for the 1 minute (secondary) bar instead of the primary bar. I thought with this code it would still work for the primary time frame but then in historical backtesting "look inside" the bar at the lower time frame once the order should be executed. How would I go about having the order work for the primary bar time frame, say 5 minutes, but still get better granularity during backtests? Basically my main problem is if during a backtest if the entry occurs on a bar and the bar moves further than the onExecution stop order there is an error, it locks up NT for a while, when it finally comes back up there are no orders after this bar and it ignores the stop completely. In realtime this is not a problem, only in backtest.

      Comment


        #4
        GreenTrade,

        I'm not sure I follow. If you want it to stay alive you will need to submit the order again regardless of which bars series you are in. Alternatively, it may even be easier to just use liveUntilCancelled = true.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          I think I am close now. If I take the CancelOrder code out below it works but leaves the order working because liveUntilCancelled = true. Once I put the code back in it does not do anything.

          Code:
          CalculateOnBarClose = false;
          
          protected override void OnBarUpdate()
          {
                  if(BarsInProgress == 0)
                  {
                         if(entryOrder == null && other entry logic)
                         {
                              entryOrder = EnterLongStop(1, true, 50000,                       Math.Max((Close[0]+(5*TickSize)),GetCurrentAsk(0)),"LongEntry");
                              barNumberOfOrder = CurrentBar; 
                         }
                         else if (CurrentBar > barNumberOfOrder + 1)
                         {
                              CancelOrder(entryOrder);     
                         }
                   }
          }
          Any thoughts on what is wrong with the CancelOrder code? Thanks!

          GT

          Comment


            #6
            You can't call CancelOrder() without first ensuring the IOrder you are passing in is not null.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              That did the trick. Thanks for your patience with me. I fixed it by changing the CancelOrder as follows:

              Code:
              else if (entryOrder != null && CurrentBar > barNumberOfOrder + 1)
                       {
                       CancelOrder(entryOrder); 
                       }
              GT

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              578 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              334 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
              553 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              551 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X