Wednesday, June 11, 2014

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