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

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

class macdyinzhi:
''''''
def init(
self,
boll_window1: int,
boll_window2: int,
boll_window3: int,
) -> None:
super().init()

    self.bg = BarGenerator(self.on_bar)
    self.am = ArrayManager()#    条形数据的时间序列容器
    # 参数
    self.macd1 = boll_window1
    self.macd2 = boll_window2
    self.macd3 = boll_window3

    self.target1: int = 0             # 目标仓位
    self.target2: int = 0             # 目标仓位

def on_tick(self, tick: TickData):
    """
    Callback of new tick data update.
    回调新的报价数据更新。
    """
    self.bg.update_tick(tick)#TICKC载入
    #初始化
def on_bar(self, bar: BarData):
    self.am.update_bar(bar)#初始化
    if self.am.inited :
        self.target1=0
        self.target2=0
        # 缓存K线序列
        self.am.update_bar(bar)
        if not self.am.inited:
            return  

        ma111, ma222, ma333 = self.am.macd(self.macd1,self.macd2,self.macd3,True)
        self.fast_ma0 = ma111[-1]
        self.fast_ma1 = ma111[-2]
        #均线最后一个和最后两个
        self.slow_ma0 = ma222[-1]
        self.slow_ma1 = ma222[-2]
        cross_over = (self.fast_ma0 > self.slow_ma0 and self.fast_ma1 < self.slow_ma1)
        cross_below =(self.fast_ma0 < self.slow_ma0 and self.fast_ma1 > self.slow_ma1)

        if cross_over:#金叉
                self.target1 = 1

        elif cross_below:#死叉
                self.target1 = -1

def get_target(self) -> int:
        """获取因子目标"""
        return self.target1


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

boll_window1 = 12
boll_window2 = 26
boll_window3 = 9    
sun=50.0
yin=25.0

fast_ma0: int = 0
fast_ma1: int = 0.0

parameters = ["boll_window1", "boll_window2","boll_window3","sun","yin"]
variables = ["fast_ma0", "fast_ma1", "slow_ma0", "slow_ma1"]

def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
    """"""
    super().__init__(cta_engine, strategy_name, vt_symbol, setting)

    self.factor = macdyinzhi(
        self.boll_window1,
        self.boll_window2,
        self.boll_window3,
    )

def on_init(self):
    """初始化"""
    self.write_log("策略初始化")
    self.load_bar(1)
def on_start(self):
    """启动"""
    pass
def on_stop(self):
    """停止"""
    pass
def on_tick(self, tick: TickData):
    """Tick推送"""
    pass
def on_bar(self, bar: BarData):
    """原始K线推送"""
    # 全撤之前委托
    self.cancel_all()

    # 推送给因子计算
    self.factor.on_bar(bar)

    # 计算目标和实际仓位差
    self.fast_ma0 = self.factor.get_target()
    # 基于仓位差执行交易
    if self.fast_ma0 > 0:
        price: float = bar.close_price

        if self.pos < 0:
            self.cover(price, 1)
            self.buy(price, abs(self.pos))
        else:
            self.buy(price, abs(self.pos))
    elif self.fast_ma0 < 0:
        price: float = bar.close_price

        if self.pos > 0:
            self.sell(price, 1)
            self.short(price, abs(self.pos))
        else:
            self.short(price, abs(self.pos))

`
不下单,拜托帮忙看看

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

可以自己在on_bar里打印看看是否是因为历史数据太少不够初始化导致在if not self.am.inited返回了

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

下单写错了,抱歉
if self.pos < 0:
self.cover(price, 1)
self.buy(price, abs(self.pos))
else:
self.buy(price, abs(self.pos))
elif self.fast_ma0 < 0:
price: float = bar.close_price

    if self.pos > 0:
        self.sell(price, 1)
        self.short(price, abs(self.pos))
    else:
        self.short(price, abs(self.pos))
Member
avatar
加入于:
帖子: 5
声望: 1

解决了,哈哈

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

沪公网安备 31011502017034号

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