feat: improve logic for liquidation price calc
This commit is contained in:
@@ -27,9 +27,12 @@ def update_liquidation_prices(
|
|||||||
total_wallet_stake = 0.0
|
total_wallet_stake = 0.0
|
||||||
if dry_run:
|
if dry_run:
|
||||||
# Parameters only needed for cross margin
|
# Parameters only needed for cross margin
|
||||||
total_wallet_stake = wallets.get_total(stake_currency)
|
total_wallet_stake = wallets.get_collateral()
|
||||||
|
|
||||||
logger.info("Updating liquidation price for all open trades.")
|
logger.info(
|
||||||
|
"Updating liquidation price for all open trades. "
|
||||||
|
f"Collateral {total_wallet_stake} {stake_currency}."
|
||||||
|
)
|
||||||
open_trades = Trade.get_open_trades()
|
open_trades = Trade.get_open_trades()
|
||||||
for t in open_trades:
|
for t in open_trades:
|
||||||
# TODO: This should be done in a batch update
|
# TODO: This should be done in a batch update
|
||||||
|
|||||||
@@ -73,6 +73,18 @@ class Wallets:
|
|||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def get_collateral(self) -> float:
|
||||||
|
"""
|
||||||
|
Get total collateral for liquidation price calculation.
|
||||||
|
"""
|
||||||
|
if self._config.get("margin_mode") == "cross":
|
||||||
|
# free includes all balances and, combined with position collateral,
|
||||||
|
# is used as "wallet balance".
|
||||||
|
return self.get_free(self._stake_currency) + sum(
|
||||||
|
pos.collateral for pos in self._positions.values()
|
||||||
|
)
|
||||||
|
return self.get_total(self._stake_currency)
|
||||||
|
|
||||||
def get_owned(self, pair: str, base_currency: str) -> float:
|
def get_owned(self, pair: str, base_currency: str) -> float:
|
||||||
"""
|
"""
|
||||||
Get currently owned value.
|
Get currently owned value.
|
||||||
@@ -139,6 +151,8 @@ class Wallets:
|
|||||||
cross_margin = 0.0
|
cross_margin = 0.0
|
||||||
if self._config.get("margin_mode") == "cross":
|
if self._config.get("margin_mode") == "cross":
|
||||||
# In cross-margin mode, the total balance is used as collateral.
|
# In cross-margin mode, the total balance is used as collateral.
|
||||||
|
# This is moved as "free" into the stake currency balance.
|
||||||
|
# strongly tied to the get_collateral() implementation.
|
||||||
for curr, bal in self._start_cap.items():
|
for curr, bal in self._start_cap.items():
|
||||||
if curr == self._stake_currency:
|
if curr == self._stake_currency:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ def test_update_liquidation_prices(mocker, margin_mode, dry_run):
|
|||||||
|
|
||||||
assert trade_mock.set_liquidation_price.call_count == 1
|
assert trade_mock.set_liquidation_price.call_count == 1
|
||||||
|
|
||||||
assert wallets.get_total.call_count == (
|
assert wallets.get_collateral.call_count == (
|
||||||
0 if margin_mode == MarginMode.ISOLATED or not dry_run else 1
|
0 if margin_mode == MarginMode.ISOLATED or not dry_run else 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user