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

请教:
问题一、用脚本策略运行多品种策略时,a = Return10Strategy(StrategyEngine,'zhangsan',vt_symbols,setting) 中的StrategyEngine传什么参数?
问题二、以下代码是用组合策略的模板写的,为什么运行时,打印的bar都是None?

from vnpy_scripttrader import init_cli_trading
from vnpy_ctp import CtpGateway
engine = init_cli_trading([CtpGateway])

setting = {
"用户名": "*",
"密码": "*",
"经纪商代码": "*",
"交易服务器":"*",
"行情服务器":"*",
"产品名称":"*",
"授权编码":"*"
}
engine.connect_gateway(setting,"CTP")

from typing import List, Dict
from datetime import datetime

from vnpy_portfoliostrategy import StrategyTemplate, StrategyEngine
from vnpy.trader.utility import BarGenerator, ArrayManager
from vnpy.trader.object import TickData, BarData

from vnpy.trader.constant import Interval

vt_symbols = ['rb2309.SHFE','rb2310.SHFE']

class Return10Strategy(StrategyTemplate):
""""""

author = "KeKe"

price_add_percent = 0.05    # 超价5%下单
fixed_pos_value = 1000000   # 每个合约做100万
signal_ts = {}
signal_total = {}
last_tick_time: datetime = None
trade_day = 0
targets_pos = {}

parameters = [
    "price_add_percent",
    "fixed_size",
]
variables = [
    "signal_ts", "signal_total",
    "trade_day", "targets_pos"
]

def __init__(
    self,
    strategy_engine: StrategyEngine,
    strategy_name: str,
    vt_symbols: List[str],
    setting: dict
):
    """"""
    super().__init__(strategy_engine, strategy_name, vt_symbols, setting)

    self.bgs: Dict[str, BarGenerator] = {}
    self.ams: Dict[str, ArrayManager] = {}

    # Obtain contract info
    for vt_symbol in self.vt_symbols:
        def on_bar(bar: BarData):
            """"""
            pass

        self.bgs[vt_symbol] = BarGenerator(on_bar)
        self.ams[vt_symbol] = ArrayManager()

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

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

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

def on_tick(self, tick: TickData):
    """
    Callback of new tick data update.
    """
    if (
        self.last_tick_time
        and self.last_tick_time.minute != tick.datetime.minute
    ):
        bars = {}
        for vt_symbol, bg in self.bgs.items():
            bars[vt_symbol] = bg.generate()
        self.on_bars(bars)

    bg: BarGenerator = self.bgs[tick.vt_symbol]
    bg.update_tick(tick)

    self.last_tick_time = tick.datetime

def on_bars(self, bars: Dict[str, BarData]):
    """"""
    # 1) 全撤
    #self.cancel_all()

    # 2.3)初始化am &计算时间序列
    for vt_symbol, bar in bars.items():
        am: ArrayManager = self.ams[vt_symbol]
        am.update_bar(bar)
        print(am)
        # if not am.inited:
        #     return

        # 信号 过去10天的收益率
        return10 = am.rocp(10)

        # 信号>0,时序信号+1,信号<0,时序信号-1
        if return10 > 0:
            self.signal_ts[vt_symbol] = 1
        elif return10 < - 0:
            self.signal_ts[vt_symbol] = -1
        else:
            self.signal_ts[vt_symbol] = 0

    # 3)信号汇总,总信号=时序信号汇总 + 横截面信号汇总
    # 基于总体信号,得到目标持仓
    for vt_symbol, bar in bars.items():
        self.signal_total[vt_symbol] = self.signal_ts[vt_symbol]
        self.targets_pos[vt_symbol] = int(
            self.fixed_pos_value / bar.close_price
        ) * self.signal_total[vt_symbol]

    # 3)交易执行(每隔10个交易日检查并且调仓)
    if self.trade_day == 0 or not self.trade_day % 10:

        for vt_symbol in self.vt_symbols:
            bar = bars.get(vt_symbol)
            if not bar:
                continue

    self.put_event()

vt_symbols = ['rb2309.SHFE','rb2310.SHFE']
a = Return10Strategy(StrategyEngine,'zhangsan',vt_symbols,setting)
while True:
for vt_symbol in vt_symbols:
print(a.bgs[vt_symbol].bar)

Member
avatar
加入于:
帖子: 4556
声望: 277

脚本策略模板的同步的,不是用来跑基于CTA策略模块/组合策略模块开发的策略的
https://www.vnpy.com/forum/topic/859-vn-pyfa-bu-v2-0-5-jiao-ben-ce-lue-he-rpcfu-wu
想要无界面跑组合策略,可以用no_ui脚本启动组合策略模块跑策略
https://gitee.com/vnpy/vnpy/blob/master/examples/no_ui/run.py

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

沪公网安备 31011502017034号

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