Please Help iCustom error

 

Hi everyone

I'm newbie for code.

I Try to coding iCustom but I have some problem.

my indicator get som zero divide error.

  double x = (a+b)/a;
  double y = a/b;
  double z = x/y;

so please help me.


//+------------------------------------------------------------------+
//|                                                         Phi2.mq4 |
//|                                                  Suttisak Boonla |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Suttisak Boonla"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window


#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1


input string pair = "USDCHF";
input double Phi = 1.618;

int limit;

//--- indicator buffers
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer);
    string short_name;
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Label1Buffer);
//--- name for DataWindow and indicator subwindow label
   short_name="Phi";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input parameter

//---
   SetIndexDrawBegin(0,60);
//--- initialization done
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   int limit;
  int counted_bars=IndicatorCounted();
  //---- check for possible errors
     if(counted_bars<0) return(-1);
  //---- the last counted bar will be recounted
     if(counted_bars>0) counted_bars--;
     limit=Bars-counted_bars;
  //---- main loop

  
//--- counting from 0 to rates_total
   ArraySetAsSeries(Label1Buffer,false);
   ArraySetAsSeries(close,false);

 

 

   
     for(int i=0; i<limit; i++)
       {
        
  double a = iClose(NULL,0,i);
  double b = iClose(pair,0,i);
  double x = (a+b)/a;
  double y = a/b;
  double z = x/y;
        //---- ma_shift set to 0 because SetIndexShift called abowe
        Label1Buffer[i]= z;
        
       }
  //---- done
   

   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+ 
 
Suttisak Boonla:

Hi everyone

I'm newbie for code.

I Try to coding iCustom but I have some problem.

my indicator get som zero divide error.

  double x = (a+b)/a;
  double y = a/b;
  double z = x/y;

so please help me.


Check for zero before you divide. If it's zero you either don't do the division or set the zero to a very small number instead so the result will be "near" zero (eg 0.0000000001)
Reason: