mirror of
https://github.com/freqtrade/freqtrade.git
synced 2026-01-19 21:40:24 +00:00
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
|
||||
if dry_run:
|
||||
# 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()
|
||||
for t in open_trades:
|
||||
# TODO: This should be done in a batch update
|
||||
|
||||
@@ -73,6 +73,18 @@ class Wallets:
|
||||
else:
|
||||
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:
|
||||
"""
|
||||
Get currently owned value.
|
||||
@@ -139,6 +151,8 @@ class Wallets:
|
||||
cross_margin = 0.0
|
||||
if self._config.get("margin_mode") == "cross":
|
||||
# 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():
|
||||
if curr == self._stake_currency:
|
||||
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 wallets.get_total.call_count == (
|
||||
assert wallets.get_collateral.call_count == (
|
||||
0 if margin_mode == MarginMode.ISOLATED or not dry_run else 1
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user