You have one object. Why are you looping over multiple bars?
I want a label object that display current price.
Edit: regarding the loop - I need it because I want also other labels that display indicator values. ie. a label that display current MA(x) price. Also, for the label displaying market open I need the loop because I can't just call iOpen on daily chart because my broker daily candle does not open at the time I need.I want a label object that display current price.
Edit: regarding the loop - I need it because I want also other labels that display indicator values. ie. a label that display current MA(x) price. Also, for the label displaying market open I need the loop because I can't just call iOpen on daily chart because my broker daily candle does not open at the time I need.Bars-1-prev_calculated is >= 0 only at start and when new bar appears otherwise it is -1 so your for loop exits without making a single cycle.
Thank you. I just copied and pasted Williams post about how to correctly do for loops... Guess I've never understood it actually. So, how does one modify the structure of the loop in order to update indicators real time?
Thank you. I just copied and pasted Williams post about how to correctly do for loops... Guess I've never understood it actually. So, how does one modify the structure of the loop in order to update indicators real time?
Extract current price reading from the loop.
You don't want to read same data for each bar and especially you don't want to redraw that object for each bar in the loop.
double price = (MarketInfo(Symbol(),MODE_BID) + MarketInfo(Symbol(),MODE_ASK)) /2; ObjectSetText(prefix+"close",DoubleToString(price,5),24,"Arial",clrWhite);
Extract current price reading from the loop.
You don't want to read same data for each bar and especially you don't want to redraw that object for each bar in the loop.
And what if I need to calculate e certain indicator inside the loop and then giving it to the label? And specially making the calculation real time and not only at new bar.
And what if I need to calculate e certain indicator inside the loop and then giving it to the label? And specially making the calculation real time and not only at new bar.
Then you calculate the value inside the loop and put the extracted lines after the loop.
In you above code, label value had no any dependency on the loop index, so it was sufficient to put those two lines before loop.
If you want to display some value dependent on the loop, then calculate that value in the loop and store it in some local variable.
For correct calculation, you already read posts William linked.
You have one label which will hold one value after the OnTick() finishes and you really shouldn't redraw text object for each pass of the loop.
Then you calculate the value inside the loop and put the extracted lines after the loop.
In you above code, label value had no any dependency on the loop index, so it was sufficient to put those two lines before loop.
If you want to display some value dependent on the loop, then calculate that value in the loop and store it in some local variable.
For correct calculation, you already read posts William linked.
You have one label which will hold one value after the OnTick() finishes and you really shouldn't redraw text object for each pass of the loop.
Yes ok, but still, indicator calculated inside the loop does not updates real time.
Do you know what the prev_calculated is doing?
So you suggesting that by doing this:
for(int iBar =Bars-MathMax(lookback, prev_calculated); iBar >= 0; --iBar) //I removed -1
I'll achieve the desired outcome without altering the proper structure of the loop?

- 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 have this indicator:
I do not understand why the label with current price does not update regularly, it seems to update once every new candle, I need it to update regoularly. Just like the built-in MA indicator does update with every new tick.
Any hint on why this does not behave as intended? Thank's.