Hello,
(New to AFL, learning the coding and testing samples.)
When I was testing the Trailing Stoploss code (with minor changes) from the KB (AmiBroker Knowledge Base » How to plot a trailing stop in the Price chart) , observed that the plotshapes are working even when the If condition is not true.
Its plotting the star shape whenever there is a valid signal, but should it even do that if the IF condition (within which it resides) is not True.
Have gone through reference for plotshapes, If else, chart execution etc., but couldn't find a reason.
Would be helpful if someone can clarify.
Thanks.
tStopLevel = 1 - Param("trailing stop %", 1, 0.1, 10, 0.1)/100;
Buy = Cross( MACD(), Signal() );
Sell = 0;
//Buy = ExRem (Buy, Sell);
trailARRAY = Null;
trailstop = 0;
dist1 = 1.5*ATR(10);
for( i = 1; i < BarCount; i++ )
{
if( trailstop == 0 AND Buy[ i ])
{
trailstop = High[ i ] * stoplevel;
PlotShapes(Buy*shapestar, colorGreen, 0, High, Offset=20); //Marking First buy signal}
PlotText( "TS1:" + trailstop, i, H[ i ]+dist1[i], colorRed, colorYellow ); //Labelling starting SL value
}
else
{
Buy[ i ] = 0; // remove excess buy signals
//PlotShapes(Buy*shapestar, colorRed, 0, High, Offset=40); // Testing to see if the exection flow gets into else statement
}
if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
if( trailstop > 0 )
{
trailstop = Max( High[ i ] * stoplevel, trailstop );
trailARRAY[ i ] = trailstop;
}
}
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",colorBlack,styleCandle);
Plot( trailARRAY,"trailing stop level", colorRed );