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

Error changing stop order

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

    Error changing stop order

    Hello,

    I get the following error on occasion in my strategy, which uses unmanaged orders:

    Cannot change order '493860972' because current order values already match. affected Order: BuyToCover 1 StopMarket @ 62.25

    I have the following code which is meant to ensure the order is only changed if the new stop price is different than current (long example):

    // Compute trailing stop. Updates TrailLineLong[]
    ComputeTrailerLong(barsAgo);

    LastTrailingLong = TrailLineLong[barsAgo];

    bool updateLastSetStopPrice = false;

    if ((StopOrder1 != null) &&
    (LastTrailingLong != LastSetStopPrice))
    {
    ChangeOrder(StopOrder1, StopOrder1.Quantity, 0, LastTrailingLong);
    trace = @"Changed StopOrder1 Long at " + LastTrailingLong.ToString(); Print(trace);
    updateLastSetStopPrice = true;
    }

    if ((EntriesPerDirection > 1) &&
    (StopOrder2 != null) &&
    (LastTrailingLong != LastSetStopPrice))
    {
    ChangeOrder(StopOrder2, StopOrder2.Quantity, 0, LastTrailingLong);
    trace = @"Changed StopOrder2 Long at " + LastTrailingLong.ToString(); Print(trace);
    updateLastSetStopPrice = true;
    }

    if ((EntriesPerDirection > 2) &&
    (StopOrder3 != null) &&
    (LastTrailingLong != LastSetStopPrice))
    {
    ChangeOrder(StopOrder3, StopOrder3.Quantity, 0, LastTrailingLong);
    trace = @"Changed StopOrder3 Long at " + LastTrailingLong.ToString(); Print(trace);
    updateLastSetStopPrice = true;
    }

    if (updateLastSetStopPrice)
    {
    LastSetStopPrice = LastTrailingLong;
    }

    This block of code is called from OnBarUpdate. The initial stop order is placed during OnExecutionUpdate when the entry orders are confirmed to be filled. LastSetStopPrice is initialized there. Is there a more robust way to ensure the order is not modified if the stop price is the same, or any other ideas on preventing this error?

    Thanks,
    Sliverm3170

    #2
    Hi Sliverm3170, thanks for your note.

    OnExecutionUpdate can be called multiple times, are you sure the order is not trying to be changed more than one time? This can be fixed with a simple boolean flag e.g.

    public bool ShouldUpdateStop = true;

    if(ShouldUpdateStop)
    {
    Update();
    ShouldUpdateStop = false;
    }

    Using Print Statements within your code is also very useful to see how the code is running:



    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi Chris,

      Thanks for the quick reply. To understand your explanation better, here's more of the log preceding the error:

      2020-01-07 08:22:17:699|1|32|Order='493860972/' Name='Stop3' New state='Accepted' Instrument='CL 02-20' Action='Buy to cover' Limit price=0 Stop price=62.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      2020-01-07 08:22:17:699|1|32|Order='493860972/' Name='Stop3' New state='Working' Instrument='CL 02-20' Action='Buy to cover' Limit price=0 Stop price=62.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      2020-01-07 08:22:19:569|1|32|Order='493860972/' Name='Stop3' New state='Accepted' Instrument='CL 02-20' Action='Buy to cover' Limit price=0 Stop price=62.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
      2020-01-07 08:22:19:569|1|32|Order='493860972/' Name='Stop3' New state='Working' Instrument='CL 02-20' Action='Buy to cover' Limit price=0 Stop price=62.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='Unable to change order' Native error='Cannot change order '493860972' because current order values already match.'
      2020-01-07 08:22:19:573|0|32|, Cannot change order '493860972' because current order values already match. affected Order: BuyToCover 1 StopMarket @ 62.25
      2020-01-07 08:22:19:570|0|4|Strategy '/161447190' submitted an order that generated the following error 'Unable to change order'. Strategy has sent cancel requests, attempted to close the position and terminated itself.

      Are the double instances of 'Accepted' and 'Working' an indication that the order is getting changed multiple times?

      The only code in OnExecutionUpdate that responds to the order being a stop order is the following:

      if ((StopOrder1 != null) && (StopOrder1 == execution.Order))
      {
      if ((execution.Order.OrderState == OrderState.Filled) ||
      (execution.Order.OrderState == OrderState.PartFilled))
      {
      StopOrder1 = null;
      }
      }

      Likewise for StopOrder2, and StopOrder3 (There is code to issue the stop orders, but that's in response to the order being EntryOrder1/2/3. Also this error usually occurs well after the trades have progressed, e.g. usually the first couple of targets are hit, etc.). Where is the order being prompted to change multiple times?

      Are you suggesting using a bool inside on OnExecutionUpdate to control OnBarupdate calling my original code to change the order?

      Comment


        #4
        Hi silverm3170, thanks for your reply.

        If you add a Print statement right before the order changing code, do you see the print statement twice? I'm not sure specifically what's happening in the strategy, so a Print statement will be the best way to find out if the code is being run twice.

        My suggestion is indeed to place some kind of blocking flag to prevent code from being run twice in OnExecutionUpdate. If this helps, we have an example of setting protective orders in this sample:


        Last edited by NinjaTrader_ChrisL; 01-07-2020, 12:57 PM.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks Chris. Looking at your example I realize I hadn't considered precision issues, i.e. my stop update calculations are doubles and not rounded down or up to nearest tick. It might be enough to trigger an update if not an exact match. I'll check that as another angle.

          Comment


            #6
            Hi silverm3170, thanks for your reply. That is a good observation. Let me know how it goes investigating that route.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by warreng86, 11-10-2020, 02:04 PM
            7 responses
            1,360 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by Perr0Grande, Today, 08:16 PM
            0 responses
            5 views
            0 likes
            Last Post Perr0Grande  
            Started by elderan, Today, 08:03 PM
            0 responses
            9 views
            0 likes
            Last Post elderan
            by elderan
             
            Started by algospoke, Today, 06:40 PM
            0 responses
            10 views
            0 likes
            Last Post algospoke  
            Started by maybeimnotrader, Today, 05:46 PM
            0 responses
            14 views
            0 likes
            Last Post maybeimnotrader  
            Working...
            X