I was used to use 8 MTF_MA.mq4 and packed them in a template. Nevertheless, I want to consolidate them into 1 mq4. I modify the code, but it draw a lot of CPU resource and slow down my computer.
Is there anything wrong in the program? Please help!
----------------------------
#property copyright "Copyright ?2007-08, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Chocolate
#property indicator_color2 FireBrick
#property indicator_color3 FireBrick
#property indicator_color4 Chocolate
#property indicator_color5 FireBrick
#property indicator_color6 FireBrick
#property indicator_color7 FireBrick
#property indicator_color8 FireBrick
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 2
#property indicator_width7 2
#property indicator_width8 2
//string short_name;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
SetIndexStyle(4,DRAW_LINE);
SetIndexStyle(5,DRAW_LINE);
SetIndexStyle(6,DRAW_LINE);
SetIndexStyle(7,DRAW_LINE);
return(0);
}
//+------------------------------------------------------------------+
//| AllAverages_v2.2 |
//+------------------------------------------------------------------+
int start()
{
DrawEMA(5, 10, 0);
DrawEMA(5, 50, 1);
DrawEMA(15, 50, 2);
DrawEMA(60, 10, 3);
DrawEMA(60, 50, 4);
DrawEMA(60, 200, 5);
DrawEMA(1440, 10, 6);
DrawEMA(1440, 50, 7);
return(0);
}
int DrawEMA(int TimeFrame, int MA_Period, int LineNo)
{
int limit, y, i, shift,mBars, mcnt_bars, draw_begin, cnt_bars=IndicatorCounted();
double aPrice[], MA[], mMA[];
switch(TimeFrame)
{
case 1 : string TF = "M1"; break;
case 5 : TF = "M5"; break;
case 15 : TF = "M15"; break;
case 30 : TF = "M30"; break;
case 60 : TF = "H1"; break;
case 240 : TF ="H4"; break;
case 1440 : TF="D1"; break;
}
draw_begin=MA_Period*TimeFrame/Period();
SetIndexDrawBegin(LineNo,draw_begin);
SetIndexLabel(LineNo,TF+"("+MA_Period+")");
SetIndexBuffer(LineNo,MA);
if(TimeFrame!=Period()) mBars = iBars(NULL,TimeFrame); else mBars = Bars;
ArrayResize(aPrice,mBars);
ArrayResize(mMA,mBars);
if(cnt_bars<1)
{
for(i=1;i<=draw_begin;i++)
{
MA[Bars-i]=iMA(NULL,TimeFrame,1,0,0,0,Bars-i);
}
mcnt_bars = 0;
}
if(mcnt_bars > 0) mcnt_bars--;
for(y=mcnt_bars;y<mBars;y++)
{
aPrice[y] = iMA(NULL,TimeFrame,1,0,0,0,mBars-y-1);
mMA[y] = EMA(aPrice[y],mMA,MA_Period,y);
if(TimeFrame == Period())
{
MA[mBars-y-1] = mMA[y];
}
}
mcnt_bars = mBars-1;
if(TimeFrame > Period())
{
if(cnt_bars>0) cnt_bars--;
limit = Bars-cnt_bars+TimeFrame/Period()-1;
for(shift=0,y=0;shift<limit;shift++)
{
if (Time[shift] < iTime(NULL,TimeFrame,y)) y++;
MA[shift] = mMA[mBars-y-1];
}
}
return;
}
double EMA(double price,double array[],int per,int bar)
{
if(bar == 2) double ema = price;
else
if(bar > 2) ema = array[bar-1] + 2.0/(1+per)*(price - array[bar-1]);
return(ema);
}
Is there anything wrong in the program? Please help!
----------------------------
#property copyright "Copyright ?2007-08, TrendLaboratory"
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Chocolate
#property indicator_color2 FireBrick
#property indicator_color3 FireBrick
#property indicator_color4 Chocolate
#property indicator_color5 FireBrick
#property indicator_color6 FireBrick
#property indicator_color7 FireBrick
#property indicator_color8 FireBrick
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 1
#property indicator_width4 1
#property indicator_width5 1
#property indicator_width6 2
#property indicator_width7 2
#property indicator_width8 2
//string short_name;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
SetIndexStyle(4,DRAW_LINE);
SetIndexStyle(5,DRAW_LINE);
SetIndexStyle(6,DRAW_LINE);
SetIndexStyle(7,DRAW_LINE);
return(0);
}
//+------------------------------------------------------------------+
//| AllAverages_v2.2 |
//+------------------------------------------------------------------+
int start()
{
DrawEMA(5, 10, 0);
DrawEMA(5, 50, 1);
DrawEMA(15, 50, 2);
DrawEMA(60, 10, 3);
DrawEMA(60, 50, 4);
DrawEMA(60, 200, 5);
DrawEMA(1440, 10, 6);
DrawEMA(1440, 50, 7);
return(0);
}
int DrawEMA(int TimeFrame, int MA_Period, int LineNo)
{
int limit, y, i, shift,mBars, mcnt_bars, draw_begin, cnt_bars=IndicatorCounted();
double aPrice[], MA[], mMA[];
switch(TimeFrame)
{
case 1 : string TF = "M1"; break;
case 5 : TF = "M5"; break;
case 15 : TF = "M15"; break;
case 30 : TF = "M30"; break;
case 60 : TF = "H1"; break;
case 240 : TF ="H4"; break;
case 1440 : TF="D1"; break;
}
draw_begin=MA_Period*TimeFrame/Period();
SetIndexDrawBegin(LineNo,draw_begin);
SetIndexLabel(LineNo,TF+"("+MA_Period+")");
SetIndexBuffer(LineNo,MA);
if(TimeFrame!=Period()) mBars = iBars(NULL,TimeFrame); else mBars = Bars;
ArrayResize(aPrice,mBars);
ArrayResize(mMA,mBars);
if(cnt_bars<1)
{
for(i=1;i<=draw_begin;i++)
{
MA[Bars-i]=iMA(NULL,TimeFrame,1,0,0,0,Bars-i);
}
mcnt_bars = 0;
}
if(mcnt_bars > 0) mcnt_bars--;
for(y=mcnt_bars;y<mBars;y++)
{
aPrice[y] = iMA(NULL,TimeFrame,1,0,0,0,mBars-y-1);
mMA[y] = EMA(aPrice[y],mMA,MA_Period,y);
if(TimeFrame == Period())
{
MA[mBars-y-1] = mMA[y];
}
}
mcnt_bars = mBars-1;
if(TimeFrame > Period())
{
if(cnt_bars>0) cnt_bars--;
limit = Bars-cnt_bars+TimeFrame/Period()-1;
for(shift=0,y=0;shift<limit;shift++)
{
if (Time[shift] < iTime(NULL,TimeFrame,y)) y++;
MA[shift] = mMA[mBars-y-1];
}
}
return;
}
double EMA(double price,double array[],int per,int bar)
{
if(bar == 2) double ema = price;
else
if(bar > 2) ema = array[bar-1] + 2.0/(1+per)*(price - array[bar-1]);
return(ema);
}