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

from vnpy_ctastrategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
from vnpy.trader.constant import Interval

class TurtleSignalStrategyPro_hour(CtaTemplate):
""""""
author = "用Python的交易员"

entry_window = 20
exit_window = 10
atr_window = 20
fixed_size = 1
rsi_lenth = 7
rsi_up = 80
rsi_down = 20

entry_up = 0
entry_down = 0
exit_up = 0
exit_down = 0
atr_value = 0

long_entry = 0
short_entry = 0
long_stop = 0
short_stop = 0

parameters = ["entry_window", "exit_window", "atr_window", "fixed_size",'rsi_lenth','rsi_up','rsi_down']
variables = ["entry_up", "entry_down", "exit_up", "exit_down", "atr_value", 'long_entry', 'short_entry', 'long_stop', 'short_stop']

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, 1, self.on_hour_bar, Interval.HOUR)
    self.am = ArrayManager()

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

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_hour_bar(self, bar: BarData):
    """
    Callback of new bar data update.
    """
    self.cancel_all()

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

    # Only calculates new entry channel when no position holding
    if not self.pos:
        self.entry_up, self.entry_down = self.am.donchian(
            self.entry_window
        )

    self.exit_up, self.exit_down = self.am.donchian(self.exit_window)


    if not self.pos:
        self.atr_value = self.am.atr(self.atr_window)

        self.long_entry = 0
        self.short_entry = 0
        self.long_stop = 0
        self.short_stop = 0

        self.send_buy_orders(self.entry_up)
        self.send_short_orders(self.entry_down)
    elif self.pos > 0:

        if self.am.rsi(self.rsi_lenth) >= self.rsi_up:

            self.sell(bar.close_price,abs(self.pos),stop=True)

        else:
            self.send_buy_orders(self.entry_up)

            sell_price = max(self.long_stop, self.exit_down)
            self.sell(sell_price, abs(self.pos), stop=True)

    elif self.pos < 0:

        if self.am.rsi(self.rsi_lenth) <= self.rsi_down:

            self.cover(bar.close_price,abs(self.pos),stop=True)

        else:

            self.send_short_orders(self.entry_down)

            cover_price = min(self.short_stop, self.exit_up)
            self.cover(cover_price, abs(self.pos), stop=True)

    self.put_event()

def on_trade(self, trade: TradeData):
    """
    Callback of new trade data update.
    """
    if trade.direction == Direction.LONG:
        self.long_entry = trade.price
        self.long_stop = self.long_entry - 2 * self.atr_value
    else:
        self.short_entry = trade.price
        self.short_stop = self.short_entry + 2 * self.atr_value

def on_order(self, order: OrderData):
    """
    Callback of new order data update.
    """
    pass

def on_stop_order(self, stop_order: StopOrder):
    """
    Callback of stop order update.
    """
    pass

def send_buy_orders(self, price):
    """"""
    t = self.pos / self.fixed_size

    if t < 1:
        self.buy(price, self.fixed_size, stop=True)

    if t < 2:
        self.buy(price + self.atr_value * 0.5, self.fixed_size*2, stop=True)

    if t < 3:
        self.buy(price + self.atr_value, self.fixed_size, stop=True)



def send_short_orders(self, price):
    """"""
    t = self.pos / self.fixed_size

    if t > -1:
        self.short(price, self.fixed_size, stop=True)

    if t > -2:
        self.short(price - self.atr_value * 0.5, self.fixed_size*2, stop=True)

    if t > -3:
        self.short(price - self.atr_value, self.fixed_size, stop=True)
Member
avatar
加入于:
帖子: 54
声望: 1

self.bg = BarGenerator(self.on_bar, 1, self.on_hour_bar, Interval.HOUR)
修改为
self.bg = BarGenerator(self.on_bar, 60, self.on_hour_bar)

Member
avatar
加入于:
帖子: 4800
声望: 290

没反应具体指的是?

Member
avatar
加入于:
帖子: 16
声望: 0

xiaohe wrote:

没反应具体指的是?
初始化和启动策略没有反应

Member
avatar
加入于:
帖子: 16
声望: 0

连龙八卦 wrote:

self.bg = BarGenerator(self.on_bar, 1, self.on_hour_bar, Interval.HOUR)
修改为
self.bg = BarGenerator(self.on_bar, 60, self.on_hour_bar)
好的 谢谢 我试试

Member
avatar
加入于:
帖子: 16
声望: 0

xiaohe wrote:

没反应具体指的是?
实例初始化各个参数都是空值,运行也没有反应 但是能回测

Member
avatar
加入于:
帖子: 16
声望: 0

小喵咪揣手手 wrote:

连龙八卦 wrote:

self.bg = BarGenerator(self.on_bar, 1, self.on_hour_bar, Interval.HOUR)
修改为
self.bg = BarGenerator(self.on_bar, 60, self.on_hour_bar)
好的 谢谢 我试试
这个还是不行

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

沪公网安备 31011502017034号

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