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

Trailing Stop

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

    #16
    Do you have the long and short trail together in one script? For some reason when I put them together the short side isn't doing anything.

    Comment


      #17
      Yes, I have short and long trail in once script.

      Comment


        #18
        Would you mind posting a copy?

        Thanks.

        Comment


          #19
          Hello brucelevy,

          My script was intentionally left one sided as this is not meant to be used a trading strategy but is instead intended to be used as an educational tool to show how to move a stop loss anytime you desire to any price you desire.

          This could be a trailing price, or could be set to the price of an indicator.

          In your script, to have both sides work, you will need completely separate logic for long trades than for short trades and these cannot use the same variable or signal names.

          May I confirm that you have separate logic for longs and for shorts and that these use completely different variables and signal names?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #20
            Hello BruceLevy,

            I have posted the stop and trailing stop part of my code below. As ChelseaB says, the variables need to be different (TrailS and TrailL), and note the separate logic for short and long positions. It was all done in OnBarUpDate so that it could be backtested - see ChelseaB's reply #13. Hope it helps.

            Code:
                  {
                    #region Variables
            			//Stops & profit target
            			private int		stoplossticks		= 6;
            			private int flatStopLossTicks = 6; //stoplossticks reset when market is flat, s/b same as 			stoplossticks setting above
            			private int	profittargetticks		= 200;
            			private double	currentStop			= 0;
            			private bool	trailS				= false;
            			private bool	trailL				= false;
            }
                   
            
            
            
            
            	protected override void Initialize()
                    {
            			// set the initial stop loss and profit target
            			
            			SetStopLoss(CalculationMode.Ticks, stoplossticks);
            			SetProfitTarget(CalculationMode.Ticks, profittargetticks);
            	}
            
            
                      protected override void OnBarUpdate()			
            		
                    {
            			if (CurrentBar < period) return;
            			
            
            	
            			
            			// Resets the stop loss to the original value when all positions are closed
            			if (Position.MarketPosition == MarketPosition.Flat)
            			{
            				currentStop = 0;			
            				stoplossticks = flatStopLossTicks;
            				trailS = false;
            				trailL = false;
            				SetStopLoss(CalculationMode.Ticks, stoplossticks);
            			}
            			
            				//Set initial stop loss position - short entry	
            				else if (Position.MarketPosition == MarketPosition.Short && trailS == false 
            				&& Math.Abs(((MAX(High,3)[0] + TickSize) - Position.AvgPrice) * tickPriceMultiplier) > stoplossticks
            				&& Math.Abs(((MAX(High,3)[0] + TickSize) - Position.AvgPrice) * tickPriceMultiplier) < maxStopTicks)
            				{
            				if (GetCurrentAsk() > (MAX(High,3)[0] + TickSize))
            				{
            				currentStop = GetCurrentAsk();
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}	
            				else
            				{
            				stoplossticks = (int) Math.Abs(((MAX(High,3)[0] + TickSize) - Position.AvgPrice) * tickPriceMultiplier); //(int) used in front due to CS0266 error 
            				currentStop = Position.AvgPrice + stoplossticks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}
            				Print (Time.ToString()+"stoplossticks:"+stoplossticks);
            				Print (Time.ToString()+" "+Close[0]+"Short - Initial Stop:"+currentStop);
            				}
            				else if (Position.MarketPosition == MarketPosition.Short && trailS == false 
            				&& Math.Abs(((MAX(High,3)[0] + TickSize) - Position.AvgPrice) * tickPriceMultiplier) > stoplossticks
            				&& Math.Abs(((MAX(High,3)[0] + TickSize) - Position.AvgPrice) * tickPriceMultiplier) >= maxStopTicks)
            				{
            				if (GetCurrentAsk() > (MAX(High,3)[0] + TickSize))
            				{
            				currentStop = GetCurrentAsk();
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}	
            				else
            				{
            				stoplossticks = maxStopTicks; //restricts max protective stop to maxStopTicks
            				currentStop = Position.AvgPrice + stoplossticks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				Print (Time.ToString()+"stoplossticks:"+stoplossticks);
            				Print (Time.ToString()+" "+Close[0]+"Short - Initial Stop:"+currentStop);
            				}
            				}
            				
            				
            			
            				//To set stop loss to trailing stop once price falls to currentStop minus 11ticks
            				else if (Position.MarketPosition == MarketPosition.Short && trailS == false 
            				&& Low[0] < currentStop - maxStopTicks * TickSize) //Possibly use GetCurrentAsk() instead of Low[0]
            			{
            				trailS = true; // after the trail is set to true, the stop loss trail takes over
            			}
            				if (Position.MarketPosition == MarketPosition.Short && trailS == true 
            				&& Low[0] <= currentStop - maxStopTicks * TickSize) //Possibly use GetCurrentAsk() instead of Low[0]
            			{	
            				currentStop = Low[0] + maxStopTicks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop);
            			}
            			
            			
            				//Set initial stop loss position - long entry	
            				else if (Position.MarketPosition == MarketPosition.Long && trailL == false 
            				&& Math.Abs(((MIN(Low,3)[0] - TickSize) - Position.AvgPrice) * tickPriceMultiplier) > stoplossticks
            				&& Math.Abs(((MIN(Low,3)[0] - TickSize) - Position.AvgPrice) * tickPriceMultiplier) < maxStopTicks)
            				{
            				if (GetCurrentBid() < (MIN(Low,3)[0] - TickSize))
            				{
            				currentStop = GetCurrentBid();
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}	
            				else
            				{
            				stoplossticks = (int) Math.Abs(((MIN(Low,3)[0] - TickSize) - Position.AvgPrice) * tickPriceMultiplier); //(int) used in front due to CS0266 error 
            				currentStop = Position.AvgPrice - stoplossticks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}
            				Print (Time.ToString()+"stoplossticks: "+stoplossticks);
            				Print (Time.ToString()+" "+Close[0]+"Long - Initial Stop:"+currentStop);
            				}				
            				else if (Position.MarketPosition == MarketPosition.Long && trailL == false 
            				&& Math.Abs(((MIN(Low,3)[0] - TickSize) - Position.AvgPrice) * tickPriceMultiplier) > stoplossticks
            				&& Math.Abs(((MIN(Low,3)[0] - TickSize) - Position.AvgPrice) * tickPriceMultiplier) >= maxStopTicks)
            				{
            				if (GetCurrentBid() < (MIN(Low,3)[0] - TickSize))
            				{
            				currentStop = GetCurrentBid();
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}	
            				else
            				{
            				stoplossticks = maxStopTicks;//restricts max protective stop to maxStopTicks
            				currentStop = Position.AvgPrice - stoplossticks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop); //My insert
            				}
            				Print (Time.ToString()+"stoplossticks: "+stoplossticks);
            				Print (Time.ToString()+" "+Close[0]+"Long - Initial Stop:"+currentStop);
            				}
            
            				//To set stop loss to trailing stop once price exceeds currentStop by 11ticks
            				else if (Position.MarketPosition == MarketPosition.Long && trailL == false 
            				&& GetCurrentBid() >= currentStop + maxStopTicks * TickSize) //Possibly use GetCurrentBid() instead of High[0]
            			{				
            				trailL = true; // after the trail is set to true, the stop loss trail takes over
            			}
            			
            				if (Position.MarketPosition == MarketPosition.Long && trailL == true 
            				&& High[0] >= currentStop + maxStopTicks * TickSize)	//Possibly use GetCurrentBid() instead of High[0]
            			{	
            				currentStop = High[0] - maxStopTicks * TickSize;
            				SetStopLoss(CalculationMode.Price, currentStop);
            			}
            
            
            
            	}
            Last edited by GeorgeW; 04-12-2016, 12:36 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cmtjoancolmenero, 04-25-2024, 03:58 PM
            19 responses
            106 views
            0 likes
            Last Post cmtjoancolmenero  
            Started by nleitman, Yesterday, 11:46 AM
            10 responses
            28 views
            0 likes
            Last Post nleitman  
            Started by ccbiasi, 11-23-2017, 06:06 AM
            6 responses
            2,216 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by Pattontje, Yesterday, 11:54 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_LuisH  
            Started by Pattontje, Today, 12:10 AM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_Erick  
            Working...
            X