Download MetaTrader 5

Need help with my indicator please

To add comments, please log in or register
Carry on your private correspondence in your profile. It is secure!
Franco
374
Franco 2010.05.28 14:28 

Hey there everyone,

 I am busy writing my first MQL5 indicator.  I'm not sure what I am doing wrong, but it does not work.  It is a very simple indicator, it one line that should be drawn, but nothing is happening.  

I would appreciate it if someone could help me spot the problem.  Like I said NOTHING is working, it wont draw any line on the chart.  Here is the code:

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1 Blue
#property indicator_label1  "GH"

extern int Lookback = 10;

double buf[];

int  SMA_PH_Handle;
int  SMA_PL_Handle;

double SMA_PH_Buf[];
double SMA_PL_Buf[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

 SetIndexBuffer(0,buf,INDICATOR_DATA);
 
 SetIndexBuffer(1,SMA_PH_Buf,INDICATOR_CALCULATIONS);
 SetIndexBuffer(2,SMA_PL_Buf,INDICATOR_CALCULATIONS);
 
 SMA_PH_Handle=iMA(NULL,0,Lookback,0,MODE_SMA,PRICE_HIGH);
 SMA_PL_Handle=iMA(NULL,0,Lookback,0,MODE_SMA,PRICE_LOW);
 
 
  
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
//---
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
     
    
    //Copy MA values into array 
     CopyBuffer(SMA_PH_Handle,0,0,to_copy,SMA_PH_Buf);
     CopyBuffer(SMA_PL_Handle,0,0,to_copy,SMA_PL_Buf);

     
//Calculations
   int x;
   int n;
   for (int i = Bars(NULL,0) - Lookback; i >= 0; i--) {
      if (close[i] >SMA_PH_Buf[i + 1]) n = 1;
      else {
         if (close[i] < SMA_PL_Buf[i + 1]) n = -1;
         else n = 0;
      }
      if (n != 0) x = n;
      if (x == -1) buf[i] = SMA_PH_Buf[i];
      else buf[i] = SMA_PL_Buf[i];
   }
   return (0);
  }
Documentation on MQL5: Standard Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Environment State / Running MQL5 Program Properties - Documentation on MQL5
Franco
374
Franco 2010.05.28 14:50  

I think I found a solution,  I changed the last line with:

return(rates_total);

And seems like it is working

 

Franco
374
Franco 2010.05.28 15:24  

Now I am struggling with another problem.  I think the problem lies with the code below, because the line wont calculate correctly.  About 10 bars before the newest bar the line just shoots straight up or down

  int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
     
    
    //Copy MA values into array 
     CopyBuffer(SMA_PH_Handle,0,0,to_copy ,SMA_PH_Buf);
     CopyBuffer(SMA_PL_Handle,0,0,to_copy,SMA_PL_Buf);

 

To add comments, please log in or register