简体   繁体   English

使用 python 在 MetaTrader5 中复制速率范围时间框架 M1

[英]Copy Rates Range Time Frame M1 in MetaTrader5 with python

I recently try to use metatrader5 in Jupyter Notebook using python.我最近尝试使用 python 在 Jupyter Notebook 中使用 metatrader5。 I have installed metatrader5 using:我使用以下方法安装了 metatrader5:

pip install MetaTrader5

When I use this code to copy rates for copy EUR_USD data, there is an error occured.当我使用此代码复制 EUR_USD 数据的汇率时,发生错误。 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.我发现 rate 中没有值,这意味着 mt5.copy_rates_range() 似乎不起作用。 The codes working with time frame D1, H1, but for time frame M1 doesn't work.使用时间框架 D1、H1 的代码,但时间框架 M1 不起作用。

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.一年期限的最短时间为 5 分钟。

your time range is very big (for mt5.TIMEFRAME_M1).您的时间范围非常大(对于 mt5.TIMEFRAME_M1)。 make a smaller range (eg 30 days)缩小范围(例如 30 天)

as clarified in documentation here如此的文档中所述

MetaTrader 5 terminal provides bars only within a history available to a user on charts. MetaTrader 5 终端仅在图表上可供用户使用的历史记录中提供柱线。 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).因此,如果您打算复制大量数据,您应该首先打开 metatrader5 平台和 select 工具菜单,然后选择选项,然后将图表中的最大条形图调整到最大值(用 9 填充所有可用数字)。 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.我毫无问题地复制了多年的 1M 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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