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

Learn AFL Code: EMA cross over system part 2

This is our second post for EMA cross using AFL. In our first post we learned to design our logic for ema cross over. Now as you have already learned how to draw price action and how to generate signal. Now you want to plot your favorite EMA in chart. how can you do it?

_SECTION_BEGIN("EMA 20/50 cross over system");

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

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

//Plot EMA in AFL chart
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();


Now you change your EMA parameters and implement your logic.
You can use more than two EMA also in AFL. Like

Plot( EMA( C,10), "10 EMA Close",ParamColor("EMA 10 Color", colorGreen ),styleNoRescale);
Plot( EMA( C,20), "20 EMA Close",ParamColor("EMA 20 Color", colorRed ),styleNoRescale);
Plot( EMA( C,30), "30 EMA Close",ParamColor("EMA 30 Color", colorGreen ),styleNoRescale);
Plot( EMA( C,40), "40 EMA Close",ParamColor("EMA 40 Color", colorRed ),styleNoRescale);

As many as you want. Goto part three for complete signal based chart.


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






Learn AFL Code : How to display price in Amibroker chart using AFL

Now lets discuss how to plot Price in your Amibroker chart using AFL.

It is very important to show price action in your chart. Here this simple code will show price in your chart.

Every Amibroker AFL starts with a section. A section is nothing but a block, that contains your AFL codes. Syntax of your AFL section is like this

_SECTION_BEGIN("Name of the section");

// Your Code

_SECTION_END();

You can use any name your like to identify your section purpose.

Comment Line in AFL code starts with "//". If you wish to make comment of your logic or code purpose then put a "//" before that line.
Use /*  ....... */ as block commnet. It will make multple lines comment.

Sample
//This is a comment line
/* This is start of our comment
second line of comment
third line
.
.
.
End of my comments */

Now take an example


_SECTION_BEGIN("price section");

//Buy Logic AFL
Buy= Cross(EMA( C, 20 ), EMA( C, 50 ));
//Sell logic for AFL
Sell= Cross(EMA( C, 50 ), EMA( C, 20 ));

_SECTION_END();
I used out last post buy and sell logic.

Now we wants to display price over our chart.

Basic way,

_SECTION_BEGIN("price section");
Plot( Close, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

Very Basic black and White candlestick chart. Boring!!!

Lets do some different things here.

_SECTION_BEGIN("Debarghya: AFL Coding");
Plot(C, "Price", colorRed, styleBar );
_SECTION_END();

Red color Bar chart.

Ok, now what I have done here? I have used "C" as closing price. But two things that we can change, that is "colorRed" and "styleBar". Lets change them as we want,

Styles:
styleCandle
styleLine
styleBar

Colors:
colorRed
colorBlack
colorYellow
colorGreen
ColorWhite
etc

So lets take some Examples,
Sample 1
_SECTION_BEGIN("Debarghya: AFL Coding");
Plot(C, "Price", colorGreen, styleBar );
_SECTION_END();

Sample 2
_SECTION_BEGIN("Debarghya: AFL Coding");
Plot(C, "Price", colorYellow, styleLine );
_SECTION_END();

Solid bar price bar
_SECTION_BEGIN("Debarghya: AFL Coding");
SetBarFillColor( IIf( Close > Open, colorWhite, colorBlack ) );
Plot(C, "Price", colorRed, styleCandle );
_SECTION_END();


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

Learn AFL code : EMA cross over system part 1

In our last post we learned about basic structure of AFL coding, and I said that, you can make your own buy and sell signal logic and can use it. So lets make some sample buy and sell signal.
I am taking a very simple trading logic, that is "EMA cross over system". We all know that is any "EMA cross over system we a higher time frame EMA and one smaller. Once the smaller EMA crosee higher a buy signal is generated and revrse is true from sell signal.

May be you have EMA/MA cross over system that uses EMA20 and EMA50. I am writing the code for it,


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

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

Now here replace 20 and 50 with your desired value. This will generate nice signal for your 20/50 EMA cross over system. Go long when EMA(20) crosses EMA(50). and sell in opposite. Go to part two for complete chart.

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

Learn AFL Code: Basic Structure of an AFL code

You may have seen many hefty block of codes of AFL. But What is the main purpose of AFl code? We wants to generate Buy or sell signals from them. So every AFL has 4 main parts

Buy = Your entry signal for long trade;

Sell = You exit signal for long trade;

Short = Your entry signal for short trade;

Cover = You exit signal for short trade;

Well now traders have their own trading plans fro different Buy or sell signals. So now decide your signal and put in here.

In the next post I will discuss different codes for entry and exit signals. Goto Part two


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

Learn AFL Code: Introduction

Hello Friends,
I have been trading since 2008. When I started trading with my first account, I had very little idea about algotrading, Metastock, Amibroker etc. I used to take decision from fundamental reasons, and once in a while I learned technical trading and was looking for my trading plan tester. Then one day I was introduced with Amibroker and AFL coding. I started lot of googling but I have not yet found any good site that helps "How to write AFL codes from Amibroker". I know there is a site contains AFL Library but it is not sufficient. New users can learn from step by step process, not from some bulky AFL sample codes.

Most traders try to download AFL codes from internet, then try to follow them. My intention is that, users must learn "How to write his own AFL code for good".

I am an MCA (Masters in Computer Application) and having both technical trading knowledge and  computer background I understand how the process goes. So I started my own AFL code writing tutorial site for users who really wants to learn AFL coding.

Happy Trading, 

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