Easy Language
Home Up

 

Assumption

The code assumes that the trailing stop is to be based on how much profit will disappear from the highest high value since you entered your trade to the current close. Thus, the plot will be based on highest high value since the entry point. If there is no buy order defined, the plot will be based on the highest high value since close last should have triggered the exit if a buy had occurred.

The code is importable to Supercharts/Tradestation from the following file: volatility and Percentage based trailing stop indicator

The code in the indicator looks like below:

Inputs: NumberOfATRs(3),TrueRangeLength(10),PercentTrail(10);
Variables: PosHigh(0),ATRValue(0),MyMarketPosition(0),MyATRTrailStop(0),MyPcntTrailStop(0);
ATRValue=AvgTrueRange(TrueRangeLength)*NumberOfATRs;
MyMarketPosition=I_MarketPosition;
If currentBar=1 then
    PosHigh=High;
If MyMarketPosition=1 and MyMarketPosition[1]=0 Then
    PosHigh=highest(High,2);
If High>PosHigh Then
    PosHigh=High;
MyATRTrailStop=PosHigh-ATRValue;
MyPcntTrailStop=PosHigh*(1-PercentTrail/100);
if MyMarketPosition=1 and close<MyATRTrailStop[1] then
    Alert("ATRTrail");
if MyMarketPosition=1 and close<MyPcntTrailStop[1] then
    Alert("PcntTrail");
plot1(MyATRTrailStop[1],"ATRTrail");
plot2(MyPcntTrailStop[1],"PcntTrail");
if close<MyATRTrailStop[1] and MyMarketPosition<1 then Begin
    PosHigh=High;
    MyATRTrailStop=PosHigh-ATRValue;
    MyPcntTrailStop=PosHigh*(1-PercentTrail/100);
End;