Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop and target orders with Account Add On fail to submit

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

    Stop and target orders with Account Add On fail to submit

    Hi,
    I am trying to add stop loss and target orders to my indicator using the information found in the ninjatrader help guides. I don't attach the link as it will land somewhere else, I attach a screenshot with the information i used.


    However, the bracket orders fail to submit, with no errors in the log.
    Could somebody please point me in the right direction?

    I attach the indicator as the code is too long to be displayed in this message.

    Thank you

    Click image for larger version

Name:	image.png
Views:	89
Size:	388.0 KB
ID:	1320150
    Last edited by gyilaoliver; 10-02-2024, 05:37 AM.

    #2
    Hello gyilaoliver,

    Are you testing that specific code or did you make modifications?

    Have you used a print to verify that part of your code is being executed?

    Comment


      #3
      This is how part of my code looks like. I used print to verify, but nothing happens

      Code:
          
                  //------------------------------------------------------------------------------------------------------------------------
                  //    LONG PENDING ORDER
                  //------------------------------------------------------------------------------------------------------------------------
                  
                      if (State !=State.Historical && enterlon && longOrder == null)
                          {
                              if ( GetCurrentAsk() < High[1] )
                              {
                                       longOrder = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Gtc, contr, 0, High[1], "", "Entry", Core.Globals.MaxDate, null);
                                              myAccount.Submit(new[] { longOrder });
                                      enterlon = false;
                                      inlong = true;
                              }
                      }    
                  //------------------------------------------------------------------------------------------------------------------------
                  //    SHORT PENDING ORDER
                  //------------------------------------------------------------------------------------------------------------------------
                      
                      if (State !=State.Historical && entershor && shortOrder == null )
                          {
                              if ( GetCurrentBid() > Low[1] )
                              {
                                        shortOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Gtc, contr, 0, Low[1], "", "Entry", Core.Globals.MaxDate, null);
                                          myAccount.Submit(new[] { shortOrder });
                                      entershor = false;
                                  inshort = true;
                              }
                          }
                  
                          
                      //   CANCEL PENDING ORDERS IF A NEW BAR OPENS AND ORDER DIDN'T TRIGGER
                          
                          if ( IsFirstTickOfBar)
                          {
                              if ( longOrder != null )
                              {
                                      myAccount.Cancel(new[] { longOrder });
                                      longOrder = null;
                                  enterlon = false;
                                  cancelbutton = false;
                              }
                              
                                  if ( shortOrder != null )
                              {
                                      myAccount.Cancel(new[] { shortOrder });
                                      shortOrder = null;
                                  entershor = false;
                                  cancelbutton = false;
                              }
                      }
                          
              }
              
                  
              #region Properties
              
              [Browsable(false)]
              [XmlIgnore]
              public Series<double> Val
              {
                  get { return Values[0]; }
              }
          
              
          
              
              
              #endregion
      
        private void OnOrderUpdate(object sender, OrderEventArgs e)
      
          {
          Print("longorder : "+longOrder+"  shortOrder : "+shortOrder);
               // Submit stop/target bracket orders
      
              if (longOrder != null && longOrder == e.Order)
      
               {
      
                    if (e.OrderState == OrderState.Filled)
      
                    {
                        
                        string oco = Guid.NewGuid().ToString("N");
      
       
      
                        takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day,
                              e.Quantity, Math.Max(High[2],High[3]), 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                        stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day,
                              e.Quantity, 0, Low[1], oco, "Stop Loss", Core.Globals.MaxDate, null);
      
                         myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                  //      longOrder = null;
      
                    }
      
               }
              
              
                if (shortOrder != null && shortOrder == e.Order)
      
               {
      
                    if (e.OrderState == OrderState.Filled)
      
                    {
                      
                        string oco = Guid.NewGuid().ToString("N");
      
       
      
                        takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day,
                              e.Quantity, 0, Math.Min(Low[2], Low[3]), oco, "Profit Target", Core.Globals.MaxDate, null);
                        stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day,
                              e.Quantity, High[1], 0, oco, "Stop Loss", Core.Globals.MaxDate, null);
      
                         myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                  //      shortOrder = null;
      
                    }
      
               }
              
               Print(takeprofitOrder+"   "+stoplossOrder);
      
          }​

      Comment


        #4
        I found a minor bug in the code, here is the correct one, but the problem persists.
        The pending orders are place correctly, and surprisingly always trigger only partially.
        From the OnOrderUpdate function I get the print for longOrder and ShortOrder, but I don't get a print for takeprofitOrder and stoplossOrder
        Attached Files

        Comment


          #5
          Hello gyilaoliver,

          I would suggest putting a print inside the conditions which submit the targets to make sure that code is being reached. If no targets are submitted its likely the condition was not true. The print you have at the bottom of OnOrderUpdate will likely be blank, you would need to monitor for those orders in OnOrderUpdate to have been submitted rather than trying to print out the variable assignment from calling the order method.

          Comment


            #6
            Modified the Print so i can see all the conditions for the bracket orders. As you can see in the print, all the conditions were met : I had a short order, Orderstate was filled, but instead of the bracket orders, some strange thing happened. I placed the sell stop order with 15 contracts, only 3 were filled.
            Code:
              private void OnOrderUpdate(object sender, OrderEventArgs e)
            
                {
                    
                         if ( e.Order == longOrder) ord = "long";
                         else if ( e.Order == shortOrder) ord = "short";
                        
                Print(ord+"   "+e.Order+"   "+e.OrderState);
                     // Submit stop/target bracket orders
            
                    if (longOrder != null && longOrder == e.Order)
            
                     {
             
                        
                        
                          if (e.OrderState == OrderState.Filled)
            
                          {
                              
                              string oco = Guid.NewGuid().ToString("N");
            
             
            
                              takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day,
                                    contr, Math.Max(High[2],High[3]), 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                              stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day,
                                    contr, 0, Low[1], oco, "Stop Loss", Core.Globals.MaxDate, null);
            
                               myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                        //      longOrder = null;
            
                          }
            
                     }
                    
                    
                      else if (shortOrder != null && shortOrder == e.Order)
            
                     {
            
                          if (e.OrderState == OrderState.Filled)
            
                          {
                            
                              string oco = Guid.NewGuid().ToString("N");
            
             
            
                              takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day,
                                    contr, 0, Math.Min(Low[2], Low[3]), oco, "Profit Target", Core.Globals.MaxDate, null);
                              stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day,
                                    contr, High[1], 0, oco, "Stop Loss", Core.Globals.MaxDate, null);
            
                               myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                        //      shortOrder = null;
            
                          }
            
                     }
                    
                     Print(takeprofitOrder+"   "+stoplossOrder);
            
                }​
            Click image for larger version

Name:	image.png
Views:	97
Size:	230.2 KB
ID:	1320219

            Comment


              #7
              Can't find the post right now, but i read somewhere in the forum that bracket orders should be placed from the OnExecutionUpdate, although i didn't find any reference in the guide. Would this be the problem?

              Comment


                #8
                Ok, important update. Added prints to all functions, the problem occurs when the takeprofitorder gets created. Wondering if the reason is that the pending order gets only partially filled?
                Code:
                  private void OnOrderUpdate(object sender, OrderEventArgs e)
                
                    {
                        
                             if ( e.Order == longOrder) ord = "long";
                             else if ( e.Order == shortOrder) ord = "short";
                            
                    Print(ord+"   "+e.Order+"   "+e.OrderState);
                         // Submit stop/target bracket orders
                
                        if (longOrder != null && longOrder == e.Order)
                
                         {
                 
                             Print("longorder");
                            
                              if (e.OrderState == OrderState.Filled)
                
                              {
                                  Print("filled");
                                  string oco = Guid.NewGuid().ToString("N");
                
                 
                            Print("create tp");
                                  takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day,
                                        contr, Math.Max(High[2],High[3]), 0, oco, "Profit Target", Core.Globals.MaxDate, null);
                                 Print("create sl");
                                  stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day,
                                        contr, 0, Low[1], oco, "Stop Loss", Core.Globals.MaxDate, null);
                        Print("submit");
                                   myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                            //      longOrder = null;
                                  Print("submitted");
                
                              }
                
                         }
                        
                        
                          else if (shortOrder != null && shortOrder == e.Order)
                
                         {
                        Print("shortorder");
                              if (e.OrderState == OrderState.Filled)
                
                              {
                                Print("filled");
                                  string oco = Guid.NewGuid().ToString("N");
                
                             Print("create tp");    
                
                                  takeprofitOrder = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day,
                                        contr, 0, Math.Min(Low[2], Low[3]), oco, "Profit Target", Core.Globals.MaxDate, null);
                                  Print("create sl");
                                  stoplossOrder     = myAccount.CreateOrder(e.Order.Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day,
                                        contr, High[1], 0, oco, "Stop Loss", Core.Globals.MaxDate, null);
                                Print("submit");
                                   myAccount.Submit(new[] { stoplossOrder, takeprofitOrder });
                            //      shortOrder = null;
                                  Print("submitted");
                
                              }
                
                         }
                        
                         Print(takeprofitOrder+"   "+stoplossOrder);
                
                    }​

                Comment


                  #9
                  Hello gyilaoliver,

                  For most brokers you should use OnExecutionUpdate to drive order logic, OnOrderUpdate can also be used but would only be suggested for IB or Rithmic connections.

                  Do you see a problem if you use 1 quantity so no partial fill happens? I don't specifically see anything wrong with what you are trying to do, are you seeing the prints that you added all the way to submitted?

                  Comment


                    #10
                    Hi Jesse,

                    unfortunately if I use only 1 contract, I don't get a fill at all, the pending order gets cancelled as it is, usually i need over 10 contracts to get some partial fill.
                    My connection is Tradovate.
                    I can try to rewrite the code for OnExecutionUpdate, but unfortunately I didn't find any reference or example on how to do that with Account Add On, all I foud works for strategies only, my code is an indicator.

                    Regarding my code in its actual format, I get prints to the point where the takeprofitOrder gets created ( regardless if it is MarketPosition.Long or Short. So all the conditions are met, i get tte print that the next step is to create the takeprofitOrder, but it is not created, nor submitted ( I posted a screenshot in my last post, i attach it here again )

                    Click image for larger version

Name:	image.png
Views:	96
Size:	38.4 KB
ID:	1320258

                    Comment


                      #11
                      Hello gyilaoliver,

                      You should be able to manually move the order to fill before it gets cancelled or temporarily remove the cancel logic for the purpose of testing.

                      From the prints it appears that the code was stopped on the portion where you create and submit the order since the submitted is not present so potentially there is a problem with the values being passed to the method.

                      In regard to using OnExecutionUpdate it would be the same process as the strategy samples. You can use the passed in data with the event to get the execution information.


                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      589 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      342 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      103 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      555 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      552 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X