Merge pull request #2164 from vnpy/master

[Fix] type error in init_cli_trading
This commit is contained in:
vn.py 2019-10-29 13:41:25 +08:00 committed by GitHub
commit 77fa4ae706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 112 deletions

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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"]

View File

@ -53,7 +53,7 @@ class RqdataClient:
self.username,
self.password,
('rqdatad-pro.ricequant.com', 16011),
use_pool=True
use_pool=True,
)
try: