Because the latest prices are constantly changing over the course of an hour, the value of moving average also changes.
This program calculates the moving average only once when the number of bars changes, and then does not calculate until the next change of number of bars.
Thanks a lot @Nagisa
Unada
I thought that the candle with shift == 0 was the candle that have just completed when my code printed the output but the one I needed was the candle with shift == 1.
Thanks a lot and have a nice day! :)Because the latest prices are constantly changing over the course of an hour, the value of moving average also changes.
This program calculates the moving average only once when the number of bars changes, and then does not calculate until the next change of number of bars.
-
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
New candle - MQL4 programming forum #3 (2014)I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
Running EA once at the start of each bar - MQL4 programming forum (2011) -
Perhaps you should read the manual, especially the examples.
How To Ask Questions The Smart Way. (2004)
How To Interpret Answers.
RTFM and STFW: How To Tell You've Seriously Screwed Up.They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
How to call indicators in MQL5 - MQL5 Articles (2010)
- 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'm taking my first steps in MQL5, in particular I'm testing the moving average indicator.
I wrote the following lines of code to print the moving average value as soon as I receive a new H1 candle (with period "14", calculated on H1 timeframe, with mode EMA and on PRICE_CLOSE).
Now if I run my EA with the Strategy Tester with the attached configuration, I can see my moving average printed in the standard output.
The problem is that the values that are printed are different from the MA14 that are shown in the candlesticks chart.
Some examples:
H1 candle of 2020.05.25 11:00:00 -> the code prints: "MQL4 moving average value: 1.088937786897514" but in the candlestick chart I can read the value 1.089115 that is different from the value calculated from my code.
Also in all the others candlestick I'm experiencing a difference of values between the moving average calulated in the code and the one plotted in the chart.
Am I missing something? Why is there this difference?
Thanks in advance :)