Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trail Stop not working any more

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

    Trail Stop not working any more

    My trailing stop is not working. It has something to do with the way that I have my initial stop and initial profit target coded as it appeared to work relatively correctly before (there were some errors, but the general idea was working correctly).

    At this point, it is not trailing at all. After it makes an initial stop movement after the initial profit target it filled, then it is supposed to trail below the SMA as soon as the SMA is above the price where the stop was initially moved to.

    Here is the code:

    Code:
    protected override void OnExecution(IExecution execution)
    		{
    			if (LongEntryA != null
    				&& LongEntryA == execution.Order)
    			{
    				// Initial Stop Losses & Profit Target
    				LongLimit = ExitLongLimit(TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongTarget[0],"LongLimit","LongEntryA");
    				LongStopA = ExitLongStop(TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongStop[0],"LongStopA","LongEntryA");
    				LongStopB = ExitLongStop(TrendPilotPricesLong(EntryPerc, StopPerc, TargetPerc).LongStop[0],"LongStopB","LongEntryB");
    			}
    		
    			if (LongLimit != null && LongLimit == execution.Order)
    			{
    				if (execution.Order.OrderState == OrderState.Filled)
    				{
    					ExitLongStop(Position.AvgPrice-(execution.Price - Position.AvgPrice),"LongStopB","LongEntryB");
    				}
    				
    				/*After the Entry B stop has made it initial move, we then wait for the SMA to catch up to the price.
    				When it catches up, then the Stop loss should trail the SMA. 
    				The distance that it trails below the SMA (for longs) will be the ((SignalCandle High) - (Signal Candle Low)*StopPerc).
    				The Trailing Stop should naturally only go up and never go down in case the SMA begins to turn downward. */
    				if (SMA(sMAPeriod)[0] - ((signalHigh - signalLow)*StopPerc) > LongStopB.StopPrice)
    				{
    					ExitLongStop(SMA(sMAPeriod)[0] - ((signalHigh - signalLow)*StopPerc),"LongStopB","LongEntryB");
    				}
    			}
    at this point, it is not trailing it and therefore it remains in the trade all the way through to the end of the testing period.

    Can you see why it is doing that?

    #2
    I have tried every possible combination that I can think of from removing elements to adding other elements. I am absolutely and completely stumped on this. :|

    Comment


      #3
      jg123,

      You are calling this in the OnExecution() and your IF statement is waiting for LongLimit to be filled.
      Once that is filled the first placement works but since that order is filled, it will not get called again thus your condition for SMA will never be called again.

      You would want to track that order in a different method and ensure that it will be called again after the initial placement
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Cal View Post
        jg123,

        You would want to track that order in a different method and ensure that it will be called again after the initial placement
        Would I then use something like OnOrderUpdate() and track when the initial stop movement has happened and then have it trail from the SMA at that point?

        Comment


          #5
          jg123,

          You might be better off tracking in OnBarUpdate().
          Just run a condition to check the market position that you are in first, such as long or short.
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            So are you saying that I just do not use OnExecution() at all?

            Comment


              #7
              No
              You can use OnExecution to place the Stop and Profit orders.
              However, if you are going to modifying the Trail stop that frequently you would want to use a method that will get called consistently and call your condition to change the Stop value
              Cal H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              558 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              324 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              545 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              547 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X