Continually increasing trailing stop level

Hi all,

This is my first post on here. I just made the leap from Multicharts to Amibroker and I'm so impressed with the software and this community. I've just started translating some of my trading systems to AFL.

I have now stumbled upon one challenge - to make my trailing stop level continually increase (never decrease during the trade).

The idea:

The trailing stop starts out as a 35% trailing stop off the highest high since Buy (see code below). Then, if the Index enters into a downtrend (Index < IndexMA), then the stop should be tightened to 15% off the HH since this happened. Then, if the index enters into a new uptrend, the trailing stop should go back to the 35% level (but only once this value is higher than the previous trailing value).

I've tried to accomplish it with IIF statements but without luck... Hope the above makes sense and someone can point me in the right direction.

Current Amibroker code:

// Trade settings
BuyPrice = Open;
SellPrice = Open;
Numpos = 10;
SetOption("MaxOpenPositions", Numpos);
SetOption( "ActivateStopsImmediately", False );
SetOption( "AllowSameBarExit", False ); 
SetPositionSize(100/Numpos, spsPercentOfEquity);

// Index and index trend
Index = Foreign("^GSPC", "C", True);
IndexMA = EMA(Index,75);

// Bollinger bands calc
Bol_top = BBandTop( C, 100, 3 );
Bol_Bot = BBandBot( C, 100, 1 );

// Buy and Sell conditions
Buy = C > Bol_top AND Index > IndexMA AND RSI(14)<90 AND 
		Ref(C,-1)<Ref(Bol_top,-1) AND ((C/Ref(C,-1))-1)<.4;
Sell = (C < Bol_Bot OR RSI(14)>=90) AND BarsSince(Buy)>19;

// Initial trailing stop level and InTrade check
intrade = Flip(Buy,Sell);
initialstop = IIf(intrade, HighestSince(Buy,H*.65),Null);

// Update the sell condition to include the initial trailing stop level
Sell = (C < Bol_Bot OR RSI(14)>=90 OR C<Ref(initialstop,-1)) AND BarsSince(Buy)>19;

// Trenddown trailing stop level and Highest_Stop value
trenddown = IIf(Cross(IndexMA,Index) AND H*.85 > initialstop AND intrade, HighestSince(Cross(IndexMA,Index), H*.85), IIf(Index<IndexMA AND intrade,HighestSince(Index<IndexMA, H*.85),initialstop));

trenddown2 = IIf(Index>IndexMA AND trenddown < initialstop AND intrade, HighestSince(Index>IndexMA, H*.85), IIf(intrade, HighestSince(Buy,H*.65),Null));

Highest_stop = Max(initialstop, trenddown);

// Update the Sell condition to include the new trailing stop level
Sell = (C < Bol_Bot OR RSI(14)>=90 OR C<Ref(Highest_stop,-1)) AND BarsSince(Buy)>19;

// Position ranking if portfolio is full
PositionScore = ROC(C,90);

// Get rid of excessive Buy and Sell signals
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

// Plotting values
Plot( Bol_top, "Top Bol", colorGreen, styleDashed, 0, 0, 0, 0,0 );
Plot(Bol_Bot, "bot bol", colorRed, styleDashed, 0,0,0,0,0);
Plot(Ref(Highest_stop,-1), "initial_trail", colorOrange, styleDashed,0,0,0,0);
PlotShapes(IIf( Ref(Buy,-1), shapeUpArrow, shapeNone), colorYellow, 0, L);
PlotShapes(IIf( Ref(Sell,-1), shapeDownArrow, shapeNone), colorYellow, 0, H);

And below is what I've done in Multicharts where it works - just not sure how to do this in AFL:

//Trailing stop calculation
if marketposition = 0 then trenddownsl = 0;

if marketposition = 0 then initialstop = 0;

if marketposition = 1 then initialstop = (highest[1](high,mro(condition1,1,1))*.65); 

if marketposition = 1 and close data2 cross below xaverage(close data2,smal) and (high*.85)>trenddownsl then trenddownsl = high*.85; 

if marketposition = 1 and close data2<xaverage(close data2,smal) and (high*.85)>trenddownsl then trenddownsl = high*.85;

if marketposition = 1 and close data2>xaverage(close data2,smal) and initialstop>trenddownsl then trenddownsl = initialstop;

if marketposition = 1 and close<initialstop and barssinceentry>19 then sell next bar at market;

if marketposition = 1 and close<trenddownsl and barssinceentry>19 then sell next bar at market;

Finally, below is attached images of where the trailing stop briefly rises higher when the index trend goes down and then decreases again.

Thanks in advance!

Recommended reading:


These two articles should give you idea how to code your own.


Thanks a lot, Tomasz!! I actually got it to work using the logic in the second article. Much appreciated!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.