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

Stoploss execution does not cancel pending target limit orders

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

    Stoploss execution does not cancel pending target limit orders

    I ran a strategy in Sim101 last night and it placed a few orders. After opening the position, the strategy then generates and places three targets and one stoploss order in the OnExecution tag, like this:
    [IMG]file:///C:/DOCUME%7E1/Bryan/LOCALS%7E1/Temp/moz-screenshot-2.png[/IMG]
    Code:
                if (Type == "Buy")
                {
                    ExitLongLimit(0, true, Target1Qty, Target1, "Target1", execution.Order.Name);
                    ExitLongLimit(0, true, Target2Qty, Target2, "Target2", execution.Order.Name);
                    ExitLongLimit(0, true, Target3Qty, Target3, "Target3", execution.Order.Name);
                    ExitLongStop(0, true, execution.Order.Quantity, StopPrice, "StopLoss", execution.Order.Name);
                }
                if (Type == "Sell")
                {
                    ExitShortLimit(0, true, Target1Qty, Target1, "Target1", execution.Order.Name);
                    ExitShortLimit(0, true, Target2Qty, Target2, "Target2", execution.Order.Name);
                    ExitShortLimit(0, true, Target3Qty, Target3, "Target3", execution.Order.Name);
                    ExitShortStop(0, true, execution.Order.Quantity, StopPrice, "StopLoss", execution.Order.Name);
                }
    If Target1 is hit, but then the Stoploss is hit next, should it not cancel the Target2 and Target3 limit orders automatically? In one case, what it did was when the stoploss hit, it then changed the Target1 and 2 orders to a different quantity and left them working. I have no idea why this would happen, and where NinjaTrader got the quantities from.

    One thing. I had two strategies running on two different charts at the same time. Can they conflict with one another somehow? I didn't think so, since the order numbers are all unique...

    I will attach the parameters screenshot for the strategy... maybe I just need to change a parameter?

    Thanks!
    Bryan

    [IMG]file:///C:/DOCUME%7E1/Bryan/LOCALS%7E1/Temp/moz-screenshot.png[/IMG][IMG]file:///C:/DOCUME%7E1/Bryan/LOCALS%7E1/Temp/moz-screenshot-1.png[/IMG]
    Attached Files
    cassb
    NinjaTrader Ecosystem Vendor - Logical Forex

    #2
    Hello cassb,

    It will not cancel the targets. They are not tied together through OCO. To cancel the order you will have to work with IOrder objects, check the status of the Stop order, and then CancelOrder() the targets once the stop is filled.

    Set methods such as SetStopLoss() will cancel other Exit orders once filled.
    Last edited by NinjaTrader_RyanM1; 05-27-2010, 09:33 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_RyanM View Post
      Hello cassb,

      It will not cancel the targets. They are not tied together through OCO. To cancel the order you will have to work with IOrder objects, check the status of the Stop order, and then CancelOrder() the targets once the stop is filled.

      Set methods such as SetStopLoss() will cancel other Exit orders once filled.
      I can't use SetStopLoss() with multiple target limit orders though. It will only deal with one SetProfitTarget() as a OCO.

      So, in the OnExecution() method, how would I cancel any working target orders, given only the execution object passed in? The CancelOrder() method will only accept an IOrder object -- so how do I get the list of IOrder objects for my working limit orders to cancel them?

      Thanks!
      Bryan
      cassb
      NinjaTrader Ecosystem Vendor - Logical Forex

      Comment


        #4
        Hello Bryan,

        The following reference sample may help with this:
        The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()



        This snippet from the IOrders page is close to what you want:
        Code:
        protected override void OnOrderUpdate(IOrder order) 
        { 
            if (entryOrder != null && entryOrder.Token == order.Token) 
            { 
                Print(order.ToString()); 
                if (order.OrderState == OrderState.Filled) 
                    entryOrder = null; 
             } 
        }

        That will check if a specific order is filled. You can then issue CancelOrder() commands to the other orders in the same block.

        The snippet works for version 6.5 but in 7 you compare objects directly, without the .token property.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_RyanM View Post
          Hello Bryan,

          The following reference sample may help with this:
          The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()



          This snippet from the IOrders page is close to what you want:
          Code:
          protected override void OnOrderUpdate(IOrder order) 
          { 
              if (entryOrder != null && entryOrder.Token == order.Token) 
              { 
                  Print(order.ToString()); 
                  if (order.OrderState == OrderState.Filled) 
                      entryOrder = null; 
               } 
          }
          That will check if a specific order is filled. You can then issue CancelOrder() commands to the other orders in the same block.

          The snippet works for version 6.5 but in 7 you compare objects directly, without the .token property.
          OK, I think that will work for me. What would the syntax for v7.0 be?

          One more thing... if I want to close all working orders, say at the end of the trading day, by using the generic ExitLong() or ExitShort() method, what order object will get passed into the OnOrderUpdate() method?
          cassb
          NinjaTrader Ecosystem Vendor - Logical Forex

          Comment


            #6
            Version 7 equivalent for that snippet:
            Code:
             
            protected override void OnOrderUpdate(IOrder order)
            {
                 if (entryOrder != null && entryOrder == order)
                 {
                      Print(order.ToString());
                      if (order.OrderState == OrderState.Filled)
                           entryOrder = null;
                 }
            }
            Above is taken from version 7 IOrders page.


            One more thing... if I want to close all working orders, say at the end of the trading day, by using the generic ExitLong() or ExitShort() method, what order object will get passed into the OnOrderUpdate() method?
            There is a default order object but this is not accessible. It's behind the scenes unless you manually create your own IOrder objects.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_RyanM View Post
              Version 7 equivalent for that snippet:
              Code:
               
              protected override void OnOrderUpdate(IOrder order)
              {
                   if (entryOrder != null && entryOrder == order)
                   {
                        Print(order.ToString());
                        if (order.OrderState == OrderState.Filled)
                             entryOrder = null;
                   }
              }
              Above is taken from version 7 IOrders page.




              There is a default order object but this is not accessible. It's behind the scenes unless you manually create your own IOrder objects.

              OK, so when I issue the ExitLong() or ExitShort() command, will that also cancel the target and stop orders as well?

              Actually, now that I look at my Output window, I think I answered my own question. When I submit the ExitLong() command, I get these order messages in the Output window. I think this is canceling the stop and target orders for me.

              Code:
              5/21/2010 1:40:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00014/Sim101' Name='StopLoss' State=Working Instrument='$EURUSD' Action=BuyToCover Limit price=0 Stop price=1.258 Quantity=32,000 Strategy='FxEngulfingStrategy' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='b6242388c2ef45619f9ae24796bb5d3d' Gtd='12/1/2099 12:00:00 AM'
              5/21/2010 1:40:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00013/Sim101' Name='Target3' State=Working Instrument='$EURUSD' Action=BuyToCover Limit price=1.2406 Stop price=0 Quantity=11,000 Strategy='FxEngulfingStrategy' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='7fed6d616bf14a259d633e5ea090a102' Gtd='12/1/2099 12:00:00 AM'
              5/21/2010 1:40:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00012/Sim101' Name='Target2' State=Working Instrument='$EURUSD' Action=BuyToCover Limit price=1.2493 Stop price=0 Quantity=0.01M Strategy='FxEngulfingStrategy' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='4f0ece38daeb4fe180d91ea22b019556' Gtd='12/1/2099 12:00:00 AM'
              5/21/2010 1:40:00 PM Cancelled pending exit order, since associated position is closed: Order='NT-00011/Sim101' Name='Target1' State=Working Instrument='$EURUSD' Action=BuyToCover Limit price=1.2522 Stop price=0 Quantity=11,000 Strategy='FxEngulfingStrategy' Type=Limit Tif=Gtc Oco='' Filled=0 Fill price=0 Token='ee289c0a8ad340f0b213b4e2c2fe2641' Gtd='12/1/2099 12:00:00 AM'
              Last edited by cassb; 05-27-2010, 12:19 PM.
              cassb
              NinjaTrader Ecosystem Vendor - Logical Forex

              Comment


                #8
                Yes, ExitLong() and ExitShort() as such are not tied to a signal name. They will exit the entire position and once flat any pending Exit orders are canceled.

                You can still create IOrder objects for these orders if additional tracking is needed
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Perr0Grande, Today, 08:16 PM
                0 responses
                2 views
                0 likes
                Last Post Perr0Grande  
                Started by elderan, Today, 08:03 PM
                0 responses
                5 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
                12 views
                0 likes
                Last Post maybeimnotrader  
                Started by quantismo, Today, 05:13 PM
                0 responses
                7 views
                0 likes
                Last Post quantismo  
                Working...
                X