Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Does this Trailing Stop code look right?

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

    Does this Trailing Stop code look right?

    Hi people.

    Since I can't find a simple example of a trailing stop code on here I thought I would take the example set Break even stop loss code and try to code a trailing stop from it.
    I'm not a real crash hot programmer more a hack really, learning as I go.

    Here is the result. Assume the variables are declared as they are in the full strategy.

    Comments about accuracy etc are welcomed.

    Code:
    // Resets the stop loss to the original value when all positions are closed
    switch (Position.MarketPosition)
    {
       case MarketPosition.Flat:
            SetStopLoss(CalculationMode.Ticks, stopLossTicks);
    	previousPrice = 0;
            break;
        case MarketPosition.Long:
                  // Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
                if (Close[0] > Position.AvgPrice + breakEvenTicks * TickSize
    		&& previousPrice == 0)
                    {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    		previousPrice = Position.AvgPrice;
    		PrintWithTimeStamp("previousPrice = "+previousPrice);
                    }
    		// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
    	else if (previousPrice	!= 0 ////StopLoss is at breakeven
     		&& GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize
    		)
    	        {
    		newPrice = previousPrice + trailStepTicks * TickSize;
    		SetStopLoss(CalculationMode.Price, newPrice);
    		previousPrice = newPrice;
    		PrintWithTimeStamp("previousPrice = "+previousPrice);
    		}
                    break;
        case MarketPosition.Short:
                    // Once the price is Less than entry price - breakEvenTicks ticks, set stop loss to breakeven
                   if (Close[0] < Position.AvgPrice - breakEvenTicks * TickSize
    		  && previousPrice == 0)
                     {
                     SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    		 previousPrice = Position.AvgPrice;
    		 PrintWithTimeStamp("previousPrice = "+previousPrice);
                     }
    		// Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
    	 else if (previousPrice	!= 0 ////StopLoss is at breakeven
     		  && GetCurrentAsk() < previousPrice - trailProfitTrigger * TickSize
    		  )
    		 {
    		 newPrice = previousPrice - trailStepTicks * TickSize;
    		 SetStopLoss(CalculationMode.Price, newPrice);
    		 previousPrice = newPrice;
    		 PrintWithTimeStamp("previousPrice = "+previousPrice);
    		 }
                     break;
               default:
                     break;
    }
    If anyone has corrections please post.

    Regards
    Chris

    #2
    Chris,

    Have not tested, but had a quick glance over. It looks fine, but curious, you could use SetTrailStop() for a trailing stop if you wanted. Either way, thank you for the efforts and sample you have posted here.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      Chris,

      Have not tested, but had a quick glance over. It looks fine, but curious, you could use SetTrailStop() for a trailing stop if you wanted. Either way, thank you for the efforts and sample you have posted here.
      I have implemented it this way as I was under the assumption that you cannot combine with a StopLoss and that I couldn't use a secondary profit trigger and step size other than a single tick step with SetTrailStop(). Am I incorrect with my understanding of the SetTrailStop() usage?

      If I am, could you show me a similar example to my post that shows how to use SetTrailStop() please.

      Which is the best method?

      Cheers

      Chris

      Comment


        #4
        You cannot combine the use of SetStopLoss() along with SetTrailStop() for the same entry order. SetTrailStop() is indeed for single tick steps.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          Chris,

          Have not tested, but had a quick glance over. It looks fine, but curious, you could use SetTrailStop() for a trailing stop if you wanted. Either way, thank you for the efforts and sample you have posted here.

          I have now tested this and it works just like the trailing stop in the ATM I have set it up with the variables to allow me to control it just like the Advanced ATM too, so if any one would like it I could post it up as a template or something.

          What do you think?

          Cheers
          Chris
          Last edited by alcamie; 05-27-2009, 02:16 AM.

          Comment


            #6
            Thanks for your efforts Chris, we would appreciate a post in our sharing section for reference.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, Yesterday, 10:06 AM
            0 responses
            22 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            19 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            14 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            10 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            41 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X