Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CancelOrder() cancelling more than I would like

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

    CancelOrder() cancelling more than I would like

    Hi,

    I am having a problem with the following code. It is very much the same as the SampleCancelOrder except I am trying to cancel orders when they are outside a specified time rather than barNumberOfOrder.

    Orders are being placed correctly however they are being cancelled almost every minute then they are replaced again to repeat the process. I only want to cancel the order using CancelOrder.

    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
                {
                    // Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order
                    if (entryOrder == null && CurrentDayOHL().CurrentLow[0] >= FH().YU[0]
                        && Position.MarketPosition == MarketPosition.Flat && priorTrade != FH().YU[0] && (ToTime(Time[0]) >= 100000 && ToTime(Time[0]) < 173000))
                    {
                        /* Our IOrder object, entryOrder, is assigned an entry order.
                        entryOrder =  EnterLongLimit(0, true, 1, FH().YU[0], "long limit entry");
                        
                        // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
                        barNumberOfOrder = CurrentBar;
                    }                
                    
                    // If entryOrder has not been filled within the time filter, cancel the order.
                    else if (entryOrder != null && (ToTime(Time[0]) >= 100000 && ToTime(Time[0]) < 173000))
                    {
                        // When entryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
                        CancelOrder(entryOrder);
                    }
                }
    Trace Orders are set to true, this is what it looks like.

    Cancelled custom managed order at 15/12/2010 4:31:03 PM: Order='e76364dd0ef247e180f3931e1c82dc2a/Sim101' Name='long limit entry' State=Working Instrument='6A 03-11' Action=Buy Limit price=0.9806 Stop price=0 Quantity=1 Strategy='SampleCancelOrder' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='e76364dd0ef247e180f3931e1c82dc2a' Gtd='1/12/2099 12:00:00 AM'

    15/12/2010 4:32:02 PM Entered internal PlaceOrder() method at 15/12/2010 4:32:02 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=0.9806 StopPrice=0 SignalName='long limit entry' FromEntrySignal=''

    Cancelled custom managed order at 15/12/2010 4:33:06 PM: Order='78011c9ebfc346e98131ec4c362bcf33/Sim101' Name='long limit entry' State=Working Instrument='6A 03-11' Action=Buy Limit price=0.9806 Stop price=0 Quantity=1 Strategy='SampleCancelOrder' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='78011c9ebfc346e98131ec4c362bcf33' Gtd='1/12/2099 12:00:00 AM'

    15/12/2010 4:35:06 PM Entered internal PlaceOrder() method at 15/12/2010 4:35:06 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=0.9806 StopPrice=0 SignalName='long limit entry' FromEntrySignal=''

    Cancelled custom managed order at 15/12/2010 4:36:36 PM: Order='67460687f4784846994f0ed1c397b8d0/Sim101' Name='long limit entry' State=Working Instrument='6A 03-11' Action=Buy Limit price=0.9806 Stop price=0 Quantity=1 Strategy='SampleCancelOrder' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='67460687f4784846994f0ed1c397b8d0' Gtd='1/12/2099 12:00:00 AM'

    15/12/2010 4:37:19 PM Entered internal PlaceOrder() method at 15/12/2010 4:37:19 PM: BarsInProgress=0 Action=Buy OrderType=Limit Quantity=1 LimitPrice=0.9806 StopPrice=0 SignalName='long limit entry' FromEntrySignal=''

    Cancelled custom managed order at 15/12/2010 4:38:20 PM: Order='6880a3308d964e53972d94ef3fde9da5/Sim101' Name='long limit entry' State=Working Instrument='6A 03-11' Action=Buy Limit price=0.9806 Stop price=0 Quantity=1 Strategy='SampleCancelOrder' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='6880a3308d964e53972d94ef3fde9da5' Gtd='1/12/2099 12:00:00 AM'

    #2
    suprsnipes, it looks like you're setting your entries to liveUntilCancelled, which is good - when are you resetting the IOrders to null then?

    Comment


      #3
      Hi Bertrand,

      I am using the following code to reset the IOrders to null.

      Code:
      protected override void OnOrderUpdate(IOrder order)
              {
                  // Checks for all updates to entryOrder.
                  if (entryOrder != null && entryOrder == order)
                  {    
                      // Check if entryOrder is cancelled.
                      if (order.OrderState == OrderState.Cancelled)
                      {
                          // Reset entryOrder back to null
                          entryOrder = null;
                          
                      }
                  }
              }
      I'm not sure what I am doing wrong here?

      Comment


        #4
        Do you reset the entry IOrder also in OnExecution when filled? I would suggest you strip down your strategy to debug it, without any CancelOrder part the orders should just persist til the end of session handling takes over, is this the case?

        Thanks,

        Comment


          #5
          Yes Bertrand I am resetting the entry IOrder in the OnExecution when filled. It's very much the same as the SampleCancelOrder Strategy.

          Code:
          protected override void OnExecution(IExecution execution)
                  {
                      /* We advise monitoring OnExecution() to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
                      which ensures your strategy has received the execution which is used for internal signal tracking.
                      
                      This first if-statement is in place to deal only with the long limit entry. */
                      if (entryOrder != null && entryOrder == execution.Order)
                      {
                          // This second if-statement is meant to only let fills and cancellations filter through.
                          if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          {
                              // Simple stop and target
                              stopOrder = ExitLongStop(0, true, 1, execution.Price - 10 * TickSize, "stop", "long limit entry");
                              targetOrder = ExitLongLimit(0, true, 1, execution.Price + 30 * TickSize, "target", "long limit entry");
                              
                              // Resets the entryOrder object to null after the order has been filled
                              if (execution.Order.OrderState != OrderState.PartFilled)
                              {
                                  entryOrder     = null;
                              }
                          }
                      }
                  }

          Comment


            #6
            Thanks, I see looks all ok to me - can you please sent me the full strategy to our support email and include on which market / timeframe you're testing it? I'll then give it a run here to check into.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            633 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            364 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            567 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X