VeighNa量化社区
你的开源社区量化交易平台
Member
离线
7 帖子
声望: 0

Rename Qt5Bluetooth.dll to Qt5Bluetooth.dll1 in folder : /site-packages/PyQt5/Qt/bin

高飞 wrote:

下面是回测日志:

14:12:11 初始化CTA回测引擎
14:12:11 策略文件加载完成
14:12:40 ----------------------------------------
14:12:40 开始加载历史数据
14:12:40 加载进度: [3%]
14:12:40 加载进度: [5%]
14:12:40 加载进度: [8%]
14:12:40 加载进度:# [11%]
14:12:40 加载进度:# [14%]
14:12:40 加载进度:# [16%]
14:12:40 加载进度:# [19%]
14:12:40 加载进度:## [22%]
14:12:40 加载进度:## [25%]
14:12:40 加载进度:## [27%]
14:12:40 加载进度:### [30%]
14:12:40 加载进度:### [33%]
14:12:40 加载进度:### [36%]
14:12:40 加载进度:### [38%]
14:12:40 加载进度:#### [41%]
14:12:40 加载进度:#### [44%]
14:12:40 加载进度:#### [47%]
14:12:40 加载进度:#### [49%]
14:12:40 加载进度:##### [52%]
14:12:40 加载进度:##### [55%]
14:12:40 加载进度:##### [58%]
14:12:40 加载进度:###### [60%]
14:12:40 加载进度:###### [63%]
14:12:40 加载进度:###### [66%]
14:12:40 加载进度:###### [68%]
14:12:40 加载进度:####### [71%]
14:12:40 加载进度:####### [74%]
14:12:40 加载进度:####### [77%]
14:12:40 加载进度:####### [79%]
14:12:40 加载进度:######## [82%]
14:12:40 加载进度:######## [85%]
14:12:40 加载进度:######## [88%]
14:12:40 加载进度:######### [90%]
14:12:40 加载进度:######### [93%]
14:12:40 加载进度:######### [96%]
14:12:40 加载进度:######### [99%]
14:12:40 加载进度:########## [100%]
14:12:40 历史数据加载完成,数据量:0
14:12:40 策略初始化完成
14:12:40 开始回放历史数据
14:12:40 历史数据回放结束
14:12:40 开始计算逐日盯市盈亏
14:12:40 成交记录为空,无法计算
14:12:40 开始计算策略统计指标
14:14:16 ----------------------------------------
14:14:16 CF005.CZCE-1m开始下载历史数据

填 rqdata 在设置中

self.eventEngine.register ==> false . repalce with self.event_engine.register

but:

test: Trigger exception has stopped
Traceback (most recent call last):
  File "E:\WARTER\python\python.v3_portable\App\lib\site-packages\vnpy\app\cta_strategy\engine.py", line 576, in call_strategy_func
    func()
  File "E:\WARTER\python\python.v3_portable\App\lib\site-packages\vnpy\app\cta_strategy\strategies\atr_rsi_strategy.py", line 56, in on_init
    print(self.balance)
AttributeError: 'AtrRsiStrategy' object has no attribute 'balance'

my strategy script :


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


class AtrRsiStrategy(CtaTemplate):
    """"""

    author = "用Python的交易员"

    atr_length = 22
    atr_ma_length = 10
    rsi_length = 5
    rsi_entry = 16
    trailing_percent = 0.8
    fixed_size = 1

    atr_value = 0
    atr_ma = 0
    rsi_value = 0
    rsi_buy = 0
    rsi_sell = 0
    intra_trade_high = 0
    intra_trade_low = 0

    parameters = ["atr_length", "atr_ma_length", "rsi_length",
                  "rsi_entry", "trailing_percent", "fixed_size"]
    variables = ["atr_value", "atr_ma", "rsi_value", "rsi_buy", "rsi_sell"]

    def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
        """"""
        super(AtrRsiStrategy, 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("Strategy initialization")

        self.rsi_buy = 50 + self.rsi_entry
        self.rsi_sell = 50 - self.rsi_entry

        self.load_bar(10)

        print(self.balance)

    def on_start(self):
        """
        Callback when strategy is started.
        """
        self.write_log("Strategy start")

    def on_stop(self):
        """
        Callback when strategy is stopped.
        """
        self.write_log("Strategy stop")

    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.cancel_all()

        am = self.am
        am.update_bar(bar)
        if not am.inited:
            return

        atr_array = am.atr(self.atr_length, array=True)
        self.atr_value = atr_array[-1]
        self.atr_ma = atr_array[-self.atr_ma_length:].mean()
        self.rsi_value = am.rsi(self.rsi_length)

        if self.pos == 0:
            self.intra_trade_high = bar.high_price
            self.intra_trade_low = bar.low_price

            if self.atr_value > self.atr_ma:
                if self.rsi_value > self.rsi_buy:
                    self.buy(bar.close_price + 5, self.fixed_size)
                elif self.rsi_value < self.rsi_sell:
                    self.short(bar.close_price - 5, self.fixed_size)

        elif self.pos > 0:
            self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
            self.intra_trade_low = bar.low_price

            long_stop = self.intra_trade_high * \
                (1 - self.trailing_percent / 100)
            self.sell(long_stop, abs(self.pos), stop=True)

        elif self.pos < 0:
            self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
            self.intra_trade_high = bar.high_price

            short_stop = self.intra_trade_low * \
                (1 + self.trailing_percent / 100)
            self.cover(short_stop, abs(self.pos), stop=True)

        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

    def onAccount(self,account):
        #'''Account information account.balance: Total funds, available: Available funds, commission: Today's handling fee, preBalance last trading day total funds '''

        #More parameter printing(account.__dict__)
            # Issue a status update event
        self.balance = account.balance
        self.putEvent()

上弦之月 wrote:

ctaengine里面加上

#----------------------------------------------------------------------
def processAccountEvent(self,event):
    """处理账户推送"""
    account = event.dict_['data']
    for name in list(self.strategyDict.keys()):
        strategy = self.strategyDict[name]
        self.callStrategyFunc(strategy, strategy.onAccount, account)    
def registerEvent(self):
    """注册事件监听"""
    self.eventEngine.register(EVENT_ACCOUNT, self.processAccountEvent)    

ctaTemplate里面加上

#----------------------------------------------------------------------
def onAccount(self, account):
    """收到账户信息推送(必须由用户继承实现)"""
    raise NotImplementedError

策略里面加上
def onAccount(self,account):
'''账户信息account.balance:总资金, available:可用资金, commission:今日手续费,preBalance上个交易日总资金'''

    #更多参数打印(account.__dict__)
        # 发出状态更新事件
        self.putEvent()

我找到它。如何使用它来查询acount.balance

© 2015-2022 微信 18391752892
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

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