I cannot divide two numbers by each other in a simple way in mql4.

 
When I want to divide the elements of two arrays that I defined as double, it does not show anything on the screen. What is the reason for this ?
I tried to write a simple code below and indicated the problematic place with the '//' part next to the vCMO variable.
Please help me, I want to plot the vCMO variable on the screen. Actually vCMO is just an example, I can't divide any array content and show it on the screen. What am I doing wrong? I defined both variables as double and I want to divide two double values to get a new value, but it doesn't do this, what am I doing wrong?
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Green
#property indicator_width1 2

double TrendUpBuffer[], SourcePrice[];
double vud1[], vdd1[];
double vUD, vDD, vCMO;
extern int Periods = 10;
input ENUM_APPLIED_PRICE Price_MA_Source = PRICE_MEDIAN;

int init()
  {
   
   IndicatorDigits(Digits);
   IndicatorBuffers(4);
   SetIndexBuffer(0, TrendUpBuffer);
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);

   SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(1,SourcePrice);

   SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(2,vud1);

   SetIndexStyle(3,DRAW_NONE);
   SetIndexBuffer(3,vdd1);
   
   return(0);
  }

int deinit()
  {
   return(0);
  }

int start()
  {
   int limit, i;
 
   int counted_bars = IndicatorCounted();
       
   if(counted_bars < 0) return(-1);

   if(counted_bars > 0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) 
   {
      if(Price_MA_Source == 0)SourcePrice[i] = Close[i];
      if(Price_MA_Source == 1)SourcePrice[i] = Open[i];
      if(Price_MA_Source == 2)SourcePrice[i] = High[i];
      if(Price_MA_Source == 3)SourcePrice[i] = Low[i];
      if(Price_MA_Source == 4)SourcePrice[i] = (High[i]+Low[i])/2;
      if(Price_MA_Source == 5)SourcePrice[i] = (High[i]+Low[i]+Close[i])/3;
      if(Price_MA_Source == 6)SourcePrice[i] = (High[i]+Low[i]+Close[i]+Close[i])/4;
   }
   
   for(i = 0; i <= limit; i++)
   {
      if(SourcePrice[i] > SourcePrice[i+1])
      {
         vud1[i] = SourcePrice[i] - SourcePrice[i+1]; 
      }
      else
      {
         vud1[i] = 0;
      }
      
      if(SourcePrice[i] < SourcePrice[i+1])
      {
         vdd1[i] = SourcePrice[i+1] - SourcePrice[i];
      }
      else
      {
         vdd1[i] = 0;
      }   
   }
   
   for(i = 0; i <= limit; i++) 
   {
          
         vUD = vud1[i] + vud1[i+1] + vud1[i+2] + vud1[i+3] + vud1[i+4] + vud1[i+5] + vud1[i+6] + vud1[i+7] + vud1[i+8];
         vDD = vdd1[i] + vdd1[i+1] + vdd1[i+2] + vdd1[i+3] + vdd1[i+4] + vdd1[i+5] + vdd1[i+6] + vdd1[i+7] + vdd1[i+8];
         
         vCMO = ((vUD-vDD)/(vUD+vDD)); // ** This is the problematic part. It doesn't divide the two numbers and since it can't do that, whatever I put in the buffer below doesn't appear on the screen. 
         //However, when you remove this part and write Close[i] to the buffer part below, it will appear on the screen, but when you do the same without removing this part, it will not appear. Why is 
         //this happening?
                      
   }
      
   for(i = Bars; i >= 0; i--) 
   {    
         TrendUpBuffer[i] = vCMO; 
         
   }
   WindowRedraw();
      
   return(0);
  }


 

It's been a while since I've coded in MQL4, so I might be a bit rusty,

But here are few problems I've found with your code-

1) This is more of a suggestion that a problem- It is advised to stop using the obsolete start() and IndicatorCounter() functions, and switch to using OnCalculate().


2) Serious bug-

int start()
  {
   int limit, i;
 
   int counted_bars = IndicatorCounted();
       
   if(counted_bars < 0) return(-1);

   if(counted_bars > 0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = 0; i <= limit; i++) 
   {
          
         vUD = vud1[i] + vud1[i+1] + vud1[i+2] + vud1[i+3] + vud1[i+4] + vud1[i+5] + vud1[i+6] + vud1[i+7] + vud1[i+8];
         vDD = vdd1[i] + vdd1[i+1] + vdd1[i+2] + vdd1[i+3] + vdd1[i+4] + vdd1[i+5] + vdd1[i+6] + vdd1[i+7] + vdd1[i+8];
         
   }

Here I might be mistaken, As I have not programmed in MQL4 in a while (switched to MQL5 a long time ago),

If I remember correctly, the very first time your start() function will run, IndicatorCounted() will return 0.

If it is the case, consider what happens in the above code snippet-

limit will equal number of total bars (limit=Bars-0),

Then in the loop below it, when i will equal limit, the vUD and VDD calculations, which try to access i+1 i+2 ........ i+8, will result in an array out of range exception.


3) Serious bug-

for(i = 0; i <= limit; i++) 
   {
          
         vUD = vud1[i] + vud1[i+1] + vud1[i+2] + vud1[i+3] + vud1[i+4] + vud1[i+5] + vud1[i+6] + vud1[i+7] + vud1[i+8];
         vDD = vdd1[i] + vdd1[i+1] + vdd1[i+2] + vdd1[i+3] + vdd1[i+4] + vdd1[i+5] + vdd1[i+6] + vdd1[i+7] + vdd1[i+8];
         
         vCMO = ((vUD-vDD)/(vUD+vDD)); // ** This is the problematic part. It doesn't divide the two numbers and since it can't do that, whatever I put in the buffer below doesn't appear on the screen. 
         //However, when you remove this part and write Close[i] to the buffer part below, it will appear on the screen, but when you do the same without removing this part, it will not appear. Why is 
         //this happening?
                      
   }
      
   for(i = Bars; i >= 0; i--) 
   {    
         TrendUpBuffer[i] = vCMO; 
         
   }

vCMO is a double, and not an array,

So in the above code snippet, the first loop always overwrites the same vCMO variable,

Meaning that in the end it will just hold the value of the last calculation and all the other loop iterations are irrelevant.

Then, in the second loop, all of the TrendUpBuffer buffer is written with the same one value that vCMO holds.


4) Minor performance bug (on its own) / Major bug (if bugs above are not resolved)-

for(i = Bars; i >= 0; i--) 
   {    
         TrendUpBuffer[i] = vCMO; 
         
   }

In the code snippet above you keep calculating all of the bars, regardless of previous calculations.

If this remains your only bug, the indicator will work properly, just not very efficiently.

However, due to bug #3 this calculation is wrong anyway.


Good luck mate.

 
AMI289 #:

It's been a while since I've coded in MQL4, so I might be a bit rusty,

But here are few problems I've found with your code-

1) This is more of a suggestion that a problem- It is advised to stop using the obsolete start() and IndicatorCounter() functions, and switch to using OnCalculate().


2) Serious bug-

Here I might be mistaken, As I have not programmed in MQL4 in a while (switched to MQL5 a long time ago),

If I remember correctly, the very first time your start() function will run, IndicatorCounted() will return 0.

If it is the case, consider what happens in the above code snippet-

limit will equal number of total bars (limit=Bars-0),

Then in the loop below it, when i will equal limit, the vUD and VDD calculations, which try to access i+1 i+2 ........ i+8, will result in an array out of range exception.


3) Serious bug-

vCMO is a double, and not an array,

So in the above code snippet, the first loop always overwrites the same vCMO variable,

Meaning that in the end it will just hold the value of the last calculation and all the other loop iterations are irrelevant.

Then, in the second loop, all of the TrendUpBuffer buffer is written with the same one value that vCMO holds.


4) Minor performance bug (on its own) / Major bug (if bugs above are not resolved)-

In the code snippet above you keep calculating all of the bars, regardless of previous calculations.

If this remains your only bug, the indicator will work properly, just not very efficiently.

However, due to bug #3 this calculation is wrong anyway.


Good luck mate.

Thank you, I could not solve my problem exactly but I will learn to use the onCalculate function.