VeighNa量化社区
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 10
声望: 0

最近在研究如何生成5分钟以及15分钟K线图,之前在论坛看到一份代码,生成的是1分钟K线,但不会改,有没有大佬能指点一下,这是我现在的代码以及运行结果

from datetime import datetime
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.database import get_database
from vnpy.trader.object import BarData, TickData
from typing import  Dict, List
from vnpy_ctastrategy import ArrayManager
from vnpy.chart import ChartWidget, VolumeItem, CandleItem
import pyqtgraph as pg
from vnpy.trader.ui import create_qapp, QtCore, QtGui
from vnpy.chart.manager import BarManager

if __name__ == "__main__":
    app = create_qapp()
    # 获取数据服务实例
    database = get_database()

    symbol = "IC88"
    exchange = Exchange.CFFEX
    interval = Interval.MINUTE
    start = datetime(2022, 2, 18)
    end = datetime(2022, 2, 22)

    # 获取k线历史数据
    bars = database.load_bar_data(
        symbol=symbol,
        exchange=exchange,
        start=start,
        end=end,
        interval=interval
    )

    am = ArrayManager(50)
    new_data = bars[:]
    line_up = []
    line_down = []

    while new_data:
        bar = new_data.pop(0)
        am.update_bar(bar)
        up, down = am.boll(20, 2)
        line_up.append(up)
        line_down.append(down)   # 这里调用合适的公式就好了

    print("K线数量是", len(bars), "指标数据是", len(line_up))

    i = 0
    while line_down:
        bars[i].down_line = line_down.pop(0)
        bars[i].up_line = line_up.pop(0)
        i = i + 1
    print("共处理了", i, "数据")

    widget = ChartWidget()
    widget.add_plot("candle", hide_x_axis=True)
    widget.add_plot("volume", maximum_height=250)
    widget.add_item(CandleItem, "candle", "candle")
    widget.add_item(VolumeItem, "volume", "volume")

    widget.add_cursor()

    history = bars
    widget.update_history(history)

    def update_bar():
        bar = new_data.pop(0)
        widget.update_bar(bar)

    timer = QtCore.QTimer()
    timer.timeout.connect(update_bar)

    widget.show()
    app.exec_()

description

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

有偿求助,499514430

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

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】