I am working on various exit methods and was having trouble with a sanity check on my ATR trailing stop so I created a simple example to test my understanding. I used a slightly modified example of code from the kb article on plotting trailing stops and compared it to the example of the ATR based trailing stop from the AFL ApplyStop function reference page. I setup a simple single symbol backtest of the two stops for comparison but I cannot get the two methods to line up.
I am sure this is an oversight on my part but after much thrashing around I still cannot spot my logic or programming flaw. Any help would be appreciated.
Jim
// test the applystop trailing stop with 3*ATR(14)
// with manual plotted stop
SetOption( "InitialEquity", 10000 );
SetTradeDelays( 0, 0, 0, 0 );
SetOption( "MaxOpenPositions", 1000 );
SetOption("ExtraColumnsLocation", 1 );
SetPositionSize( 20, spsPercentOfEquity );
// --- end backtester settings ---
Buy = (PDI(14) > MDI(14));
ExitOption = Optimize("Exit Opt", 2, 1, 2, 1);
switch(ExitOption)
{
case 1: // Trailing ATR Stop (ApplyStop NO PLOT OF SL )
Sell = 0;
ApplyStop(stopTypeTrailing, stopModePoint, 3*ATR(14), True, True );
break;
case 2: // Trailing ATR Stop (manual)
Sell = 0;
trailARRAY = Null;
trailstop = 0;
stoplevel = 3*ATR(14);
for( i = 1; i < BarCount; i++ )
{
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] - stoplevel[ i ];
}
else Buy[ i ] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] <= trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
if( trailstop > 0 )
{
trailstop = Max( High[ i ] - stoplevel[ i ], trailstop );
trailARRAY[ i ] = trailstop;
}
}
// Plot( trailARRAY,"trailing stop level", colorRed )
Plot( stopLevel,"trailing stop level", colorBlack); // for sanity compare to 3 times the ATR indicator - they match
break;
};