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.
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.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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?