Problem with a loop

 

Hi,

I want to calculate the average volume of the 5 bars (no. 2-6) and I did it this way:

double vol2=iVolume(NULL, 0, shift+2);
double vol3=iVolume(NULL, 0, shift+3);
double vol4=iVolume(NULL, 0, shift+4);
double vol5=iVolume(NULL, 0, shift+5);
double vol6=iVolume(NULL, 0, shift+6);
double averagevol=(vol2+vol3+vol4+vol5+vol6)/5;


but I would like to make the period variable, so I tried it with a loop:

int AvPeriod=5;
double vol=0;
for(i=shift+2;i<=shift+2+AvPeriod;i++)
{
vol=vol+iVolume(NULL, 0, shift+i);

}

double averagevol=vol/AvPeriod;


I think there is just a very small bug in it but I can't find it. Seems to be logical but the results are wrong.

Thanks for some help!


PS. Does anyone know why the code I post is not colored like in other postings?!

 
mar:

Hi,

PS. Does anyone know why the code I post is not colored like in other postings?!

Did you use the SRC button ?

so you have 5 values, at shift+2, shift+2+1, shift+2+2, shift+2+3, shift+2+4, shift+2+5 . . . ah, that is 6 values not 5.

try this . . .

int AvPeriod=5;
double vol=0;

for(i=shift+2; i < shift + 2 + AvPeriod; i++)
{
vol=vol+iVolume(NULL, 0, shift+i);

}

double averagevol=vol/AvPeriod;
 

Hi Raptor,

you are right. But after I corrected it I still get wrong results. I can't understand why... My purpose it to compare the last bars volume to the average volume of the bars 2-6 but my indicator shows completely wrong arrows when I use a loop to calculate the average volume. It only works when I do it like described above. That makes no sense to me?! The calculation is pretty simple but the loop doesn't work. Very strange....

 

Sorry . . . one more problem I just spotted . ..

int AvPeriod=5;
double vol=0;

for(i=shift+2; i < shift + 2 + AvPeriod; i++)
{
vol=vol+iVolume(NULL, 0, i  );     //  i incorporates shift ! !

}

double averagevol=vol/AvPeriod;
 
It works!! Thank you very much for your help!!
 
mar:
It works!! Thank you very much for your help!!
Of course it does . . . . when you have a problem with mql4 code there is always a reason and solution, you just need to methodically work through it and find the solution. In the process of fixing issues you will learn . . .
 
Why use a function call when a simple array access will do?
iVolume(NULL, 0, i  ) == Volume[i]
 

@WHRoeder: because I am a beginner and I don't know about array access. The logic of loops is clear to me because of some VBA knowledge but the rest I still have to learn.

@Raptor: You are right. I learn very much from fixing issues and also from looking at other codes and try to understand it. But pretty often I have problems with the syntax of MQ4. Like I wrote this small indicator should compare the last bars' volume to the average volume of the last 5 bars before. This can be interesting when analyzing CFDs. I'm not sure if it is really usable but it's interesting for me to make progress in coding. In my indicator everything works fine now but there is one thing I don't understand. The indicator should alert me when the "high volume bar" appeared. But of course only once. Unfortunately the indicator shows an alert every tick and I am too stupid to understand why.

I don' know why I get an alert every tick because I the alert is only allowed when Time0!=Time[0] and after the alert I set Time0=Time[0]. In my opinion after the first alert the criteria are not fulfilled because Time0=Time[0] and so the alert should not be shown. But obviously I am wrong. Do you have an idea why it is shown with every tick?

int start()
  {
   datetime Time0;
   for(shift=Bars-1;shift>=0;shift--)
   {    
          Buffer1[shift+1]=1;
          Buffer2[shift+1]=0;
     
     double vol=0;
          for(i=shift+2;i < shift + 2 + AvPeriod; i++)
          {
             vol=vol+iVolume(NULL, 0, i);
          }
          double vol1=iVolume(NULL, 0, shift+1);
          double AvVol=vol/AvPeriod;
          
          if (vol1>=AvVol*VolFaktor)
          {
             Buffer1[shift+1]=0;
             Buffer2[shift+1]=1;
             if (displayAlert==true && Time0!=Time[0])                                   
        {
          Alert("High Volume ",Symbol());      
          Time0=Time[0];                                   
        } 
          }
        }

 
mar:

@WHRoeder: because I am a beginner and I don't know about array access. The logic of loops is clear to me because of some VBA knowledge but the rest I still have to learn.

@Raptor: You are right. I learn very much from fixing issues and also from looking at other codes and try to understand it. But pretty often I have problems with the syntax of MQ4. Like I wrote this small indicator should compare the last bars' volume to the average volume of the last 5 bars before. This can be interesting when analyzing CFDs. I'm not sure if it is really usable but it's interesting for me to make progress in coding. In my indicator everything works fine now but there is one thing I don't understand. The indicator should alert me when the "high volume bar" appeared. But of course only once. Unfortunately the indicator shows an alert every tick and I am too stupid to understand why.

I don' know why I get an alert every tick because I the alert is only allowed when Time0!=Time[0] and after the alert I set Time0=Time[0]. In my opinion after the first alert the criteria are not fulfilled because Time0=Time[0] and so the alert should not be shown. But obviously I am wrong. Do you have an idea why it is shown with every tick?

start() runs for each tick, and for each tick you are declaring the variable Time0, as you are not giving it an initial value it is declared with a value of 0. You can fix this in 2 ways, either give this variable global scope ( declare it as a global ) or make it static.
 

Now I changed it to "static datetime Time0=-1;" and the result is that the alert doesn't appear every tick but every new bar even if the conditions are not met. It's better then before but still not what I really want. I looked in other indicators to see how this problem can be solved but I don't find a difference to mine... It seems to me that it is the same but I still don't find the error.

In the MQL4-reference I read that Time[0] is the time of the current's bar open. So I can't understand why the Time0!=Time[0] doesn't work. Is it so complicated or am I so stupid?

 
mar:

Now I changed it to "static datetime Time0=-1;" and the result is that the alert doesn't appear every tick but every new bar even if the conditions are not met. It's better then before but still not what I really want. I looked in other indicators to see how this problem can be solved but I don't find a difference to mine... It seems to me that it is the same but I still don't find the error.

In the MQL4-reference I read that Time[0] is the time of the current's bar open. So I can't understand why the Time0!=Time[0] doesn't work. Is it so complicated or am I so stupid?

Add a Print() statement to print the other variables used in the other conditions so you can see what they are when the Alert happens, then you can fix your error.
Reason: