上弦之月 wrote:
ctaengine里面加上
from vnpy.trader.event import (EVENT_TICK, EVENT_ORDER, EVENT_TRADE, EVENT_POSITION, EVENT_ACCOUNT) #-------------------------------------------------------------------------------------- def process_account_event(self,event: Event): """ 收到账户事件推送 """ account = event.data for strategy_name in self.strategies.keys(): strategy = self.strategies[strategy_name] self.call_strategy_func(strategy, strategy.on_account, account) #------------------------------------------------------------------------------------ def register_event(self): """ 注册事件监听 """ self.event_engine.register(EVENT_TICK, self.process_tick_event) self.event_engine.register(EVENT_ORDER, self.process_order_event) self.event_engine.register(EVENT_TRADE, self.process_trade_event) self.event_engine.register(EVENT_POSITION, self.process_position_event) self.event_engine.register(EVENT_ACCOUNT, self.process_account_event)
cta_strategy\template.py里面加上
#---------------------------------------------------------------------- @virtual def on_account(self, account): """ 账户信息推送 """ pass
策略里面加上
#------------------------------------------------------------------------------------ def on_account(self,account): """ 账户信息balance:总资金, available:可用资金, commission:今日手续费,pre_balance上个交易日总资金 """ self.account = account
———————————————————————————————————————————————
以上是如何实现策略里面获取资金的信息的修改办法。比如我启动了2个策略 ,这两个策略经过交易都有了持仓信息,A策略有1手,B策略也有了1手。而这些信息在cta_strategy\engine.py中的
def process_position_event(self, event: Event):
""""""
position = event.data
self.offset_converter.update_position(position)
里是获取到所有持仓信息的。我想让 def on_account(self,account)返回的信息变成,可用资金-A的持仓x止损价格-B的持仓x止损价格。这样我就可以用真正的可用资金再来决定是否要建新仓。这个如何实现了?大神们帮帮忙。