Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Order Exit, ATRTrailingStop

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

    #16
    Hello TOOLMachine462,

    If the position is already closed, you will not be able to close it again.. There is no output that you have posted that the position was closed. Also, trace orders does not show when an order fills or the position changes.

    To use an IOrder (in a managed strategy) make an IOrder variable. When placing the order assign this to the variable.
    Code:
    private IOrder myOrder;
    
    if (/* conditions to enter */)
    {
    myOrder = EnterLongLimit("mV5 L2");
    }
    
    if (myOrder != null)
    {
    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));
    }
    Below is a link to the help guide on IOrder.


    You can print the current position as well.
    Print(Position.MarketPosition.ToString());


    The order is ignored because there is no position created from an entry named 'mV5 L2'.

    Basically I would want to see that the entry order is filled and has this correct name and the position is still open.
    Last edited by NinjaTrader_ChelseaB; 07-29-2016, 08:57 AM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #17
      Both of the orders are Sell orders from entry signal mV5 L2, Exit signal name mV5 T2. So the first one listed below is the close position order but then it tries again and gets ignored because it's already closed. That's my understanding anyway. We are trying to fix exit order for mV5 L3 not L2. L2/T2 is working great.

      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'
      Last edited by TOOLMachine462; 07-28-2016, 05:04 PM.

      Comment


        #18
        I attached the output as you requested. This Output is with the SetStoploss() with the only criteria being that price is above the ATRTrailingStop before it sets the stoploss for a long. It's not working at all for the L3. Here is the relevant code LOL. I give up. I will send you the full script if you want to run it. I don't understand why the SetStopLoss is working on my other strategy but not this one. I really appreciate your help!

        Code:
        public class SwingRTHamBASEv5 : Strategy
            {
                #region Variables
               		
        		
        		//ATRStop variables
        		private int aTRStopPeriod = 						3;	// Default ATR Stop period
        		private double aTRStopMulti = 						1.5;// Default ATR Stop multiplier
        			
        		
        		//ATRTrailingStop variables
        		private int aTRTSperiod =	 						3;	// Sets ATR Trailing Stop period
        		private double	aTRTSmulti =						1.5;// Sets ATR Trailing Stop multiplier
        		
        		
                #endregion
        
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
        			{
             		TraceOrders = true;
        			}
                    //Long Indicators: deleted
        			
        			//Short Indicators: deleted
        			
        			//Stop Loss Indicator:
        			Add (ATR(ATRStopPeriod));
        			Add (ATRTrailingStop(ATRTSmulti, ATRTSperiod));
        						
        			// Triggers the exit on close function 30 seconds prior to session end 
            		ExitOnClose = true;
            		ExitOnCloseSeconds = 30;
        			
        	
        			EntriesPerDirection = 1;
        			EntryHandling 		= EntryHandling.UniqueEntries;
        			
        			CalculateOnBarClose = true;
                }
        			private IOrder myOrder;
               
                protected override void OnBarUpdate()
                {
        			#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("mV5 L1", CalculationMode.Ticks, StopLoss, false);
        				SetStopLoss("mV5 L2", CalculationMode.Ticks, StopLoss, false);
        				SetStopLoss("mV5 L3", CalculationMode.Ticks, StopLoss, false);
        				SetStopLoss("mV5 S1", CalculationMode.Ticks, StopLoss, false);
        				SetStopLoss("mV5 S2", CalculationMode.Ticks, StopLoss, false);
        				SetStopLoss("mV5 S3", CalculationMode.Ticks, StopLoss, false);
        								
        				SetProfitTarget("mV5 L1", CalculationMode.Ticks, T1ticks);
        				SetProfitTarget("mV5 S1", 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("mV5 L1", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
        					SetStopLoss("mV5 L2", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, false);
        					SetStopLoss("mV5 L3", CalculationMode.Price, Position.AvgPrice + 1 * TickSize, 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("mV5 S1", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
        					SetStopLoss("mV5 S2", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
        					SetStopLoss("mV5 S3", CalculationMode.Price, Position.AvgPrice - 1 * TickSize, false);
        					}
        				
        				
        				
        			}
        			//If a long position is open, use ATRTrailingStop after Stochastic .K moves into OB
        			else if (Position.MarketPosition == MarketPosition.Long)
        			{
        				if ((Close[0] > ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0]))
        				//&& (Stochastics(5, LgPeriodK, LgStochSmooth).K[0] > LgStochUp))
        					{
        					SetStopLoss("mV5 L3", CalculationMode.Price,  ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
        					}
        			}
        				
        			// If a short position is open, use ATRTrailingStop after Stochastic .K moves into OS
        			else if (Position.MarketPosition == MarketPosition.Short)
        			{
        				if ((Close[0] < ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0]))
        				//&& (Stochastics(5, ShtPeriodK, ShtStochSmooth).K[0] < ShtStochLow))
        					
        					{
        					SetStopLoss("mV5 S3", CalculationMode.Price, ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
        					}
        			}
        			#endregion
        								
                    #region Executions
        						
        			if (myOrder != null)
        			{
        			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));
        			}
        
        			// Condition set 1 (LONG)
                    if (Entry condition)
                        
                    	{
                        myOrder = EnterLong(1, "mV5 L1");
        		myOrder = EnterLong(1, "mV5 L2");
        		myOrder = EnterLong(1, "mV5 L3");
                    	}
        
                    // Condition set 2 (LONG)
                    if (Stochastics(5, LgPeriodK, LgStochSmooth).K[0] > LgStochUp)
        				
                    	{
                       ExitLong("mV5 T2", "mV5 L2");
        				
                    	}
        			
        			// Condition set 3 (LONG)
        			// If a long position is open, use ATRTrailingStop after Stochastic .K moves into OB	
        //			if (//(Close[0] > ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
        //				CrossAbove( Stochastics(5, LgPeriodK, LgStochSmooth).K, LgStochUp, BarsSinceEntry("mV5 L3"))
        //				//(Stochastics(5, LgPeriodK, LgStochSmooth).K[(BarsSinceEntry("mV5 L3"))] > LgStochUp
        //				&& CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod),0))
        //				{
        //					
        //				//ExitLong("ATR Exit", "mV5 L3");
        //				SetStopLoss("mV5 L3", CalculationMode.Price, ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
        //				}
        
        			if (myOrder != null)
        			{
        			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));
        			}		
        
                    // Condition set 4 (SHORT)
                    if (Entry condition)
                        
                    	{
                        myOrder = EnterShort(1, "mV5 S1");
        		myOrder = EnterShort(1, "mV5 S2");
        		myOrder = EnterShort(1, "mV5 S3");
                    	}
        
                    // Condition set 5 (SHORT)
                    if (Stochastics(5, ShtPeriodK, ShtStochSmooth).K[0] < ShtStochLow)
        				
                    	{
                       ExitShort("mV5 T2", "mV5 S2");
        				}
        			
        			// 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("mV5 S3"))] < ShtStochLow
        //				//&&
        //				CrossAbove(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod),0))
        //				{
        //					
        //				ExitShort("ATR Exit", "mV5 S3");
        //					
        //				//SetStopLoss("mV5 S3", CalculationMode.Price, ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
        //				}
        
        			#endregion
        Attached Files

        Comment


          #19
          Hello TOOLMachine462,

          I think was misunderstanding. I had attempted to clarify in post #14 that you are no longer using SetStopLoss with the order.

          This is because you only want to exit when the stochastics has reached a certain line and then the price falls to 1 tick below the ATRTrailingStop price.

          Instead, you are wanting to set SetStopLoss to the a number of ticks the ATRTrailingStop when placing the entry.


          Focusing on the output:

          The stop loss is placed at 9 ticks from the entry price at 8:24 and the entry is submitted
          7/18/2016 8:24:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L3' Mode=Ticks Value=9 Currency=0 Simulated=False

          7/18/2016 8:24:00 AM Entered internal PlaceOrder() method at 7/18/2016 8:24:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='mV5 L3' FromEntrySignal=''
          again at 8:32

          again at 8:52
          7/18/2016 8:52:00 AM Entered internal PlaceOrder() method at 7/18/2016 8:52:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='mV5 L3' FromEntrySignal=''

          7/18/2016 8:52:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L3' Mode=Ticks Value=16 Currency=0 Simulated=False
          The stop is also modified after this
          7/18/2016 9:04:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='mV5 L3' Mode=Price Value=18428 Currency=0 Simulated=False

          7/18/2016 9:04:00 AM Amended stop order: Order='NT-01709/Backtest' Name='Stop loss' State=Working Instrument='YM 09-16' Action=Sell Limit price=0 Stop price=18428 Quantity=1 Strategy='SwingRTHamBASEv5' Type=Stop Tif=Gtc Oco='NT-01376-2576' Filled=0 Fill price=0 Token='e914a1aaf1014d769e40b83eeaae8162' Gtd='12/1/2099 12:00:00 AM'
          again on 7/19 at 8:20
          and last at 11:16


          The FromEntrySignal and SignalNames appear to match.
          At this point I would expect that if the stop loss were not working, that the position would be increasing and not decreasing. What do you have entries per direction set to? Is this set to 3?
          If so, I would expect the script to start ignoring orders due to hitting the entries per direction.

          However, there is still no output to show that this is not working. We can see that the stop loss is being set before the order. We can see the entry order being submitted. After this there is no more information.

          You should print the position when it changes and also print the order information as well..

          Code:
          protected override void OnPositionUpdate(IPosition position)
          {
          	Print(string.Format("{0} | Position: {1} | Qty: {2}", Time[0], Position.MarketPosition, Position.Quantity));
          }
          
          protected override void OnOrderUpdate(IOrder order)
          {
          	if (order.OrderState == OrderState.PendingSubmit)
          	{
          		Print(string.Format("{0} | ORDER Placed | order: {1} | fromEntrySignal: {2} | stop price: {3} | limit price: {4}", order.Time, order.Name, order.FromEntrySignal, order.StopPrice, order.LimitPrice ));
          	}
          
          	else if (order.OrderState == OrderState.Filled)
          	{
          		Print(string.Format("{0} | ORDER FILL | order: {1} | fromEntrySignal: {2} | fillprice: {3}", order.Time, order.Name, order.FromEntrySignal, order.AvgFillPrice));
          	}
          }

          As a tip, when you are debugging it is always best to isolate the code in question (either by adding just the relevant code to a new script, or instead just commenting out any unrelated code). This is so that there are not as many prints and the prints are easier to understand, so that position changes are not combined with other orders, and you can also print execution information without mixing this as well.

          All that said, this code would not be able to run.
          if (Entry condition)
          This would not be able to compile. So this may not be the actual script you are testing.
          Last edited by NinjaTrader_ChelseaB; 07-29-2016, 10:30 AM.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Hello and thank you for your help. To clarify again what my intentions are, from post #6:
            Long position: Once the %K (.K) of Stochastic reaches above the upper stoch line (80), 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(20), 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.
            I am trying to avoid the stop runner candles with the long tails that take out stops and then continue. I can run a tighter stop if I can make it exit after close below. At this point I would be fine with just getting the freaking normal SetStopLoss=to ATRTrailingStop to work but I didn't think it was working. Is it showing it's working according to the Output? Thanks!

            Comment


              #21
              Hi TOOLMachine462,

              How do you know that the stop loss isn't working?

              You would need to demonstrate that the stop loss is not being placed and is not working for me so that I can help you to further investigate.

              When the entry fills, the stop should be submitted. Are you able to show the stop is not being submitted?

              If the stop is submitted, at what price is the stop working? Can you demonstrate that the price travelled through the stop without the stop filling.

              I have provided you with prints that can help answer these questions.


              Also, as another tip, to export a script so that this may be shared:
              To export a NinjaTrader 7 NinjaScript do the following:
              1. Click File -> Utilities -> Export NinjaScript
              2. Enter a unique name for the file in the value for 'File name:'
              3. Select the strategy from the objects list on the left -> click the right facing arrow ">" to add the strategy to the export
              4. Click the 'Export' button -> click 'yes' to add any referenced indicators to the export -> click OK to clear the export location message


              By default your exported file will be in the following location:
              • (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript\<export_file_name.z ip>


              Below is a link to the help guide on Exporting NinjaScripts.
              http://www.ninjatrader.com/support/h...nt7/export.htm
              Chelsea B.NinjaTrader Customer Service

              Comment


                #22
                Ok I am at work right now but I will try and get you what you requested this weekend. I know how to export script but I don't really want to post it publicly.

                Comment


                  #23
                  Hello TOOLMachine462,

                  Make a reduced version of the script that just deals with this order.

                  Remove any non-relevant code.

                  This also helps to further zero in on what is causing the error and prevents the NinjaTrader staff from spending too much time trying to reduce it for you so that we can work with only the code causing the behavior.

                  (When sending in a script to our support email, this is typically the first thing we ask for is a reduced version that highlights the behavior)


                  If you are not wanting to share your code, please send then to platformsupport [at] ninjatrader [dot] com.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #24
                    Will do! Thank you and have a great weekend!

                    Comment


                      #25
                      Hello Chelsea,
                      SOLVED: ALMOST
                      I emailed my reduced script, output, and image to support. It is working for the most part now that I am using CrossAbove/Below the ATRTrailingStop (ATRTS) & the ExitLong/Short. This activates it automatically when the price gets on desired side of ATRTS indicator. BUT it still has random failures and I have indicated this in the email. I do not know why it is failing. Please let me know if you need anything else. Sorry for the delay as I have been unable to get this accomplished earlier.

                      Comment


                        #26
                        Ok so I did the print like you requested but this is not the same trade as in the image I sent you kin the email. Will this work or do you want me to do that exact trade?. In the following code the 2 nd trade fails to use the ATRTrailingStop indicator to exit the trade when the bar closes on wrong side. Instead it stays in until Exit on CLose. Can you please help me decipher it. It crosses back and forth over the indicator several times. Thanks

                        Code:
                        8/1/2016 8:42:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='V4 L2' Mode=Ticks Value=24 Currency=0 Simulated=False
                        8/1/2016 8:42:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='V4 S2' Mode=Ticks Value=24 Currency=0 Simulated=False
                        8/1/2016 8:42:00 AM | name: ATR Exit | order state: Filled | current price: 4722.5 | stop price: 0 | limit price 0
                        8/1/2016 8:49:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='V4 L2' Mode=Ticks Value=30 Currency=0 Simulated=False
                        8/1/2016 8:49:00 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='V4 S2' Mode=Ticks Value=30 Currency=0 Simulated=False
                        8/1/2016 8:49:00 AM | name: ATR Exit | order state: Filled | current price: 4729.25 | stop price: 0 | limit price 0
                        8/1/2016 8:49:00 AM Entered internal PlaceOrder() method at 8/1/2016 8:49:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 L2' FromEntrySignal=''
                        8/1/2016 8:49:00 AM | ORDER Placed | order: V4 L2 | fromEntrySignal:  | stop price: 0 | limit price: 0
                        8/1/2016 8:49:00 AM | ORDER Placed | order: Stop loss | fromEntrySignal: V4 L2 | stop price: 4722 | limit price: 0
                        8/1/2016 8:49:00 AM | ORDER FILL | order: V4 L2 | fromEntrySignal:  | fillprice: 4729.5
                        8/1/2016 8:49:00 AM | Position: Long | Qty: 1
                        8/1/2016 8:56:00 AM | name: V4 L2 | order state: Filled | current price: 4732.5 | stop price: 0 | limit price 0
                        8/1/2016 8:56:00 AM Entered internal PlaceOrder() method at 8/1/2016 8:56:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 L2' FromEntrySignal=''
                        8/1/2016 8:56:00 AM Ignored PlaceOrder() method at 8/1/2016 8:56:00 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 L2' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                        8/1/2016 9:03:00 AM Entered internal PlaceOrder() method at 8/1/2016 9:03:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 L2' FromEntrySignal=''
                        8/1/2016 9:03:00 AM Ignored PlaceOrder() method at 8/1/2016 9:03:00 AM: Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 L2' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'
                        8/1/2016 10:41:00 AM Entered internal PlaceOrder() method at 8/1/2016 10:41:00 AM: BarsInProgress=0 Action=SellShort OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='V4 S2' FromEntrySignal=''
                        8/1/2016 10:41:00 AM | ORDER Placed | order: Close position | fromEntrySignal:  | stop price: 0 | limit price: 0
                        8/1/2016 10:41:00 AM | ORDER Placed | order: V4 S2 | fromEntrySignal:  | stop price: 0 | limit price: 0
                        8/1/2016 10:41:00 AM Cancelled pending exit order, since associated position is closed: Order='NT-00197/Backtest' Name='Stop loss' State=Working Instrument='NQ 09-16' Action=Sell Limit price=0 Stop price=4722 Quantity=1 Strategy='YM OPT 7m $5.6k/.5k 6.0 79% 6/6-7/30' Type=Stop Tif=Gtc Oco='NT-00132-444' Filled=0 Fill price=0 Token='43f1fa1d23dc4b39b202ec9f197ec7c2' Gtd='12/1/2099 12:00:00 AM'
                        8/1/2016 10:41:00 AM | ORDER FILL | order: Close position | fromEntrySignal:  | fillprice: 4755.25
                        8/1/2016 10:41:00 AM | Position: Flat | Qty: 0
                        8/1/2016 10:41:00 AM | ORDER Placed | order: Stop loss | fromEntrySignal: V4 S2 | stop price: 4762.75 | limit price: 0
                        8/1/2016 10:41:00 AM | ORDER FILL | order: V4 S2 | fromEntrySignal:  | fillprice: 4755.25
                        8/1/2016 10:41:00 AM | Position: Short | Qty: 1
                        8/1/2016 3:15:00 PM Cancelled order due to end of session handling: BarsInProgress=0: Order='NT-00200/Backtest' Name='Stop loss' State=Working Instrument='NQ 09-16' Action=BuyToCover Limit price=0 Stop price=4762.75 Quantity=1 Strategy='YM OPT 7m $5.6k/.5k 6.0 79% 6/6-7/30' Type=Stop Tif=Gtc Oco='NT-00134-444' Filled=0 Fill price=0 Token='4985c54e11f54b57b2ccc4a48c6a52c2' Gtd='12/1/2099 12:00:00 AM'
                        8/1/2016 3:15:00 PM | ORDER Placed | order: Exit on close | fromEntrySignal:  | stop price: 0 | limit price: 0
                        8/1/2016 3:15:00 PM | ORDER FILL | order: Exit on close | fromEntrySignal:  | fillprice: 4745
                        8/1/2016 3:15:00 PM | Position: Flat | Qty: 0
                        Attached Files
                        Last edited by TOOLMachine462; 08-15-2016, 01:21 PM.

                        Comment


                          #27
                          Hello TOOLMachine462,

                          I am currently working with you through email. Would you like to return to the forum?

                          Lets clarify the behavior that is incorrect.

                          You are no longer attempting to set a stop loss to the price of an ATRTrailingStop indicator. Instead, you are now attempting to place an exit market order when a condition is true. Is this correct?

                          Have you added a print to see when the condition is true?
                          Is the condition evaluating as true, and the order not being submitted?
                          Is the condition true, the order is being submitted, but the order is not being filled?

                          May we see the output of the prints that show the condition is evaluating as true?
                          Last edited by NinjaTrader_ChelseaB; 08-16-2016, 09:43 AM.
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #28
                            I think I figured it out. I will have to do some further testing but from what I can tell, the indicator has more lag than I thought. I think it is taking more bars to make a crossover than on just the current bar. I remembered that I had to change the crossabove/below bar from [0] to [1] and that made me wonder if it's just missing some because the ATRTS is crossing over price further back on occasion. Soooo, I added CrossAbove/Below bars [0] || [1] || [2] and I can't find any failures in the last 2 months on the 2 optimizations I just checked. Crazy! I'll have to verify further later, I'm exhausted and going to bed. Thanks for all your help!!!!

                            Comment


                              #29
                              Ok I have some print statements. It just stops updating for the rest of the session for no reason @ 9:31am on 8/11/2016. 8/12 goes all the way to the normal session close for my template. Template looks fine, all days are exactly the same, 1am-3:15pm. I included image also. Is my historical data maybe corrupt? It's plotting data though....

                              Also, it seems like the prints are backwards, current bar crosses below and it says false and that the previous 3 bars are true but they are above the trail @ 9:03am.

                              Why is the current bar L0 print only showing when there's a true within the print set?

                              Image is 7min chart. 1st bar after 9am is 9:03am

                              Code:
                              8/11/2016 9:03:00 AM - CrossBelow L0:False
                              8/11/2016 9:03:00 AM - CrossBelow L1:True
                              8/11/2016 9:03:00 AM - CrossBelow L2:True
                              8/11/2016 9:03:00 AM - CrossBelow L3:True
                              8/11/2016 9:10:00 AM - CrossBelow L0:False
                              8/11/2016 9:10:00 AM - CrossBelow L1:False
                              8/11/2016 9:10:00 AM - CrossBelow L2:True
                              8/11/2016 9:10:00 AM - CrossBelow L3:True
                              8/11/2016 9:17:00 AM - CrossBelow L0:False
                              8/11/2016 9:17:00 AM - CrossBelow L1:False
                              8/11/2016 9:17:00 AM - CrossBelow L2:False
                              8/11/2016 9:17:00 AM - CrossBelow L3:True
                              8/11/2016 9:24:00 AM - CrossBelow L1:False
                              8/11/2016 9:24:00 AM - CrossBelow L2:False
                              8/11/2016 9:24:00 AM - CrossBelow L3:False
                              8/11/2016 9:31:00 AM - CrossBelow L1:False
                              8/11/2016 9:31:00 AM - CrossBelow L2:False
                              8/11/2016 9:31:00 AM - CrossBelow L3:False
                              8/12/2016 1:07:00 AM - CrossBelow L1:False
                              8/12/2016 1:07:00 AM - CrossBelow L2:False
                              8/12/2016 1:07:00 AM - CrossBelow L3:False
                              8/12/2016 1:14:00 AM - CrossBelow L1:False
                              8/12/2016 1:14:00 AM - CrossBelow L2:False
                              8/12/2016 1:14:00 AM - CrossBelow L3:False
                              This is what I coded just FYI

                              Code:
                              // Condition set 2 (LONG EXIT)
                                          if (Position.MarketPosition == MarketPosition.Long
                                              && CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 0)
                              				|| CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 1)
                              				|| CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 2)
                              				|| CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 3)
                              				)
                              Print(Time[0].ToString()+" - CrossBelow L0:"+CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 0).ToString());
                              Print(Time[0].ToString()+" - CrossBelow L1:"+CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 1).ToString());
                              Print(Time[0].ToString()+" - CrossBelow L2:"+CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 2).ToString());
                              Print(Time[0].ToString()+" - CrossBelow L3:"+CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 3).ToString());
                                          {
                                              ExitLong("ATR Exit", "V1 L1");
                                          }
                              Attached Files
                              Last edited by TOOLMachine462; 08-19-2016, 09:36 PM. Reason: add L0 question

                              Comment


                                #30
                                Hello TOOLMachine462,

                                You cannot put a print in between an if statement and the action block.

                                if (1 == 0)
                                Print("hello");
                                {
                                Print("goodbye");
                                }

                                This will always print goodbye and will never print hello. The action block is not associated with the if statement.

                                if (1 ==0)
                                {
                                Print("hello");
                                Print("goodbye");
                                }

                                This will never print anything ever as the action block is associated with the if and the if will never evaluate as true.

                                if (1 == 1)
                                {
                                Print("hello");
                                Print("goodbye");
                                }

                                This will print both hello and goodbye as the action block is associated with the if and the if will always evaluate as true.


                                Your condition is followed by a print. This means the print will only appear when the condition is true and the action block below it, is not associated with any if and the code for this will always be run.

                                Move the print outside of the action block and print the values of the variables in the if statement so that we may see how the if statement will evaluate.

                                Below is a link to a video that demonstrates this. Please watch this video and then setup a print outside of the if statement that prints the values of the variables in the conditions.
                                Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...



                                That said, the following code is redundant:
                                Code:
                                || CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 1)
                                || CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 2)
                                || CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 3)
                                CrossBelow(Close, ATRTrailingStop(ATRTSmulti, ATRTSperiod), 3)
                                This already checks that the was a crossbelow within 1 bar, and also within 2 bars, and also within 3 bars. The other conditions are redundant as they check the same thing.


                                I am not able to see that the chart is a 7 minute chart. There are no timestamps on the bars and we are not able to see the top of the chart.
                                However, we are able to see that the session does not start at 9:00. This means that its entirely possible to have a bar that ends in 9:03 as this is not the first evaluated bar. Your prints only show if the condition is true. This means if the condition is not true, there will be no print for that bar.
                                Last edited by NinjaTrader_ChelseaB; 08-22-2016, 02:42 PM.
                                Chelsea B.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by AveryFlynn, Today, 04:57 AM
                                1 response
                                10 views
                                0 likes
                                Last Post NinjaTrader_Erick  
                                Started by Max238, Today, 01:28 AM
                                4 responses
                                37 views
                                0 likes
                                Last Post Max238
                                by Max238
                                 
                                Started by r68cervera, Today, 05:29 AM
                                1 response
                                9 views
                                0 likes
                                Last Post NinjaTrader_ChelseaB  
                                Started by geddyisodin, Today, 05:20 AM
                                1 response
                                11 views
                                0 likes
                                Last Post NinjaTrader_Gaby  
                                Started by timko, Today, 06:45 AM
                                2 responses
                                14 views
                                0 likes
                                Last Post NinjaTrader_ChristopherJ  
                                Working...
                                X