From e64cfd72236533390db84059e8d2674a45d1944c Mon Sep 17 00:00:00 2001 From: 1122455801 Date: Wed, 20 Feb 2019 10:01:18 +0800 Subject: [PATCH] Update double_ma_strategy.py --- .../strategies/double_ma_strategy.py | 119 ++++++++++++++++-- 1 file changed, 109 insertions(+), 10 deletions(-) diff --git a/vnpy/app/cta_strategy/strategies/double_ma_strategy.py b/vnpy/app/cta_strategy/strategies/double_ma_strategy.py index 779bdbad..5baa1223 100644 --- a/vnpy/app/cta_strategy/strategies/double_ma_strategy.py +++ b/vnpy/app/cta_strategy/strategies/double_ma_strategy.py @@ -1,20 +1,119 @@ -from vnpy.app.cta_strategy import CtaTemplate +from vnpy.app.cta_strategy import ( + CtaTemplate, + StopOrder, + Direction, + TickData, + BarData, + TradeData, + OrderData, + BarGenerator, + ArrayManager, +) class DoubleMaStrategy(CtaTemplate): - author = "用Python的交易员" + author = '用Python的交易员' + + fast_window = 10 + slow_window = 20 - fast_window = 10 - slow_window = 20 - - fast_ma = 0.0 - slow_ma = 0.0 - - parameters = ["fast_window", "slow_window"] - variables = ["fast_ma", "slow_ma"] + fast_ma0 = 0.0 + fast_ma1 = 0.0 + slow_ma0 = 0.0 + slow_ma1 = 0.0 + + parameters = [ 'fast_window', 'slow_window'] + variables = ['fast_ma0','fast_ma1','slow_ma0','slow_ma1'] + def __init__(self, cta_engine, strategy_name, vt_symbol, setting): """""" super(DoubleMaStrategy, self).__init__( cta_engine, strategy_name, vt_symbol, setting ) + + self.bg = BarGenerator(self.on_bar) + self.am = ArrayManager() + + 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("策略启动") + 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_bar(self, bar: BarData): + """ + Callback of new bar data update. + """ + + am = self.am + am.update_bar(bar) + if not am.inited: + return + + fast_ma = am.sma(self.fast_window, array=True) + self.fast_ma0 = fast_ma[-1] + self.fast_ma1 = fast_ma[-2] + + slow_ma = am.sma(self.slow_window, array=True) + self.slow_ma0 = slow_ma[-1] + self.slow_ma1 = slow_ma[-2] + + cross_over = self.fast_ma0>self.slow_ma0 and self.fast_ma1self.slow_ma1 + + if cross_over: + if self.pos == 0: + self.buy(bar.close_price, 1) + elif self.pos < 0: + self.cover(bar.close_price, 1) + self.buy(bar.close_price, 1) + + elif cross_below: + if self.pos == 0: + self.short(bar.close_price, 1) + elif self.pos > 0: + self.sell(bar.close_price, 1) + self.short(bar.close_price, 1) + + self.put_event() + + 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 \ No newline at end of file