写了一个很简单的“黎明之星”的蜡烛形态策略(多空点以及止盈止损位置如下图所示)
入场方式用的是停止单。但是策略跑出来后,开仓点,止盈,止损都是乱七八糟的。求大家帮忙看下我哪里出问题了,感激!!
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
from vnpy.trader.constant import Interval
class morning_star(CtaTemplate):
author = "Leo_Monster"
fixed_size=1
wining_ratio=1.1
sl_pip=0.00050
sl_entry_long=0
sp_entry_long=0
sl_entry_short=0
sp_entry_short=0
entry_price = 0
parameters = ['fixed_size','wining_ratio','sl_pip']
variables = ['sl_entry_long','sp_entry_long','sl_entry_short','sp_entry_short','entry_price']
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,window=1,interval=Interval.HOUR,on_window_bar=self.on_60min_bar)
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("策略启动")
self.put_event()
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")
self.put_event()
def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg.update_tick(tick)
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 on_bar(self, bar: BarData):
self.bg.update_bar(bar)
def on_60min_bar(self,bar: BarData):
am = self.am
am.update_bar(bar)
if not self.am.inited:
return
self.up_trend = self.am.low_array[-2] < self.am.low_array[-1] and\
self.am.low_array[-2] < self.am.low_array[-3] and\
self.am.high_array[-1] > self.am.high_array[-2] and\
self.am.high_array[-3] > self.am.high_array[-2]
self.down_trend = self.am.high_array[-2] > self.am.high_array[-3] and\
self.am.high_array[-2] > self.am.high_array[-1] and\
self.am.low_array[-3] < self.am.low_array[-2] and\
self.am.low_array[-1] < self.am.low_array[-2]
if self.pos == 0:
if self.up_trend:
self.entry_price=self.am.high_array[-1]
self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点
self.buy(self.entry_price,1,True) #做多头仓位
self.sell(self.sl_entry_long,abs(self.pos),True)#止损
self.sell(self.sp_entry_long,abs(self.pos)) #止盈
if self.down_trend:
self.entry_price=self.am.low_array[-1]
self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点
self.sell(self.entry_price,1,True) #做空头仓位
self.cover(self.sl_entry_short,abs(self.pos),True) #止损
self.cover(self.sp_entry_short,abs(self.pos)) #止盈
if self.pos < 0:
if self.up_trend:
self.entry_price=self.am.high_array[-1] #入场价
self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点
self.cover(bar.close_price,abs(self.pos)) #平空头仓
self.buy(self.entry_price,1,True) #做多头仓位
self.sell(self.sl_entry_long,abs(self.pos),True) #止损
self.sell(self.sp_entry_long,abs(self.pos)) # 止盈
if self.down_trend:
self.entry_price=self.am.low_array[-1] #入场价
self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点
self.cover(bar.close_price,abs(self.pos)) #平掉之前所持有的空头仓
self.sell(self.entry_price,1,True) #再做空
self.cover(self.sl_entry_short,abs(self.pos),True) #止损
self.cover(self.sp_entry_short,abs(self.pos)) #止盈
if self.pos > 0:
if self.up_trend:
self.entry_price=self.am.high_array[-1]
self.sl_entry_long=self.am.low_array[-2] #做多 止盈点
self.sp_entry_long=self.am.high_array[-1]+abs(self.am.high_array[-1]-self.am.low_array[-2]) #做空 止损点
self.sell(bar.close_price,abs(self.pos)) #平掉之前的多头仓
self.buy(self.entry_price,1,True) #再开一个多头仓
self.sell(self.sl_entry_long,abs(self.pos),True) #止损
self.sell(self.sp_entry_long,abs(self.pos)) #止盈
self.buy(bar.close_price,1)
if self.down_trend:
self.entry_price=self.am.low_array[-1] #入场价
self.sl_entry_short=self.am.high_array[-2] #做空 止盈点
self.sp_entry_short=self.am.low_array[-1]+abs(self.am.high_array[-2]-self.am.low_array[-1]) #做空 止损点
self.sell(bar.close_price,abs(self.pos)) #平多头仓位
self.sell(self.entry_price,1,True) # 下空头仓位
self.cover(self.sl_entry_short,abs(self.pos),True) #止损
self.cover(self.sp_entry_short,abs(self.pos)) #止盈
self.put_event()