From 746ac00854d5292f7c98614f0cb0ccf55b6aa15f Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 30 Sep 2019 11:22:16 +0800 Subject: [PATCH 1/5] [Fix] max limit bug with rqdatac --- vnpy/trader/rqdata.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/vnpy/trader/rqdata.py b/vnpy/trader/rqdata.py index 1df27232..de4a8d37 100644 --- a/vnpy/trader/rqdata.py +++ b/vnpy/trader/rqdata.py @@ -49,8 +49,12 @@ class RqdataClient: if not self.username or not self.password: return False - rqdata_init(self.username, self.password, - ('rqdatad-pro.ricequant.com', 16011)) + rqdata_init( + self.username, + self.password, + ('rqdatad-pro.ricequant.com', 16011), + use_pool=True, + ) try: df = rqdata_all_instruments(date=datetime.now()) From 21477371690421c0302905d5a1d1333181b99095 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 28 Oct 2019 14:43:12 +0800 Subject: [PATCH 2/5] [Del] remove query trade and history order function --- vnpy/gateway/hbdm/hbdm_gateway.py | 97 +------------------------------ 1 file changed, 3 insertions(+), 94 deletions(-) diff --git a/vnpy/gateway/hbdm/hbdm_gateway.py b/vnpy/gateway/hbdm/hbdm_gateway.py index b8de5b76..e0a57d33 100644 --- a/vnpy/gateway/hbdm/hbdm_gateway.py +++ b/vnpy/gateway/hbdm/hbdm_gateway.py @@ -58,7 +58,7 @@ STATUS_HBDM2VT = { ORDERTYPE_VT2HBDM = { OrderType.MARKET: "opponent", - OrderType.LIMIT: "limit", + OrderType.LIMIT: "limit" } ORDERTYPE_HBDM2VT = {v: k for k, v in ORDERTYPE_VT2HBDM.items()} ORDERTYPE_HBDM2VT[1] = OrderType.LIMIT @@ -284,41 +284,7 @@ class HbdmRestApi(RestClient): self.add_request( method="POST", path="/api/v1/contract_openorders", - callback=self.on_query_active_order, - data=data, - extra=currency - ) - - # History Orders - data = { - "symbol": currency, - "trade_type": 0, - "type": 2, - "status": 0, - "create_date": 7 - } - - self.add_request( - method="POST", - path="/api/v1/contract_hisorders", - callback=self.on_query_history_order, - data=data, - extra=currency - ) - - def query_trade(self): - """""" - for currency in self.currencies: - data = { - "symbol": currency, - "trade_type": 0, - "create_date": 7 - } - - self.add_request( - method="POST", - path="/api/v1/contract_matchresults", - callback=self.on_query_trade, + callback=self.on_query_order, data=data, extra=currency ) @@ -549,7 +515,7 @@ class HbdmRestApi(RestClient): for position in self.positions.values(): self.gateway.on_position(position) - def on_query_active_order(self, data, request): + def on_query_order(self, data, request): """""" if self.check_error(data, "查询活动委托"): return @@ -582,61 +548,6 @@ class HbdmRestApi(RestClient): self.gateway.write_log(f"{request.extra}活动委托信息查询成功") - def on_query_history_order(self, data, request): - """""" - if self.check_error(data, "查询历史委托"): - return - - for d in data["data"]["orders"]: - timestamp = d["create_date"] - dt = datetime.fromtimestamp(timestamp / 1000) - time = dt.strftime("%H:%M:%S") - - orderid = d["order_id"] - - order = OrderData( - orderid=orderid, - symbol=d["contract_code"], - exchange=Exchange.HUOBI, - price=d["price"], - volume=d["volume"], - type=ORDERTYPE_HBDM2VT[d["order_price_type"]], - direction=DIRECTION_HBDM2VT[d["direction"]], - offset=OFFSET_HBDM2VT[d["offset"]], - traded=d["trade_volume"], - status=STATUS_HBDM2VT[d["status"]], - time=time, - gateway_name=self.gateway_name, - ) - self.gateway.on_order(order) - - self.gateway.write_log(f"{request.extra}历史委托信息查询成功") - - def on_query_trade(self, data, request): - """""" - if self.check_error(data, "查询成交"): - return - - for d in data["data"]["trades"]: - dt = datetime.fromtimestamp(d["create_date"] / 1000) - time = dt.strftime("%H:%M:%S") - - trade = TradeData( - tradeid=d["match_id"], - orderid=d["order_id"], - symbol=d["contract_code"], - exchange=Exchange.HUOBI, - price=d["trade_price"], - volume=d["trade_volume"], - direction=DIRECTION_HBDM2VT[d["direction"]], - offset=OFFSET_HBDM2VT[d["offset"]], - time=time, - gateway_name=self.gateway_name, - ) - self.gateway.on_trade(trade) - - self.gateway.write_log(f"{request.extra}成交信息查询成功") - def on_query_contract(self, data, request): # type: (dict, Request)->None """""" if self.check_error(data, "查询合约"): @@ -663,7 +574,6 @@ class HbdmRestApi(RestClient): self.gateway.write_log("合约信息查询成功") self.query_order() - self.query_trade() def on_send_order(self, data, request): """""" @@ -1041,7 +951,6 @@ class HbdmDataWebsocketApi(HbdmWebsocketApiBase): tick_data = data["tick"] if "bids" not in tick_data or "asks" not in tick_data: - print(data) return bids = tick_data["bids"] From 877c703e0ffc0eec784cd544f7f259768cfe095e Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 28 Oct 2019 14:43:34 +0800 Subject: [PATCH 3/5] [Add] support for FOK and IOC order of HBDM --- vnpy/gateway/hbdm/hbdm_gateway.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vnpy/gateway/hbdm/hbdm_gateway.py b/vnpy/gateway/hbdm/hbdm_gateway.py index e0a57d33..b055f32b 100644 --- a/vnpy/gateway/hbdm/hbdm_gateway.py +++ b/vnpy/gateway/hbdm/hbdm_gateway.py @@ -58,7 +58,9 @@ STATUS_HBDM2VT = { ORDERTYPE_VT2HBDM = { OrderType.MARKET: "opponent", - OrderType.LIMIT: "limit" + OrderType.LIMIT: "limit", + OrderType.FOK: "fok", + OrderType.FAK: "ioc" } ORDERTYPE_HBDM2VT = {v: k for k, v in ORDERTYPE_VT2HBDM.items()} ORDERTYPE_HBDM2VT[1] = OrderType.LIMIT From bacd74657f8a31bbb96d59e7d79a1f890e406581 Mon Sep 17 00:00:00 2001 From: lostleaf Date: Mon, 28 Oct 2019 19:51:24 +0800 Subject: [PATCH 4/5] =?UTF-8?q?[Fix]=20=E4=BF=AE=E5=A4=8D=20init=5Fcli=5Ft?= =?UTF-8?q?rading=20=E7=9A=84=20type=20hint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/script_trader/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vnpy/app/script_trader/cli.py b/vnpy/app/script_trader/cli.py index a68a76cb..b6935abf 100644 --- a/vnpy/app/script_trader/cli.py +++ b/vnpy/app/script_trader/cli.py @@ -1,4 +1,4 @@ -from typing import Sequence +from typing import Sequence, Type from vnpy.event import EventEngine, Event from vnpy.trader.engine import MainEngine @@ -14,7 +14,7 @@ def process_log_event(event: Event): print(f"{log.time}\t{log.msg}") -def init_cli_trading(gateways: Sequence[BaseGateway]): +def init_cli_trading(gateways: Sequence[Type[BaseGateway]]): """""" event_engine = EventEngine() event_engine.register(EVENT_LOG, process_log_event) From 766d705e7adf9a59d3134b363a0be3999e3b9f57 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 28 Oct 2019 22:14:25 +0800 Subject: [PATCH 5/5] [Mod] fix algo control bugs in BasicSpreadStrategy --- .../strategies/basic_spread_strategy.py | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/vnpy/app/spread_trading/strategies/basic_spread_strategy.py b/vnpy/app/spread_trading/strategies/basic_spread_strategy.py index b1213531..a72504e3 100644 --- a/vnpy/app/spread_trading/strategies/basic_spread_strategy.py +++ b/vnpy/app/spread_trading/strategies/basic_spread_strategy.py @@ -87,6 +87,8 @@ class BasicSpreadStrategy(SpreadStrategyTemplate): # No position if not self.spread_pos: + self.stop_close_algos() + # Start open algos if not self.buy_algoid: self.buy_algoid = self.start_long_algo( @@ -98,27 +100,20 @@ class BasicSpreadStrategy(SpreadStrategyTemplate): self.short_price, self.max_pos, self.payup, self.interval ) - # Stop close algos - if self.sell_algoid: - self.stop_algo(self.sell_algoid) - - if self.cover_algoid: - self.stop_algo(self.cover_algoid) - # Long position elif self.spread_pos > 0: + self.stop_open_algos() + # Start sell close algo if not self.sell_algoid: self.sell_algoid = self.start_short_algo( self.sell_price, self.spread_pos, self.payup, self.interval ) - # Stop buy open algo - if self.buy_algoid: - self.stop_algo(self.buy_algoid) - # Short position elif self.spread_pos < 0: + self.stop_open_algos() + # Start cover close algo if not self.cover_algoid: self.cover_algoid = self.start_long_algo( @@ -126,10 +121,6 @@ class BasicSpreadStrategy(SpreadStrategyTemplate): self.spread_pos), self.payup, self.interval ) - # Stop short open algo - if self.short_algoid: - self.stop_algo(self.short_algoid) - self.put_event() def on_spread_pos(self): @@ -166,3 +157,19 @@ class BasicSpreadStrategy(SpreadStrategyTemplate): Callback when new trade data is received. """ pass + + def stop_open_algos(self): + """""" + if self.buy_algoid: + self.stop_algo(self.buy_algoid) + + if self.short_algoid: + self.stop_algo(self.short_algoid) + + def stop_close_algos(self): + """""" + if self.sell_algoid: + self.stop_algo(self.sell_algoid) + + if self.cover_algoid: + self.stop_algo(self.cover_algoid)