# counting bars

197

Hey,

I want to count the number of bars it has been since the last MA crossover. Example chart below, which would have the answer of12 bars including the current bar.

I think I need to use a for or while loop of some sort but I never really understood them when I went through an online JS course.

This is my newbie attempt at returning the number of bars since the last crossover.

```int count, nBars;

double periodicity1 = 10;
double periodicity2 = 20;

double FastSMA0 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count);
double SlowSMA0 = iMA(NULL,0,periodicity2,0,MODE_SMA,0,count);
double FastSMA1 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count+1);
double SlowSMA1 = iMA(NULL,0,periodicity2,0,MODE_SMA,0,count+1);

for (count = 0 ; count <= Bars ; count++)
{
nBars + 1;
if (FastSMA0 > SlowSMA0 && FastSMA1 < SlowSMA1)
{
Print ("Crossover upwards. Count ended at ",nBars);
break;
}
else if (FastSMA0 < SlowSMA0 && FastSMA1 > SlowSMA1)
{
Print ("Crossover downwards. Count ended at ",nBars);
break;
}
else
{
continue;
}
}```

Any pointers in the right direction would be nice. Before I progress to the next stage of my self-appointed MQL4 training.

15913

1. Your fast/slow variables (for the test) are set outside the loop, they never change.
2. `count = 0 ; count <= Bars`
The number of bars on the chart is Bars. They are numbered [0 .. Bars-1] so count should never be equal to Bars
3. `ended at ",nBars`
When you find a cross at count, isn't that the number of bars since the cross?
197

Cheers. This seems to work now.

```int count, nBars;

double periodicity1 = 10;
double periodicity2 = 20;

for (count = 0 ; count <= Bars-1 ; count++)
{
double FastSMA0 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count);
double SlowSMA0 = iMA(NULL,0,periodicity2,0,MODE_SMA,0,count);
double FastSMA1 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count+1);
double SlowSMA1 = iMA(NULL,0,periodicity2,0,MODE_SMA,0,count+1);

nBars = nBars + 1;

if (FastSMA0 > SlowSMA0 && FastSMA1 < SlowSMA1)
{
Comment ("Crossover upwards. Count ended at ",nBars);
break;
}
else if (FastSMA0 < SlowSMA0 && FastSMA1 > SlowSMA1)
{
Comment ("Crossover downwards. Count ended at ",nBars);
break;
}
else
{
continue;
}
}```
197

Yes, I was just looking to find the count between the current bar and last cross. It's not terribly useful at the moment but it's a stepping stone.
15913

 Still problems ```for (count = 0 ; count <= Bars-1 ; count++){       :       double FastSMA1 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count+1);``` Simplification. Don't code [0 .. Limit-1] code as up to limit [0 .. Limit) ```for (count = 0 ; count < Bars ; count++){       :       double FastSMA1 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count+1);``` But when count == Bars-1, count+1 does not exist. ```for (count = 0 ; count < Bars-1 ; count++){                            // Lookback=1       :       double FastSMA1 = iMA(NULL,0,periodicity1,0,MODE_SMA,0,count+1); // Lookback=1```
197

I think the mistake I made is choosing Bars as the limit when I could have just chosen any large number instead, like 1000.

I can see why there might be a problem with count+1 if the count ever reaches the limit, but the rest of the code should break the loop before it ever reaches a number that high. I don't expect to count more than 50.

I'll do a test later by reducing the limit to something like 5. I think this might crash the programme though!

15913

eempc: I think the mistake I made is choosing Bars as the limit when I could have just chosen any large number instead, like 1000.
The tester only guarantees that you have 100 bars. What if there is no cross? Don't expect, find the cross. If there isn't one, the loop exits with count == Bars-1