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

需求:杠杆监视器(LeverageMonitor.py)

目标:给github上的vn.py项目加上杠杆监视器功能

描述:
假设当前持仓为单一品种sc2305,手数为n=n0,当前权益为Q0,当前可用资金为A0,当前使用保证金为R0=Q0-A0,资金使用率目标为T0=R0/Q0=60%。当T0>60%时,触发减仓1手,并不断循环,直到T0<=60%为止。
同时,假设当n=n0+1时,权益为Q1=Q0,当前可用资金为A1,当前使用保证金为R1=Q1-A1,资金使用率目标为T1=R1/Q1=60%。当T1<60%时,触发发送一封邮件到指定邮箱。

帖子后面会持续更新代码完成进度,也请各位大神指教

Member
avatar
加入于:
帖子: 3
声望: 1

先简单把想法敲出来,起码先得有个模子

先是照抄大神 上弦之月 的作业

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

然后是杠杆监视器策略的代码:

from vnpy_ctastrategy import (
    CtaTemplate,
    StopOrder,
    TickData,
    BarData,
    TradeData,
    OrderData,
    BarGenerator,
    ArrayManager,
    )


class LeverageMonitor(CtaTemplate):
    author = "alexfucn"
    parameters = []
    variables = []

    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) -> None:
        """
        Callback of new tick data update.
        """
        pass

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

        self.put_event()

    def on_account(self,account):  
        """
        账户信息balance:总资金, available:可用资金, commission:今日手续费,pre_balance上个交易日总资金
        """
        self.balance = account.balance
        self.available=account.available

        less = (self.balance-self.available)/self.balance>0.60
        more = (self.balance-(self.available-(self.balance-self.available)/self.pos))/self.balance<0.60
        #不确定self.pos能否获得具体仓位品种手数,可能要通过手工输入或外链获取        
        #杠杠比率目标通过外链获取会比较方便根据当时情况进行调整

        if less:
            self.sell(bar.close_price, 1)

        elif more:
            self.send_email(msg, "you can buy more")

        self.put_event()

纯小白,代码之后再对着书慢慢修吧

Member
avatar
加入于:
帖子: 3
声望: 1

调整了一下,应该好一点了。

from vnpy_ctastrategy import (
    CtaTemplate,
    StopOrder,
    TickData,
    BarData,
    TradeData,
    OrderData,
    BarGenerator,
    ArrayManager,
    )


class LeverageMonitor(CtaTemplate):

    """
    本策略属于辅助策略,一般需与其它主策略配合使用。
    策略的当前版本是基于单品种,净持仓为多仓的情况。其它情况下,需对本策略作进一步修订。
    """

    author = "alexfucn"

    less = 0.0
    more = 0.0

    parameters = []
    variables = ["less","more"]

    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) -> None:
        """
        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


        less = (self.balance-self.available)/self.balance>0.60
        more = (self.balance-(self.available-(self.balance-self.available)/self.pos))/self.balance<0.60
        #不确定self.pos能否获得具体仓位品种手数,可能要通过手工输入或外链获取        
        #杠杠比率目标通过外链获取会比较方便根据当时情况进行调整

        if less:
            self.sell(bar.close_price, 1)

        elif more:
            self.send_email("you can buy more",self )

        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 send_email(self, msg) -> None:
        """
        Send email to default receiver.
        """
        if self.inited:
            self.cta_engine.send_email(msg, self)

        self.put_event()



    def on_account(self,account):  
        """
        账户信息balance:总资金, available:可用资金, commission:今日手续费,pre_balance上个交易日总资金
        """

        self.balance = account.balance
        self.available=account.available

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

沪公网安备 31011502017034号

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