Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

'Cancelled pending exit order, since associated position is closed' issue

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

    'Cancelled pending exit order, since associated position is closed' issue

    I have been trying to include two positions in my algorithmic trading strategy, each with their separate entry points, stop losses and targets. I have included the following code in the OnStateChange method to allow for multiple orders:

    if (State == State.SetDefaults)
    {
    EntriesPerDirection = 2;
    EntryHandling = EntryHandling.UniqueEntries;
    TraceOrders = true;
    }

    I have also assigned seperate Order variables (myEntryOrder and mySecondEntryOrder) to each of my positions and given them a different name (Order1 and Order2) as can be seen in the code below:

    if (_AllParametersTrue == true && myEntryOrder == null)
    {
    EnterShortStopMarket(0, true, _Quantity, EntryPrice, "Order1");
    SetStopLoss("Order1", CalculationMode.Price, (High[0] + 2 * ATR(14)[0]), false);
    }


    if (_AllParametersTrue == true && mySecondEntryOrder == null)
    {
    EnterShortStopMarket(0, true, _Quantity, EntryPrice, "Order2");
    SetStopLoss("Order2", CalculationMode.Price, (High[0] + 2 * ATR(14)[0]), false);
    SetProfitTarget("Order2", CalculationMode.Price, Target, false);
    }


    After countless hours spent trying to debug this issue I have had no success. I continue to get the message: Cancelled pending exit order, since associated position is closed
    This message appears in the output when the stop loss of my first entry order is hit, NinjaTrader then automatically closes my other position.

    I have a theory as to why this is happening but I am not certain that I am correct.

    I included trace orders in the first method to identify what is going wrong. Everything works fine initially - Once the short stop market orders are filled the profit targets and initial stop losses are set correctly, a quantity of 1 is assigned to each of the stops/targets in the order menu of the control centre (which is the default quantity that the variable _Quantity in the methods above represent). BUT as soon as the candle in which I entered the trade closes, the quantity for each of the stops/targets changes to 2 (which is the total quantity of order 1 and order 2 combined). When this happens I get these messages in the output window:

    8/02/2022 7:50:01 PM Strategy 'StrategyDebugging'/255187316': Amended stop order orderId='d879393074da4222b8716f2d97fe2091' account='Playback101' name='Stop loss' orderState=Accepted instrument='EURUSD' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=1.14354 quantity=1 tif=Gtc oco='7053db5918d44d0092a99165690c5ded' filled=0 averageFillPrice=0 onBehalfOf='' id=123 time='2022-02-08 19:42:35' gtd='2099-12-01' statementDate='2022-02-08' stopPriceChanged=1.14354

    8/02/2022 7:50:01 PM Strategy 'StrategyDebugging'/255187316': Amended target order orderId='eb6e0561a9e2413e87d3c83c95554626' account='Playback101' name='Profit target' orderState=Working instrument='EURUSD' orderAction=BuyToCover orderType='Limit' limitPrice=1.14006 stopPrice=0 quantity=1 tif=Gtc oco='5b078cb127bd4793a5699aa327d79b9a' filled=0 averageFillPrice=0 onBehalfOf='' id=125 time='2022-02-08 19:42:35' gtd='2099-12-01' statementDate='2022-02-08' limitPriceChanged=1.14006

    8/02/2022 7:50:01 PM Strategy 'StrategyDebugging'/255187316': Amended stop order orderId='504fdbfb1dfc489ca649a5cbf3c976cc' account='Playback101' name='Stop loss' orderState=Accepted instrument='EURUSD' orderAction=BuyToCover orderType='Stop Market' limitPrice=0 stopPrice=1.14354 quantity=1 tif=Gtc oco='5b078cb127bd4793a5699aa327d79b9a' filled=0 averageFillPrice=0 onBehalfOf='' id=124 time='2022-02-08 19:42:35' gtd='2099-12-01' statementDate='2022-02-08' stopPriceChanged=1.14354

    My theory is that because the order quantity change from 1 to 2, as soon as the first stop loss is hit, all of the positions are closed.

    I am unsure why this happens and have gotten nowhere from my efforts in trying to debug this issue.

    #2
    Hi, thanks for posting.

    If you change your stop loss to set before submitting the order does the behavior change?

    Code:
    if (_AllParametersTrue == true && myEntryOrder == null)
    {
    SetStopLoss("Order1", CalculationMode.Price, (High[0] + 2 * ATR(14)[0]), false);
    EnterShortStopMarket(0, true, _Quantity, EntryPrice, "Order1");
    }
    
    
    if (_AllParametersTrue == true && mySecondEntryOrder == null)
    {
    SetStopLoss("Order2", CalculationMode.Price, (High[0] + 2 * ATR(14)[0]), false);
    SetProfitTarget("Order2", CalculationMode.Price, Target, false);
    EnterShortStopMarket(0, true, _Quantity, EntryPrice, "Order2");
    }
    If this does not resolve, I will need to see a version of your strategy that can reproduce the message so I can re-create it on my PC, then we can work towards a solution.

    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      This has resolved the issue! Thank you so much Chris, my pain has finally come to an end.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by JoMoon2024, Today, 06:56 AM
      0 responses
      6 views
      0 likes
      Last Post JoMoon2024  
      Started by Haiasi, 04-25-2024, 06:53 PM
      2 responses
      19 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      6 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,786 views
      0 likes
      Last Post Leafcutter  
      Started by poplagelu, Today, 05:00 AM
      0 responses
      3 views
      0 likes
      Last Post poplagelu  
      Working...
      X