If you want your bot to catch indicator values during the candle (not only after it closes), you need to:
-
Use "Every tick" mode in Strategy Tester
-
In MT5 Strategy, select Every tick based on real ticks.
-
This simulates tick by tick price changes within the candle.
-
Your EA will then see intra bar indicator changes.
-
-
Make sure your code looks at the current bar
-
Use shift=0 when calling indicator values, which means "current bar/real time".
-
Example:
-
//--- input parameters input int Kperiod = 14; input int Dperiod = 3; input int Slowing = 3; int stochHandle; double k[], d[]; //-------------------------------------------------------------------------- int OnInit() { ArraySetAsSeries(k, true); ArraySetAsSeries(d, true); stochHandle = iStochastic(_Symbol, PERIOD_M30, Kperiod, Dperiod, Slowing, MODE_SMA, STO_LOWHIGH); if(stochHandle == INVALID_HANDLE) { Print("Failed to create Stochastic handle, error: ", GetLastError()); return(INIT_FAILED); } return(INIT_SUCCEEDED); } //-------------------------------------------------------------------------- void OnTick() { //--- get %K and %D values if(CopyBuffer(stochHandle, 0, 0, 2, k) < 0) // buffer 0 = %K line { Print("Error copying K: ", GetLastError()); return; } if(CopyBuffer(stochHandle, 1, 0, 2, d) < 0) // buffer 1 = %D line { Print("Error copying D: ", GetLastError()); return; } //--- use shift=0: current bar (still forming!)... This gives you the real time values as the candle is being formed double k_current = k[0]; double d_current = d[0]; //--- use shift=1: last closed bar, not real time double k_previous = k[1]; double d_previous = d[1]; }
Besides the advice given in the previous post, also do some research into "tester_everytick_calculate" preprocessor property.
tester_everytick_calculate string In the Strategy Tester, indicators are only calculated when their data are accessed, i.e. when the values of indicator buffers are requested. This provides a significantly faster testing and optimization speed, if you do not need to obtain indicator values on each tick.
By specifying the tester_everytick_calculate property, you can enable the forced calculation of the indicator on every tick.
Indicators in the Strategy Tester are also forcibly calculated on every tick in the following cases:
- when testing in the visual mode;
- if the indicator has any of the following functions: EventChartCustom, OnChartEvent, OnTimer;
- if the indicator was created using the compiler with build number below 1916.
This feature only applies in the Strategy Tester, while in the terminal indicators are always calculated on each received tick.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
please correct me if i am wrong
i have found that the mql5 bot cant know the on real time while testing the strategy , the values of the indicator and they know only the values at the closure of the candle like for example a 30 min candle
how can i fix this ? as i want for example a value to be caught during the candle not solely at the end
making a big gap between the bot purpose and the trades that are placed wrongly