从metatrader5获取当前报价数据

问题描述 投票:0回答:1

我已经以不同的方式写了大约三次,但我不断得到一个空的数据框。我忽略了什么?该代码是类的一部分。


import pytz
import MetaTrader5 as mt5
import pandas as pd
from datetime import datetime
from datetime import timedelta

def tick_data(self):
    utc_time = pytz.timezone('UTC')
    noww = datetime.now(utc_time)
    date_ = noww - timedelta(days=8)date_from = date_.replace(hour=0, minute=0, second=0, microsecond=0)
    date_to = noww.date()
    ticks = mt5.copy_ticks_range(self.symbol, date_from, date_to, mt5.COPY_TICKS_ALL)
    mt5.shutdown()
    ticks_frame = pd.DataFrame(ticks)
    print(ticks_frame.head(10))
eurusd_data = PairData('EURUSD')
eurusd_data.tick_data()

下面的代码不使用类实例,但几乎是同样的问题。我认为我的时间翻译是问题,所以我只使用日期而不使用类,但它仍然给了我一个空的数据框。那么问题出在哪里呢?


import pytz
import MetaTrader5 as mt5
import pandas as pd
from datetime import datetime

def tick_data(symbol):
    symbol = mt5.symbol_select('EURGBP', True)
    if not symbol:print('please insert the correct symbol')
    utc_time = pytz.timezone('UTC')
    date_from = datetime(2023, 1, 10, tzinfo=utc_time)
    date_to = datetime(2023, 1, 18, tzinfo=utc_time)
    bars = mt5.copy_ticks_range(symbol, date_from, date_to, mt5.COPY_TICKS_ALL)
    b_frame = pd.DataFrame(bars)
    print(b_frame.head(17))
tick_data('EURGBP')

我需要一个数据框,但一直得到一个空数据框。如果第一个代码有效,我会很高兴。

python android-livedata forex metatrader5
1个回答
0
投票

在第一个示例中只需更改

date_to = noww.date()

date_to = noww

date_to = noww.replace(hour=0, minute=0, second=0, microsecond=0)
© www.soinside.com 2019 - 2024. All rights reserved.