add get_position exchange wrapper
This commit is contained in:
parent
9f4f65e457
commit
ed65692257
@ -1158,44 +1158,6 @@ class Exchange:
|
||||
balances.pop("total", None)
|
||||
balances.pop("used", None)
|
||||
|
||||
if self.trading_mode == TradingMode.FUTURES:
|
||||
|
||||
open_orders_response: List[dict] = self._api.fetch_open_orders()
|
||||
open_orders: dict = {}
|
||||
for order in open_orders_response:
|
||||
symbol: str = order['symbol']
|
||||
open_orders[symbol] = order
|
||||
|
||||
positions: List[dict] = self._api.fetch_positions()
|
||||
for position in positions:
|
||||
symbol = position['symbol']
|
||||
market: dict = self.markets[symbol]
|
||||
size: float = self._contracts_to_amount(symbol, position['contracts'])
|
||||
side: str = position['side']
|
||||
if size > 0:
|
||||
|
||||
if symbol in open_orders:
|
||||
order = open_orders[symbol]
|
||||
order_amount: float = order['remaining']
|
||||
order_side: str = order['side']
|
||||
if order_side == 'buy' or order_side == 'long':
|
||||
order_amount = 0
|
||||
else:
|
||||
order_amount = 0
|
||||
|
||||
if side == 'long' or side == 'buy':
|
||||
currency = market['base']
|
||||
free = size - order_amount
|
||||
|
||||
balances[currency] = {
|
||||
'free': free,
|
||||
'used': order_amount,
|
||||
'total': size,
|
||||
}
|
||||
balances['free'][currency] = free
|
||||
balances['used'][currency] = order_amount
|
||||
balances['total'][currency] = size
|
||||
|
||||
return balances
|
||||
except ccxt.DDoSProtection as e:
|
||||
raise DDosProtection(e) from e
|
||||
@ -1205,6 +1167,22 @@ class Exchange:
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e) from e
|
||||
|
||||
@retrier
|
||||
def get_positions(self) -> List[Dict]:
|
||||
if self._config['dry_run'] or self.trading_mode != TradingMode.FUTURES:
|
||||
return []
|
||||
try:
|
||||
positions: List[Dict] = self._api.fetch_positions()
|
||||
self._log_exchange_response('fetch_positions', positions)
|
||||
return positions
|
||||
except ccxt.DDoSProtection as e:
|
||||
raise DDosProtection(e) from e
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
f'Could not get positions due to {e.__class__.__name__}. Message: {e}') from e
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e) from e
|
||||
|
||||
@retrier
|
||||
def get_tickers(self, symbols: List[str] = None, cached: bool = False) -> Dict:
|
||||
"""
|
||||
|
@ -23,6 +23,7 @@ class Wallet(NamedTuple):
|
||||
free: float = 0
|
||||
used: float = 0
|
||||
total: float = 0
|
||||
position: float = 0
|
||||
|
||||
|
||||
class Wallets:
|
||||
@ -108,6 +109,18 @@ class Wallets:
|
||||
for currency in deepcopy(self._wallets):
|
||||
if currency not in balances:
|
||||
del self._wallets[currency]
|
||||
# TODO-lev: Implement dry-run/backtest counterpart
|
||||
positions = self._exchange.get_positions()
|
||||
for position in positions:
|
||||
symbol = position['symbol']
|
||||
if position['side'] is None:
|
||||
# Position is not open ...
|
||||
continue
|
||||
size = self._exchange._contracts_to_amount(symbol, position['contracts'])
|
||||
|
||||
self._wallets[symbol] = Wallet(
|
||||
symbol, position=size
|
||||
)
|
||||
|
||||
def update(self, require_update: bool = True) -> None:
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user