Wednesday, December 21, 2011

AFL Code Writing : Lesson 3 (Part 2)

Today, we will give more power to our afl user. Our user will be able to change the color and style of moving average line without editing the code. But how? Let's see

By the below code, our user will not able to change the color through Parameters... (Right click on the chart and you will find the Parameters...). Because our below code is not dynamic. If our user wants to change the color of line s/he needs to change the code.

//Below code is not a dynamic code
Plot (MA(Close,50), "MA", colorRed);


But how can make our code dynamic so that our users can change the color as their desire without changing the code every time? It's easy. You have done this before. Just remember it. If you can't remember, don't worry. Just see the below code.

//Now our user will able to change the color of line dynamically
Plot (MA(Close,50), "MA", ParamColor("Color", colorRed)
);

By adding ParamColor(), we have given power to our user to change the color through Parameters without changing the code. But our above code is not fully dynamic because our user is not able to change the line style dynamically.

//Now our user will able to change the line style dynamically
Plot (MA(Close,50), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));


In a previous lesson, I explained the ParamColor() and ParamStyle(). So, I'm not going to explain it again. Below is the upto date complete code :

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

Date : December 20, 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,50), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));
_SECTION_END();

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


In our next lesson, I'll show you how can we make our moving average code more dynamic. Some of my friends are eager to see the moving average cross over. Don't worry. I'll explain it. Allah Hafiz.

No comments:

Post a Comment