Hello, I have some problem with mt4, I tried to make a little code wich calculate the High average for "period" Bars but after compiling, the mt4 of my broker crashed. Please can you try, the problem is it my computer, I don't understand, have you got the same problem ? thanks
while(i>=0) { for(int k = 0 ; k <=period-1 ; k ++) { sum += sum + High[i]; } mmh[i] = sum/period; }Where is variable i decreased ?
//IndicatorBuffers(1); You Have already set the buffer int countedbar = IndicatorCounted(); int i = Bars - countedbar - 1; int period = 20; if(i>Bars - period) i = Bars - period -1; while(i>=0) { sum = 0; for(int k = 0 ; k < period ; k ++) { sum += High[i + k]; } mmh[i] = sum/period; i-- } return(rates_total);
Not compiled or tested
sum was not reset to zero and why were you adding it to itself?
I'm not sure about the IndicatorBuffers(1)
Shouldn't that be in init? and is it necessary as you already have the buffer.
ok thanks moderator for helping :) now the correct code for high average of period
#property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 Indigo double mmh[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { SetIndexBuffer(0,mmh); SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2); return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int countedbar = IndicatorCounted(); int i = Bars - countedbar - 1; int period = 20; double sum; while(i>=0) { sum = 0; for(int k = i ; k<= i +period -1 ; k ++) { sum = sum + High[k]; } mmh[i] = sum/period; i--; } return(0); } //+------------------------------------------------------------------+
ok thanks moderator for helping :) now the correct code for high average of period
Did you note in my earlier post that I checked the value of i against bars ?
if(i>Bars - period) i = Bars - period -1;
What happens in your code when the indicator is first initialised
int countedbar = IndicatorCounted(); int i = Bars - countedbar - 1; //Eg Bars = 1000 and counted bars = 0 i= 999 int period = 20; double sum; while(i>=0) { sum = 0; for(int k = i ; k<= i +period -1 ; k ++) //On the 2nd pass k=i+1 , k=999+1 , 1000 , High[1000] does not exist { //So array out of range sum = sum + High[k]; } mmh[i] = sum/period; i--; }
ok thanks gumrai, I note your precision about
if(i>Bars - perdiod) { i = Bars - period - 1; }
Now that you got it working while(i>=0) { sum = 0; for(int k = i ; k<= i +period -1 ; k ++) { sum = sum + High[k]; } mmh[i] = sum/period; i--; }
You can simplify it while(i>=0) { mmh[i] = iMA(NULL,0, period ,0, MODE_SMA, PRICE_HIGH, i); i--; }
This doesn't handle the look back of period. int countedbar = IndicatorCounted();
int i = Bars - countedbar - 1; //Eg Bars = 1000 and counted bars = 0 i= 999
int period = 20;
Don't look past the end int period = 20; int countedbar = IndicatorCounted(); if( countedbar < period) countedbar = period; for(int i = Bars - countedbar - 1; i >= 0; i--) mmh[i] = iMA(NULL,0, period ,0, MODE_SMA, PRICE_HIGH, i);
WHRoeder
Didn't I already explain your point 2 a few posts back?
Yes you did | if(i>Bars - period) i = Bars - period -1; |
I just showed my version | if( countedbar < period) countedbar = period; |
WHRoeder
Didn't I already explain your point 2 a few posts back?
Hi GumRai,
Looking at the original (first post on the top), since that is the new format we need to get used to, is there a point in using
IndicatorCounted(); High[]; Bars;
if we already have available :
rates_total high[] prev_calculated
?
Hi GumRai,
Looking at the original (first post on the top), since that is the new format we need to get used to, is there a point in using
if we already have available :
?
Well, I was just adjusting the OP's code, apart from that old habits die hard :)
To be honest, I've only just learnt how to use the new arrays. Before I was trying &high[index] and I now know that they need to be set as series if one wants to use them in the way that we are used to.
There seems so much new stuff that it will take some time to change. I do try to use the SymbolInfo stuff instead of Market info as that looks pretty good.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, I have some problem with mt4, I tried to make a little code wich calculate the High average for "period" Bars but after compiling, the mt4 of my broker crashed. Please can you try, the problem is it my computer, I don't understand, have you got the same problem ? thanks