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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      67 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      36 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      60 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      62 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      53 views
      0 likes
      Last Post CarlTrading  
      Working...
      X