Home >  > Vn.py学习记录三

Vn.py学习记录三

0

一、下载RQdata数据
1.获取账号
通RiceQuant注册一个试用账号。然后会得到一个make.bat的文件。用文本编辑打开,可以看到里面存了你的账号、密码。

2.修改设置
打开F:\vnpy\vnpy-2.0.2\tests\backtesting这个文件夹,再打开里面的getdata.py,将上面获取到的账号、密码添加进去。
并设定好你要下载的vt_symbol、start_date、End_data这些。

3.下载数据
执行getdata.py,就可以下载数据了。

二、截取最近三根K线的值

from vnpy.app.cta_strategy import (
    CtaTemplate,
    StopOrder,
    TickData,
    BarData,
    TradeData,
    OrderData,
    BarGenerator,
    ArrayManager,
)


class MaDbwStrategy(CtaTemplate):
    author = "蜗牛博客snailtoday.com"

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super(MaDbwStrategy, self).__init__(
            cta_engine, strategy_name, vt_symbol, setting
        )

        self.bg = BarGenerator(self.on_bar, 5, self.on_5min_bar)
        self.am = ArrayManager()

    def on_init(self):
        """
        Callback when strategy is inited.
        """
        self.write_log("策略初始化")
        self.load_bar(10)

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("策略启动")
        self.put_event()

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("策略停止")

        self.put_event()

    def on_tick(self, tick: TickData):
        """
        Callback of new tick data update.
        """
        self.bg.update_tick(tick)

    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """

        self.bg.update_bar(bar)


    def on_5min_bar(self, bar: BarData):
        """"""
        self.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return
        
        print('上一根K线的开盘价是{}:'.format(am.open_array[-1]))
        print('上一根K线的最高价是{}:'.format(am.high_array[-1]))
        print('上一根K线的最低价是{}:'.format(am.low_array[-1]))
        print('上一根K线的收盘价是{}:'.format(am.close_array[-1]))
        print('上二根K线的开盘价是{}:'.format(am.open_array[-2]))
        print('上三根K线的开盘价是{}:'.format(am.open_array[-3]))

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        pass

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass

然后用以下的代码进行验证:

from vnpy.app.cta_strategy import (
    CtaTemplate,
    StopOrder,
    TickData,
    BarData,
    TradeData,
    OrderData,
    BarGenerator,
    ArrayManager,
)


class TestBarStrategy(CtaTemplate):
    author = "蜗牛博客snailtoday.com"

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super(TestBarStrategy, self).__init__(
            cta_engine, strategy_name, vt_symbol, setting
        )

        self.bg = BarGenerator(self.on_bar)
        self.am = ArrayManager()

    def on_init(self):
        """
        Callback when strategy is inited.
        """
        self.write_log("策略初始化")
        self.load_bar(10)

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("策略启动")
        self.put_event()

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("策略停止")

        self.put_event()

    def on_tick(self, tick: TickData):
        """
        Callback of new tick data update.
        """
        self.bg.update_tick(tick)

    def on_bar(self, bar: BarData):
        """
        Callback of new bar data update.
        """
        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        print('上一根K线的最高价是{}:'.format(am.high_array[-1]))
       

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        pass

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass

验证结果:

虽然我设定的日期是:

start = datetime(2018,3,29),
end = datetime(2018,5,28),

从结果来看,5月28日的数据并没有加载,只加载到了5月28日的前一天5月25日的数据。

验证前三根K线

print('上一根K线的最高价是{}:'.format(am.high_array[-1]))
print('上二根K线的最高价是{}:'.format(am.high_array[-2]))
print('上三根K线的最高价是{}:'.format(am.high_array[-3]))

结果和上面的一致

四、合约代码
直接在vn.py里面查询即可。比如:rb1906.SHFE

五、事件引擎

例二:
注册事件:

github.com/msincenselee/vnpy

本文暂无标签

发表评论

*

*