From ad36f0ead4d84321ad7bc0f70f9ac2f6e58ac27b Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Fri, 14 Jun 2019 16:03:45 +0800 Subject: [PATCH] [Mod] change logic of grid algo --- examples/no_ui/run.py | 2 - vnpy/app/algo_trading/algos/grid_algo.py | 59 +++++++++--------------- vnpy/app/algo_trading/ui/display.py | 1 - 3 files changed, 22 insertions(+), 40 deletions(-) diff --git a/examples/no_ui/run.py b/examples/no_ui/run.py index 29cdbcd4..60ba5e48 100644 --- a/examples/no_ui/run.py +++ b/examples/no_ui/run.py @@ -91,8 +91,6 @@ def run_parent(): ): trading = True - trading = True - # Start child process in trading period if trading and child_process is None: print("启动子进程") diff --git a/vnpy/app/algo_trading/algos/grid_algo.py b/vnpy/app/algo_trading/algos/grid_algo.py index 9c140a08..690c91a5 100644 --- a/vnpy/app/algo_trading/algos/grid_algo.py +++ b/vnpy/app/algo_trading/algos/grid_algo.py @@ -13,18 +13,15 @@ class GridAlgo(AlgoTemplate): default_setting = { "vt_symbol": "", "price": 0.0, - "trade_count": 0.0, "step_price": 0.0, "step_volume": 0, - "interval": 0, + "interval": 10, } variables = [ - "last_pos", + "pos", "timer_count", - "vt_orderid", - "traded", - "count" + "vt_orderid" ] def __init__( @@ -39,17 +36,14 @@ class GridAlgo(AlgoTemplate): # Parameters self.vt_symbol = setting["vt_symbol"] self.price = setting["price"] - self.trade_count = setting["trade_count"] self.step_price = setting["step_price"] self.step_volume = setting["step_volume"] self.interval = setting["interval"] # Variables - self.vt_orderid = "" - self.count = 0 - self.traded = 0 - self.last_pos = 0 self.timer_count = 0 + self.vt_orderid = "" + self.pos = 0 self.last_tick = None self.subscribe(self.vt_symbol) @@ -74,12 +68,19 @@ class GridAlgo(AlgoTemplate): if self.vt_orderid: self.cancel_all() - # Calculate target volume to buy + # Calculate target volume to buy and sell target_buy_distance = ( self.price - self.last_tick.ask_price_1) / self.step_price target_buy_position = math.floor( target_buy_distance) * self.step_volume - target_buy_volume = target_buy_position - self.last_pos + target_buy_volume = target_buy_position - self.pos + + # Calculate target volume to sell + target_sell_distance = ( + self.price - self.last_tick.bid_price_1) / self.step_price + target_sell_position = math.ceil( + target_sell_distance) * self.step_volume + target_sell_volume = self.pos - target_sell_position # Buy when price dropping if target_buy_volume > 0: @@ -88,22 +89,17 @@ class GridAlgo(AlgoTemplate): self.last_tick.ask_price_1, min(target_buy_volume, self.last_tick.ask_volume_1) ) - - # Calculate target volume to sell - target_sell_distance = ( - self.price - self.last_tick.bid_price_1) / self.step_price - target_sell_position = math.ceil( - target_sell_distance) * self.step_volume - target_sell_volume = self.last_pos - target_sell_position - # Sell when price rising - if target_sell_volume > 0: + elif target_sell_volume > 0: self.vt_orderid = self.sell( self.vt_symbol, self.last_tick.bid_price_1, min(target_sell_volume, self.last_tick.bid_volume_1) ) + # Update UI + self.put_variables_event() + def on_order(self, order: OrderData): """""" if not order.is_active(): @@ -111,21 +107,10 @@ class GridAlgo(AlgoTemplate): self.put_variables_event() def on_trade(self, trade: TradeData): - """""" - self.last_pos = self.update_last_pos(self.last_pos, trade) - - self.traded += trade.volume - self.count += 1 - if self.count >= self.trade_count: - self.write_log(f"已交易数量:{self.traded},总成交次数:{self.count}") - self.stop() - else: - self.put_variables_event() - - def update_last_pos(self, pos, trade: TradeData): """""" if trade.direction == Direction.LONG: - pos += trade.volume + self.pos += trade.volume else: - pos -= trade.volume - return pos + self.pos -= trade.volume + + self.put_variables_event() diff --git a/vnpy/app/algo_trading/ui/display.py b/vnpy/app/algo_trading/ui/display.py index 5bbbe2e5..ff23b03b 100644 --- a/vnpy/app/algo_trading/ui/display.py +++ b/vnpy/app/algo_trading/ui/display.py @@ -15,7 +15,6 @@ NAME_DISPLAY_MAP = { "display_volume": "挂出数量", "stop_price": "触发价格", "price_add": "委托超价", - "trade_count": "交易次数限制", "step_price": "网格交易间距", "step_volume": "网格交易数量", "order_type": "类型",