Hello everyone!
I made an indicator as a request for MT5 that works fine. And now I want to have it also on MT4.
So I did it, and it works well too. When plotted on the chart or when parameters are changed, the indi "refreshes" very well. BUT, sometimes, when the timeframe period is changed (works only on M1, M5 or M15), the lines and the counter value duplicates, as if the bars loop was executed twice after initialization (I really see that happens only when uninit reason is 5). And as it doesn't always happen, I'm having a hard time figuring out what's causing this...
I tried to make the small code as possible where the problem occurs to post here, so here it goes ... I'm using build 1260 by the way.
Also, because it's for a probabilistic strategy, the user requires that he can input how many bars to load on the chart.
Please, if it's wrong and someone can show me the right way to do this, I'll be very grateful, thanks!!
You can start by moving the initialization of all your global variables (those that will be changed during execution) into OnInit()... because changing of timeframe does not reset those variables.
You can start by moving the initialization of all your global variables (those that will be changed during execution) into OnInit()... because changing of timeframe does not reset those variables.
It does with an indicator.
It does with an indicator.
Ah... if not that, then adding this line in OnInit() will do the trick :
IndicatorBuffers(1); SetIndexBuffer(0, Buffer1); SetIndexArrow(0, 242); ArrayInitialize(Buffer1, EMPTY_VALUE);
Ah... if not that, then adding this line in OnInit() will do the trick :
I always initialize my buffers in OnCalculate() so that the buffers are cleared if the chart is updated (eg after a disconnection from the server)
if(prev_calculated==0) ArrayInitialize(Buffer1, EMPTY_VALUE);
I always initialize my buffers in OnCalculate() so that the buffers are cleared if the chart is updated (eg after a disconnection from the server)
Thanks for the answers guys... I actually had ArrayInitialize on my code,I removed to post here because I think it was not necessary. Anyways, neither on OnInit or OnCalculate works, so the problem stills. I noticed that when happens, it initialize right for a brief of a second, then the values and Vlines duplicates.
I start to think it's a bug. I find some similar issues on old posts, but it seems there was a problem that was fixed in next builds. I updated the code.
Thanks for the answers guys... I actually had ArrayInitialize on my code,I removed to post here because I think it was not necessary. Anyways, neither on OnInit or OnCalculate works, so the problem stills. I noticed that when happens, it initialize right for a brief of a second, then the values and Vlines duplicates.
I start to think it's a bug. I find some similar issues on old posts, but it seems there was a problem that was fixed in next builds. I updated the code.
Yes it's a bug but in your code.
So I did it, and it works well too. When plotted on the chart or when parameters are changed, the indi "refreshes" very well. BUT, sometimes, when the timeframe period is changed (works only on M1, M5 or M15), the lines and the counter value duplicates, as if the bars loop was executed twice after initialization (I really see that happens only when uninit reason is 5). And as it doesn't always happen, I'm having a hard time figuring out what's causing this...
This is exactly what is happening, OnCalculate() CAN be called more then once after the initialization, it's normal behaviour and you have to deal with it.
Send and Keith, give you both a part of the solution.
You can start by moving the initialization of all your global variables (those that will be changed during execution) into OnInit()... because changing of timeframe does not reset those variables.
I always initialize my buffers in OnCalculate() so that the buffers are cleared if the chart is updated (eg after a disconnection from the server)
Seng post was not exact, but he was right saying you need to take care of the initialization. Keith is right to initialize the buffer when prev_calculated is 0.
You need to initialize ALL your global variables (well if they are related to your indicator calculation of course) in OnCalculate(), when prev_calculated is 0.
Like :
if(prev_calculated==0) { ArrayInitialize(Buffer1, EMPTY_VALUE); TotalWins=0; // Up to you to add the other globals needing to be initialized... }
Yes it's a bug but in your code.
This is exactly what is happening, OnCalculate() CAN be called more then once after the initialization, it's normal behaviour and you
have to deal with it.
Send and Keith, give you both a part of the solution.
Seng post was not exact, but he was right saying you need to take care of the initialization. Keith is right to initialize the buffer when prev_calculated is 0.
You need to initialize ALL your global variables (well if they are related to your indicator calculation of course) in OnCalculate(), when prev_calculated is 0.
Like :
YES! It works!
if(prev_calculated<1) { ArrayInitialize(Buffer1, EMPTY_VALUE); TotalWins=0; Price = 0; ObjCount = 0; TotalWins = 0; TeveEntrada = false; Vrun = true; Tmcount = 0; } else limit++;
I tried Seng solution to initialize the variables in OnInit, as it desn't work I didn't think to try on OnCalculate. I thought that when changed timeframes, it would start on OnInit. Well, learned.
Thank you all very much!
Yes it's a bug but in your code.
This is exactly what is happening, OnCalculate() CAN be called more then once after the initialization, it's normal behaviour and you
have to deal with it.
Send and Keith, give you both a part of the solution.
Seng post was not exact, but he was right saying you need to take care of the initialization. Keith is right to initialize the buffer when prev_calculated is 0.
You need to initialize ALL your global variables (well if they are related to your indicator calculation of course) in OnCalculate(), when prev_calculated is 0.
Like :
Now I recall... I hit the same issue, and solved it your way before... then I forgot the specifics...
But I've always thought it wasn't a bug... it was caused by the staggered loading (intentional, perhaps, for start-up speed, or it genuinely had to retrieve more data from server) of chart data - like, while OnCalculate() starts being called, more data gets loaded, hence prev_calculated becomes 0...
Now I recall... I hit the same issue, and solved it your way before... then I forgot the specifics...
But I've always thought it wasn't a bug... it was caused by the staggered loading (intentional, perhaps, for start-up speed, or it genuinely had to retrieve more data from server) of chart data - like, while OnCalculate() starts being called, more data gets loaded, hence prev_calculated becomes 0...
Yes there are several triggers for OnCalculate() to be called with prev_calculated=0 ( I know at least 3, but there are maybe more).
- 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 everyone!
I made an indicator as a request for MT5 that works fine. And now I want to have it also on MT4.
So I did it, and it works well too. When plotted on the chart or when parameters are changed, the indi "refreshes" very well. BUT, sometimes, when the timeframe period is changed (works only on M1, M5 or M15), the lines and the counter value duplicates, as if the bars loop was executed twice after initialization (I really see that happens only when uninit reason is 5). And as it doesn't always happen, I'm having a hard time figuring out what's causing this...
I tried to make the small code as possible where the problem occurs to post here, so here it goes ... I'm using build 1260 by the way.
Also, because it's for a probabilistic strategy, the user requires that he can input how many bars to load on the chart.
Please, if it's wrong and someone can show me the right way to do this, I'll be very grateful, thanks!!