Download MetaTrader 5
To add comments, please log in or register
Do you know that MQL5 can create custom graphical tools?
trader_2016
6
trader_2016 2016.03.10 11:02 

Hi All,

I am trying to create a moving average indicator of the OBV. However when I attach it to a chart, it grinds MT4 to a halt and sometimes even crashed MT4. Am I doing something fundamentally wrong here? I tried to first use iMAonArray and when that did not work, then tried to do it manually, however with the same degenerative result.  Here is the code, can anyone see what is wrong with it?

Thx 

 

#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow   // OBV color
#property indicator_color2 Magenta  // OBV_MA_fast color
#property indicator_color3 Cyan     // OBV_MA_slow color

//--- input parameters
extern int OBV_MA_fast=5,OBV_MA_slow=300; 

//--- buffers
double OBVBuf[];
double MA_fast_Buf[];
double MA_slow_Buf[];

int init()
{
   IndicatorShortName("MA of the OBV");
   
   // drawing settings  OBV
   SetIndexBuffer(0,OBVBuf);
   SetIndexLabel(0,"OBV");                    
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);  
   
   // drawing settings of Moving Average of OBV_MA_fast
   SetIndexBuffer(1,MA_fast_Buf);
   SetIndexLabel(1,"MA("+OBV_MA_fast+") of OBV");       
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);            
   
   // drawing settings of Moving Average of OBV_MA_slow
   SetIndexBuffer(2,MA_slow_Buf);
   SetIndexLabel(2,"MA("+OBV_MA_slow+") of OBV");       
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,1);    
   
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int start()
{
   int i,counted_bars=IndicatorCounted();
   if(counted_bars<0) counted_bars=0;
   double MA_fast_temp=0,MA_slow_temp=0;
   
   i=Bars-counted_bars-1;   // Index of the first uncounted
   while(i>=0)              // Loop for uncounted bars
   {
      OBVBuf[i]=(iOBV(NULL,0,PRICE_CLOSE,i)); 
      for (int j=0; j<OBV_MA_fast; j++)
      {
         MA_fast_temp+=iOBV(NULL,0,PRICE_CLOSE,i+j);
      }
      MA_fast_Buf[i]=MA_fast_temp/OBV_MA_fast;

      for (j=0; j<OBV_MA_slow; j++)
      {
         MA_slow_temp+=iOBV(NULL,0,PRICE_CLOSE,i+j);
      }
      MA_slow_Buf[i]=MA_slow_temp/OBV_MA_slow;      

      //MA_fast_Buf[i]=iMAOnArray(OBVBuf,0,OBV_MA_fast,0,0,i);
      //MA_slow_Buf[i]=iMAOnArray(OBVBuf,0,OBV_MA_slow,0,0,i);
   }
   
   return(0);
}

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

//----
return(0);
}
//+------------------------------------------------------------------+
Vasyl Nosal
18201
Vasyl Nosal 2016.03.10 13:14  

just count OBV buf

and than create new while and count MAonarray 

whroeder1
13596
whroeder1 2016.03.10 13:34  
  1. for (int j=0; j<OBV_MA_fast; j++){
      MA_fast_temp+=iOBV(NULL,0,PRICE_CLOSE,i+j);
    }
    MA_fast_Buf[i]=MA_fast_temp/OBV_MA_fast;
    
    Don't you want to zero your temp for each bar?
  2. Handle your look backs correctly
    int LB = MathMax(OBV_MA_fast, OBV_MA_slow);
    i=Bars-1-MathMax(LB,counted_bars);   // Index of the first uncounted
    
/
To add comments, please log in or register