diff --git a/vnpy/app/cta_strategy/strategies/multi_time_frame_strategy.py b/vnpy/app/cta_strategy/strategies/multi_timeframe_strategy.py similarity index 67% rename from vnpy/app/cta_strategy/strategies/multi_time_frame_strategy.py rename to vnpy/app/cta_strategy/strategies/multi_timeframe_strategy.py index 56046ae1..2b406a98 100644 --- a/vnpy/app/cta_strategy/strategies/multi_time_frame_strategy.py +++ b/vnpy/app/cta_strategy/strategies/multi_timeframe_strategy.py @@ -1,53 +1,54 @@ from vnpy.app.cta_strategy import ( CtaTemplate, StopOrder, - Direction, TickData, BarData, TradeData, OrderData, BarGenerator, - ArrayManager, + ArrayManager, ) class MultiTimeframeStrategy(CtaTemplate): """""" - author = '用Python的交易员' - rsi_signal = 20 - rsi_window = 14 - fast_window = 5 - slow_window = 20 - fixed_size = 1 - - rsi_value = 0 - rsi_long = 0 - rsi_short = 0 - fast_ma = 0 - slow_ma = 0 - ma_trend = 0 - + rsi_signal = 20 + rsi_window = 14 + fast_window = 5 + slow_window = 20 + fixed_size = 1 + + rsi_value = 0 + rsi_long = 0 + rsi_short = 0 + fast_ma = 0 + slow_ma = 0 + ma_trend = 0 + + parameters = ['rsi_signal', 'rsi_window', + 'fast_window', 'slow_window', + 'fixed_size'] + + variables = ['rsi_value', 'rsi_long', 'rsi_short', + 'fast_ma', 'slow_ma', 'ma_trend'] - parameters = [ 'rsi_signal', 'rsi_window', 'fast_window', 'slow_window','fixed_size'] - variables = ['rsi_value','rsi_long','rsi_short','fast_ma','slow_ma','ma_trend'] - def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(MultiTimeframeStrategy, self).__init__( cta_engine, strategy_name, vt_symbol, setting - ) + ) self.rsi_long = 50 + self.rsi_signal self.rsi_short = 50 - self.rsi_signal - - self.bg5 = BarGenerator(self.on_bar,5, self.on_5min_bar) + + self.bg5 = BarGenerator(self.on_bar, 5, self.on_5min_bar) self.am5 = ArrayManager() - self.bg15 = BarGenerator(self.on_bar,15, self.on_15min_bar) + self.bg15 = BarGenerator(self.on_bar, 15, self.on_15min_bar) self.am15 = ArrayManager() - + def on_init(self): """ Callback when strategy is inited. @@ -60,7 +61,7 @@ class MultiTimeframeStrategy(CtaTemplate): Callback when strategy is started. """ self.write_log("策略启动") - + def on_stop(self): """ Callback when strategy is stopped. @@ -79,15 +80,15 @@ class MultiTimeframeStrategy(CtaTemplate): """ self.bg5.update_bar(bar) self.bg15.update_bar(bar) - - def on_5min_bar(self, bar:BarData): + + def on_5min_bar(self, bar: BarData): """""" self.cancel_all() self.am5.update_bar(bar) if not self.am5.inited: return - + if not self.ma_trend: return @@ -95,30 +96,29 @@ class MultiTimeframeStrategy(CtaTemplate): if self.pos == 0: if self.ma_trend > 0 and self.rsi_value >= self.rsi_long: - self.buy(bar.close_price+5, self.fixed_size) + self.buy(bar.close_price + 5, self.fixed_size) elif self.ma_trend < 0 and self.rsi_value <= self.rsi_short: - self.short(bar.close_price-5, self.fixed_size) + self.short(bar.close_price - 5, self.fixed_size) elif self.pos > 0: if self.ma_trend < 0 or self.rsi_value < 50: - self.sell(bar.close_price-5, abs(self.pos)) - + self.sell(bar.close_price - 5, abs(self.pos)) + elif self.pos < 0: if self.ma_trend > 0 or self.rsi_value > 50: - self.cover(bar.close_price+5, abs(self.pos)) + self.cover(bar.close_price + 5, abs(self.pos)) self.put_event() - - def on_15min_bar(self, bar:BarData): + def on_15min_bar(self, bar: BarData): """""" - self.am15.update_bar(bar) + self.am15.update_bar(bar) if not self.am15.inited: return - + self.fast_ma = self.am15.sma(self.fast_window) self.slow_ma = self.am15.sma(self.slow_window) - + if self.fast_ma > self.slow_ma: self.ma_trend = 1 else: @@ -141,4 +141,3 @@ class MultiTimeframeStrategy(CtaTemplate): Callback of stop order update. """ pass - \ No newline at end of file