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

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

)
from vnpy.trader.constant import Direction, Offset
class demo11(CtaTemplate):

author="action华"
k1=1.0
k3=3.1
n_window=9
unit=6





w=0
dev=0
intra_trade_high=0
intra_trade_low=0
long_stop=0
short_stop=0
long_entry=0
short_entry=0
long_out=0
short_out=0



parameters = ["k1", "n_window", "k3","unit"]
variables = ["w", "dev","intra_trade_high","intra_trade_low","long_stop", "short_stop","long_entry","short_entry","long_out","short_out"]
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, 5, self.on_5min_bar)
    self.am = ArrayManager()
    self.bars = []
    self.range=[]

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("策略启动")

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

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.bg.update_bar(bar)

def on_5min_bar(self, bar: BarData):
    """"""
    self.cancel_all()

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


    self.w=bar.high_price-bar.low_price
    self.range.append(self.w)
    if len(self.range)<=5:
        return
    else:
        self.range.pop(0)


    self.dev=abs(sum(self.range)-self.range[-1])/4

    if self.pos==0:
        self.n = self.am.atr(self.n_window)
        if self.range[-1]<self.k3*self.dev:
            self.intra_trade_high=bar.high_price
            self.intra_trade_low=bar.low_price
            if 0<bar.close_price-bar.open_price<self.k1*self.range[-1]:
                self.buy(bar.close_price,self.unit, stop=True) 
            if 0<bar.open_price-bar.close_price<self.k1*self.range[-1]:
                self.short(bar.close_price,self.unit, stop=True)

    if self.pos>0:
        self.intra_trade_high=max(self.intra_trade_high,bar.high_price)
        self.intra_trade_low=bar.low_price
        self.long_stop=self.intra_trade_high-self.n*bar.high_price*0.0001
        self.sell(self.long_stop, abs(self.pos), stop=True)

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

        self.short_stop = self.intra_trade_low +self.n*bar.high_price*0.0001
        self.cover(self.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):

    # 记录开仓价格
    if trade.offset == Offset.OPEN:
        # 区分多空开仓价
        if trade.direction == Direction.LONG:
            self.long_entry = trade.price
        else:
            self.short_entry = trade.price
    elif trade.offset == Offset.CLOSE:
        if trade.direction == Direction.LONG:
            self.long_out = trade.price
        else:
            self.short_out = trade.price
    self.put_event()

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

本地停止单要盘口价格触发委托价格才能超价发出的

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

沪公网安备 31011502017034号

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