correct trailing stop code afl please

#1
Hi experts
here is afl code for trailing stop.
The code doesnt readily draw trailstop lines.(need to be fixed)
but it uses a starting date and percentage in parameters.
Is there a way to get rid of starting date?(need to fix)

It is not clear what minimum number of days are needed for starting date to keep up accuracy in trailstop calculation.

Please help remove the date condition if possible or suggest minimum number of days .this matters if sufficient data is not available in my pc.

thanks & regards
ford

HTML:
Version(5.20); // requires v5.20  
SetBarsRequired(sbrAll);  

// get start date  
//Start = Cross( DateNum(), ParamDate("Start date", "2005-10-30" ) );  
Start = Cross( DateNum(), ParamDate("Start date", "2013-04-01" ) );  

Started = Flip( Start, 0 );  
 
StopMode = ParamList("Stop Mode", "Fixed|Chandelier|Modified ATR" );  
StopLevel = Param("Fixed perc %", 5, 0.1, 50, 0.1)/100;  
StopATRFactor = Param("ATR multiple", 4, 0.5, 10, 0.1 );  
StopATRPeriod = Param("ATR period", 14, 3, 50 );  
 
// calculate support and resistance levels  
if( StopMode == "Fixed" ) // fixed percent trailing stop  
{  
 sup = C * ( 1 - stoplevel );  
 res = C * ( 1 + stoplevel );  
}  
else // Chandelier ATR-based stop  
if( StopMode == "Chandelier" )  
{  
 sup = C - StopATRFactor * ATR( StopATRPeriod );  
 res = C + StopATRFactor * ATR( StopATRPeriod );  
}  
else  
{  
 HL = H - L;  
 MAHL =  1.5 * MA( HL, StopATRPeriod );  
 HiLo = IIf( HL < MAHL, HL, MAHL );  
 H1 = Ref( H, -1 );  
 L1 = Ref( L, -1 );  
 C1 = Ref( C, -1 );  
 Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );  
 Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );  
 
 diff1 = Max( HiLo, HRef );  
 diff2 = Max( diff1, LRef );  
 
 ATRmod = Wilders( diff2, StopATRPeriod );  
    
 sup = C - StopATRFactor * ATRmod ;  
 res = C + StopATRFactor * ATRmod ;  
}  
 
// calculate trailing stop line  
trailARRAY = Null;  
trailstop = 0;  
for( i = 1; i < BarCount; i++ )  
{  
 if( Started[ i ] == 0 ) continue;  
 
 if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )  
   trailstop = Max( trailstop, sup[ i ] );  
 else  
 if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )  
    trailstop = Min( trailstop, res[ i ] );  
 else  
   trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );        
 
 trailARRAY[ i ] = trailstop;  
}  
 
// generate buy/sell signals based on crossover with trail stop line  
Buy = Start OR Cross( C, trailArray );  
Sell = Cross( trailArray, C );  
 
PlotShapes(Buy*shapeUpArrow,colorGreen,0,trailarray);  
PlotShapes(Sell*shapeDownArrow,colorRed,0,trailarray);  
 
Plot( Close,"Price",colorBlack,styleBar);  
//SetBarFillColor( colorYellow );  
TRAILCOLOR =IIf(C>trailARRAY,colorBlue,colorRed);
Plot( trailARRAY,"trailing stop level", TRAILCOLOR, styleLine );
 

casoni

Well-Known Member
#2
this will plot .. removed start date

StopMode = ParamList("Stop Mode", "Fixed|Chandelier|Modified ATR" );

StopLevel = Param("Fixed perc %", 1, 0.1, 5, 0.1)/100;

StopATRFactor = Param("ATR multiple", 4, 0.5, 10, 0.1 );
StopATRPeriod = Param("ATR period", 14, 3, 50,1 );

// calculate support and resistance levels
if( StopMode == "Fixed" ) // fixed percent trailing stop
{
sup = C * ( 1 - stoplevel );
res = C * ( 1 + stoplevel );
}
else // Chandelier ATR-based stop
if( StopMode == "Chandelier" )
{
sup = C - StopATRFactor * ATR( StopATRPeriod );
res = C + StopATRFactor * ATR( StopATRPeriod );
}
else
{
HL = H - L;
MAHL = 1.5 * MA( HL, StopATRPeriod );
HiLo = IIf( HL < MAHL, HL, MAHL );
H1 = Ref( H, -1 );
L1 = Ref( L, -1 );
C1 = Ref( C, -1 );
Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );
Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );

diff1 = Max( HiLo, HRef );
diff2 = Max( diff1, LRef );

ATRmod = Wilders( diff2, StopATRPeriod );

sup = C - StopATRFactor * ATRmod ;
res = C + StopATRFactor * ATRmod ;
}

// calculate trailing stop line
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
//if( Started[ i ] == 0 ) continue;

if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );

trailARRAY[ i ] = trailstop;
}

// generate buy/sell signals based on crossover with trail stop line
Buy = Cross( C, trailArray );
Sell = Cross( trailArray, C );

PlotShapes(Buy*shapeUpArrow,colorGreen,0,trailarray);
PlotShapes(Sell*shapeDownArrow,colorRed,0,trailarray);

Plot( Close,"Price",colorBlack,styleBar);
//SetBarFillColor( colorYellow );
TRAILCOLOR =IIf(C>trailARRAY,colorBlue,colorRed);
Plot( trailARRAY,"trailing stop level", TRAILCOLOR, styleLine );
 
#3
Hi Casoni

Thanks for the help.
It is ok now.

Which stop is better?
Not really clear on this.
Fixed percent stop=We need to use 0.5% in intraday trading(max 2%)
in EOD trading,fixed percentage of 5 to 7% is good as per William oNeil

Chandelier stop = atr factor seems too high at 4,factor of 1.7 to 3 can be used depending on how it suits the instrument.
Modified ATR = atr factor 2.6 or 2.7 seems good-percentage has no effect in this case
Online trading academy recommends ATR factor 1.5 for daytrading.

Please comment on this
Modern pros use combination stop-it combines percentage stop-say 2% risk on total capital of trade and Risk per share RPS(SPACE BETWEEN ENTRY AND STOPLOSS).
What sort optimised Atr Factor fits weklll for nifty futures or nifty index?

regards
ford
 

casoni

Well-Known Member
#4
Hello ford,
i think ..its better to optimise and find out which stops are better.
for eod / ieod .

to optimise:
replace these lines

StopLevel = Param("Fixed perc %", 1, 0.1, 5, 0.1)/100;
StopATRFactor = Param("ATR multiple", 4, 0.5, 10, 0.1 );
StopATRPeriod = Param("ATR period", 14, 3, 50,1 );


with

StopLevel= Optimize("period",Param("period",1,0.2,5,0.1),0.2,5,0.1)/100;
StopATRFactor = Optimize("multiple",Param("mult",4, 0.5, 10, 0.1 ),0.5, 10, 0.1);
StopATRPeriod= Optimize("period",Param("period",14, 3, 50,1),3, 50,1);

thank you
 

Similar threads

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?