[Add] query history order and trade data
This commit is contained in:
parent
6a64313fbf
commit
3b279c68eb
@ -57,7 +57,8 @@ ORDERTYPE_VT2HBDM = {
|
|||||||
OrderType.LIMIT: "limit",
|
OrderType.LIMIT: "limit",
|
||||||
}
|
}
|
||||||
ORDERTYPE_HBDM2VT = {v: k for k, v in ORDERTYPE_VT2HBDM.items()}
|
ORDERTYPE_HBDM2VT = {v: k for k, v in ORDERTYPE_VT2HBDM.items()}
|
||||||
|
ORDERTYPE_HBDM2VT[1] = OrderType.LIMIT
|
||||||
|
ORDERTYPE_HBDM2VT[3] = OrderType.MARKET
|
||||||
|
|
||||||
DIRECTION_VT2HBDM = {
|
DIRECTION_VT2HBDM = {
|
||||||
Direction.LONG: "buy",
|
Direction.LONG: "buy",
|
||||||
@ -174,7 +175,7 @@ class HbdmRestApi(RestClient):
|
|||||||
|
|
||||||
self.gateway = gateway
|
self.gateway = gateway
|
||||||
self.gateway_name = gateway.gateway_name
|
self.gateway_name = gateway.gateway_name
|
||||||
|
|
||||||
self.host = ""
|
self.host = ""
|
||||||
self.key = ""
|
self.key = ""
|
||||||
self.secret = ""
|
self.secret = ""
|
||||||
@ -254,12 +255,47 @@ class HbdmRestApi(RestClient):
|
|||||||
def query_order(self):
|
def query_order(self):
|
||||||
""""""
|
""""""
|
||||||
for currency in self.currencies:
|
for currency in self.currencies:
|
||||||
|
# Open Orders
|
||||||
data = {"symbol": currency}
|
data = {"symbol": currency}
|
||||||
|
|
||||||
self.add_request(
|
self.add_request(
|
||||||
method="POST",
|
method="POST",
|
||||||
path="/api/v1/contract_openorders",
|
path="/api/v1/contract_openorders",
|
||||||
callback=self.on_query_order,
|
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,
|
||||||
data=data,
|
data=data,
|
||||||
extra=currency
|
extra=currency
|
||||||
)
|
)
|
||||||
@ -271,7 +307,7 @@ class HbdmRestApi(RestClient):
|
|||||||
path="/api/v1/contract_contract_info",
|
path="/api/v1/contract_contract_info",
|
||||||
callback=self.on_query_contract
|
callback=self.on_query_contract
|
||||||
)
|
)
|
||||||
|
|
||||||
def new_local_orderid(self):
|
def new_local_orderid(self):
|
||||||
""""""
|
""""""
|
||||||
with self.order_count_lock:
|
with self.order_count_lock:
|
||||||
@ -320,7 +356,7 @@ class HbdmRestApi(RestClient):
|
|||||||
|
|
||||||
for req in reqs:
|
for req in reqs:
|
||||||
local_orderid = self.new_local_orderid()
|
local_orderid = self.new_local_orderid()
|
||||||
|
|
||||||
order = req.create_order_data(
|
order = req.create_order_data(
|
||||||
local_orderid,
|
local_orderid,
|
||||||
self.gateway_name
|
self.gateway_name
|
||||||
@ -412,7 +448,7 @@ class HbdmRestApi(RestClient):
|
|||||||
for d in data["data"]:
|
for d in data["data"]:
|
||||||
key = f"{d['contract_code']}_{d['direction']}"
|
key = f"{d['contract_code']}_{d['direction']}"
|
||||||
position = self.positions.get(key, None)
|
position = self.positions.get(key, None)
|
||||||
|
|
||||||
if not position:
|
if not position:
|
||||||
position = PositionData(
|
position = PositionData(
|
||||||
symbol=d["contract_code"],
|
symbol=d["contract_code"],
|
||||||
@ -426,17 +462,18 @@ class HbdmRestApi(RestClient):
|
|||||||
position.frozen = d["frozen"]
|
position.frozen = d["frozen"]
|
||||||
position.price = d["cost_hold"]
|
position.price = d["cost_hold"]
|
||||||
position.pnl = d["profit"]
|
position.pnl = d["profit"]
|
||||||
|
|
||||||
for position in self.positions.values():
|
for position in self.positions.values():
|
||||||
self.gateway.on_position(position)
|
self.gateway.on_position(position)
|
||||||
|
|
||||||
def on_query_order(self, data, request):
|
def on_query_active_order(self, data, request):
|
||||||
""""""
|
""""""
|
||||||
if self.check_error(data, "查询委托"):
|
if self.check_error(data, "查询活动委托"):
|
||||||
return
|
return
|
||||||
|
|
||||||
for d in data["data"]["orders"]:
|
for d in data["data"]["orders"]:
|
||||||
dt = datetime.fromtimestamp(d["created_at"] / 1000)
|
timestamp = d["created_at"]
|
||||||
|
dt = datetime.fromtimestamp(timestamp / 1000)
|
||||||
time = dt.strftime("%H:%M:%S")
|
time = dt.strftime("%H:%M:%S")
|
||||||
|
|
||||||
if d["client_order_id"]:
|
if d["client_order_id"]:
|
||||||
@ -460,7 +497,62 @@ class HbdmRestApi(RestClient):
|
|||||||
)
|
)
|
||||||
self.gateway.on_order(order)
|
self.gateway.on_order(order)
|
||||||
|
|
||||||
self.gateway.write_log(f"{request.extra}委托信息查询成功")
|
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
|
def on_query_contract(self, data, request): # type: (dict, Request)->None
|
||||||
""""""
|
""""""
|
||||||
@ -487,6 +579,7 @@ class HbdmRestApi(RestClient):
|
|||||||
self.gateway.write_log("合约信息查询成功")
|
self.gateway.write_log("合约信息查询成功")
|
||||||
|
|
||||||
self.query_order()
|
self.query_order()
|
||||||
|
self.query_trade()
|
||||||
|
|
||||||
def on_send_order(self, data, request):
|
def on_send_order(self, data, request):
|
||||||
""""""
|
""""""
|
||||||
|
Loading…
Reference in New Issue
Block a user