Returned from the hibernation.
In our last tutorials we show you how to add simple moving average in your chart. We also learnt that how to make the moving average more dynamic for our user. Today, I will show you how to write moving average crossover.
Now, we will recall our last simple moving average code:
_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();
Today, we will see a moving average crossover between 10 days moving average and 30 days moving average. We already have code for moving average. We will use it. I have just copied the above moving average code 2 times as follows:
_SECTION_BEGIN("MA 30");
//Below code will display 30 days simple moving average line
Plot (MA(Close, Param("Periods", 30, 2, 300)), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));
_SECTION_END();
_SECTION_BEGIN("MA 10");
//Below code will display 10 days simple moving average line
Plot (MA(Close, Param("Periods", 10, 2, 300)), "MA", ParamColor("Color", colorBlue), ParamStyle("Style", styleThick));
_SECTION_END();
I have made the following changes on our above two moving averages:
- I have given separate names for our two moving averages. One is MA 30 and another one is MA 10. MA stands for Moving Average.
- I have changed 50 days to 30 days and 10 days.
- I have also changed the 10 days moving average color from from red to blue as our both moving average color were same.
Now, our final code for the two moving averages crossover as follows:
/*
-------------------------------
AFL code writing for moving average cross-over.
Date : May 25, 2013
-------------------------------
*/
_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("MA 30");
//Below code will display 30 days simple moving average line
Plot (MA(Close, Param("Periods",30, 2, 300)), "MA", ParamColor("Color", colorRed), ParamStyle("Style", styleThick));
_SECTION_END();
_SECTION_BEGIN("MA 10");
//Below code will display 10 days simple moving average line
Plot (MA(Close, Param("Periods", 10, 2, 300)), "MA", ParamColor("Color",colorBlue), ParamStyle("Style", styleThick));
_SECTION_END();
//-----------------------------
Allah Hafiz