Download MetaTrader 5

Cannot understand what's wrong with my buffers

To add comments, please log in or register
Are you a good trader? Become a signals provider and make even more money!
anddm
20
anddm 2015.03.27 16:07 

Hi everyone!

I tried to code the Chaikin Volatility indicator but it does not work at all. I know that there is plenty of examples of such an indicator, but my point is to understand what I am missing and whether I got the MQL language.

The problem I am encountering is the following: no data is drawn in the chart window, nothing. And I cannot see where is the problem because the code looks logically correct and the debugger simply says everything is fine (0 errors, 0 warnings).

I think the problem relies on the array/buffer handling, which I don't still get. This issue is driving me crazy.

Thanks in advances for any comment you will post.

#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Yellow
#property indicator_width1  1
#property indicator_color2  Gold
#property indicator_width2  1

#include <MovingAverages.mqh>
    
input int  InpChaikinPeriod = 10;               
input int  InpChaikinShift = 10;    

double CHV[];
double CHVMA[];
double EMA_HL[];
double HL[];   


//+------------------------------------------------------------------+


int OnInit()
{
   IndicatorDigits(5);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,CHV);   
   SetIndexLabel(0,"Chaikin Volatility");  
   IndicatorShortName("CHV");
   
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,CHVMA);   
   SetIndexLabel(1,"Chaikin MA");  
   IndicatorShortName("CHVMA");

   return(INIT_SUCCEEDED);

}


//+------------------------------------------------------------------+

  
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[])
{
  
   ArraySetAsSeries(CHV,false);
   ArraySetAsSeries(CHVMA,false);
   ArraySetAsSeries(HL,false);
   ArraySetAsSeries(EMA_HL,false);
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
   
   int i,pos;
   int tmp_period = InpChaikinPeriod + InpChaikinShift;
   
   if ( prev_calculated<1 ) {
      for(i=0; i<tmp_period; i++) {
         CHV[i]=0.0;
         CHVMA[i]=0.0;
         HL[i]=0.0;
         EMA_HL[i]=0.0;
         }
      }
     
   pos = tmp_period - 1;

   if ( prev_calculated>tmp_period ) pos = prev_calculated-1;
   
   for ( i=pos; i<rates_total; i++ ) { HL[i] = high[i] - low[i]; }
      
   ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpChaikinPeriod,HL,EMA_HL);
      
   for ( i=pos; i < rates_total; i++ ) { 
      CHV[i] = ( EMA_HL[i] - EMA_HL[i-InpChaikinShift] ) / EMA_HL[i-InpChaikinShift]*100;
      }  

   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpChaikinPeriod,CHV,CHVMA);
  
   return(rates_total); 
         
}
Vladimir Karputov
Moderator
46089
Vladimir Karputov 2015.03.27 17:41  
//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  Yellow
#property indicator_width1  1
#property indicator_color2  Gold
#property indicator_width2  1

#include <MovingAverages.mqh>

input int  InpChaikinPeriod= 10;
input int  InpChaikinShift = 10;

double CHV[];
double CHVMA[];
double EMA_HL[];
double HL[];
//+------------------------------------------------------------------+

int OnInit()
  {
   IndicatorDigits(5);
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,CHV,INDICATOR_DATA);
   SetIndexLabel(0,"Chaikin Volatility");
   IndicatorShortName("CHV");

   SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,CHVMA,INDICATOR_DATA);
   SetIndexLabel(1,"Chaikin MA");
   //IndicatorShortName("CHVMA");

   SetIndexBuffer(2,EMA_HL,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,HL,INDICATOR_CALCULATIONS);

   ArraySetAsSeries(CHV,false);
   ArraySetAsSeries(CHVMA,false);
   ArraySetAsSeries(HL,false);
   ArraySetAsSeries(EMA_HL,false);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+

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[])
  {
   ArraySetAsSeries(open,false);
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);

   int i,pos;
   int tmp_period=InpChaikinPeriod+InpChaikinShift;

   if(prev_calculated==0)
     {
      for(i=0; i<tmp_period; i++)
        {
         CHV[i]=i;
         CHVMA[i]=i;
         HL[i]=i;
         EMA_HL[i]=i;
        }
     }
   pos=tmp_period-1;

   if(prev_calculated>tmp_period)
      pos=prev_calculated-1;

   for(i=pos; i<rates_total; i++)
     {
      HL[i]=high[i]-low[i];
     }

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,InpChaikinPeriod,HL,EMA_HL);

   for(i=pos; i<rates_total; i++)
     {
      CHV[i]=(EMA_HL[i]-EMA_HL[i-InpChaikinShift])/EMA_HL[i-InpChaikinShift]*100;
     }

   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpChaikinPeriod,CHV,CHVMA);

   return(rates_total);
  }
//+------------------------------------------------------------------+


Screenshot:

1

To add comments, please log in or register