Thursday, December 22, 2011

AFL Code Writing : Lesson 3 (Part 3)

Today we will make our simple moving average more dynamic. By the below code, our user is able to change the color and style of the moving average line without changing the code each time :

//Below code will display 100 days simple moving average line
Plot (MA(Close,100), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));


But your user is not able to change the period dynamically. Like s/he would like to see the 50 days simple moving average. S/he needs to change the code. BTW, we can solve the problem by Param() function. Pls. see the below modified line of our simple moving average :

//Your user will be able to change the periods without changing the code
Plot (MA(Close, Param("Periods", 50)), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));


By right button clicking or pressing Ctrl+R on the chart, your user is able to change the periods of moving average as per their desire. Pls. see the below image :


By dragging the bar, your user will be able to change the periods without changing the code. If you still confused please see the below explanation image :


Syntax :  Param("Periods", 50)
Meaning : By writing Param(), we are telling that it will be added in Parameters... so that our user can change the value without changing the code each time . "Periods" is name and 50 is default value.

Please keep in mind that by default Minimum value is 0 and Maximum value is 100. That is your user will be able to see the 0 to 100 days simple moving average. If s/he wish to see the 200 days moving average, s/he will not able to see it. Okay, we will give him/her a power to see upto 300 days simple moving average by the below code :

//Your user will be able to see 2 to 300 days simple moving average
Plot (MA(Close, Param("Periods", 50, 2, 300)), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));


Syntax :  Param("Periods", 50, 2, 300)
Meaning : Here 2 is the minimum value and 300 is the maximum value.

Below is the our upto date complete code :

/*
-------------------------------
I've written this Code for the
purpose of my blog writing.

 Date : December 21, 2011
-------------------------------
*/

_SECTION_BEGIN("Price");
//The below code will display the price through candlesticks
Plot( Close, "Close", ParamColor("Color", colorGold), ParamStyle("Style", styleCandle, maskAll));
_SECTION_END();


_SECTION_BEGIN("Moving Average");
//Below code will display 50 days simple moving average line
Plot (MA(Close, Param("Periods", 50, 2, 300)), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));
_SECTION_END();

//-----------------------------


Allah Haifz.

2 comments:

  1. Please let me know how can I learn next lessons ?

    1] Do you have any online course?

    2] Do you have any institute?

    3] Have you writen any book on this so that we can buy?

    I like to learn complete AFL code writing.

    Your help on this will be highly appreciated.

    Thank you,

    Regards,
    Shahadat
    e-mail : shahadat@edenintl.com

    ReplyDelete