Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Initialize Trailing Stop After Other Order Triggered

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

    Initialize Trailing Stop After Other Order Triggered

    Hello

    I am noticing an error in one of my strategies.

    Currently the trailing stop is being initilized too soon.

    Two IOrder exit variables are called
    LongLimit & ShortLimit.

    The Trailing stop is to be initilized after one of these two orders are triggered and executed.

    Currently, in OnBarUpdate() I have this:

    Code:
    			// Long SMA Trailing Stop
    			if (BarsInProgress == 1)
    			{
    				if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
    				
    				{
    					adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
    					LongStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] - ((signalHigh-signalLow)*StopPerc),"LongBStop","LongEntryB");
    				}
    				
    			// Short SMA Trailing Stop
    				if (Close[0] < SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc));
    				{
    					adjustedStopShort = SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc);
    					ShortStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc),"ShortBStop","ShortEntryB");
    				}
    			}
    But what i am thinking that I should do is move this code to OnExecution() and do something like this:

    Code:
    if (LongLimit != null
         && LongLimit = execution.Order)
    { 
         			// Long SMA Trailing Stop
    			if (BarsInProgress == 1)
    			{
    				if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
    				
    				{
    					adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
    					LongStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] - ((signalHigh-                       signalLow)*StopPerc),"LongBStop","LongEntryB");
    				}
    }
    Can you just confirm if I am thinking correctly with this? Getting into trailing stops and multi-time frame coding seems to get pretty confusing for me pretty quickly.

    Thanks

    #2
    Originally posted by jg123 View Post
    Hello

    I am noticing an error in one of my strategies.

    Currently the trailing stop is being initilized too soon.

    Two IOrder exit variables are called
    LongLimit & ShortLimit.

    The Trailing stop is to be initilized after one of these two orders are triggered and executed.

    Currently, in OnBarUpdate() I have this:

    Code:
                // Long SMA Trailing Stop
                if (BarsInProgress == 1)
                {
                    if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
                    
                    {
                        adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
                        LongStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] - ((signalHigh-signalLow)*StopPerc),"LongBStop","LongEntryB");
                    }
                    
                // Short SMA Trailing Stop
                    if (Close[0] < SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc));
                    {
                        adjustedStopShort = SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc);
                        ShortStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] + ((signalHigh-signalLow)*StopPerc),"ShortBStop","ShortEntryB");
                    }
                }
    But what i am thinking that I should do is move this code to OnExecution() and do something like this:

    Code:
    if (LongLimit != null
         && LongLimit = execution.Order)
    { 
                     // Long SMA Trailing Stop
                if (BarsInProgress == 1)
                {
                    if (Close[0] > SMA(BarsArray[0], sMAPeriod)[0]+((signalHigh-signalLow)*StopPerc))
                    
                    {
                        adjustedStopLong = SMA(BarsArray[0], sMAPeriod)[0]-((signalHigh-signalLow)*StopPerc);
                        LongStopB = ExitLongStop(0,true,DefaultQuantity,SMA(BarsArray[0], sMAPeriod)[0] - ((signalHigh-                       signalLow)*StopPerc),"LongBStop","LongEntryB");
                    }
    }
    Can you just confirm if I am thinking correctly with this? Getting into trailing stops and multi-time frame coding seems to get pretty confusing for me pretty quickly.

    Thanks
    If you wish to perform an action based on execution of an order, they you should be using the OnExecution() event handler.

    Comment


      #3
      Hello jg123,

      Thank you for your post.

      OnExecution() is the correct way to go about it. A trailing stop needs to trail during price updates, OnExecution() only calls during executions. So you may wish to set a variable in OnExecution when the entries are executed and then check for this variable in OnBarUpdate() to set the trailing stop. When the stop executes set the variable back to it's original value.

      Please let me know if you have any questions.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      639 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      366 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      107 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      569 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      572 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X