AFL for trailing stoploss lines in steps-required

pkgmtnl

Well-Known Member
#1
- Dear Sir,

I want following Amibroker AFL codes to be developed for any set of BUY - SHORT conditions.

Let's first discuss for BUY-
1. As soon as BUY is planted, a SL 2% of BUY Price should be in place which would trail as under.

2. In case Price reaches 1% of BUYPrice, then trail SL should move to BUYPrice.

3. If Price moves further by another 1% of BUY Price, then Trail SL Should move upwards by 0.5% of BUYPrice and so on.

4. In case of Point no 1, if after BUY , the price does move or retraced, then, either SELL will come as per sell conditions or SL @2% whichever comes first.

The above will be drawn in similar manner for SHORT and trail SL shall move as explained for BUY conditions above.

Plz advise, if these codes can b written by any member?.

For illustration purposes, you may use EMA cross overs as Buy, Sell, Short and Cover Conditions, which I can be changed with any other conditions. The steps increase in Trail SL can be made parameters.

Plz advise.
 

pkgmtnl

Well-Known Member
#2
Buy = 1; // replace with your buy rule
Sell = 0; // replace with your sell rule
StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;
trailARRAY = Null;
trailstop = 0;
for( i = 1;
i < BarCount; i++ ) { if( trailstop == 0 AND Buy[ i ] ) { trailstop = High[ i ] * stoplevel; } 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, trailstop );
trailARRAY[ i ] = trailstop;
}
}
PlotShapes(Buy*shapeUpArrow,colorGreen,0,Low);
PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
Plot( Close,"Price",colorBlack,styleBar);
Plot( trailARRAY,"trailing stop level", colorRed );


Courtesy Amibroker Forum
 

pkgmtnl

Well-Known Member
#3
_SECTION_BEGIN( “intraday.afl” );
per10 = 0920;
per11 = 1130;
per12 = Param( “Trade Exit(HHMM)”, 1510, 1200, 1510, 100 );
sl_buy = 1 – Param(“StopLoss_buy(%)”, 0.3, 0.3, 1, 0.1)/100;
sl_short = 1 + Param(“StopLoss_short(%)”, 0.3, 0.3, 1, 0.1)/100;
tgt_buy = 1 + Param(“Target_buy(%)”, 1, 0.5, 2, 0.25)/100;
tgt_short = 1 – Param(“Target_short(%)”, 1, 0.5, 2, 0.25)/100;
intra = ParamToggle( “Activate Intraday Mode”, “NO|YES” );
intraex = intra AND TimeNum() > per12 * 100;
intraen = !intra OR ( TimeNum() per10 * 100 );
Col_sl_buy = ParamColor( “Color of sl_buy”, colorRed);
Col_sl_short = ParamColor( “Color of sl_short”, colorBlue);
Col_tgt_buy = ParamColor( “Color of tgt_buy”, colorRed );
Col_tgt_short = ParamColor( “Color of tgt_short”, colorBlue );
Buy2 = Buy;
Sell2 = Sell;
Short2 = Short;
Cover2 = Cover;
bflag = sflag = 0;
slarr_buy = tgtarr_buy=slarr_short = tgtarr_short = Null;
for ( i = 10; i < BarCount; i++ )
{
if ( Buy2 AND intraen AND bflag == 0 )
{
Buy = 1;
bflag = 1;
if ( sl_buy )
slarr_buy = C * sl_buy;
if ( tgt_buy )
tgtarr_buy = C * tgt_buy;
}
else
Buy = 0;
if ( bflag AND Buy == 0 )
{
slarr_buy = slarr_buy[i-1];
tgtarr_buy = tgtarr_buy[i-1];
}
if ( ( Sell2 OR intraex OR( L 0 ) OR ( H > tgtarr_buy AND tgt_buy > 0 ) ) AND bflag )
{
Sell = 1;
bflag = 0;
}
else
Sell = 0;
if ( Short2 AND intraen AND sflag == 0 )
{
Short = 1;
sflag = 1;
if ( sl_short )
slarr_short = C * sl_short;
if ( tgt_short )
tgtarr_short = C * tgt_short;
}
else
Short = 0;
if ( sflag AND Short == 0 )
{
slarr_short = slarr_short[i-1];
tgtarr_short = tgtarr_short[i-1];
}
if ( ( Cover2 OR intraexOR( H > slarr_short[i-1] AND sl_short > 0 ) OR ( L 0 ) ) AND sflag )
{
Cover = 1;
sflag = 0;
}
else
Cover = 0;
}


Courtesy Algoji...
 

Similar threads