Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Order Exit, ATRTrailingStop

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

    Multiple Order Exit, ATRTrailingStop

    Hello,
    I am trying to create a scale out strategy. It takes a set target off 1st then 2nd when a certain indicator level is hit. The 3rd exit needs to be triggered when stochastic reached OB or OS conditions (target 2 criteria). I think the 3rd stoploss is getting reset and is not activating. Please help. Do I need to iOrder management?
    Thanks!

    Code:
    #region Stoploss reset, ATR Stop set, ATRTrailStop & Breakeven (BE)
    			// Resets the stop loss to the original value when all positions are closed
    		if (Position.MarketPosition == MarketPosition.Flat)
    			{
    				//Set initial stop - ATR Stop in ticks
    				int StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));
    				
    				SetStopLoss("SWING LONG1", CalculationMode.Ticks, StopLoss, false);
    				SetStopLoss("SWING LONG2", CalculationMode.Ticks, StopLoss, false);
    				SetStopLoss("SWING LONG3", CalculationMode.Ticks, StopLoss, false);
    				SetStopLoss("SWING SHORT1", CalculationMode.Ticks, StopLoss, false);
    				SetStopLoss("SWING SHORT2", CalculationMode.Ticks, StopLoss, false);
    				SetStopLoss("SWING SHORT3", CalculationMode.Ticks, StopLoss, false);
    								
    				SetProfitTarget("SWING LONG1", CalculationMode.Ticks, T1ticks);
    				SetProfitTarget("SWING SHORT1", CalculationMode.Ticks, T1ticks);
    				
    			}
    			
    			// If a long position is open, allow for stop loss modification to breakeven
    			else if (Position.MarketPosition == MarketPosition.Long)
    			{
    				// Once the price is greater than entry price +BETrigger ticks, set stop loss to breakeven + 1
    				if (Close[0] > Position.AvgPrice + BETrigger * TickSize)
    					{
    					SetStopLoss("SWING LONG1", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
    					SetStopLoss("SWING LONG2", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
    					SetStopLoss("SWING LONG3", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
    					}
    					
    				// If a long position is open, use ATRTrailingStop after Stochastic .K moves into OB	
    				if ((Close[0] > ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
    				&& (Stochastics(5, LgPeriodK, LgStochSmooth).K[(BarsSinceEntry("SWING LONG3"))] > LgStochUp))
    					{
    					SetStopLoss("SWING LONG3", CalculationMode.Price,  ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
    					}
    			}
    				
    			// If a short position is open, allow for stop loss modification to breakeven
    			else if (Position.MarketPosition == MarketPosition.Short)
    			{
    				// Once the price is greater than entry price +BETrigger ticks, set stop loss to breakeven + 1
    		        if (Close[0] < Position.AvgPrice - BETrigger * TickSize)
    					{
    					SetStopLoss("SWING SHORT1", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
    					SetStopLoss("SWING SHORT2", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
    					SetStopLoss("SWING SHORT3", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
    					}
    				
    				
    				// If a short position is open, use ATRTrailingStop after Stochastic .K moves into OS
    				if ((Close[0] < ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
    				&& (Stochastics(5, ShtPeriodK, ShtStochSmooth).K[(BarsSinceEntry("SWING SHORT3"))] < ShtStochLow))
    					
    					{
    					SetStopLoss("SWING SHORT3", CalculationMode.Price, ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
    					}
    			}

    #2
    Hello TOOLMachine462,

    Are you trying to trigger an exit after criteria is met or are you trying to move the stop loss from one price to another after certain criteria.

    (edit)
    Keep in mind, that once a stop loss is set, it cannot be unset and will continue to have its old values when a signal name is re-used. The stop loss can be moved by calling SetStopLoss() again with the same signal name and a different price, however, it cannot be unset.


    What is the exact behavior that is wrong? Is the stop loss not showing as working once the entry order fills? (you can check the state or price of an order on the orders tab of the Control Center for real-time orders or in the Strategy Performance)

    Is the stop loss working but not filling when the price hits it?

    Is the stop loss being submitted at an unexpected price?

    Is the stop loss being rejected or ignored due to be placed at an invalid price?

    Do you have TraceOrders = true; in the scripts Initialize() method?
    Last edited by NinjaTrader_ChelseaB; 06-23-2016, 07:12 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hello Chelsea,
      Thanks for your reply! The initial ATR static stop is working for all 3 orders. The breakeven is working for any open orders. The switch from being a breakeven to being an ATR Trailing Stop is not working for the 3rd order. It stays at a breakeven.

      What I am actually wanting to accomplish is make the 3rd order exit the trade when a bar closes under/over the ATRTrailingStop, not when the price only reaches that level. I was asking the original post question to try and get me close. Hope this makes sense.

      Thanks for your help.

      Comment


        #4
        Hi TOOLMachine462,

        Is there a condition that exits the script with a market order once the bar has closed under the ATR?

        Where is the code that exits the position at that moment?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          There isn't one yet. I was trying to get it to work as a regular stop first because I didn't know how to get it to work the other way. If you can help me make it place the order to exit when a bar closes against the ATR trail, that would be awesome!

          Comment


            #6
            Hi TOOLMachine462,

            To exit the market immediately use ExitLong().

            For example:

            if (Position.MarketPosition == MarketPosition.Long && Close[0] < myATRValue)
            {
            ExitLong("swing long3 exit", "SWING LONG3");
            }

            ExitLong(string signalName, string fromEntrySignal)
            http://ninjatrader.com/support/helpG...7/exitlong.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I know how to tell it when to exit using the ATRTrailingStop but I don't know how to tell it to do so only after overbought or oversold according to stochastic.

              Thanks

              Comment


                #8
                Hello TOOLMachine462,

                The Stochastic doesn't have plot that signals when overbought or oversold. What is your criteria for overbought or oversold?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  This is what I have so far but it's not working. Thanks

                  Code:
                  // If a long position is open, use ATRTrailingStop after Stochastic .K moves into OB	
                  				if ((Close[0] > ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
                  				&& (Stochastics(5, LgPeriodK, LgStochSmooth).K[(BarsSinceEntry("SWING LONG3"))] > LgStochUp))
                  					{
                  					SetStopLoss("SWING LONG3", CalculationMode.Price,  ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
                  }

                  Comment


                    #10
                    Hello TOOLMachine462,

                    In English, what are the rules you are wanting to turn into code?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Long position: Once the %K (.K) of Stochastic reaches above the upper stoch line, use the ATRTrailingStop as an exit, only when a bar closes below the ATRTrialingStop.

                      Short: Once the %K (.K) of Stochastic reaches below the lower stoch line, use the ATRTrailingStop as an exit, only when a bar closes above the ATRTrialingStop.

                      Just to be clear, when I say ATRTrailingStop, I'm talking about the indicator called ATRTrailingStop not a trailing stop based on the ATR. I got this indicator here on the forum.

                      Thanks,
                      ARKM

                      Comment


                        #12
                        Hi ARKM,

                        Thanks for the description. From the description the code appears to be correct.

                        At what part is the behavior failing?

                        Is the condition evaluating as true?

                        Is the price returned by the ATRTrailingStop indicator a valid price for the stop loss for the direction of the order (buy or sell)?

                        Is the order being ignored?

                        Do you have prints in your code to show how the condition is evaluating?
                        Do you have TraceOrders = true; in the Initialize() method to see if any orders are being ignored?

                        If so, please include the output from the Output Window for these prints and trace order information with your next post.

                        Below is a link to a quick video I've made previously that demonstrates using prints to understand behavior.
                        http://www.screencast.com/t/PlWr7PpFq
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Ok I tried the ExitLong() and I used the TraceOrders. It's still not exiting the L3. It prints the following repeatedly:

                          Code:
                          7/22/2016 2:52:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L1' Mode=Price Value=18435 Currency=0 Simulated=False
                          7/22/2016 2:52:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L2' Mode=Price Value=18435 Currency=0 Simulated=False
                          7/22/2016 2:52:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L3' Mode=Price Value=18435 Currency=0 Simulated=False
                          7/22/2016 2:52:00 PM Entered internal PlaceOrder() method at 7/22/2016 2:52:00 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='mV5 T2' FromEntrySignal='mV5 L2'
                          7/22/2016 2:52:00 PM Ignored PlaceOrder() method at 7/22/2016 2:52:00 PM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='mV5 T2' FromEntrySignal='mV5 L2' Reason='SignalName does not have a matching FromEntrySignal to exit'
                          7/22/2016 2:56:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L1' Mode=Price Value=18435 Currency=0 Simulated=False
                          7/22/2016 2:56:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L2' Mode=Price Value=18435 Currency=0 Simulated=False
                          7/22/2016 2:56:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L3' Mode=Price Value=18435 Currency=0 Simulated=False
                          I tried turning off all static stops for the 3rd order just to see if it was resetting on every bar update but it didn't make a difference.

                          What if I execute the ATRTrailingStop after the target 2 (T2) order is filled? This would be the same thing as using the Stoch above 80 or below 20 since that is the criteria for Target 2. I can get the ATRTrailingStop to work normal as a SetStopLoss but I don't want to exit when price just hits the trailing stop, I want to wait until price CLOSES on the other side. Thanks!
                          Last edited by TOOLMachine462; 07-26-2016, 09:41 PM.

                          Comment


                            #14
                            Hello TOOLMachine462,

                            I'd like to clarify the action that is not happening that you are expecting.

                            Based on post #3, you are no longer wanting to use SetStopLoss. Instead, you want to wait until the current price falls below the value from ATRTraillingStop and then submit an exit market order, is this correct?

                            I am seeing that an order is being ignored.
                            7/22/2016 2:52:00 PM Ignored PlaceOrder() method at 7/22/2016 2:52:00 PM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='mV5 T2' FromEntrySignal='mV5 L2' Reason='SignalName does not have a matching FromEntrySignal to exit'
                            This message states that there is no order that has been filled or is working that has the signal name 'mV5 L2'.

                            Is the 'mV5 T2' order the market exit order?

                            Are you certain there is a working order or a filled order for an open position where the entry order used the signal name 'mV5 L2'?

                            If so, can you print the order information for this order?
                            Replace myOrder with the IOrder handle of your entry order that uses the signal name of 'mV5 L2'.
                            Code:
                            Print(string.Format("{0} | name: {1} | order state: {2} | current price: {3} | stop price: {4} | limit price {5}", Time[0], myOrder.Name, myOrder.OrderState, Close[0], myOrder.StopPrice, myOrder.LimitPrice));
                            Last edited by NinjaTrader_ChelseaB; 07-27-2016, 11:29 AM.
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Yes you are correct. I would prefer to have it exit when price closes on other side of ATRTrailingStop. I am not even able to get the SetStoploss to work for the ATRTrailingStop for this strategy but it is working on another strategy and the code looks the same.

                              The TraceOrders below show that one position is close and then it tries to close it again causing the ignore. These are the same order names, mV5 L2. See below. I read on this forum that this happens and is normal?

                              Code:
                              7/22/2016 2:52:00 PM Entered internal PlaceOrder() method at 7/22/2016 2:52:00 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='mV5 T2' FromEntrySignal='mV5 L2' 
                              7/22/2016 2:52:00 PM Ignored PlaceOrder() method at 7/22/2016 2:52:00 PM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='mV5 T2' FromEntrySignal='mV5 L2' Reason='SignalName does not have a matching FromEntrySignal to exit'
                              I am not understanding the iOrder request you asked me to do. Unmanaged orders is something I am trying to understand but having trouble. "mV5 L3" or "mV5 S3" are the names of the order I am having troubles with. Please post a link to an example of what you are requesting. Thanks.
                              Last edited by TOOLMachine462; 07-28-2016, 04:52 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by bortz, 11-06-2023, 08:04 AM
                              47 responses
                              1,606 views
                              0 likes
                              Last Post aligator  
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post Javierw.ok  
                              Working...
                              X