Download MetaTrader 5

Function problem

To add comments, please log in or register
Abraham
89
Abraham 2011.01.25 15:01 

I've written this indicator and I get the response "not all control paths return a value" in my function area (at the bottom) and I can't work out what is wrong... can anybody help? I seem to have something wrong with my function structure. I don't fully understand the relationship between for(int i=pos;i<rates_total;i++) in the main section and for(int k=StartPosition;k<RatesCount;k++) in my function. The reason I have this problem is because the graph was not reading my function in real time and I don't know why so I tried to add (int k=StartPosition;k<RatesCount;k++) to fix this.


//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1

#property indicator_type1   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_label1  "test"


//---- indicator buffer

double                    ExtOBVBuffer16[];
double                    ExtOBVBuffer17[];

//+------------------------------------------------------------------+
//| On Balance Volume initialization function                        |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- define indicator buffer

   SetIndexBuffer(0,ExtOBVBuffer17, INDICATOR_DATA);
   SetIndexBuffer(1,ExtOBVBuffer16, INDICATOR_CALCULATIONS);

//--- set indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,0);
   IndicatorSetString(INDICATOR_SHORTNAME,"test");
//---- OnInit done
  }
//+------------------------------------------------------------------+
//| On Balance Volume                                                |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,
                const double &Close[],
                const int &Spread[])
  {

  //--- variables
   int    pos;
//--- check for bars count
   if(rates_total<2)
      return(0);
//--- starting calculation
   pos=prev_calculated-1;
//--- correct position, when it's first iteration
    
for(int i=pos;i<rates_total;i++)
{

   ExtOBVBuffer16[i] += OBV1(5, Close,pos,rates_total);
        
     }
   
    
//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }

   //**************
   // OBV1 FUNCTION
   //**************
  
double OBV1 (int n, const double &Close1[],int StartPosition, int RatesCount)
  
   {
   for(int k=StartPosition;k<RatesCount;k++)
{
         int counter = 0;
     
   if(Close1[k]-Close1[k-1]<0 && Close1[k-1]-Close1[k-2]<0 && Close1[k-2]-Close1[k-3]<0) {counter +=-1;}
   if(Close1[k]-Close1[k-1]>0 && Close1[k-1]-Close1[k-2]>0 && Close1[k-2]-Close1[k-3]>0) {counter +=1;}

    
      if(counter>0){ExtOBVBuffer17[k]=1;}
      if(counter<0){ExtOBVBuffer17[k]=-1;}
      if(counter==0){ExtOBVBuffer17[k]=0;}
     
     
      return(ExtOBVBuffer17[k]);
      }
   }
  

Ilyas
1209
Ilyas 2011.01.25 15:26  

please, insert codes properly

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1

#property indicator_type1   DRAW_LINE
#property indicator_color1  DodgerBlue
#property indicator_label1  "test"


//---- indicator buffer

double                    ExtOBVBuffer16[];
double                    ExtOBVBuffer17[];
//+------------------------------------------------------------------+
//| On Balance Volume initialization function                        |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- define indicator buffer

   SetIndexBuffer(0,ExtOBVBuffer17,INDICATOR_DATA);
   SetIndexBuffer(1,ExtOBVBuffer16,INDICATOR_CALCULATIONS);

//--- set indicator digits
   IndicatorSetInteger(INDICATOR_DIGITS,0);
   IndicatorSetString(INDICATOR_SHORTNAME,"test");
//---- OnInit done
  }
//+------------------------------------------------------------------+
//| On Balance Volume                                                |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,const int prev_calculated,                            // WRONG PARAMETERS IN OnCalculate FUNCTION!
                const double &Close[],
                const int &Spread[])
  {

//--- variables
   int    pos;
//--- check for bars count
   if(rates_total<2)
      return(0);
//--- starting calculation
   pos=prev_calculated-1;
//--- correct position, when it's first iteration

   for(int i=pos;i<rates_total;i++)
     {

      ExtOBVBuffer16[i]+=OBV1(5,Close,pos,rates_total);

     }

//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//**************
// OBV1 FUNCTION
//**************

double OBV1(int n,const double &Close1[],int StartPosition,int RatesCount)

  {
   for(int k=StartPosition;k<RatesCount;k++)
     {
      int counter=0;

      if(Close1[k]-Close1[k-1]<0 && Close1[k-1]-Close1[k-2]<0 && Close1[k-2]-Close1[k-3]<0) {counter +=-1;}
      if(Close1[k]-Close1[k-1]>0 && Close1[k-1]-Close1[k-2]>0 && Close1[k-2]-Close1[k-3]>0) {counter +=1;}


      if(counter>0){ExtOBVBuffer17[k]=1;}
      if(counter<0){ExtOBVBuffer17[k]=-1;}
      if(counter==0){ExtOBVBuffer17[k]=0;}

      return(ExtOBVBuffer17[k]);
     }

// WHERE RETURN?
  }
//+------------------------------------------------------------------+

To add comments, please log in or register