Thursday, June 12, 2014

Learn AFL Code : Stochastic histrogram AFL code


Here is the condition.

put an histogram behind the stochastic as similar as the MACD histogram, where the upside bars are only drawn if %K => 80 and %K =< 20

Here is the Code:
_SECTION_BEGIN("AFl by Debarghya Mukherjee");
periods = Param( "Periods", 8, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
Dsmooth = Param( "%D avg", 4, 1, 200, 1 );
Plot( d1 = StochK( periods , Ksmooth ), "Stochastic (%K)", colorWhite, styleLine );
Plot( StochD( periods , Ksmooth, DSmooth ), "Stochastic (%D)", colorRed, styleLine );

Plot( IIf(d1 <= 80 AND d1 >= 20, d1-20, IIf(d1 > 80, 80-d1, d1-20)), "Histogram", IIf(d1 <= 80 AND d1 >= 20, colorRed, colorGreen) , styleHistogram );

PlotGrid( 80 );
PlotGrid( 20 );
_SECTION_END();

Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Learn AFL Code : Display Buy and Sell price in price chart using AFL


_SECTION_BEGIN("Debarghya: AFL Coding");

// Simple logic to plot your EMA. You use any EMA value
em3=EMA(C,3);
em13=EMA(C,13);
Plot( em3, "EMA(3)", colorRed );
Plot( em13, "EMA(13)", colorGreen );

PlotOHLC( Open,High,Low,Close, "Price", colorRed, styleCandle);
PlotOHLC( em3,em3,em13,em13, "", IIf( em3 > em13, colorGreen, colorRed ), styleCloud | styleClipMinMax );

Buy=Cross( em3, em13 );
Sell= Cross( em13,em3 );
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
dist = 1.7*ATR(10);

PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-20);

for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "Buy\n@" + C[ i ], i, L[ i ]-dist[i], colorGreen,colorYellow );
if( Sell[i] ) PlotText( "Sell\n@" + C[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}
_SECTION_END();

I am using
PlotText( ''text'', x, y, color, bkcolor = colorDefault )
to display entry and exit level.

Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Learn AFL code: How to make a Price band using AFL code


Here is the Code for above image

_SECTION_BEGIN("Debarghya: AFL Coding");

SetBarFillColor( IIf( Close > Open, colorWhite, colorBlack ) );
Plot(C, "Price", colorRed, styleCandle );

Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+"Debarghya: AFL Coding";

Plot(EMA(C,3),"EMA(3)",colorGreen,styleLine);
Plot(EMA(C,13),"EMA(13)",colorYellow,styleLine);

//PlotOHLC( Open,High,Low,Close, "MA", colorRed, styleCandle);
PlotOHLC( EMA(C,3),EMA(C,3),EMA(C,13),EMA(C,13), "EMA BAND", IIf( EMA(C,3)>EMA(C,13) , colorDarkBlue , colorRed ), styleCloud);
_SECTION_END();


Well this a humble band of EMA(3) and EMA(13) along with the price. Entire band is easy to visualize. PlotOHLC() function is making this band.
PlotOHLC( open, high, low, close, name, color/barcolor, style = styleCandle | styleOwnScale, minvalue = {empty}, maxvalue = {empty}, XShift = 0, ZOrder = 0 )


Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Wednesday, June 11, 2014

Learn AFL Code : How to customize Title of an AFL chart

Let set the title in the AFL code.

here is the Syntax:

Title = " {{NAME}} {{DATE}} {{INTERVAL}} "+"Your Text"+" Chart values :{{VALUES}} ";

{{NAME}}: Script Name
{{DATE}}: date
{{INTERVAL}}: Time interval, 5/10/15/30 min
"Your Text" : Any text you want to use. This text you can use as your signature of the code.
{{VALUES}}: Other options, like EMA, MA etc settings values will be there.


Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Learn AFL Code : How to draw colorful background AFL code

Now I want to set the background color of any chart
My function is
SetChartBkColor( color )

here is a sample code,
_SECTION_BEGIN("Debarghya: AFL Coding");

SetChartBkColor( ColorRGB( 120, 130, 140 ));


SetBarFillColor( IIf( Close > Open, colorWhite, colorBlack ) );
Plot(C,"Price", colorBlack, styleCandle );
Plot(EMA(C,3),"SMA(3)", colorRed );
Plot(EMA(C,13),"SMA(13)", colorGreen );

_SECTION_END();

Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Learn AFL Code : Draw Bolinger Band in a Price chart

This very simple code will add a Bolinger Band in your price chart

_SECTION_BEGIN("BB price chart");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( Close, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );

P = ParamField("Price field",-1);
Periods = Param("Periods", 20, 2, 300, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorCycle );
Style = ParamStyle("Style");


Plot( BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style );
Plot( BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style );

_SECTION_END();

Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com

Learn AFL code : How to show shape in afl

Well we have have plotted our price and desired EMA in the chart. Now may be we want to put an arrow in the chart. This will help us to recognize our signal better. How can I do this? Here is the code for it.

_SECTION_BEGIN("visual arrow ema cross over");
SetChartOptions(0,chartShowArrows|chartShowDates);
Plot( Close, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Buy= Cross(EMA( C, 20 ), EMA( C, 50 ));
Sell= Cross(EMA( C, 50 ), EMA( C, 20 ));

PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-20);

Plot( EMA( C,20), "20 EMA Close",ParamColor("EMA 20 Color", colorGreen ),styleNoRescale);
Plot( EMA( C,50), "50 EMA Close",ParamColor("EMA 50 Color", colorRed ),styleNoRescale); 
_SECTION_END();


here Plot function is making this arrow.
SYNTAX :PlotShapes( shape, color, layer = 0, yposition = graph0, offset = -12, XShift = 0 );

PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
PlotShapes(IIf(Buy==1, shapeUpArrow , shapeNone), colorGreen, 0,Low, Offset=-20);

Here I used Arrow shape, but if you want you have wide range of shape options available. here is the list
Constants for shapes:
shapeNone, shapeUpArrow, shapeDownArrow, shapeHollowUpArrow, shapeHollowDownArrow, shapeSmallUpTriangle, shapeSmallDownTriangle, shapeHollowSmallUpTriangle, shapeHollowSmallDownTriangle, shapeUpTriangle, shapeDownTriangle, shapeHollowUpTriangle, shapeHollowDownTriangle, shapeSmallSquare, shapeHollowSmallSquare, shapeSquare, shapeHollowSquare, shapeSmallCircle, shapeHollowSmallCircle, shapeCircle, shapeHollowCircle, shapeStar, shapeHollowStar, shapeDigit0, shapeDigit1, shapeDigit2, shapeDigit3, shapeDigit4, shapeDigit5, shapeDigit6, shapeDigit7, shapeDigit8, shapeDigit9, shapePositionAbove

So instead of using (Upper arrow)
PlotShapes(IIf(Sell==1, shapeDownArrow, shapeNone), colorRed, 0,High, Offset=-20);
We can use this also (Upper triangle)
PlotShapes(IIf(Buy==1, shapeUpTriangle , shapeNone), colorGreen, 0,Low, Offset=-20);




Debarghya Mukherjee
Mobile: (+91)-9038787021
Email: debarghya_mkr@yahoo.com