Add dry-run position wallet calculation

This commit is contained in:
Matthias 2022-02-22 19:14:46 +01:00
parent 6562511137
commit 13e74c5693

View File

@ -8,7 +8,7 @@ from typing import Dict, NamedTuple, Optional
import arrow import arrow
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT from freqtrade.constants import UNLIMITED_STAKE_AMOUNT
from freqtrade.enums import RunMode from freqtrade.enums import RunMode, TradingMode
from freqtrade.exceptions import DependencyException from freqtrade.exceptions import DependencyException
from freqtrade.exchange import Exchange from freqtrade.exchange import Exchange
from freqtrade.persistence import LocalTrade, Trade from freqtrade.persistence import LocalTrade, Trade
@ -23,7 +23,6 @@ class Wallet(NamedTuple):
free: float = 0 free: float = 0
used: float = 0 used: float = 0
total: float = 0 total: float = 0
position: float = 0
class PositionWallet(NamedTuple): class PositionWallet(NamedTuple):
@ -76,6 +75,7 @@ class Wallets:
""" """
# Recreate _wallets to reset closed trade balances # Recreate _wallets to reset closed trade balances
_wallets = {} _wallets = {}
_positions = {}
open_trades = Trade.get_trades_proxy(is_open=True) open_trades = Trade.get_trades_proxy(is_open=True)
# If not backtesting... # If not backtesting...
# TODO: potentially remove the ._log workaround to determine backtest mode. # TODO: potentially remove the ._log workaround to determine backtest mode.
@ -84,15 +84,11 @@ class Wallets:
else: else:
tot_profit = LocalTrade.total_profit tot_profit = LocalTrade.total_profit
tot_in_trades = sum(trade.stake_amount for trade in open_trades) tot_in_trades = sum(trade.stake_amount for trade in open_trades)
used_stake = 0.0
if self._config.get('trading_mode', 'spot') != TradingMode.FUTURES:
current_stake = self.start_cap + tot_profit - tot_in_trades current_stake = self.start_cap + tot_profit - tot_in_trades
_wallets[self._config['stake_currency']] = Wallet( total_stake = current_stake
self._config['stake_currency'],
current_stake,
0,
current_stake
)
for trade in open_trades: for trade in open_trades:
curr = self._exchange.get_pair_base_currency(trade.pair) curr = self._exchange.get_pair_base_currency(trade.pair)
_wallets[curr] = Wallet( _wallets[curr] = Wallet(
@ -101,7 +97,33 @@ class Wallets:
0, 0,
trade.amount trade.amount
) )
else:
tot_in_trades = 0
for position in open_trades:
# size = self._exchange._contracts_to_amount(position.pair, position['contracts'])
size = position.amount
# TODO-lev: stake_amount in real trades does not include the leverage ...
collateral = position.stake_amount / position.leverage
leverage = position.leverage
tot_in_trades -= collateral
_positions[position.pair] = PositionWallet(
position.pair, position=size,
leverage=leverage,
collateral=collateral,
side=position.trade_direction
)
current_stake = self.start_cap + tot_profit
used_stake = tot_in_trades
total_stake = current_stake - tot_in_trades
_wallets[self._config['stake_currency']] = Wallet(
currency=self._config['stake_currency'],
free=current_stake,
used=used_stake,
total=total_stake
)
self._wallets = _wallets self._wallets = _wallets
self._positions = _positions
def _update_live(self) -> None: def _update_live(self) -> None:
balances = self._exchange.get_balances() balances = self._exchange.get_balances()
@ -121,7 +143,7 @@ class Wallets:
# TODO-lev: Implement dry-run/backtest counterpart # TODO-lev: Implement dry-run/backtest counterpart
positions = self._exchange.get_positions() positions = self._exchange.get_positions()
self._positions = [] self._positions = {}
for position in positions: for position in positions:
symbol = position['symbol'] symbol = position['symbol']
if position['side'] is None or position['collateral'] == 0.0: if position['side'] is None or position['collateral'] == 0.0: