Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CancelOrder after 10 bars from entry issue

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

    CancelOrder after 10 bars from entry issue

    During Backtest, there is only 1 trade even if parameters change in the strategy. There is no error in compiling and with TraceOrders = true, nothing in the output windows. Code as follow, please help to solve the issue.

    public class VscraperOnly : Strategy
    {
    #region Variables

    private int target = 24;
    private int stop = 12;
    private int entryOffset = 1;
    private int entryOffsetBars = 8;
    private bool be2 = false;
    private bool be3 = false;
    private bool _initialized = false;
    private IOrder entryOrder = null;
    private int barNumberOfOrder = 0;

    #endregion

    protected override void Initialize()
    {
    Add(Vscraper());

    CalculateOnBarClose = true;
    EntryHandling = EntryHandling.UniqueEntries;
    EntriesPerDirection = 1;
    TraceOrders = true;

    SetProfitTarget("GoLong", CalculationMode.Ticks, target);
    SetStopLoss("GoLong", CalculationMode.Ticks, stop, false);
    SetProfitTarget("GoShort", CalculationMode.Ticks, target);
    SetStopLoss("GoShort", CalculationMode.Ticks, stop, false);

    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {
    if (!_initialized)
    {
    _initialized = true;
    }
    if (BarsInProgress != 0) return;
    if (CurrentBar < 35) return;
    if (Position.MarketPosition != MarketPosition.Flat) return;

    // Condition set 1
    if (Vscraper().Signal[0] == 1)
    {
    if(entryOrder == null)
    {
    entryOrder = EnterLongLimit(Close[0] - entryOffset*TickSize, "GoLong");
    barNumberOfOrder = CurrentBar;
    }

    if (CurrentBar > barNumberOfOrder + entryOffsetBars)
    CancelOrder(entryOrder);
    }

    // Condition set 2
    if (Vscraper().Signal[0] == -1)

    {
    if(entryOrder == null)
    {
    entryOrder = EnterShortLimit(Close[0] + entryOffset*TickSize, "GoShort");
    barNumberOfOrder = CurrentBar;
    }

    if (CurrentBar > barNumberOfOrder + entryOffsetBars)
    CancelOrder(entryOrder);
    }

    //Condition set 3
    if (Vscraper().Signal[0] == 0) return;

    }

    #2
    er111222, is this the full code you use? I don't see where you're nullifying the iorder objects again after they are filled / in terminal state?

    When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until

    Comment


      #3
      Yes, this is the full code

      Comment


        #4
        Then you certainly miss the code section to reset the IOrders again to null state as shown in the sample I posted.

        Comment


          #5
          Thanks for the sample... really help!

          After changed with the addition of the null code, it gives only a few trades, compiles without error, in the output window only have 2 lines the last line is:

          14/03/2011 9:13:38 PM Cancelled expired order: BarsInProgress=0: Order='NT-00000/Backtest' Name='GoLong' State=Working Instrument='ES 06-11' Action=Buy Limit price=1275.5 Stop price=0 Quantity=1 Strategy='VscraperOnly' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='48beac76a8f749fba952349d08a277c8' Gtd='1/12/2099 12:00:00 AM'

          May be something to do with the "Cancelled expired order"??

          Code as follow:

          {
          #region Variables

          private int target = 24;
          private int stop = 12;
          //private int entryOffset = 1;
          private int entryOffsetBars = 8;
          private bool be2 = false;
          private bool be3 = false;
          private bool _initialized = false;
          private int barNumberOfOrder = 0;
          private IOrder entryOrder = null; // This variable holds an object representing our entry order.
          private IOrder stopOrder = null; // This variable holds an object representing our stop loss order.
          private IOrder targetOrder = null; // This variable holds an object representing our profit target order.
          private IOrder mar****rder = null; // This variable holds an object representing our market EnterLong() order.

          #endregion

          protected override void Initialize()
          {
          Add(Vscraper());

          CalculateOnBarClose = true;
          EntryHandling = EntryHandling.UniqueEntries;
          EntriesPerDirection = 1;
          TraceOrders = true;
          /*
          SetProfitTarget("GoLong", CalculationMode.Ticks, target);
          SetStopLoss("GoLong", CalculationMode.Ticks, stop, false);
          SetProfitTarget("GoShort", CalculationMode.Ticks, target);
          SetStopLoss("GoShort", CalculationMode.Ticks, stop, false);
          */
          CalculateOnBarClose = true;
          }

          protected override void OnBarUpdate()
          {
          if (!_initialized)
          {
          _initialized = true;
          }
          if (BarsInProgress != 0) return;
          if (CurrentBar < 35) return;
          if (Position.MarketPosition != MarketPosition.Flat) return;

          // Condition set 1
          if (Vscraper().Signal[0] == 1 && entryOrder == null && mar****rder == null)
          {
          entryOrder = EnterLongLimit(Close[0] - 3*TickSize, "GoLong");
          barNumberOfOrder = CurrentBar;
          }

          else if (entryOrder != null && CurrentBar > barNumberOfOrder + entryOffsetBars)
          {
          CancelOrder(entryOrder);
          }

          // Condition set 2
          if (Vscraper().Signal[0] == -1 && entryOrder == null && mar****rder == null)
          {
          entryOrder = EnterShortLimit(Close[0] + 3*TickSize, "GoShort");
          barNumberOfOrder = CurrentBar;
          }

          else if (entryOrder != null && CurrentBar > barNumberOfOrder + entryOffsetBars)
          {
          CancelOrder(entryOrder);
          }
          }

          protected override void OnExecution(IExecution execution)
          {
          //deal with long limit entry
          if (entryOrder != null && entryOrder == execution.Order)
          {
          if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
          {
          stopOrder = ExitLongStop(0, true, 1, execution.Price - stop * TickSize, "stop", "GoLong");
          targetOrder = ExitLongLimit(0, true, 1, execution.Price + target * TickSize, "target", "GoLong");

          stopOrder = ExitShortStop(0, true, 1, execution.Price + stop * TickSize, "stop", "GoShort");
          targetOrder = ExitShortLimit(0, true, 1, execution.Price - target * TickSize, "target", "GoShort");

          if (execution.Order.OrderState != OrderState.PartFilled)
          {
          entryOrder = null;
          }
          }
          }
          }

          Comment


            #6
            The order expiration would be expected - per default the limit orders would only last one bar update. Please try with market orders for example and see if you would get more trades / expected signals, this would then mean in turn that you need to submit your limit orders with liveUntilCancelled set to true.

            Comment


              #7
              OK, did the entry market order test and you are right, it shows hundred of orders.

              Then, I included the liveUntilCancelled on true with the following code changed in the 2 entry conditions, run the backtest and it still only have 3 trades on the first 3 days of the backtest 3 month period.

              // Condition set 1
              if (Vscraper().Signal[0] == 1 && entryOrder == null && mar****rder == null)
              {
              entryOrder = EnterLongLimit(0, true, 1, Close[0] - entryOffset * TickSize, "GoLong");
              barNumberOfOrder = CurrentBar;
              }

              else if (entryOrder != null && CurrentBar > barNumberOfOrder + entryOffsetBars)
              {
              CancelOrder(entryOrder);
              }

              // Condition set 2
              if (Vscraper().Signal[0] == -1 && entryOrder == null && mar****rder == null)
              {
              entryOrder = EnterShortLimit(0, true, 1, Close[0] + entryOffset * TickSize, "GoShort");
              barNumberOfOrder = CurrentBar;
              }

              else if (entryOrder != null && CurrentBar > barNumberOfOrder + entryOffsetBars)
              {
              CancelOrder(entryOrder);
              }

              Comment


                #8
                That is likely due to the difference between Market Orders and Limit Orders. Check that where you are expecting a trade, the Limit Order would actually have been triggered. If the bar on which you are expecting the trade to occur never crossed the value at the Limit Order, then your limit order could never have been filled, while a Market Order would always fill, as a market order is essentially asking the Market Makers to take advantage of you, and fill you at whatever price they can push it to while your order is waiting,

                Comment


                  #9
                  Add "entryOrder = null;" in the code seems to resolve the issue:

                  else if (entryOrder != null && CurrentBar > barNumberOfOrder + entryOffsetBars)
                  {
                  CancelOrder(entryOrder);
                  entryOrder = null;
                  }

                  Comment

                  Latest Posts

                  Collapse

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