Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exit Position Issue

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

    Exit Position Issue

    In a strategy I am using SetStopLoss() and SetProfitTarget() before Entering orders with EnterLongMIT/EnterShortMIT.

    At a certain condition I would like to exit the position. I tried several methods, including CancelOrder using OnOrderUpdate to get the order link.

    Any mehod I use rise the same error:

    An Enter() method to submit an entry order at '1/19/2023 12:16:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules that Reduce Unwanted Positions' in the Help Guide for detailed explanation.
    What is the correct way to exit the position with its stop and target orders?

    #2
    Looking further into my backtest chart, this message only occurred once, when the system exited due to EOD, so maybe what I am doing s correct, and the error message is confusing.
    1. Am I doing the correct thing?
    2. Any way to avoid it?

    Click image for larger version

Name:	Screenshot 2023-03-27 at 14.20.45.png
Views:	183
Size:	360.9 KB
ID:	1242837

    Comment


      #3
      Hello, thanks for writing in. There should be no use of Exit order if you are using SetStopLoss/SetProfitTarget. The best way to exit the position is to use either CloseStrategy() and that will close all open position, cancel all working orders and disable the strategy or you call SetStopLoss again while in the position and move it to the market price.

      Comment


        #4
        Thank you for your reply Chris. I want to clarify that I am only trying to cancel the signal order, not the StopLoss or ProfitTarget orders that are related.

        Looking at my debugging output, it looks like most of the CancelOrder works fine, until I get such an error message.

        Furthermore, when I commented out the CancelOrder, I still got the error message.

        Code:
                private void ExitPosition()
                {
                    if (entryOrder!=null)
                    {
                        Print("ExitPosition", "entryOrder="
                                +(entryOrder.Name+entryOrder.FromEntrySignal!=""?(", FromEntrySignal="+entryOrder.FromEntrySignal):"")
                                +", Position="+Position.MarketPosition+", Filled="+entryOrder.Filled);
        
                        CancelOrder(entryOrder);
                    }
                }
        
                protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                {
                      if (order.Name==LongOrderName || order.Name==ShortOrderName)
                          entryOrder = order;
        
                      // Evaluates for all updates to entryOrder.
                      if (entryOrder!=null && entryOrder==order)
                      {
                          // Check if entryOrder is cancelled.
                          if (entryOrder.OrderState==OrderState.Cancelled)
                          {
                              // Reset entryOrder back to null
                              entryOrder = null;
                          }
                      }
                }
        Last edited by Shai Samuel; 03-28-2023, 05:48 AM.

        Comment


          #5
          Hi Shai, The strategy can not call CancelOrder() on an order that has already been filled. If the order is not filled, you can use this method to cancel the order. CancelOrder is not causing the ignored Entry order this is happening because your strategy has an active entry order and your strategy is trying to submit an entry order in the opposite direction before that initial entry order is canceled.

          Comment


            #6
            Thank you Chris.

            Got you:
            1. If order was not fully filled, I can use: CancelOrder(entryOrder);
            2. Otherwise, I need to set SetStopLoss to something like: SetStopLoss(ActiveOrderName, CalculationMode.Ticks, 0, false);​
            Only then I can handle the next order of the opposite side.

            Could you please confirm this:

            Code:
                    private void ExitPosition(string source)
                    {
                        if (entryOrder!=null)
                        {
                            // if a part of the order is not yet filled, cancel it
                            if (entryOrder.Filled!=entryOrder.Quantity)
                                CancelOrder(entryOrder);
            
                            // if the order is running, exit now.
                            if (Position.MarketPosition!=MarketPosition.Flat)
                                SetStopLoss(Position.MarketPosition==MarketPosition.Long?LongOrderName:ShortOrderName, CalculationMode.Ticks, 0, false);
                        }
                    }
            ​

            Comment


              #7
              Hi Shai, this code looks good, but please test it out on the playback/sim101 accounts.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              66 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              141 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              76 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              47 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              51 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X