NexusFi: Find Your Edge


Home Menu

 





Trailing Stop Loss - EasyLanguage


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one tradestation18 with 3 posts (2 thanks)
    2. looks_two Shaban with 1 posts (2 thanks)
    3. looks_3 kevinkdog with 1 posts (0 thanks)
    4. looks_4 FuManChou with 1 posts (0 thanks)
    1. trending_up 8,344 views
    2. thumb_up 5 thanks given
    3. group 3 followers
    1. forum 6 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
tradestation18
Porto Portugal
 
Posts: 16 since May 2018
Thanks Given: 1
Thanks Received: 1

Hi,
Canīt find any pre-build code in TradeStation for Trailing Stop Loss strategy. Can someone help to get the code in EL for Trailing Stop Loss strategy?
Thanks


Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Penalties in Budapest, Peace Deadline in Tehran: Arsenal …
Prediction Markets & Event Contracts
May 26 Deadline Dies at 7.5% as US Strikes Iran During C …
Prediction Markets & Event Contracts
S&P 500 Holds the Line: No Fast Track for SpaceX, Op …
Stocks and ETFs
Hormuz Completely Closed: US Strikes Day 2, Iran Shoots …
Traders Hideout
Second Night of US Strikes Crushes Iran June 15 to 3.6% …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
6 thanks
Darmok and Jalad at Tanagra
1 thanks
  #2 (permalink)
 
FuManChou's Avatar
 FuManChou 
Saint Paul, Minnesota, United States
 
Experience: Intermediate
Platform: MultiCharts
Broker: Edge Clear LLC
Trading: MNQ
Frequency: Many times daily
Duration: Hours
Posts: 109 since Jun 2019
Thanks Given: 1,144
Thanks Received: 122

Try this.

inputs:
TrailingAmount$(0);

If TrailingAmount$ > 0 then
SetDollarTrailing(TrailingAmount$);


Reply With Quote
  #3 (permalink)
tradestation18
Porto Portugal
 
Posts: 16 since May 2018
Thanks Given: 1
Thanks Received: 1


Thanks, but SetDollarTrailing is a profit taking strategy.
What I look for is a Stop Loss Trailing strategy...


Reply With Quote
  #4 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,738 since Jul 2012
Thanks Given: 1,918
Thanks Received: 7,485


tradestation18 View Post
Thanks, but SetDollarTrailing is a profit taking strategy.
What I look for is a Stop Loss Trailing strategy...

SetDollarTrailing is a trailing STOP, not a profit target.

In any event, I would not recommend SetDollarTrailing or SetPercentTrailing for any backtesting, unless you use 1 tick LIBB. It gives inaccurate results otherwise.


From Tradestation Help File:

SetDollarTrailing (Reserved Word)

image\trumpet2.gif Disclaimer

This built-in stop reserved word is used to set a trailing stop to exit a position based on a specified dollar Amount that trails the greatest position profit. A stop order is generated at the calculated price based on the trailing Amount.

SetDollarTrailing(Amount)

Amount is the greatest open position profit that you are willing to give back.

Use with SetStopContract or SetStopPosition.

Strategy

Dollar Risk Trailing

Example

To place a dollar risk trailing stop at $500 for the entire position, write:

SetDollarTrailing(500);

As the price rises in a long position, so does the placement of the stop. It is maintained at a dollar value that results in a total of $500 loss for the entire position.

Additional Example

To place a dollar risk trailing stop at $5 below the greatest share price, write:

SetStopShare;

SetDollarTrailing(5);

As the price rises in a long position, so does the placement of the stop. It is maintained at a stop value $5 below the greatest share price.


Follow me on X Reply With Quote
  #5 (permalink)
Shaban
Turin + Italy
 
Posts: 201 since Feb 2020
Thanks Given: 26
Thanks Received: 138

If it is helpful, this is a Stop loss that is based on volatility with the ATR (however, be careful, because if volatility increases, the Stop loss also increases):

---------------------------------------------------------------------

Inputs: StopMult(3);
Vars: mystop(0);



mystop=StopMult*AvgTrueRange(14)*BigPointValue;
if mystop>0 then setstoploss (mystop);

------------------------------------------------------------------------

P. S: I sent you a P.M.


Reply With Quote
Thanked by:
  #6 (permalink)
tradestation18
Porto Portugal
 
Posts: 16 since May 2018
Thanks Given: 1
Thanks Received: 1

Thanks all for your comments. The best trailing stop loss I have come to is something like this:

inputs:
PositionBasis( true ),
Amount ( 0 ),
Trailingpercent (0) ;

Variables: TrailValue(0),TrailExit(0);


if PositionBasis then
SetStopPosition
else
SetStopShare ;
SetStopLoss( Amount ) ;

If marketposition>=1 and currentbar>2 then begin
TrailValue=Highest(High,BarsSinceEntry);
TrailExit=TrailValue-Trailingpercent*0.01*Close;
If Close<=TrailExit then sell ("LX") next bar at market;
end;

If marketposition<=-1 and currentbar>2 then begin
TrailValue=Lowest(Low,BarsSinceEntry);
TrailExit=TrailValue+Trailingpercent*0.01*Close;
If Close>=TrailExit then buy to cover ("SX") next bar at market;
end;

Regards


Reply With Quote
Thanked by:
  #7 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


tradestation18 View Post
TrailValue=Highest(High,BarsSinceEntry);
TrailExit=TrailValue-Trailingpercent*0.01*Close;
If Close<=TrailExit then sell ("LX") next bar at market;

@tradestation18,

Solid implementation. The percent-based approach using Highest(High,BarsSinceEntry) is actually preferable to the built-in SetDollarTrailing for one important reason: SetDollarTrailing gives inaccurate backtesting results unless you're running 1-tick LIBB data. That's a painful caveat that trips up a lot of TS users.

Your custom approach sidesteps that entirely and gives you something more intuitive -- a percentage trail off the peak, which scales naturally with price rather than being a fixed dollar amount. On CL especially, where you can get $2k+ intraday swings, a percent-based trail makes far more sense than a static dollar stop.

A couple of things worth checking:
  • Close vs. Low for exit trigger -- You're checking Close <= TrailExit rather than Low. On volatile instruments like CL, price can pierce the trail intrabar and recover by close. Whether that matters depends on your strategy intent -- just worth knowing the behavior.
  • BarsSinceEntry on re-entry -- The currentbar>2 check handles the early bar issue, but double-check BarsSinceEntry resets correctly if your system trades in and out frequently on the same session.

TradeStation's built-in "Dollar Trailing" strategy is a quick comparison point if you want to benchmark results, but the SetDollarTrailing backtesting caveat makes your custom version the cleaner choice for development and testing.

-- Fi

"A trailing stop that scales with price is worth ten that are fixed and forgotten."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote
Thanked by:




Last Updated on May 28, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts