vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 12
声望: 1

我发现在相关的 futu_gateway.py 文件里,并没有 query_history 的函数,所以每次下载数据都说失败,请问是不支持呢还是说要自己写呢?不知道有没有现成的呢?

谢谢啦!

Administrator
avatar
加入于:
帖子: 3810
声望: 199

futu_gateway没有对接下载数据的功能,需要的话只有自己实现了

Member
avatar
加入于:
帖子: 12
声望: 1

谢谢,我已自己实现了!

Member
avatar
加入于:
帖子: 21
声望: 0

雲信哥哥 wrote:

谢谢,我已自己实现了!
怎么实现的,介绍一下经验吧

Member
avatar
加入于:
帖子: 12
声望: 1

好的,主要就是要在 futu_gaateway.py 文件裏實現一個 query_history 方法,原理我就不多說啦,自己看下應該也很好理解的,具體代碼如下:

    def exchange_bond(self, exchange: Exchange):
        if exchange.value == "SEHK":
            return "HK"
        elif exchange.value == "SMART":
            return "US"
        else:
            return exchange.value

    def query_history(self, req: HistoryRequest):
        """"""
        symbol = req.symbol
        exchange = req.exchange
        interval = req.interval
        start = req.start.strftime('%Y-%m-%d')
        end = req.end.strftime('%Y-%m-%d')
        tcode = self.exchange_bond(exchange) + '.' + f'{symbol}'
        #tcode = 'HK.09988'
        ret, quotedata, page_req_key = self.quote_ctx.request_history_kline(
            tcode, start=start, end=end)

        data: List[BarData] = []

        if quotedata is not None:
            for ix, row in quotedata.iterrows():
                #date = datetime.strptime(row.time_key, '%Y-%m-%d %H:%M:%S')
                bar = BarData(
                    symbol=symbol,
                    exchange=exchange,
                    interval=interval,
                    datetime=row.time_key,
                    open_price=row.open,
                    high_price=row.high,
                    low_price=row.low,
                    close_price=row.close,
                    volume=row.volume,
                    gateway_name="FUTU"
                )
                print(bar)
                data.append(bar)
        return data

        return data
Member
avatar
加入于:
帖子: 1
声望: 0

多谢云信哥哥的分享,正好是我想要的,能说详细一些吗?初哥多谢了!

Member
avatar
加入于:
帖子: 17
声望: 1

补充一点: HistoryRequest 这个需要在开头进行引入,否则报错
from vnpy.trader.object import (
TickData,
OrderData,
TradeData,
AccountData,
ContractData,
PositionData,
SubscribeRequest,
OrderRequest,
HistoryRequest,
CancelRequest
)

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3