Getting price data aligned to a time

 

Very often we need to code our indicator to display a historical point in time to check if the data calculated and the signal generated during backtest are the same and also we need a way to paper trade visually to validate our strategy before embark to code an EA.

For illustration, if our strategy is based on crossing of MA4 and MA16 and the trading timeframe is H1 and say the MAs crossed at D'2012.8.1 12:05'

if we display the indicator MA4 and MA16 in H1 chart, we will not be able to know if the signal is crossed at 12:05, we can only tell it is crossed at 12:00.  Hence we need to add a parameter to the indicator to "freeze" the calculation at 12:05.  We need to implement our indicator to calculate the moving average based on the close at 12:05 instead of 12:59 to align the reference time to 12:05.  This will need the CopyRates function to accept a paramter to align the rates to the datetime we need:

CopyRates(symbol,PERIOD_H1, D'2012.8.1 12:05',3,rates,aligned)

when aligned is false, it behaves like normal copyrates, 12:00-12:59, 11:00-11:59, 10:00-10:59 rates will be copied based on PERIOD_H1

when aligned is true,  it aligns the last data to the time provided, 12:00-12:05, 11:00-11:59, 10:00-10:59, for 11:00 and 10:00 we can easy get it from PERIOD_H1, but for 12:00-12:05 we will need to drill down to PERIOD_M1 to get the rates.

I have built such indicator but it took more than 5 minutes still PERIOD_M1 request is -1.  But when I go into debug mode the data quickly become available.  This unpredictability is very frustrating as when i swap the symbol to check other symbol, sometimes the indicator shows somtimes it does not (at every tick, the indicator just keep waiting for PERIOD_M1 to be available).  Sometimes I have to physically open M1 chart to "force" the formation of M1 data quickly but it does not work all the time.

 My questions:

1.  what is the most efficient and effective way to solve this requirement?

2.  can MQL5 extend CopyRates function to include such feature?

3. can MQL5 provides a means to request the price data and commit to the memory without swapping out?  At the start of my indicator OnInit, I can request for PERIOD_H1 as well as PERIOD_M1 data, if data is available in my harddisk, the function should block until the price data is built, if not download should start and the control returns to the indicator to poll in the next tick.  Once build, it shouldnt be swapped out unless the indicator releases it.  This will greatly improve the predictability of the indicator.

Documentation on MQL5: Timeseries and Indicators Access / CopyRates
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
  • www.mql5.com
Timeseries and Indicators Access / CopyRates - Documentation on MQL5