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

Issue with SL/TP

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

    Issue with SL/TP

    Hello everybody,

    I got some problem to well-run my system. I just would like to have one trade per candle (if it meets a condition) with one stop loss and one take profit different for each trade. I use the unmanaged approach to do this but it doesn't work correctly with SL/TP, why ?

    Thanks a lot !!

    Code:
    		double Condition, Target, StopL;
    		private IOrder entryOrder = null;
    		private IOrder entryOrder2 = null;
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose = true;
    	    Unmanaged = true;
    			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			
    
    			if (Condition == 1) {
    				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, Target, StopL, "", "Enter Long");
    			} else entryOrder2 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, Target, StopL, "", "Enter Sell");
    
    }

    #2
    Hello,

    When you are calling SubmitOrder() in the unmanaged approach, the "stop price" and "limit price" parameters do not represent an automatically placed stop loss and profit target, but rather the stop or limit price to use if the order is a stop market, stop limit, or limit order.

    When using the unmanaged approach, you will need to place pending orders manually to be used as your profit target and stop loss. You will need to call SubmitOrder() two more times, using either a stop market, stop limit, or limit order for the two sides of the bracket.

    Please let me know if I can assist further.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Haaaaan you're right, thanks ! I'm tired

      But, how could I (or Do I need to) link the Stoploss/Target profit with the trade ? Because for exemple, if I got one trade with SL =5 at the candle 1, and one trade with SL = 8 at the candle two, the stop loss of my first trade won't change ?

      Thanks

      Comment


        #4
        There is not really a way to link the orders together. That is one of the challenging aspects of the unmanaged approach -- you will always need to keep track of where your orders are and how you intend to use them. You will also need to set up your own OCO (one cancels other) logic to ensure that the stop loss is cancelled when the profit target is hit, and vice versa. Using unique signal names can help to keep track of which orders are which ("Target1," "Target2," etc.).
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Hmmm okay thanks.

          By the way I didn't ask but your answer seems to mean I can do what I would like to do with the managed approach ? Because I just didn't use it because I thought it was impossible to have several orders running at the same time with different SL/TP

          Comment


            #6
            You are correct -- the managed approach would probably not meet your needs here, because of the need to place multiple sets of stop losses and targets.
            Dave I.NinjaTrader Product Management

            Comment


              #7
              Hmm thanks, because I guess I have to create the same number of OCO as the potential number of running orders at the same time, right ? Complicated if it's the case..

              Comment


                #8
                Yes, it can get a bit complex. You will essentially need to check the signal name of the order in the OnOrderUpdate() or OnExecution() events to see when one of the bracket orders are filled, then cancel the other order accordingly. You will need to have a separate set of logic for each set of brackets you would like to be active at once.
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  Thanks Dave,

                  Well I got an idea, I'll do something like this :

                  Code:
                  //////Order
                  			if (Condition == 1  && Numberofposition ==1) {
                  				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "one", "Enter Long");
                  				stoporder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "one", "Enter Long");
                  				targetorder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "one", "Enter Long");
                  			} else {
                  				entryOrder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "two", "Enter Sell");
                  				stoporder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "two", "Enter Long");
                  				targetorder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target,0, "two", "Enter Long");
                  }
                  
                  
                  
                  //////Order
                  			if (Condition == 1  && Numberofposition ==2) {
                  				entryOrder3 = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "X", "Enter Long");
                  				stoporder3 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "X", "Enter Long");
                  				targetorder3 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "X", "Enter Long");
                  			} else {
                  				entryOrder4 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, 0, 0, "Y", "Enter Sell");
                  				stoporder4 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, StopL, "Y", "Enter Long");
                  				targetorder4 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "Y", "Enter Long");
                  			}

                  -No mistake ?
                  - How could I retrieve the number of orders already opened and not closed ?

                  Comment


                    #10
                    Everything looks alright, but I recommend testing it to be sure.

                    To determine the number of open orders, you will need to keep track of them manually in your code. Whenever an entry order is filled, you can increment a variable, and whenever a position is closed, you can decrement that variable.

                    Please let me know if I can assist further.
                    Dave I.NinjaTrader Product Management

                    Comment


                      #11
                      Thanks for answering Dave !

                      Well, so the last issue seems to be this "number of running orders" variable as I know when an order is oppened (so no problem to increment the variable) but not when it's closed.

                      You said :

                      You will essentially need to check the signal name of the order in the OnOrderUpdate() or OnExecution() events to see when one of the bracket orders are filled

                      The idea would be to check if the orders called "X" or "Y" or "Z" is running, right ? At the end we would know how many orders are running by checking them one by one thanks to their names.
                      If I'm right, do you have any example of these few code lines or any keyword to use ? I never saw this before

                      Thanks
                      Last edited by After; 03-25-2015, 11:50 AM.

                      Comment


                        #12
                        I was actually thinking that you could increment a variable in the same code block that you enter a position, then decrement that variable every time a filled order is closed or a target/stop are executed. This way, you would not have to worry about checking to see which trades are active at a given time -- you will always have an accurate running count that increases and decreases as needed.

                        If you do want to loop through all orders to check their states, you could do something like the following:

                        Code:
                        foreach(IOrder myOrder in Orders)
                           {
                           [I]check state and add to count...[/I]
                           }
                        Dave I.NinjaTrader Product Management

                        Comment


                          #13
                          It's a very good idea Dave. As I said, no problem to increment the variable, but how could I know easily when an order is closed to decrement it ?

                          Comment


                            #14
                            Something like this within OnOrderUpdate() should do the trick:

                            Code:
                            foreach(IOrder myOrder in Orders)
                               {
                               if(myOrder.Name == "Entry1" && myOrder.OrderState == OrderState.Filled)
                               openTrades++;
                            
                               if(myOrder.Name == "Stop1" && myOrder.OrderState == OrderState.Filled) 
                               openTrades--;
                               }
                            etc.
                            Last edited by NinjaTrader_DaveI; 03-25-2015, 01:23 PM.
                            Dave I.NinjaTrader Product Management

                            Comment


                              #15
                              Thanks, I didn't know at all these key-words.

                              I tried to have only one running trade at once.. as you can see on the picture it doesn't work.

                              Code:
                               protected override void OnBarUpdate()
                                      {
                              	
                              	
                              
                              			//////Order
                              			if (Condition == 1 && openTrades==0 ) {
                              				entryOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, 1, 0, 0, "one", "Enter Long");
                              				stoporder = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "one", "Stop");
                              				targetorder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "one", "Target");
                              			} else if (openTrades ==0) {
                              				entryOrder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Market, 1, Target, StopL, "two", "Enter Sell");
                              				stoporder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0,StopL, "two", "Stop2");
                              				targetorder2 = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Target, 0, "two", "Target2");
                              			}
                              			
                              			foreach(IOrder myOrder in Orders)
                               			  {
                              			  	 if(myOrder.Name == "Enter Long" && myOrder.OrderState == OrderState.Filled)
                                				 openTrades++;
                              				
                              				if(myOrder.Name == "Enter Sell" && myOrder.OrderState == OrderState.Filled)
                                				 openTrades++;
                              
                                		  		 if(myOrder.Name == "Stop" && myOrder.OrderState == OrderState.Filled) 
                                				openTrades--;
                              				if(myOrder.Name == "Target" && myOrder.OrderState == OrderState.Filled) 
                                				openTrades--;
                              				if(myOrder.Name == "Stop2" && myOrder.OrderState == OrderState.Filled) 
                                				openTrades--;
                              				if(myOrder.Name == "Target2" && myOrder.OrderState == OrderState.Filled) 
                                				openTrades--;
                                		    }
                              
                              			
                                      }
                              Any idea why it's wrong ?
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by AaronKoRn, Today, 09:49 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Today, 08:42 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Today, 07:51 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,980 views
                              3 likes
                              Last Post jhudas88  
                              Started by rbeckmann05, Today, 06:48 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post rbeckmann05  
                              Working...
                              X