AFL writing guide for new user

I am very thankful to you for taking initiative to teach other...

it will be great if you please help in making afl forfolowing condition

candles shold be green if close > ema 21 and MACD >0

candles shold be red if close < ema 21 and MACD <0

candles shold be white if no condition satisfied


THANKING YOU IN ADVANCE
 
oh, very good........... I found this thread.
I am searching that how I can write my small afls using that language.
can I know how I can get a pointer when macd histogram is in divergence.
 
CAN SOME BODY REPAIR THIS AFL.


_SECTION_BEGIN("Swing");
no = 22;
sloss = HAI_F1(no);
a = HAI_F2(no);
b = HAI_F3(no);
state = HAI_F4(no);
sss = HAI_F5(no);
uptrend = HAI_F6(20,10,29,22);
downtrend = HAI_F7(20,10,29,13);

style = a * styleStaircase + b * styleStaircase;
PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
_SECTION_END();

col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
PlotShapes( shapeUpArrow * a, colorGreen,0,L);
PlotShapes( shapeDownArrow * b, colorRed,0,H);

Filter = a OR b OR sss ;
AddColumn(C,"close",1.2);
AddColumn( IIf( a, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (a,colorYellow, colorPink ));
AddColumn( IIf( b, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (b,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));

_SECTION_BEGIN("TRADE");
_N(Title = EncodeColor(colorWhite) + "{{NAME}} - {{INTERVAL}} {{DATE}} " );
_SECTION_END();

_SECTION_BEGIN("TRADE");
Buy = a;
Sell = b;

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-30);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-35);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=30);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=40);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-35);

dist = ATR(20);
for (i=0; i<BarCount; i++) {
if ( Buy )
PlotText("Buy:" + O[ i ], i, L[ i ]-3*dist, colorGreen);
if ( Sell )
PlotText("Sell:" + O[ i ], i, H[ i ]+3*dist, colorRed);
}
_SECTION_END();

_SECTION_BEGIN("Trend");

Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();

//d = Close > Ref( ChandelierHL(ATR(3),20), -1);
//e =Close < Ref( ChandelierHL(ATR(3),20), -1);
//f = Close < Ref( ChandelierHL(ATR(3),20), -1);
//g = Close > Ref( ChandelierHL(ATR(3),20), -1);

Buy = a AND uptrend ;
Short = b AND downtrend ;
Sell = b AND downtrend ;
Cover = a AND uptrend;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);
Short=ExRem(Short,Cover);

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);

// Plot the Buy and Sell arrows.
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));

//Plot(sloss,"Swing",colorYellow,styleStaircase);

//SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),

//ParamColor("BgBottom", ColorRGB( 172,172,172 )),ParamColor("titleblock",ColorRGB( 172,172,172 )));
//Alerts
AlertIf( Buy, "SOUND C:\\Windows\\Media\\tada.wav", "Buy",1,1+2+4+8);
AlertIf( Short, "SOUND C:\\Windows\\Media\\notify.wav", "Short",2,1+2+4+8);

GraphXSpace = 5;

/* -------------------------------------------------------------------------------------- */

SetChartBkColor(colorViolet);
SetChartOptions(0,chartShowArrows|chartShowDates);

/* -------------------------------------------------------------------------------------- */

////////////////////////////////////////////////////////////////////////////////////////////////
messageboard = ParamToggle("Message Board","Show|Hide",0);
showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);

style = a * styleStaircase + b * styleStaircase;

PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
if (showsl == 0) Plot(sloss,"Stop Loss",colorCustom14,styleDots);
exitlong = Cross(sloss, H);
PlotShapes(exitlong * shapeNone, colorBlack,0,H,-10);
exitshort = Cross(L, sloss);
PlotShapes(exitshort * shapeNone, colorBlack,0,L,-15);

Buy = exitshort;
Sell = exitlong;
//Short = Sell;
//Cover = Buy;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Short = ExRem(Short, Cover);
//Cover = ExRem(Cover, Short);
AlertIf( Buy, "", "BUY @ " + C, 1 );
AlertIf( Sell, "", "SELL @ " + C, 2 );

for (i=BarCount-1; i>1; i--) {
if (Buy == 1) {
entry = O;
sig = "BUY";
sl = sloss;
tar1 = entry + (entry * .0040);
tar2 = entry + (entry * .0085);
tar3 = entry + (entry * .0179);
bars = i;
i = 0;
}
if (Sell == 1) {
sig = "SELL";
entry = O;
sl = sloss;
tar1 = entry - (entry * .0040);
tar2 = entry - (entry * .0085);
tar3 = entry - (entry * .0212);
bars = i;
i = 0;
}
}

Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, sloss[BarCount-1], Ref(sloss, -1));
sl = ssl[BarCount-1];

printf("Last " + sig + " Signal came " + (BarCount-bars) + " bars ago");
printf("\n" + sig + " @ : " + entry + "\nStop Loss : " + sl + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"+ "\nTarget_1 : " + tar1 + "\nTarget_2 : " + tar2 + "\nTarget_3 : " + tar3);
printf("\nCurrent P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2));

if (messageboard == 0) {
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if (sig =="BUY")
GfxSelectSolidBrush( colorGreen ); // this is the box background color
else
GfxSelectSolidBrush( colorRed ); // this is the box background color

pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 05;
x2 = 200;

y = Status( "pxchartheight" );

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 143, x2, y , 7, 7 ) ;
GfxTextOut( ( " TRADE "), 10, y-140) ;
GfxTextOut( Name(),13,y-120);

GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-100);
GfxTextOut( ("Trailing SL : " + sl + " "), 13, y-80);
GfxTextOut( ("TGT:1 : " + tar1), 13, y -60);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-40);
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-20);;
}
 
Hello Members,
Below is a code which is a simple "Explore" to get the list of stocks which have gone into Buy / Sell zone, basis the EMA Crossover / Crossunder....

In this formulae, the BUY is working properly. However, SELL does not work. I even tried using CL_Sell (which is mentioned below as comments, in line7). But it just does not work.

Criteria :
Sell if Close < Min EMA AND crossover of EMA has happened i.e 200EMA > 100EMA > 50EMA (and close is below that) - NOT WORKING
Buy if Close > Max EMA AND crossover of EMA has happened. i.e. 50EMA > 100EMA > 200EMA (and Close is above that) - THIS IS WORKING...

Can someone please check and modify the code to correct the problem?

Thanking you in anticipation.

Warm Regards...RB



e1=EMA(C,50);
e2=EMA(C,100);
e3=EMA(C,200);

Maxema=Max(e3,Max(e2,e1));
Minema=Min(e1,Min(e2,e3));

//CL_Buy = e1>e1>e3;
//CL_Sell = e1<e2<e3;

EMA_buy=IIf((Close>Maxema) AND (C > (e1>e2>e3)),1,0);

EMA_sell=IIf((Close<Minema) AND (C < (e3>e2>e1)),1,0);


Filter = EMA_Buy OR EMA_Sell ;

AddColumn(EMA_buy,"EMA_Buy", format = 1.2, IIf(EMA_Buy == True, textColor = colorGreen, textColor = colorLightGrey));
AddColumn(EMA_sell,"EMA_Sell", format = 1.2, IIf(EMA_Sell == True, textColor = colorRed, textColor = colorLightGrey));
 

johnnypareek

Well-Known Member
CAN SOME BODY REPAIR THIS AFL.


_SECTION_BEGIN("Swing");
no = 22;
sloss = HAI_F1(no);
a = HAI_F2(no);
b = HAI_F3(no);
state = HAI_F4(no);
sss = HAI_F5(no);
uptrend = HAI_F6(20,10,29,22);
downtrend = HAI_F7(20,10,29,13);

style = a * styleStaircase + b * styleStaircase;
PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
_SECTION_END();

col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
PlotShapes( shapeUpArrow * a, colorGreen,0,L);
PlotShapes( shapeDownArrow * b, colorRed,0,H);

Filter = a OR b OR sss ;
AddColumn(C,"close",1.2);
AddColumn( IIf( a, 66,1 ), "buy", formatChar, 1, bkcolor =IIf (a,colorYellow, colorPink ));
AddColumn( IIf( b, 83,1 ), "sell", formatChar, 1, bkcolor =IIf (b,colorPink, colorYellow ));
AddColumn( IIf( sss, 87,1 ), "wait", formatChar, 1, bkcolor =IIf (sss,colorYellow, colorRed ));

_SECTION_BEGIN("TRADE");
_N(Title = EncodeColor(colorWhite) + "{{NAME}} - {{INTERVAL}} {{DATE}} " );
_SECTION_END();

_SECTION_BEGIN("TRADE");
Buy = a;
Sell = b;

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-30);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-35);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=30);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=40);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-35);

dist = ATR(20);
for (i=0; i<BarCount; i++) {
if ( Buy )
PlotText("Buy:" + O[ i ], i, L[ i ]-3*dist, colorGreen);
if ( Sell )
PlotText("Sell:" + O[ i ], i, H[ i ]+3*dist, colorRed);
}
_SECTION_END();

_SECTION_BEGIN("Trend");

Plot( 2, /* defines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();

//d = Close > Ref( ChandelierHL(ATR(3),20), -1);
//e =Close < Ref( ChandelierHL(ATR(3),20), -1);
//f = Close < Ref( ChandelierHL(ATR(3),20), -1);
//g = Close > Ref( ChandelierHL(ATR(3),20), -1);

Buy = a AND uptrend ;
Short = b AND downtrend ;
Sell = b AND downtrend ;
Cover = a AND uptrend;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Cover=ExRem(Cover,Short);
Short=ExRem(Short,Cover);

Filter=Buy OR Sell;
Filter= Cover OR Short;

AddColumn( Buy, "Buy", 1);
AddColumn(Sell, "Sell", 1);
AddColumn(Close,"Close",1.2);
AddColumn(Volume,"Volume",1.0);

// Plot the Buy and Sell arrows.
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, IIf(Buy,colorGreen,colorRed), 0, IIf(Buy,Low,High));

//Plot(sloss,"Swing",colorYellow,styleStaircase);

//SetChartBkGradientFill( ParamColor("BgTop", ColorRGB( 172,172,172 )),

//ParamColor("BgBottom", ColorRGB( 172,172,172 )),ParamColor("titleblock",ColorRGB( 172,172,172 )));
//Alerts
AlertIf( Buy, "SOUND C:\\Windows\\Media\\tada.wav", "Buy",1,1+2+4+8);
AlertIf( Short, "SOUND C:\\Windows\\Media\\notify.wav", "Short",2,1+2+4+8);

GraphXSpace = 5;

/* -------------------------------------------------------------------------------------- */

SetChartBkColor(colorViolet);
SetChartOptions(0,chartShowArrows|chartShowDates);

/* -------------------------------------------------------------------------------------- */

////////////////////////////////////////////////////////////////////////////////////////////////
messageboard = ParamToggle("Message Board","Show|Hide",0);
showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);

style = a * styleStaircase + b * styleStaircase;

PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
if (showsl == 0) Plot(sloss,"Stop Loss",colorCustom14,styleDots);
exitlong = Cross(sloss, H);
PlotShapes(exitlong * shapeNone, colorBlack,0,H,-10);
exitshort = Cross(L, sloss);
PlotShapes(exitshort * shapeNone, colorBlack,0,L,-15);

Buy = exitshort;
Sell = exitlong;
//Short = Sell;
//Cover = Buy;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
//Short = ExRem(Short, Cover);
//Cover = ExRem(Cover, Short);
AlertIf( Buy, "", "BUY @ " + C, 1 );
AlertIf( Sell, "", "SELL @ " + C, 2 );

for (i=BarCount-1; i>1; i--) {
if (Buy == 1) {
entry = O;
sig = "BUY";
sl = sloss;
tar1 = entry + (entry * .0040);
tar2 = entry + (entry * .0085);
tar3 = entry + (entry * .0179);
bars = i;
i = 0;
}
if (Sell == 1) {
sig = "SELL";
entry = O;
sl = sloss;
tar1 = entry - (entry * .0040);
tar2 = entry - (entry * .0085);
tar3 = entry - (entry * .0212);
bars = i;
i = 0;
}
}

Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, sloss[BarCount-1], Ref(sloss, -1));
sl = ssl[BarCount-1];

printf("Last " + sig + " Signal came " + (BarCount-bars) + " bars ago");
printf("\n" + sig + " @ : " + entry + "\nStop Loss : " + sl + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"+ "\nTarget_1 : " + tar1 + "\nTarget_2 : " + tar2 + "\nTarget_3 : " + tar3);
printf("\nCurrent P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2));

if (messageboard == 0) {
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );

if (sig =="BUY")
GfxSelectSolidBrush( colorGreen ); // this is the box background color
else
GfxSelectSolidBrush( colorRed ); // this is the box background color

pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 05;
x2 = 200;

y = Status( "pxchartheight" );

GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 143, x2, y , 7, 7 ) ;
GfxTextOut( ( " TRADE "), 10, y-140) ;
GfxTextOut( Name(),13,y-120);

GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-100);
GfxTextOut( ("Trailing SL : " + sl + " "), 13, y-80);
GfxTextOut( ("TGT:1 : " + tar1), 13, y -60);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-40);
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-20);;
}


I guess it need some dll or atleast another afl which should have to be in include folder of ami.

johnny
 

johnnypareek

Well-Known Member
Hello Members,
Below is a code which is a simple "Explore" to get the list of stocks which have gone into Buy / Sell zone, basis the EMA Crossover / Crossunder....

In this formulae, the BUY is working properly. However, SELL does not work. I even tried using CL_Sell (which is mentioned below as comments, in line7). But it just does not work.

Criteria :
Sell if Close < Min EMA AND crossover of EMA has happened i.e 200EMA > 100EMA > 50EMA (and close is below that) - NOT WORKING
Buy if Close > Max EMA AND crossover of EMA has happened. i.e. 50EMA > 100EMA > 200EMA (and Close is above that) - THIS IS WORKING...

Can someone please check and modify the code to correct the problem?

Thanking you in anticipation.

Warm Regards...RB



e1=EMA(C,50);
e2=EMA(C,100);
e3=EMA(C,200);

Maxema=Max(e3,Max(e2,e1));
Minema=Min(e1,Min(e2,e3));

//CL_Buy = e1>e1>e3;
//CL_Sell = e1<e2<e3;

EMA_buy=IIf((Close>Maxema) AND (C > (e1>e2>e3)),1,0);

EMA_sell=IIf((Close<Minema) AND (C < (e3>e2>e1)),1,0);


Filter = EMA_Buy OR EMA_Sell ;

AddColumn(EMA_buy,"EMA_Buy", format = 1.2, IIf(EMA_Buy == True, textColor = colorGreen, textColor = colorLightGrey));
AddColumn(EMA_sell,"EMA_Sell", format = 1.2, IIf(EMA_Sell == True, textColor = colorRed, textColor = colorLightGrey));
hmmm In sell condition is it that 50ema is greater then 100 and 100 is greater then 200??? I guess it should be opposite.Try below given

e1=EMA(C,50);
e2=EMA(C,100);
e3=EMA(C,200);

Maxema=Max(e3,Max(e2,e1));
Minema=Min(e1,Min(e2,e3));

//CL_Buy = e1>e1>e3;
//CL_Sell = e1<e2<e3;

EMA_buy=IIf((Close>Maxema) AND (C > (e1>e2>e3)),1,0);

EMA_sell=IIf((Close<Minema) AND (C < (e3<e2<e1)),1,0);


Filter = EMA_Buy OR EMA_Sell ;

AddColumn(EMA_buy,"EMA_Buy", format = 1.2, IIf(EMA_Buy == True, textColor = colorGreen, textColor = colorLightGrey));
AddColumn(EMA_sell,"EMA_Sell", format = 1.2, IIf(EMA_Sell == True, textColor = colorRed, textColor = colorLightGrey));

In fact the modified isell cond.. is good for short. For exit long
EMA_sell=IIf((Close<Minema) AND (C < e1),1,0);// This shuld be fin to exit longs.

johnny
 

johnnypareek

Well-Known Member
I am very thankful to you for taking initiative to teach other...

it will be great if you please help in making afl forfolowing condition

candles shold be green if close > ema 21 and MACD >0

candles shold be red if close < ema 21 and MACD <0

candles shold be white if no condition satisfied


THANKING YOU IN ADVANCE
hi,

Here it is-
mycolor = IIf(C>EMA(C,21) AND MACD()>0, colorGreen, IIf(C<EMA(C,21) AND MACD()<0, colorRed,colorWhite));
Plot( Close, "Close", mycolor, styleNoTitle | styleCandle | styleThick );

johnny
 
hmmm In sell condition is it that 50ema is greater then 100 and 100 is greater then 200??? I guess it should be opposite.Try below given

e1=EMA(C,50);
e2=EMA(C,100);
e3=EMA(C,200);

Maxema=Max(e3,Max(e2,e1));
Minema=Min(e1,Min(e2,e3));

//CL_Buy = e1>e1>e3;
//CL_Sell = e1<e2<e3;

EMA_buy=IIf((Close>Maxema) AND (C > (e1>e2>e3)),1,0);

EMA_sell=IIf((Close<Minema) AND (C < (e3<e2<e1)),1,0);


Filter = EMA_Buy OR EMA_Sell ;

AddColumn(EMA_buy,"EMA_Buy", format = 1.2, IIf(EMA_Buy == True, textColor = colorGreen, textColor = colorLightGrey));
AddColumn(EMA_sell,"EMA_Sell", format = 1.2, IIf(EMA_Sell == True, textColor = colorRed, textColor = colorLightGrey));

In fact the modified isell cond.. is good for short. For exit long
EMA_sell=IIf((Close<Minema) AND (C < e1),1,0);// This shuld be fin to exit longs.

johnny

Dear Johnny

At the outset, thanks for the prompt reply....
I understood what u are saying. But the C<e1 condition is not for EXITing long positions. I use the same for starting a short....
Secondly,I'm looking for a sureshot confirmation of SELL when c< e1 AND e1 is less than e2 AND e2 is less than e3....
(this is a clear indication that the trend has reversed...and hence I'm looking for this criteria...) HENCE, REQUESTING YOU TO PLEASE RELOOK INTO THE SAME AND HELP TO RESOLVE THE ISSUE...
Also, wanted to point out that -> strangely, this works PERFECT for the BUY condition i.e. C> e1 AND (e1> e2 > e3)....Then why should it not work for Selll.....???
PLEASE HELP:confused:

Warm regards...RB
 
Hello friends,

I am very new to AFL writing and have no knowledge on language but trying it searching information on web. Stuck in few things and would be great if anyone can help solving it.

A) I am looking AFL to plot trendline on similar values on chart. For e.g.

Day1 Day2 Day3 Day4 Day5
113 121 119 121 126 (Close Price )

As above the value of Close price in Day2 & Day4 is same, require trendline at "121"

B) Also looking AFL for High value since the "BUY" trigger

please help
 

Similar threads