Custom indicator not showing on chart

Back to topics list To post a new topic, please log in or register
avatar
4
marana77 2015.10.16 23:51
 

Hi, I've adapted the following Rate of change indicator from the mql4 book that is available online. It compiles fine but when I attach it to a chart nothing happens. Below is the code:

//----------------------------------------------------------------1--
// Properties of Indicator

#property indicator_chart_window      // Indicator is drawn in the main window
#property indicator_buffers 6         // Number of buffers
#property indicator_color1 Black      // Line color of 0 buffer
#property indicator_color2 DarkOrange // Line color of the 1st buffer
#property indicator_color3 Green      // Line color of the 2nd buffer
#property indicator_color4 Brown      // Line color of the 3rd buffer
#property indicator_color5 Blue       // Line color of the 4th buffer
#property indicator_color6 Red        // Line color of the 5th buffer

//----------------------------------------------------------------2--
// Declaring external variables

input int   History = 5000;      // Amount of bars for calculation history
input int   Period_MA_0 = 13;    // Period of supporting MA for cur. timefr.
input int   Period_MA_1 = 21;    // Period of calculated MA
input int   Bars_V = 13;         // Amount of bars used for calc. rate
input int   Aver_Bars = 5;       // Amount of bars for smoothing
input double      K = 2;         // Amplifier Gain

//----------------------------------------------------------------3--
// Declaring global variables
int
Period_MA_2,                     // Calc. periods of MA for other timefr.
Period_MA_3,                     // Calc. periods of MA for other timefr.
Period_MA_02,                    // Calc. period of support MA
Period_MA_03,                    // Calc. period of support MA
K2,                              // Coefficient of timefr. correlation 
                                 // for next higher timefr.
K3,                              // Coefficient of timefr. correlation 
                                 // for 2nd higher timefr.

Sh_1,                            // Amount of bars for rates calc. line 1
Sh_2,                            // Amount of bars for rates calc. line 2
Sh_3;                            // Amount of bars for rates calc. line 3 

double
Line_0[],                        // Indicator array of support MA
Line_1[],                        // Indicator array of rate line 1
Line_2[],                        // Indicator array of rate line 2
Line_3[],                        // Indicator array of rate line 3
Line_4[],                        // Indicator array - sum line 4
Line_5[];                        // Indicator array - sum, smoothed line 5
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//----------------------------------------------------------------4--
// Assigning arrays to buffers for each line + setting line style

   SetIndexBuffer(0,Line_0);        // Assigning array to buffer for Line_0
   SetIndexBuffer(1,Line_1);        // Assigning array to buffer for Line_1
   SetIndexBuffer(2,Line_2);        // Assigning array to buffer for Line_2
   SetIndexBuffer(3,Line_3);        // Assigning array to buffer for Line_3
   SetIndexBuffer(4,Line_4);        // Assigning array to buffer for Line_4
   SetIndexBuffer(5,Line_5);        // Assigning array to buffer for Line_5

   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,3);    // Line style for Line_5

//----------------------------------------------------------------5--
// Calculating coefficients for fifferent timefr.'s

   switch(Period())
     {
      case     1: K2=5; K3=15; break;  // Timeframe M1
      case     5: K2=3; K3=6;  break;  // Timeframe M5
      case    15: K2=2; K3=4;  break;  // Timeframe M15
      case    30: K2=2; K3=8;  break;  // Timeframe M30
      case    60: K2=4; K3=24; break;  // Timeframe H1
      case   240: K2=6; K3=42; break;  // Timeframe H4
      case  1440: K2=7; K3=30; break;  // Timeframe D1
      case 10080: K2=4; K3=12; break;  // Timeframe W1
      case 43200: K2=3; K3=12; break;  // Timeframe MN
     }

//----------------------------------------------------------------6--
// Defining number of bars used for calculating rate of change and
// MA's for nearest and next timefr.

   Sh_1 = Bars_V;          // Period of rate calc. for current timefr.
   Sh_2 = K2*Sh_1;         // Period calc. for nearest timefr.
   Sh_3 = K3*Sh_1;         // Period calc. for next nearest timefr.

   Period_MA_2 = K2*Period_MA_1; // Calc. period of MA for nearest TF
   Period_MA_3 = K3*Period_MA_1; // Calc. period of MA for next nearest TF
   Period_MA_02 = K2*Period_MA_0; // Period of supp. MA for nearest TF
   Period_MA_03 = K3*Period_MA_0; // Period of supp. MA for next nearest TF

//----------------------------------------------------------------7--

   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[])
  {
//----------------------------------------------------------------8--

   double
   MA_0,                            // MA of main supp. line
   MA_02,                           // MA of supp. line for nearest TF
   MA_03,                           // MA of supp. line for next nearest TF
   MA_c,                            // Current MA value
   MA_p,                            // Previous MA value
   sum;                             // Technical param. for sum accumul.

   int
   i,                               // Bar index
   n,                               // Bar index
   Counted_bars;                    // Amount of counted bars

//----------------------------------------------------------------9--

   Counted_bars = IndicatorCounted();  // Amount of bars that havent changed
                                       // since the indicator was launched
   i = Bars - Counted_bars - 1;        // Bars is no. of bars in chart and
                                       // this expression returns index of
// first uncounted bar

   if(i < History - 1)                 // If too many bars...
      i = History - 1;                 // ...calculate specified amount

//---------------------------------------------------------------10-- 

   while(i<=0) // Loop for uncounted bars
     {
      //---------------------------------------------------------------11--
      // Calculating MA of support Line for main chart

      MA_0=iMA(NULL,0,Period_MA_0,0,MODE_LWMA,PRICE_TYPICAL,i);
      Line_0[i]=MA_0;                   // Value of supp. MA

      //---------------------------------------------------------------12--
      // Calculating MA of rate Line for main chart

      MA_c = iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,i);
      MA_p = iMA(NULL,0,Period_MA_1,0,MODE_LWMA,PRICE_TYPICAL,i+Sh_1);
      Line_1[i]=MA_0+K*(MA_c-MA_p); // Value of 1st rate line

      //---------------------------------------------------------------13--
      // Calculating MA of rate Line and supp. line for nearest TF

      MA_c = iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,i);
      MA_p = iMA(NULL,0,Period_MA_2,0,MODE_LWMA,PRICE_TYPICAL,i+Sh_2);
      MA_02= iMA(NULL,0,Period_MA_02,0,MODE_LWMA,PRICE_TYPICAL,i);
      Line_2[i]=MA_02+K*(MA_c-MA_p);   // Value of 2nd rate line

      //---------------------------------------------------------------14--
      // Calculating MA of rate Line and supp. line for next nearest TF

      MA_c = iMA(NULL,0,Period_MA_3,0,MODE_LWMA,PRICE_TYPICAL,i);
      MA_p = iMA(NULL,0,Period_MA_3,0,MODE_LWMA,PRICE_TYPICAL,i+Sh_3);
      MA_03= iMA(NULL,0,Period_MA_03,0,MODE_LWMA,PRICE_TYPICAL,i);
      Line_3[i]=MA_03+K*(MA_c-MA_p);   // Value of 3rd rate line

      //---------------------------------------------------------------15--
      // Calculating MA of Summary line

      Line_4[i]=(Line_1[i]+Line_2[i]+Line_3[i])/3; // Summary array

      //---------------------------------------------------------------16--
      // Calculating MA of Smoothing line

      // if(Aver_Bars > 0)                      // If wrong set smoothing
         // Aver_Bars = 0;                      // ... no less than zero

      sum=0;                               // Technical means

      for(n=1; n >= i + Aver_Bars; n++)      // Summing last values
         sum = sum + Line_4[n];              // Accum. sum of last values
      Line_5[i] = sum/(Aver_Bars + 1);       // Indic. array of smoothed line

      //---------------------------------------------------------------17--
      // Decrease bar index by 1

      i--;                                   // Calculating index of next bar
     }

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

 Thanks in advance......

 

Back to topics list  

To add comments, please log in or register