Quote:
|
Originally Posted by alanlaw
Dear All,
Could anyone tell me how to declare a array and than use the iMAOnArray or IMA function and to program a moving average on indicators, like macd histogram or momentum?
Please help me. Urgent.I have spend few days on working this.
Thank you very much.
Victor
|
Hi Victor,
If I understood your problem correctly this code might help:
PHP Code:
//+------------------------------------------------------------------+
//| iMAOnArray Demo.mq4 |
//| Codersguru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link "http://www.forex-tsd.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
string short_name = "iMAOnArray demo";
IndicatorShortName(short_name);
//----
return(1);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,limit;
double MyArray[]; //declare an array
int counted_bars=IndicatorCounted();
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
ArrayResize( MyArray, limit); //resize the array to the number of bars
ArraySetAsSeries(MyArray,true); //reverse the array direction.
for(i=0; i<limit; i++)
{
MyArray[i] = iMA(NULL,0,13,8,MODE_EMA,PRICE_CLOSE,i);
Print("MyArray = ",MyArray[i]);
}
for(i=0; i<limit; i++)
{
ExtMapBuffer1[i] = iMAOnArray(MyArray,limit,13,8,MODE_EMA,i);
Print("ExtMapBuffer1 = " , ExtMapBuffer1[i]);
}
return(0);
}
//+------------------------------------------------------------------+