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

不多说,直接贴代码,代码不多

from datetime import datetime

import pandas as pd

from vnpy.trader.constant import (
    Exchange,
    Interval
)
from vnpy.trader.database import (
       convert_tz
)
from vnpy.trader.database import database_manager
from vnpy.database.mongodb.mongodb_database import (
    DbBarData,
    MongodbDatabase
)

class MongodbDatabaseExtend(MongodbDatabase):
    """"""
    def __init__(self) -> None:
        super(MongodbDatabaseExtend, self).__init__()

    def load_bar_data_dataframe(
        self,
        symbol:str,
        exchange: Exchange,
        interval:Interval,
        start:datetime,
        end:datetime
    ):
        df = pd.DataFrame()
        s:QuerySet = DbBarData.objects(
            symbol=symbol,
            exchange=exchange.value,
            interval=interval.value,
            datetime__gte=convert_tz(start),
            datetime__lte=convert_tz(end),
        )

        data = []
        for bar in s:
            data.append([bar.datetime, bar.open_price, bar.close_price, bar.high_price, bar.low_price, bar.volume])

        df = pd.DataFrame(data, columns=['datetime','open','close','high','low','volume'])
        return df

if __name__=="__main__":
    db_manger = MongodbDatabaseExtend()
    df = db_manger.load_bar_data_dataframe(
        'btcusdt',
        Exchange.BINANCE,
        Interval.MINUTE,
        datetime(2021,4,25,8, 0),
        datetime(2021,4,25,12, 0)
    )
    print(df)

返回结果

               datetime      open     close      high       low     volume
0   2021-04-25 08:00:00  50047.84  50106.68  50132.65  50047.83  58.046298
1   2021-04-25 08:01:00  50101.12  50088.93  50179.39  50082.04  36.085477
2   2021-04-25 08:02:00  50088.93  49852.96  50088.94  49851.74  90.735405
3   2021-04-25 08:03:00  49852.97  50055.62  50084.31  49852.96  54.309657
4   2021-04-25 08:04:00  50055.29  50169.12  50265.29  50041.49  85.004411
..                  ...       ...       ...       ...       ...        ...
236 2021-04-25 11:56:00  49677.36  49698.97  49719.02  49677.04  16.636203
237 2021-04-25 11:57:00  49698.96  49650.00  49702.75  49635.13  26.174736
238 2021-04-25 11:58:00  49650.00  49683.05  49696.82  49642.06  18.224997
239 2021-04-25 11:59:00  49689.03  49615.54  49694.46  49615.50  13.967710
240 2021-04-25 12:00:00  49626.76  49683.94  49705.04  49600.24   5.887034
Administrator
avatar
加入于:
帖子: 4655
声望: 266

感谢分享

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

沪公网安备 31011502017034号