【问题标题】:MT5 python not returning updated dataMT5 python 不返回更新数据
【发布时间】:2022-11-18 14:43:27
【问题描述】:

MT5 未返回最新指数的数据

import MetaTrader5 as mt5
mt5.initialize()
import pandas as pd

instrument = mt5.copy_rates_from_pos('BTCUSD',mt5.TIMEFRAME_H1,0,20)
instrument = pd.DataFrame(instrument)
instrument['time'] = pd.to_datetime(instrument['time'], unit = 's')
instrument = instrument.set_index(['time'])

当我每小时运行此代码时,它总是返回前一个柱作为最后一个索引(而不是最近的柱)。它应该返回最近的柱,因为初始柱索引设置为 0。

未更新的数据示例:

In()
instrument['open'].tail(5)

Out()
2022-10-29 11:00:00    20767.92
2022-10-29 12:00:00    20917.95
2022-10-29 13:00:00    20945.44
2022-10-29 14:00:00    20763.64
2022-10-29 15:00:00    20690.48

如果我在 10 秒后运行相同的代码,它会返回正确的信息(最近的柱作为最后一个索引)。

正确(更新)数据:

In()
instrument['open'].tail(5)

Out()
2022-10-29 12:00:00    20917.95
2022-10-29 13:00:00    20945.44
2022-10-29 14:00:00    20763.64
2022-10-29 15:00:00    20690.48
2022-10-29 16:00:00    20756.35

因此,据我所知,终端在执行 mt5.copy_rates_from_pos 时没有更新信息。

如何强制终端下载和更新之前的数据?

谢谢你的帮助

【问题讨论】:

    标签: python metatrader5


    【解决方案1】:

    如果您愿意,您实际上可以每秒更新一次收盘价。您必须使用日期时间运行它,否则您不会从 mt5 返回更新的日期和时间。并运行您的函数以从内部循环中提取数据。

    from datetime import datetime
    import time
    import MetaTrader5 as mt
    import pandas as pd
    
    q = 0
    while q < 1:
        dt = datetime.now()
        mt = dt.strftime( '%H:%M:%S' )
        if dt.second > dt.second - 1:
            pull your data here
            print output
        time.sleep( 1 )
    

    请记住,只有收盘价会每秒更新一次,除非在柱线区间内创下新高或新低,否则它们也会更新。希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2016-03-23
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多