From 1a8a167ba10cf7ee7bb7df394c51df4697e7a679 Mon Sep 17 00:00:00 2001 From: 1122455801 Date: Thu, 30 May 2019 16:04:55 +0800 Subject: [PATCH 1/3] Create dma_algo.py --- vnpy/app/algo_trading/algos/dma_algo.py | 99 +++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 vnpy/app/algo_trading/algos/dma_algo.py diff --git a/vnpy/app/algo_trading/algos/dma_algo.py b/vnpy/app/algo_trading/algos/dma_algo.py new file mode 100644 index 00000000..14849c94 --- /dev/null +++ b/vnpy/app/algo_trading/algos/dma_algo.py @@ -0,0 +1,99 @@ +from vnpy.trader.constant import Offset, Direction, OrderType +from vnpy.trader.object import TradeData, OrderData, TickData +from vnpy.trader.engine import BaseEngine + +from vnpy.app.algo_trading import AlgoTemplate + + +class DmaAlgo(AlgoTemplate): + """""" + + display_name = "DMA 直接委托" + + default_setting = { + "vt_symbol": "", + "direction": [Direction.LONG.value, Direction.SHORT.value], + "order_type": [ + OrderType.MARKET.value, + OrderType.LIMIT.value, + OrderType.STOP.value, + OrderType.FAK.value, + OrderType.FOK.value + ], + "price": 0.0, + "volume": 0.0, + "offset": [ + Offset.NONE.value, + Offset.OPEN.value, + Offset.CLOSE.value, + Offset.CLOSETODAY.value, + Offset.CLOSEYESTERDAY.value + ] + } + + variables = [ + "traded", + "vt_orderid", + "order_status", + ] + + def __init__( + self, + algo_engine: BaseEngine, + algo_name: str, + setting: dict + ): + """""" + super().__init__(algo_engine, algo_name, setting) + + # Parameters + self.vt_symbol = setting["vt_symbol"] + self.direction = Direction(setting["direction"]) + self.order_type = OrderType(setting["order_type"]) + self.price = setting["price"] + self.volume = setting["volume"] + self.offset = Offset(setting["offset"]) + + # Variables + self.vt_orderid = "" + self.traded = 0 + self.order_status = "" + + self.subscribe(self.vt_symbol) + self.put_parameters_event() + self.put_variables_event() + + def on_tick(self, tick: TickData): + """""" + if not self.vt_orderid: + if self.direction == Direction.LONG: + self.vt_orderid = self.buy( + self.vt_symbol, + self.price, + self.volume, + self.order_type, + self.offset + ) + + else: + self.vt_orderid = self.sell( + self.vt_symbol, + self.price, + self.volume, + self.order_type, + self.offset + ) + self.put_variables_event() + + def on_order(self, order: OrderData): + """""" + self.traded = order.traded + self.order_status = order.status + + if not order.is_active(): + self.stop() + self.put_variables_event() + + def on_trade(self, trade: TradeData): + """""" + pass From ef5848934f7666b1bd20d1dae4b2972c4a9bb15b Mon Sep 17 00:00:00 2001 From: 1122455801 Date: Thu, 30 May 2019 16:04:58 +0800 Subject: [PATCH 2/3] Update stop_algo.py --- vnpy/app/algo_trading/algos/stop_algo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnpy/app/algo_trading/algos/stop_algo.py b/vnpy/app/algo_trading/algos/stop_algo.py index 14b5e94f..a25bc0e9 100644 --- a/vnpy/app/algo_trading/algos/stop_algo.py +++ b/vnpy/app/algo_trading/algos/stop_algo.py @@ -85,7 +85,7 @@ class StopAlgo(AlgoTemplate): if tick.limit_down: price = max(price, tick.limit_down) - self.vt_orderid = self.buy( + self.vt_orderid = self.sell( self.vt_symbol, price, self.volume, From 31d4f0b04ef2acb0f83af9d0cc71f03c7fffc196 Mon Sep 17 00:00:00 2001 From: 1122455801 Date: Thu, 30 May 2019 16:05:03 +0800 Subject: [PATCH 3/3] Update engine.py --- vnpy/app/algo_trading/engine.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vnpy/app/algo_trading/engine.py b/vnpy/app/algo_trading/engine.py index 1bfbd20c..a3dc7740 100644 --- a/vnpy/app/algo_trading/engine.py +++ b/vnpy/app/algo_trading/engine.py @@ -49,6 +49,7 @@ class AlgoEngine(BaseEngine): from .algos.stop_algo import StopAlgo from .algos.best_limit_algo import BestLimitAlgo from .algos.grid_algo import GridAlgo + from .algos.dma_algo import DmaAlgo self.add_algo_template(TwapAlgo) self.add_algo_template(IcebergAlgo) @@ -56,6 +57,7 @@ class AlgoEngine(BaseEngine): self.add_algo_template(StopAlgo) self.add_algo_template(BestLimitAlgo) self.add_algo_template(GridAlgo) + self.add_algo_template(DmaAlgo) def add_algo_template(self, template: AlgoTemplate): """"""