Why does this program use up CPU resources? Please Help!

 
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_Peri od+")");
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);

}

 

Dear All,

What I needed is one allowing me to have 8 MA at the same time. I have tried to modify the code, I spent a long time, but the resultant mq4 draw a lot of CPU resource. So, I wonder if there is a ready made one that may suit me.

Please attached one if you have one yourself.

Many thanks,

kk007


 
//+------------------------------------------------------------------+
//|                                                         8_Ma.mq4 |
//|                                         Copyright © 2011, NADAV. |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, NADAV."

#property indicator_chart_window
#property indicator_buffers 8

#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Orange
#property indicator_color4 White
#property indicator_color5 Red
#property indicator_color6 Blue
#property indicator_color7 Orange
#property indicator_color8 White
               

string    indName="8_MA";

//---- buffers
double ExtMapBuf_1[];
double ExtMapBuf_2[];
double ExtMapBuf_3[];
double ExtMapBuf_4[];
double ExtMapBuf_5[];
double ExtMapBuf_6[];
double ExtMapBuf_7[];
double ExtMapBuf_8[];

extern int MA_ExtMapBuf_1 = 10;
extern int MA_ExtMapBuf_2 = 20;
extern int MA_ExtMapBuf_3 = 30;
extern int MA_ExtMapBuf_4 = 40;
extern int MA_ExtMapBuf_5 = 50;
extern int MA_ExtMapBuf_6 = 60;
extern int MA_ExtMapBuf_7 = 70;
extern int MA_ExtMapBuf_8 = 80;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(7,DRAW_LINE,STYLE_SOLID,2);
   IndicatorDigits(Digits);

   SetIndexBuffer(0,ExtMapBuf_1);
   SetIndexBuffer(1,ExtMapBuf_2);
   SetIndexBuffer(2,ExtMapBuf_3);
   SetIndexBuffer(3,ExtMapBuf_4);
   SetIndexBuffer(4,ExtMapBuf_5);
   SetIndexBuffer(5,ExtMapBuf_6);
   SetIndexBuffer(6,ExtMapBuf_7);
   SetIndexBuffer(7,ExtMapBuf_8);



  //----
   return(0);
  } 

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()                         
  {
  
   int limit;
   int counted_bars=IndicatorCounted();
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
             
   
   for(int i=0; i<limit; i++)
   ExtMapBuf_1[i] = iMA(NULL,0,MA_ExtMapBuf_1,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_2[i] = iMA(NULL,0,MA_ExtMapBuf_2,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_3[i] = iMA(NULL,0,MA_ExtMapBuf_3,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_4[i] = iMA(NULL,0,MA_ExtMapBuf_4,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_5[i] = iMA(NULL,0,MA_ExtMapBuf_5,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_6[i] = iMA(NULL,0,MA_ExtMapBuf_6,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_7[i] = iMA(NULL,0,MA_ExtMapBuf_7,0,MODE_SMA,PRICE_CLOSE,i);

   for(i=0; i<limit; i++)
   ExtMapBuf_8[i] = iMA(NULL,0,MA_ExtMapBuf_8,0,MODE_SMA,PRICE_CLOSE,i);

    
//--------------------------------------------------------------------
   return;                          
  }
//--------------------------------------------------------------------
 
qjol:


Thanks, but this one is nothing to do with multiple timeframe :(
 
post the code in properly formatted manner if you want people to even try to decipher it, nobody can read this unformatted mess.
 
#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_width1  1  
#property indicator_color2 FireBrick
#property indicator_width2  1  
#property indicator_color3 FireBrick
#property indicator_width3  1  
#property indicator_color4 Chocolate
#property indicator_width4  1  
#property indicator_color5 FireBrick
#property indicator_width5  1  
#property indicator_color6 FireBrick
#property indicator_width6  2  
#property indicator_color7 FireBrick
#property indicator_width7  2  
#property indicator_color8 FireBrick
#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);
}


void 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];
      }
   }
}


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);
}
Oops! New forum user, me. cheers!
 
Any hope?