Copying rates from different timeframes

 

Guys,

I am trying to code an indicator for D1 charts, which uses M1 price/volume/spread/etc. data to calculate the indicator daily values. I use this code to copy rates:


MqlRates       RatesM1[],RatesD1[];
datetime       first_bar_date=SeriesInfoInteger(_Symbol,PERIOD_M1,SERIES_FIRSTDATE);
datetime       last_bar_date=SeriesInfoInteger(_Symbol,PERIOD_M1,SERIES_LASTBAR_DATE);

ArraySetAsSeries(RatesM1,true);
ArraySetAsSeries(RatesD1,true);

int            CopyRatesM1Check=CopyRates(_Symbol,PERIOD_M1,first_bar_date,last_bar_date,RatesM1);
int            CopyRatesD1Check=CopyRates(_Symbol,PERIOD_D1,first_bar_date,last_bar_date,RatesD1);

but it doesn't work. For some reason I have to change chart timeframe inside the code to be able to read the rates:

MqlRates       RatesM1[],RatesD1[];
datetime       first_bar_date=SeriesInfoInteger(_Symbol,PERIOD_M1,SERIES_FIRSTDATE);
datetime       last_bar_date=SeriesInfoInteger(_Symbol,PERIOD_M1,SERIES_LASTBAR_DATE);

ArraySetAsSeries(RatesM1,true);
ArraySetAsSeries(RatesD1,true);

int            CopyRatesM1Check=CopyRates(_Symbol,PERIOD_M1,first_bar_date,last_bar_date,RatesM1);
ChartSetSymbolPeriod(0,_Symbol,PERIOD_D1);
int            CopyRatesD1Check=CopyRates(_Symbol,PERIOD_D1,first_bar_date,last_bar_date,RatesD1);

However, this creates all sorts of further issues. For example, (this piece of code is placed in OnInit() section) it runs twice! and if I have opened a file it causes the second run to fail, and etc. Does any of you guys have any idea how to deal with this?

Thanks a lot,

Nima

 

but it doesn't work

What that means exactly ?

Don't place such code in OnInit(), it will never work correctly.

 
Alain Verleyen:

What that means exactly ?

Don't place such code in OnInit(), it will never work correctly

Let's say I want to copy M1 and D1 data, and I attach the indicator to chart on M1 timeframe. when I run the code it will only copy M1 rates, and won't copy D1 rates. Basically, CopyRates function works properly only if used to copy rates at current timeframe.

Can you elaborate on why not in OnInit?


Thanks a alot,

Nima

 
NeeZo_NeeZO:

Let's say I want to copy M1 and D1 data, and I attach the indicator to chart on M1 timeframe. when I run the code it will only copy M1 rates, and won't copy D1 rates. Basically, CopyRates function works properly only if used to copy rates at current timeframe.

Can you elaborate on why not in OnInit?


Thanks a alot,

Nima

CopyRates() works properly on any timeframe. But the timeseries may be updating or needing download, check the return code and error code in code of error.

There is a greater chance timeseries are not yet available in OnInit(). Use OnTimer() or OnCalculate() instead.

 

Just a little comment ...

Price Rates are the same on all time frames !!!.

 
Osama Shaban:

Just a little comment ...

Price Rates are the same on all time frames !!!.

It's an old thread, I know, but someone may be reading it in future (like me now), and may get confused, so I will complement the information:


Price Rates are "the same" just for the last [the most recent] bar.  Technically index[0] if ordered as AsSeries.

If you need rates from older bars (like Bar.index[1] or older) , they will be different for each timeframe you query them.

 
rrocchi:

It's an old thread, I know, but someone may be reading it in future (like me now), and may get confused, so I will complement the information:


Price Rates are "the same" just for the last [the most recent] bar.  Technically index[0] if ordered as AsSeries.

If you need rates from older bars (like Bar.index[1] or older) , they will be different for each timeframe you query them.

prices are different at the same time but in different frame rates

how do I get the same prices in different time frames?

 

Hi, 

I recently try to use metatrader5 in Jupyter Notebook using python .

When I use this code to copy rates, there are error that shown below:

utc_from = datetime(2017, 1, 5, tzinfo=timezone)
utc_to = datetime(2019, 12, 27, hour = 23, minute=59, tzinfo=timezone)
rates = mt5.copy_rates_range("EURUSD", mt5.TIMEFRAME_M1, utc_from, utc_to)
print("Display obtained data 'as is'")
counter=0
for rate in rates:
counter+=1
if counter<=10:
print(rate)


Then error occured :

TypeError Traceback (most recent call last)
<ipython-input-10-5307ec9302f7> in <module>
2 print("Display obtained data 'as is'")
3 counter=0
---->   4 for rate in rates: 
5  counter+=1 
6 if counter<=10: 
TypeError: 'NoneType' object is not iterable 

This code working with Time Frame D1 and H1 (I have try it), but doesn't work with Time Frame M1.

Any solution for this? Thanks

Reason: