简体   繁体   中英

Copy Rates Range Time Frame M1 in MetaTrader5 with python

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

pip install MetaTrader5

When I use this code to copy rates for copy EUR_USD data, there is an error occured. The codes shown below:

from datetime import datetime
import matplotlib.pyplot as plt
import pandas as pd
import MetaTrader5 as mt5
import pytz

mt5.initialize()
print(mt5.terminal_info())   
print(mt5.version())

pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1500)  

timezone = pytz.timezone("Etc/UTC")
utc_from = datetime(2017, 1, 5, tzinfo=timezone)
utc_to = datetime(2019, 12, 27, hour = 23, 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 an error occured:

Display obtained data 'as is'
---------------------------------------------------------------------------
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

I found that there are no values in rates which means mt5.copy_rates_range() seems doesn't work. The codes working with time frame D1, H1, but for time frame M1 doesn't work.

Are there any solution for this...? Thank you

You can copy only set amount of bars. I don't know if this is intentional or a bug. Use smaller date range or larger timeframe. The minimum timeframe for one year period is 5 minutes.

your time range is very big (for mt5.TIMEFRAME_M1). make a smaller range (eg 30 days)

as clarified in documentation here

MetaTrader 5 terminal provides bars only within a history available to a user on charts. The number of bars available to users is set in the "Max. bars in chart" parameter.

so if you intend to copy huge data you should first of all open metatrader5 platform and select tools menu then options then charts and modefy max bars in chart to the maximum (fill all available digits with nines). After that you must open the chart of the symbol,if you did not open it before, and allow the platform to update the chart. You may try scrolling the chart to download the whole range.

try your code now and don't worry. I copy years of 1M data with no problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM