Skip to content

Commit

Permalink
update the position.
Browse files Browse the repository at this point in the history
  • Loading branch information
lin committed Dec 6, 2020
1 parent 286173e commit 025ec1e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ folder by using the multi-threads for speeding.
V2.1.7.4版本对订单状态查询和更新,
特别是在在网络失去连接的时候能够进行查询和更新。

3.V2.1.7.5 : update the on_trade event and binance future api position
3. V2.1.7.5 : update the on_trade event and binance future api position
更新对on_trade交易事件的推送,websocket断开的时候订单的成交能够重新推送,并且能够计算策略的仓位。

4. V2.1.7.6 : update the position. 对订阅的交易对,都会推送其头寸的更新.

## 联系方式
微信: bitquant51
Expand Down
2 changes: 1 addition & 1 deletion howtrader/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "2.1.7.5"
__version__ = "2.1.7.6"
11 changes: 8 additions & 3 deletions howtrader/gateway/binances/binances_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def __init__(self, gateway: BinancesGateway):
self.gateway_name: str = gateway.gateway_name

self.trade_ws_api: BinancesTradeWebsocketApi = self.gateway.trade_ws_api
self.market_ws_api: BinancesDataWebsocketApi = self.gateway.market_ws_api

self.key: str = ""
self.secret: str = ""
Expand Down Expand Up @@ -348,7 +349,7 @@ def query_position(self) -> Request:
data = {"security": Security.SIGNED}

if self.usdt_base:
path = "/fapi/v1/positionRisk"
path = "/fapi/v2/positionRisk"
else:
path = "/dapi/v1/positionRisk"

Expand Down Expand Up @@ -562,7 +563,11 @@ def on_query_account(self, data: dict, request: Request) -> None:
self.gateway.write_log("账户资金查询成功")

def on_query_position(self, data: dict, request: Request) -> None:
""""""
"""
Query position.
"""
symbols = self.market_ws_api.ticks.keys()

for d in data:
position = PositionData(
symbol=d["symbol"],
Expand All @@ -574,7 +579,7 @@ def on_query_position(self, data: dict, request: Request) -> None:
gateway_name=self.gateway_name,
)

if position.volume:
if position.volume or d['symbol'].lower() in symbols:
self.gateway.on_position(position)

self.gateway.write_log("持仓信息查询成功")
Expand Down
19 changes: 10 additions & 9 deletions howtrader/trader/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,13 @@ def query_history(self, req: HistoryRequest, gateway_name: str) -> Optional[List
return gateway.query_history(req)
else:
return None
#
# def query_account_position(self):
# """
# query the account and position
# """
# for gateway in self.gateways.values():
# gateway.query_account()
# gateway.query_position()

def query_position(self):
"""
query the account and position
"""
for gateway in self.gateways.values():
gateway.query_position()

def close(self) -> None:
"""
Expand Down Expand Up @@ -433,7 +432,7 @@ def process_contract_event(self, event: Event) -> None:

def process_timer(self, event: Event) -> None:
"""
update the orders, positions, account by timer, for we may be disconnected from server update push.
update the orders, positions by timer, for we may be disconnected from server update push.
"""
if self.timer_count >= SETTINGS.get('order_update_timer', 120):
self.timer_count = 0
Expand All @@ -442,6 +441,8 @@ def process_timer(self, event: Event) -> None:
if order.datetime and (datetime.now(order.datetime.tzinfo) - order.datetime).seconds > SETTINGS.get('order_update_timer', 120):
req = order.create_query_request()
self.main_engine.query_order(req, order.gateway_name)

self.main_engine.query_position()
else:
self.timer_count += 1

Expand Down

0 comments on commit 025ec1e

Please sign in to comment.