diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index fbd64f04..c4622fc2 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -91,7 +91,7 @@ class CtaEngine(BaseEngine): self.stop_order_count = 0 # for generating stop_orderid self.stop_orders = {} # stop_orderid: stop_order - self.init_executor = ThreadPoolExecutor(max_workers=1) + self.init_executor = ThreadPoolExecutor(max_workers=3) self.rq_client = None self.rq_symbols = set() 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) 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) diff --git a/vnpy/gateway/hbdm/hbdm_gateway.py b/vnpy/gateway/hbdm/hbdm_gateway.py index b8de5b76..b055f32b 100644 --- a/vnpy/gateway/hbdm/hbdm_gateway.py +++ b/vnpy/gateway/hbdm/hbdm_gateway.py @@ -59,6 +59,8 @@ STATUS_HBDM2VT = { ORDERTYPE_VT2HBDM = { OrderType.MARKET: "opponent", 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 @@ -284,41 +286,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 +517,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 +550,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 +576,6 @@ class HbdmRestApi(RestClient): self.gateway.write_log("合约信息查询成功") self.query_order() - self.query_trade() def on_send_order(self, data, request): """""" @@ -1041,7 +953,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"] diff --git a/vnpy/trader/rqdata.py b/vnpy/trader/rqdata.py index 04fd17fe..539ecf87 100644 --- a/vnpy/trader/rqdata.py +++ b/vnpy/trader/rqdata.py @@ -53,7 +53,7 @@ class RqdataClient: self.username, self.password, ('rqdatad-pro.ricequant.com', 16011), - use_pool=True + use_pool=True, ) try: