Help with AFL.

#1
Hi friends

My name is Manish, I am a full time trader in Indian Equity and commodity markets. I have a twin ATR system for trading Nifty on Intra day and a simple (2,14) ATR system to trade stocks on daily chart.

Problem is that the ATR stop loss is too big for my 3% money management rule and so I often have to wait for the price to retrace close to the ATR line while still in the buy/sell zone.

Find below my AFL that I am using.

I was hoping if someone could tweak the code to explore when the CMP is x points from the ATR. that ways I won't have to explore manually to place the order and auto explore will give me options where ATR is a Buy/sell and price is x points away and (hence stop loss is small).

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 = Cover= Cross( C, trailArray );
Sell = Short= 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 );
 
#2
Code:
Filter= 100 * abs(Close - trailARRAY) / Close < 3 ;
// change 3 to whatever % you want
OR

Code:
Filter= abs(Close - trailARRAY)  < 30 ;
// change 30 to whatever Points you want

:) Happy
 
#4
Hi Happy,

Sorry for bothering you again. can u also write one line code to filter buys and sells which have come x days ago and are still active.


That ways I would be saved from the whipsaws and enter the ones which are ongoing.
 
#5
Hi Happy,

Sorry for bothering you again. can u also write one line code to filter buys and sells which have come x days ago and are still active.


That ways I would be saved from the whipsaws and enter the ones which are ongoing.
Not writing the final code for you . . . try to do it yourself . . . with help from amibroker help file :)

learn about the keyword . . . . . . . Barssince()

Barssince(Buy)+1 will give you no of days since last buy was triggered


can use Barssince(Buy) < Barssince(Sell) to filter current active buys



Cheers

:) Happy
 

Similar threads