vn.py官网
你的开源社区量化交易平台
Member
avatar
加入于:
帖子: 24
声望: 0

我在策略的on_order里打印价格,怎么输出的数字只有1位小数,比如9.6。有人碰到这个现象吗?

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

Member
avatar
加入于:
帖子: 2747
声望: 190

请问交易的是什么品种,回测的时候pricetick填的正确吗?

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

是股票日线数据,日志如下
short creat0 2000-06-28 00:00:00+08:06 1.8414
on order 2000-06-28 00:00:00+08:06 orderid 1 Direction.SHORT Status.NOTTRADED price 1.8 vol 1 trade 0
on order 2000-06-28 00:00:00+08:06 orderid 1 Direction.SHORT Status.ALLTRADED price 1.8 vol 1 trade 1
on trade===> 2000-06-29 00:00:00+08:06 tradeid 1 Direction.SHORT price 1.8446 vol 1 orderid 1
代码如下

class DoubleMaStrategy(CtaTemplate):
    author = "用Python的交易员"

    fast_window = 10
    slow_window = 20

    fast_ma0 = 0.0
    fast_ma1 = 0.0

    slow_ma0 = 0.0
    slow_ma1 = 0.0

    parameters = ["fast_window", "slow_window"]
    variables = ["fast_ma0", "fast_ma1", "slow_ma0", "slow_ma1"]

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super().__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.
        """
        # print(bar.datetime,bar.open_price)
        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return
        
        fast_ma = am.sma(self.fast_window, array=True)
        self.fast_ma0 = fast_ma[-1]
        self.fast_ma1 = fast_ma[-2]

        slow_ma = am.sma(self.slow_window, array=True)
        self.slow_ma0 = slow_ma[-1]
        self.slow_ma1 = slow_ma[-2]

        cross_over = self.fast_ma0 > self.slow_ma0 and self.fast_ma1 < self.slow_ma1
        cross_below = self.fast_ma0 < self.slow_ma0 and self.fast_ma1 > self.slow_ma1

        if cross_over:
            if self.pos == 0:
                print('buy creat0',bar.datetime,bar.close_price)
                self.buy(bar.close_price, 1)
            elif self.pos < 0:
                print('cover creat',bar.datetime,bar.close_price, 'pos',self.pos)
                self.cover(bar.close_price, 1)
                print('buy creat',bar.datetime,bar.close_price)
                self.buy(bar.close_price, 1)

        elif cross_below:
            if self.pos == 0:
                print('short creat0',bar.datetime,bar.close_price)
                self.short(bar.close_price, 1)
            elif self.pos > 0:
                print('sell creat',bar.datetime,bar.close_price)
                self.sell(bar.close_price, 1)
                print('short creat',bar.datetime,bar.close_price)
                self.short(bar.close_price, 1)

        self.put_event()

    def on_order(self, order: OrderData):
        """
        Callback of new order data update.
        """
        print('on order',order.datetime,'orderid',order.orderid,order.direction,order.status, 'price',round(order.price,4), 'vol',order.volume,'trade',order.traded)

    def on_trade(self, trade: TradeData):
        """
        Callback of new trade data update.
        """
        print('on trade===>',trade.datetime,'tradeid',trade.tradeid,trade.direction,'price',trade.price,'vol',trade.volume,'orderid',trade.orderid)
        self.put_event()

    def on_stop_order(self, stop_order: StopOrder):
        """
        Callback of stop order update.
        """
        pass
Member
avatar
加入于:
帖子: 2747
声望: 190

你的on_order打印价格的部分不是写了round函数吗?你的on_trade直接打印的价格不是不止一位小数吗?

Administrator
avatar
加入于:
帖子: 4469
声望: 302

请价差你回测时候的pricetick,是设置了多少,这个必须和实盘的合约最小价格跳动一致

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

沪公网安备 31011502017034号

【用户协议】
【隐私政策】