AFL writing guide for new user

Debarghya Mukherjee Sir,

In http://www.niftyformula.com/ an excellent Day trading Technique using Bollinger Band is given as detailed below. I request the Senior members please Give an AFL with Buy/Sell arrows for the trading Technique please

Nifty Formula
Day Trading Technique using Bollinger Bands

Procedure for buy/long trade:

In this strategy we take three candles into consideration before commencing any trade...

 Primary candle
 Secondary candle (Confirmation Candle)
 Trade candle

• When markets fall, many candles touch lower bollinger band continuously. Primary candle is the last candlestick which touches the lower band during its fall. It must be noted that the colour of the primary candle is NOT inportant, can be green or red.
• Secondary candle is the one next to primary candle. It must be a bull candle and close above the previous one. Very important to note that the secondary candle do not touch the lower band. All these 3 features indicate that the stock is tending higher taking strong support at bottom.
• Trade candle - Initiate the buy/long trade and place stoploss just below the low of first candle.

Note: At times continuously 4 to 5 candles touch the lower bollinger band. Please note that the last candle which touches the lower band must be taken into consideration as primary candle

• In this method, as the stoploss is very small, target can be same or double the stoploss.
• The above trade can be confirmed with the help of stochastic indicator as this too is a trend reversal indicator. Make sure %K faces upwards and starts travelling from 20 towards 100

With Regards
RaniHosur
if you have afl code for your bolinger band concept, could you please send me
 
Below is the AFL code in heikanashi. But I don't know how to plot buy and sell signal, set period, trailing stop loss and target and since i am new in trading, will this afl code work in day trading and if so, at what time frame? Please help.

_SECTION_BEGIN("Gou special");
/*
Heikin-Ashi(Koma-Ashi) with Moving Average Type
*/
SetChartOptions(2, chartWrapTitle);

// Calculate Moving Average
MAPeriod = Param("MA Period", 15, 1, 100);
MAOpen = EMA(Open, MAPeriod);
MAHigh = EMA(High, MAPeriod);
MALow = EMA(Low, MAPeriod);
MAClose = EMA(Close, MAPeriod);

HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
HaOpen = AMA(Ref(HaClose, -1), 0.5);

// for graph collapse
for(i = 0; i <= MAPeriod; i++) HaClose = Null;
/*
// same
// HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
HaOpen[ 0 ] = HaClose[ 0 ];
for(i = 1; i < BarCount; i++) {
HaOpen = (HaOpen[i - 1] + HaClose[i - 1]) / 2;
}
*/

HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
HaLow = Min(MALow, Min(HaClose, HaOpen));

// outs comments
"BarIndex = " + BarIndex();
"Open = " + Open;
"High = " + High;
"Low = " + Low;
"Close = "+ Close;
"HaOpen = " + HaOpen;
"HaHigh = " + HaHigh;
"HaLow = " + HaLow;
"HaClose = "+ HaClose;

// Plot graphs
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} HaOpen %g, HaHigh %g,
HaLow %g, HaClose %g (%.1f%%) {{VALUES}}",
HaOpen, HaHigh, HaLow, HaClose, SelectedValue(ROC( HaClose, 1))));
PlotOHLC(HaOpen, HaHigh, HaLow, HaClose, _DEFAULT_NAME(), ParamColor("Color",
colorBlack), styleCandle);
_SECTION_END();
_SECTION_END();
 
Last edited:
Below is the AFL code in heikanashi. But I don't know how to plot buy and sell signal, set the period,trailing stop loss and target and since i am new in trading, will this afl code work in day trading and if so, at what time frame? Please help.

_SECTION_BEGIN("Gou special");
/*
Heikin-Ashi(Koma-Ashi) with Moving Average Type
*/
SetChartOptions(2, chartWrapTitle);

// Calculate Moving Average
MAPeriod = Param("MA Period", 15, 1, 100);
MAOpen = EMA(Open, MAPeriod);
MAHigh = EMA(High, MAPeriod);
MALow = EMA(Low, MAPeriod);
MAClose = EMA(Close, MAPeriod);

HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
HaOpen = AMA(Ref(HaClose, -1), 0.5);

// for graph collapse
for(i = 0; i <= MAPeriod; i++) HaClose = Null;
/*
// same
// HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
HaOpen[ 0 ] = HaClose[ 0 ];
for(i = 1; i < BarCount; i++) {
HaOpen = (HaOpen[i - 1] + HaClose[i - 1]) / 2;
}
*/

HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
HaLow = Min(MALow, Min(HaClose, HaOpen));

// outs comments
"BarIndex = " + BarIndex();
"Open = " + Open;
"High = " + High;
"Low = " + Low;
"Close = "+ Close;
"HaOpen = " + HaOpen;
"HaHigh = " + HaHigh;
"HaLow = " + HaLow;
"HaClose = "+ HaClose;

// Plot graphs
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} HaOpen %g, HaHigh %g,
HaLow %g, HaClose %g (%.1f%%) {{VALUES}}",
HaOpen, HaHigh, HaLow, HaClose, SelectedValue(ROC( HaClose, 1))));
PlotOHLC(HaOpen, HaHigh, HaLow, HaClose, _DEFAULT_NAME(), ParamColor("Color",
colorBlack), styleCandle);
_SECTION_END();
_SECTION_END();
 
Last edited:
Hi friends,

i want help for one afl code here i write rules so please help me.

1. if day first candle close in green and next candle break high of first candle so generate buy call.
stop loss below first candle day low and if second candle not hit stop loss and close and start third candle so stop loss trail from first candle low to second candle day low.
max profit 50 points book profit or exit call generate.

2. 1. if day first candle close in red and next candle break low of first candle so generate sell call.
stop loss above first candle day high and if second candle not hit stop loss and close and start third candle so stop loss trail from first candle high to second candle day high.
max profit 50 points book profit or exit call generate.
 

casoni

Well-Known Member
if you have afl code for your bolinger band concept, could you please send me
Hello nirmal ,

check this setup ,

SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");
BTOP=BBandTop( P, Periods, Width );
BBOT=BBandBot( P, Periods, Width );
Plot(BTOP , "BBTop" + _PARAM_VALUES(), Color, Style );
Plot(BBOT, "BBBot" + _PARAM_VALUES(), Color, Style );



// Secondary candle after 4 bars above bbandtop

UP4= Ref(H,-4)>Ref(BTOP,-4) AND Ref(H,-3)>Ref(BTOP,-3) AND Ref(H,-2)>Ref(BTOP,-2) AND Ref(H,-1)>Ref(BTOP,-1) AND H<BTOP AND C<Ref(C,-1);
PlotShapes(UP4*23,colorGold,0,H,10);

// Secondary candle after 4 barsbelow bbandbot

DN4= Ref(L,-4)<Ref(BBOT,-4) AND Ref(L,-3)<Ref(BBOT,-3) AND Ref(L,-2)<Ref(BBOT,-2) AND Ref(L,-1)<Ref(BBOT,-1) AND L>BBOT AND C>Ref(C,-1);
PlotShapes(DN4*23,colorGold,0,L,-10);

like this you can add condition for 5 - 6 bars/candles .
Hope this heps
Thank you
 
Below is the AFL code in heikanashi. But I don't know how to plot buy and sell signal, set the period,trailing stop loss and target and since i am new in trading, will this afl code work in day trading and if so, at what time frame? Please help.

_SECTION_BEGIN("Gou special");
/*
Heikin-Ashi(Koma-Ashi) with Moving Average Type
*/
SetChartOptions(2, chartWrapTitle);

// Calculate Moving Average
MAPeriod = Param("MA Period", 15, 1, 100);
MAOpen = EMA(Open, MAPeriod);
MAHigh = EMA(High, MAPeriod);
MALow = EMA(Low, MAPeriod);
MAClose = EMA(Close, MAPeriod);

HaClose = (MAOpen + MAHigh + MALow + MAClose) / 4;
HaOpen = AMA(Ref(HaClose, -1), 0.5);

// for graph collapse
for(i = 0; i <= MAPeriod; i++) HaClose = Null;
/*
// same
// HaOpen = (Ref(HaOpen, -1) + Ref(HaClose, -1)) / 2;
HaOpen[ 0 ] = HaClose[ 0 ];
for(i = 1; i < BarCount; i++) {
HaOpen = (HaOpen[i - 1] + HaClose[i - 1]) / 2;
}
*/

HaHigh = Max(MAHigh, Max(HaClose, HaOpen));
HaLow = Min(MALow, Min(HaClose, HaOpen));

// outs comments
"BarIndex = " + BarIndex();
"Open = " + Open;
"High = " + High;
"Low = " + Low;
"Close = "+ Close;
"HaOpen = " + HaOpen;
"HaHigh = " + HaHigh;
"HaLow = " + HaLow;
"HaClose = "+ HaClose;

// Plot graphs
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} HaOpen %g, HaHigh %g,
HaLow %g, HaClose %g (%.1f%%) {{VALUES}}",
HaOpen, HaHigh, HaLow, HaClose, SelectedValue(ROC( HaClose, 1))));
PlotOHLC(HaOpen, HaHigh, HaLow, HaClose, _DEFAULT_NAME(), ParamColor("Color",
colorBlack), styleCandle);
_SECTION_END();
_SECTION_END();


i have put the screen short of one heikinashi, if anyone of us have it plis share the afl code
 
back testing code
the following are the coding done for the esignal backtesting for the bollinger indicator (%b) - entry -exit - stoploss - target .

PROGRAM

global variable Declaration :

Var xUpper=null;
Var xLower=null;
Var Stop=null;
Var Target=null;
Var bInit=false;

function main()
{
if(getCurrentBarIndex()==0)
return;
if(bInit==false)
xUpper=Upperbollingerband(10);
xlower=Lowerbollingerband(10);
bInit=true;
}
return new Array(xUpper.getvalue(0), Xlower.getvalue(0));

Var nU=xUpper.getvalue(-1);
Var nL=xLower.getvalue(-1);
if(nU==null || nL==null)
return;
if(Strategy.isLong()==false && Low(0)<=nL)
{
Strategy.doLong("Long Signal",Strategy.LIMIT , Strategy.THISBAR, Strategy.DEFAULT , Math.Min(nL ,open(0));
elseif(Strategy.isShort()==false && high(0)>=nU)
{
Strategy.doShort("Short Signal ", Strategy.LIMIT , Strategy.THISBAR , Strategy.DEFAULT , Math.max(nU,open(0));
}
}
stop = Entry - 3*ATR(10);
Target =Entry+5*ATR(10);
}


Plz check the code , i am getting compling and execution problem for the backtesting code . can anyone correct the mistakes .Revert me back with right executsble code.

hari prasad
mail : [email protected]
 
back testing code
the following are the coding done for the esignal backtesting for the bollinger indicator (%b) - entry -exit - stoploss - target .

PROGRAM

global variable Declaration :

Var xUpper=null;
Var xLower=null;
Var Stop=null;
Var Target=null;
Var bInit=false;

function main()
{
if(getCurrentBarIndex()==0)
return;
if(bInit==false)
xUpper=Upperbollingerband(10);
xlower=Lowerbollingerband(10);
bInit=true;
}
return new Array(xUpper.getvalue(0), Xlower.getvalue(0));

Var nU=xUpper.getvalue(-1);
Var nL=xLower.getvalue(-1);
if(nU==null || nL==null)
return;
if(Strategy.isLong()==false && Low(0)<=nL)
{
Strategy.doLong("Long Signal",Strategy.LIMIT , Strategy.THISBAR, Strategy.DEFAULT , Math.Min(nL ,open(0));
elseif(Strategy.isShort()==false && high(0)>=nU)
{
Strategy.doShort("Short Signal ", Strategy.LIMIT , Strategy.THISBAR , Strategy.DEFAULT , Math.max(nU,open(0));
}
}
stop = Entry - 3*ATR(10);
Target =Entry+5*ATR(10);
}


Plz check the code , i am getting compling and execution problem for the backtesting code . can anyone correct the mistakes .Revert me back with right executsble code.

hari prasad
mail : [email protected]
 

Similar threads