2018-11-17 20:16:32 +00:00
|
|
|
# pragma pylint: disable=W0603
|
|
|
|
""" Wallet """
|
2019-02-28 22:26:29 +00:00
|
|
|
|
2018-11-17 20:16:32 +00:00
|
|
|
import logging
|
2020-01-15 05:42:53 +00:00
|
|
|
from typing import Any, Dict, NamedTuple
|
|
|
|
|
|
|
|
import arrow
|
|
|
|
|
2018-11-17 20:16:32 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2019-12-15 08:48:35 +00:00
|
|
|
from freqtrade.persistence import Trade
|
2018-11-17 20:16:32 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-12-29 08:01:58 +00:00
|
|
|
# wallet data structure
|
2018-11-21 18:47:51 +00:00
|
|
|
class Wallet(NamedTuple):
|
2018-11-21 22:35:44 +00:00
|
|
|
currency: str
|
2018-11-21 18:47:51 +00:00
|
|
|
free: float = 0
|
|
|
|
used: float = 0
|
|
|
|
total: float = 0
|
|
|
|
|
|
|
|
|
2019-09-12 01:39:52 +00:00
|
|
|
class Wallets:
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2019-02-28 22:26:29 +00:00
|
|
|
def __init__(self, config: dict, exchange: Exchange) -> None:
|
|
|
|
self._config = config
|
|
|
|
self._exchange = exchange
|
|
|
|
self._wallets: Dict[str, Wallet] = {}
|
2019-12-15 08:48:35 +00:00
|
|
|
self.start_cap = config['dry_run_wallet']
|
2020-01-15 05:42:53 +00:00
|
|
|
self._last_wallet_refresh = 0
|
2018-11-24 15:37:28 +00:00
|
|
|
self.update()
|
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def get_free(self, currency: str) -> float:
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-26 01:18:29 +00:00
|
|
|
if balance and balance.free:
|
|
|
|
return balance.free
|
2018-11-23 09:17:10 +00:00
|
|
|
else:
|
2018-11-24 15:37:28 +00:00
|
|
|
return 0
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def get_used(self, currency: str) -> float:
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-28 14:36:32 +00:00
|
|
|
if balance and balance.used:
|
|
|
|
return balance.used
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2020-02-02 04:00:40 +00:00
|
|
|
def get_total(self, currency: str) -> float:
|
2019-02-28 22:26:29 +00:00
|
|
|
balance = self._wallets.get(currency)
|
2018-11-28 14:36:32 +00:00
|
|
|
if balance and balance.total:
|
|
|
|
return balance.total
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2019-12-15 08:48:35 +00:00
|
|
|
def _update_dry(self) -> None:
|
2019-12-15 10:03:40 +00:00
|
|
|
"""
|
|
|
|
Update from database in dry-run mode
|
|
|
|
- Apply apply profits of closed trades on top of stake amount
|
|
|
|
- Subtract currently tied up stake_amount in open trades
|
|
|
|
- update balances for currencies currently in trades
|
|
|
|
"""
|
2019-12-24 05:27:11 +00:00
|
|
|
# Recreate _wallets to reset closed trade balances
|
|
|
|
_wallets = {}
|
2019-12-15 08:48:35 +00:00
|
|
|
closed_trades = Trade.get_trades(Trade.is_open.is_(False)).all()
|
2019-12-15 09:26:56 +00:00
|
|
|
open_trades = Trade.get_trades(Trade.is_open.is_(True)).all()
|
2019-12-15 08:48:35 +00:00
|
|
|
tot_profit = sum([trade.calc_profit() for trade in closed_trades])
|
2019-12-15 09:26:56 +00:00
|
|
|
tot_in_trades = sum([trade.stake_amount for trade in open_trades])
|
2019-12-15 08:48:35 +00:00
|
|
|
|
2019-12-15 09:26:56 +00:00
|
|
|
current_stake = self.start_cap + tot_profit - tot_in_trades
|
2019-12-24 05:27:11 +00:00
|
|
|
_wallets[self._config['stake_currency']] = Wallet(
|
2019-12-15 08:48:35 +00:00
|
|
|
self._config['stake_currency'],
|
|
|
|
current_stake,
|
|
|
|
0,
|
|
|
|
current_stake
|
|
|
|
)
|
|
|
|
|
|
|
|
for trade in open_trades:
|
|
|
|
curr = trade.pair.split('/')[0]
|
2019-12-24 05:27:11 +00:00
|
|
|
_wallets[curr] = Wallet(
|
2019-12-15 08:48:35 +00:00
|
|
|
curr,
|
|
|
|
trade.amount,
|
|
|
|
0,
|
|
|
|
trade.amount
|
|
|
|
)
|
2019-12-24 05:27:11 +00:00
|
|
|
self._wallets = _wallets
|
2019-12-15 08:48:35 +00:00
|
|
|
|
|
|
|
def _update_live(self) -> None:
|
2019-02-28 22:26:29 +00:00
|
|
|
balances = self._exchange.get_balances()
|
2018-11-17 20:16:32 +00:00
|
|
|
|
|
|
|
for currency in balances:
|
2019-02-28 22:26:29 +00:00
|
|
|
self._wallets[currency] = Wallet(
|
2018-11-21 18:47:51 +00:00
|
|
|
currency,
|
|
|
|
balances[currency].get('free', None),
|
|
|
|
balances[currency].get('used', None),
|
|
|
|
balances[currency].get('total', None)
|
|
|
|
)
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2020-01-15 05:42:53 +00:00
|
|
|
def update(self, require_update: bool = True) -> None:
|
|
|
|
"""
|
|
|
|
Updates wallets from the configured version.
|
|
|
|
By default, updates from the exchange.
|
|
|
|
Update-skipping should only be used for user-invoked /balance calls, since
|
|
|
|
for trading operations, the latest balance is needed.
|
|
|
|
:param require_update: Allow skipping an update if balances were recently refreshed
|
|
|
|
"""
|
|
|
|
if (require_update or (self._last_wallet_refresh + 3600 < arrow.utcnow().timestamp)):
|
|
|
|
if self._config['dry_run']:
|
|
|
|
self._update_dry()
|
|
|
|
else:
|
|
|
|
self._update_live()
|
|
|
|
logger.info('Wallets synced.')
|
|
|
|
self._last_wallet_refresh = arrow.utcnow().timestamp
|
2019-11-24 18:41:51 +00:00
|
|
|
|
|
|
|
def get_all_balances(self) -> Dict[str, Any]:
|
|
|
|
return self._wallets
|