2018-11-17 20:16:32 +00:00
|
|
|
# pragma pylint: disable=W0603
|
2024-05-12 14:32:47 +00:00
|
|
|
"""Wallet"""
|
2019-02-28 22:26:29 +00:00
|
|
|
|
2018-11-17 20:16:32 +00:00
|
|
|
import logging
|
2020-09-22 17:37:31 +00:00
|
|
|
from copy import deepcopy
|
2023-05-14 08:37:16 +00:00
|
|
|
from datetime import datetime, timedelta
|
2022-02-19 09:58:17 +00:00
|
|
|
from typing import Dict, NamedTuple, Optional
|
2020-01-15 05:42:53 +00:00
|
|
|
|
2023-12-12 21:43:46 +00:00
|
|
|
from freqtrade.constants import UNLIMITED_STAKE_AMOUNT, Config, IntOrInf
|
2022-02-22 18:14:46 +00:00
|
|
|
from freqtrade.enums import RunMode, TradingMode
|
2021-02-03 19:00:33 +00:00
|
|
|
from freqtrade.exceptions import DependencyException
|
2018-11-17 20:16:32 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2023-04-24 10:13:24 +00:00
|
|
|
from freqtrade.misc import safe_value_fallback
|
2021-03-13 09:16:32 +00:00
|
|
|
from freqtrade.persistence import LocalTrade, Trade
|
2023-05-14 08:37:16 +00:00
|
|
|
from freqtrade.util.datetime_helpers import dt_now
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-02-19 09:58:17 +00:00
|
|
|
class PositionWallet(NamedTuple):
|
|
|
|
symbol: str
|
|
|
|
position: float = 0
|
2024-08-15 14:59:30 +00:00
|
|
|
leverage: Optional[float] = 0 # Don't use this - it's not guaranteed to be set
|
2022-02-19 09:58:17 +00:00
|
|
|
collateral: float = 0
|
2024-05-12 14:32:47 +00:00
|
|
|
side: str = "long"
|
2022-02-19 09:58:17 +00:00
|
|
|
|
|
|
|
|
2019-09-12 01:39:52 +00:00
|
|
|
class Wallets:
|
2024-02-25 08:07:53 +00:00
|
|
|
def __init__(self, config: Config, exchange: Exchange, is_backtest: bool = False) -> None:
|
2019-02-28 22:26:29 +00:00
|
|
|
self._config = config
|
2024-02-25 08:07:53 +00:00
|
|
|
self._is_backtest = is_backtest
|
2019-02-28 22:26:29 +00:00
|
|
|
self._exchange = exchange
|
|
|
|
self._wallets: Dict[str, Wallet] = {}
|
2022-02-19 09:58:17 +00:00
|
|
|
self._positions: Dict[str, PositionWallet] = {}
|
2024-05-12 14:32:47 +00:00
|
|
|
self.start_cap = config["dry_run_wallet"]
|
2023-05-14 08:37:16 +00:00
|
|
|
self._last_wallet_refresh: Optional[datetime] = None
|
2021-02-27 09:31:21 +00:00
|
|
|
self.update()
|
2018-11-24 15:37:28 +00:00
|
|
|
|
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
|
|
|
|
|
2024-08-14 19:18:47 +00:00
|
|
|
def get_owned(self, pair: str, base_currency: str) -> float:
|
|
|
|
"""
|
|
|
|
Get currently owned value.
|
|
|
|
Designed to work across both spot and futures.
|
|
|
|
"""
|
|
|
|
if self._config.get("trading_mode", "spot") != TradingMode.FUTURES:
|
|
|
|
return self.get_total(base_currency) or 0
|
|
|
|
if pos := self._positions.get(pair):
|
|
|
|
return pos.position
|
|
|
|
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
|
2024-04-28 13:16:45 +00:00
|
|
|
- Apply profits of closed trades on top of stake amount
|
2019-12-15 10:03:40 +00:00
|
|
|
- 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 = {}
|
2022-02-22 18:14:46 +00:00
|
|
|
_positions = {}
|
2021-01-28 05:37:42 +00:00
|
|
|
open_trades = Trade.get_trades_proxy(is_open=True)
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
|
|
|
# Live / Dry-run mode
|
2021-07-10 10:18:55 +00:00
|
|
|
tot_profit = Trade.get_total_closed_profit()
|
2021-03-13 09:16:32 +00:00
|
|
|
else:
|
2024-02-25 08:07:53 +00:00
|
|
|
# Backtest mode
|
2024-08-12 08:38:57 +00:00
|
|
|
tot_profit = LocalTrade.bt_total_profit
|
2023-07-15 14:56:05 +00:00
|
|
|
tot_profit += sum(trade.realized_profit for trade in open_trades)
|
2021-11-11 11:06:18 +00:00
|
|
|
tot_in_trades = sum(trade.stake_amount for trade in open_trades)
|
2022-02-22 18:14:46 +00:00
|
|
|
used_stake = 0.0
|
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
if self._config.get("trading_mode", "spot") != TradingMode.FUTURES:
|
2022-02-22 18:14:46 +00:00
|
|
|
for trade in open_trades:
|
|
|
|
curr = self._exchange.get_pair_base_currency(trade.pair)
|
2024-09-22 11:17:27 +00:00
|
|
|
used_stake += sum(
|
|
|
|
o.stake_amount for o in trade.open_orders if o.ft_order_side == trade.entry_side
|
|
|
|
)
|
|
|
|
pending = sum(
|
|
|
|
o.amount
|
|
|
|
for o in trade.open_orders
|
|
|
|
if o.amount and o.ft_order_side == trade.exit_side
|
|
|
|
)
|
|
|
|
|
|
|
|
_wallets[curr] = Wallet(curr, trade.amount - pending, pending, trade.amount)
|
|
|
|
|
|
|
|
current_stake = self.start_cap + tot_profit - tot_in_trades
|
|
|
|
total_stake = current_stake + used_stake
|
2022-02-22 18:14:46 +00:00
|
|
|
else:
|
|
|
|
tot_in_trades = 0
|
|
|
|
for position in open_trades:
|
|
|
|
# size = self._exchange._contracts_to_amount(position.pair, position['contracts'])
|
|
|
|
size = position.amount
|
2022-03-05 14:53:40 +00:00
|
|
|
collateral = position.stake_amount
|
2022-02-22 18:14:46 +00:00
|
|
|
leverage = position.leverage
|
2022-03-09 18:02:22 +00:00
|
|
|
tot_in_trades += collateral
|
2022-02-22 18:14:46 +00:00
|
|
|
_positions[position.pair] = PositionWallet(
|
2024-05-12 14:32:47 +00:00
|
|
|
position.pair,
|
|
|
|
position=size,
|
2022-02-22 18:14:46 +00:00
|
|
|
leverage=leverage,
|
|
|
|
collateral=collateral,
|
2024-05-12 14:32:47 +00:00
|
|
|
side=position.trade_direction,
|
2022-02-22 18:14:46 +00:00
|
|
|
)
|
2022-03-09 18:02:22 +00:00
|
|
|
current_stake = self.start_cap + tot_profit - tot_in_trades
|
2022-02-22 18:14:46 +00:00
|
|
|
used_stake = tot_in_trades
|
2022-03-09 18:02:22 +00:00
|
|
|
total_stake = current_stake + tot_in_trades
|
2019-12-15 08:48:35 +00:00
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
_wallets[self._config["stake_currency"]] = Wallet(
|
|
|
|
currency=self._config["stake_currency"],
|
2022-02-22 18:14:46 +00:00
|
|
|
free=current_stake,
|
|
|
|
used=used_stake,
|
2024-05-12 14:32:47 +00:00
|
|
|
total=total_stake,
|
2019-12-15 08:48:35 +00:00
|
|
|
)
|
2019-12-24 05:27:11 +00:00
|
|
|
self._wallets = _wallets
|
2022-02-22 18:14:46 +00:00
|
|
|
self._positions = _positions
|
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:
|
2021-04-26 12:12:52 +00:00
|
|
|
if isinstance(balances[currency], dict):
|
|
|
|
self._wallets[currency] = Wallet(
|
|
|
|
currency,
|
2024-08-15 05:29:19 +00:00
|
|
|
balances[currency].get("free", 0),
|
|
|
|
balances[currency].get("used", 0),
|
|
|
|
balances[currency].get("total", 0),
|
2021-04-26 12:12:52 +00:00
|
|
|
)
|
2020-09-22 17:37:31 +00:00
|
|
|
# Remove currencies no longer in get_balances output
|
|
|
|
for currency in deepcopy(self._wallets):
|
|
|
|
if currency not in balances:
|
|
|
|
del self._wallets[currency]
|
2022-02-19 15:28:51 +00:00
|
|
|
|
2022-02-23 18:19:42 +00:00
|
|
|
positions = self._exchange.fetch_positions()
|
2024-08-15 13:44:59 +00:00
|
|
|
_parsed_positions = {}
|
2022-02-18 06:00:39 +00:00
|
|
|
for position in positions:
|
2024-05-12 14:32:47 +00:00
|
|
|
symbol = position["symbol"]
|
|
|
|
if position["side"] is None or position["collateral"] == 0.0:
|
2022-02-18 06:00:39 +00:00
|
|
|
# Position is not open ...
|
|
|
|
continue
|
2024-05-12 14:32:47 +00:00
|
|
|
size = self._exchange._contracts_to_amount(symbol, position["contracts"])
|
|
|
|
collateral = safe_value_fallback(position, "collateral", "initialMargin", 0.0)
|
2024-08-15 14:59:30 +00:00
|
|
|
leverage = position.get("leverage")
|
2024-08-15 13:44:59 +00:00
|
|
|
_parsed_positions[symbol] = PositionWallet(
|
2024-05-12 14:32:47 +00:00
|
|
|
symbol,
|
|
|
|
position=size,
|
2022-02-19 09:58:17 +00:00
|
|
|
leverage=leverage,
|
|
|
|
collateral=collateral,
|
2024-05-12 14:32:47 +00:00
|
|
|
side=position["side"],
|
2022-02-18 06:00:39 +00:00
|
|
|
)
|
2024-08-15 13:44:59 +00:00
|
|
|
self._positions = _parsed_positions
|
2018-11-17 20:16:32 +00:00
|
|
|
|
2021-02-10 18:23:11 +00:00
|
|
|
def update(self, require_update: bool = True) -> None:
|
2020-01-15 05:42:53 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2023-05-14 08:37:16 +00:00
|
|
|
now = dt_now()
|
|
|
|
if (
|
|
|
|
require_update
|
|
|
|
or self._last_wallet_refresh is None
|
|
|
|
or (self._last_wallet_refresh + timedelta(seconds=3600) < now)
|
|
|
|
):
|
2024-05-12 14:32:47 +00:00
|
|
|
if not self._config["dry_run"] or self._config.get("runmode") == RunMode.LIVE:
|
2020-01-15 05:42:53 +00:00
|
|
|
self._update_live()
|
2021-01-28 05:37:42 +00:00
|
|
|
else:
|
|
|
|
self._update_dry()
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
2024-05-12 14:32:47 +00:00
|
|
|
logger.info("Wallets synced.")
|
2023-05-14 08:37:16 +00:00
|
|
|
self._last_wallet_refresh = dt_now()
|
2019-11-24 18:41:51 +00:00
|
|
|
|
2022-02-19 09:58:17 +00:00
|
|
|
def get_all_balances(self) -> Dict[str, Wallet]:
|
2019-11-24 18:41:51 +00:00
|
|
|
return self._wallets
|
2021-02-03 19:00:33 +00:00
|
|
|
|
2022-02-19 09:58:17 +00:00
|
|
|
def get_all_positions(self) -> Dict[str, PositionWallet]:
|
|
|
|
return self._positions
|
|
|
|
|
2023-04-25 15:46:58 +00:00
|
|
|
def _check_exit_amount(self, trade: Trade) -> bool:
|
|
|
|
if trade.trading_mode != TradingMode.FUTURES:
|
|
|
|
# Slightly higher offset than in safe_exit_amount.
|
2023-04-26 04:45:09 +00:00
|
|
|
wallet_amount: float = self.get_total(trade.safe_base_currency) * (2 - 0.981)
|
2023-04-25 15:46:58 +00:00
|
|
|
else:
|
|
|
|
# wallet_amount: float = self.wallets.get_free(trade.safe_base_currency)
|
|
|
|
position = self._positions.get(trade.pair)
|
|
|
|
if position is None:
|
|
|
|
# We don't own anything :O
|
|
|
|
return False
|
|
|
|
wallet_amount = position.position
|
|
|
|
|
|
|
|
if wallet_amount >= trade.amount:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def check_exit_amount(self, trade: Trade) -> bool:
|
|
|
|
"""
|
|
|
|
Checks if the exit amount is available in the wallet.
|
|
|
|
:param trade: Trade to check
|
|
|
|
:return: True if the exit amount is available, False otherwise
|
|
|
|
"""
|
|
|
|
if not self._check_exit_amount(trade):
|
|
|
|
# Update wallets just to make sure
|
|
|
|
self.update()
|
|
|
|
return self._check_exit_amount(trade)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2021-07-14 18:55:11 +00:00
|
|
|
def get_starting_balance(self) -> float:
|
|
|
|
"""
|
|
|
|
Retrieves starting balance - based on either available capital,
|
|
|
|
or by using current balance subtracting
|
|
|
|
"""
|
|
|
|
if "available_capital" in self._config:
|
2024-05-12 14:32:47 +00:00
|
|
|
return self._config["available_capital"]
|
2021-07-14 18:55:11 +00:00
|
|
|
else:
|
|
|
|
tot_profit = Trade.get_total_closed_profit()
|
|
|
|
open_stakes = Trade.total_open_trades_stakes()
|
2024-05-12 14:32:47 +00:00
|
|
|
available_balance = self.get_free(self._config["stake_currency"])
|
2021-07-14 18:55:11 +00:00
|
|
|
return available_balance - tot_profit + open_stakes
|
|
|
|
|
2021-07-11 09:20:31 +00:00
|
|
|
def get_total_stake_amount(self):
|
|
|
|
"""
|
|
|
|
Return the total currently available balance in stake currency, including tied up stake and
|
|
|
|
respecting tradable_balance_ratio.
|
|
|
|
Calculated as
|
|
|
|
(<open_trade stakes> + free amount) * tradable_balance_ratio
|
|
|
|
"""
|
|
|
|
val_tied_up = Trade.total_open_trades_stakes()
|
2021-07-10 10:30:00 +00:00
|
|
|
if "available_capital" in self._config:
|
2024-05-12 14:32:47 +00:00
|
|
|
starting_balance = self._config["available_capital"]
|
2021-07-10 10:30:00 +00:00
|
|
|
tot_profit = Trade.get_total_closed_profit()
|
|
|
|
available_amount = starting_balance + tot_profit
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Ensure <tradable_balance_ratio>% is used from the overall balance
|
|
|
|
# Otherwise we'd risk lowering stakes with each open trade.
|
|
|
|
# (tied up + current free) * ratio) - tied up
|
2024-05-12 14:32:47 +00:00
|
|
|
available_amount = (
|
|
|
|
val_tied_up + self.get_free(self._config["stake_currency"])
|
|
|
|
) * self._config["tradable_balance_ratio"]
|
2021-07-11 09:20:31 +00:00
|
|
|
return available_amount
|
|
|
|
|
|
|
|
def get_available_stake_amount(self) -> float:
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
Return the total currently available balance in stake currency,
|
|
|
|
respecting tradable_balance_ratio.
|
|
|
|
Calculated as
|
2021-04-20 13:55:48 +00:00
|
|
|
(<open_trade stakes> + free amount) * tradable_balance_ratio - <open_trade stakes>
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
free = self.get_free(self._config["stake_currency"])
|
2021-07-10 10:30:00 +00:00
|
|
|
return min(self.get_total_stake_amount() - Trade.total_open_trades_stakes(), free)
|
2021-02-03 19:00:33 +00:00
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
def _calculate_unlimited_stake_amount(
|
|
|
|
self, available_amount: float, val_tied_up: float, max_open_trades: IntOrInf
|
|
|
|
) -> float:
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
Calculate stake amount for "unlimited" stake amount
|
|
|
|
:return: 0 if max number of trades reached, else stake_amount to use.
|
|
|
|
"""
|
2023-12-12 21:43:46 +00:00
|
|
|
if max_open_trades == 0:
|
2021-02-03 19:00:33 +00:00
|
|
|
return 0
|
|
|
|
|
2023-12-12 21:43:46 +00:00
|
|
|
possible_stake = (available_amount + val_tied_up) / max_open_trades
|
2021-04-21 15:22:16 +00:00
|
|
|
# Theoretical amount can be above available amount - therefore limit to available amount!
|
2021-04-21 17:27:21 +00:00
|
|
|
return min(possible_stake, available_amount)
|
2021-02-03 19:00:33 +00:00
|
|
|
|
2021-04-21 17:27:21 +00:00
|
|
|
def _check_available_stake_amount(self, stake_amount: float, available_amount: float) -> float:
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
Check if stake amount can be fulfilled with the available balance
|
|
|
|
for the stake currency
|
|
|
|
:return: float: Stake amount
|
2021-02-10 18:45:59 +00:00
|
|
|
:raise: DependencyException if balance is lower than stake-amount
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
if self._config["amend_last_stake_amount"]:
|
2021-02-03 19:00:33 +00:00
|
|
|
# Remaining amount needs to be at least stake_amount * last_stake_amount_min_ratio
|
|
|
|
# Otherwise the remaining amount is too low to trade.
|
2024-05-12 14:32:47 +00:00
|
|
|
if available_amount > (stake_amount * self._config["last_stake_amount_min_ratio"]):
|
2021-02-03 19:00:33 +00:00
|
|
|
stake_amount = min(stake_amount, available_amount)
|
|
|
|
else:
|
|
|
|
stake_amount = 0
|
|
|
|
|
|
|
|
if available_amount < stake_amount:
|
|
|
|
raise DependencyException(
|
|
|
|
f"Available balance ({available_amount} {self._config['stake_currency']}) is "
|
|
|
|
f"lower than stake amount ({stake_amount} {self._config['stake_currency']})"
|
|
|
|
)
|
|
|
|
|
|
|
|
return stake_amount
|
|
|
|
|
2023-12-12 21:43:46 +00:00
|
|
|
def get_trade_stake_amount(
|
2024-05-12 14:32:47 +00:00
|
|
|
self, pair: str, max_open_trades: IntOrInf, edge=None, update: bool = True
|
|
|
|
) -> float:
|
2021-02-03 19:00:33 +00:00
|
|
|
"""
|
|
|
|
Calculate stake amount for the trade
|
|
|
|
:return: float: Stake amount
|
|
|
|
:raise: DependencyException if the available stake amount is too low
|
|
|
|
"""
|
|
|
|
stake_amount: float
|
2024-04-18 20:51:25 +00:00
|
|
|
# Ensure wallets are up-to-date.
|
2022-01-30 18:35:46 +00:00
|
|
|
if update:
|
|
|
|
self.update()
|
2021-04-21 17:27:21 +00:00
|
|
|
val_tied_up = Trade.total_open_trades_stakes()
|
2021-07-11 09:20:31 +00:00
|
|
|
available_amount = self.get_available_stake_amount()
|
2021-02-03 19:00:33 +00:00
|
|
|
|
|
|
|
if edge:
|
|
|
|
stake_amount = edge.stake_amount(
|
|
|
|
pair,
|
2024-05-12 14:32:47 +00:00
|
|
|
self.get_free(self._config["stake_currency"]),
|
|
|
|
self.get_total(self._config["stake_currency"]),
|
|
|
|
val_tied_up,
|
2021-02-03 19:00:33 +00:00
|
|
|
)
|
|
|
|
else:
|
2024-05-12 14:32:47 +00:00
|
|
|
stake_amount = self._config["stake_amount"]
|
2021-02-03 19:00:33 +00:00
|
|
|
if stake_amount == UNLIMITED_STAKE_AMOUNT:
|
2021-04-21 17:27:21 +00:00
|
|
|
stake_amount = self._calculate_unlimited_stake_amount(
|
2024-05-12 14:32:47 +00:00
|
|
|
available_amount, val_tied_up, max_open_trades
|
|
|
|
)
|
2021-02-03 19:00:33 +00:00
|
|
|
|
2021-04-21 17:27:21 +00:00
|
|
|
return self._check_available_stake_amount(stake_amount, available_amount)
|
2021-07-11 09:20:31 +00:00
|
|
|
|
2024-05-12 14:32:47 +00:00
|
|
|
def validate_stake_amount(
|
|
|
|
self,
|
|
|
|
pair: str,
|
|
|
|
stake_amount: Optional[float],
|
|
|
|
min_stake_amount: Optional[float],
|
|
|
|
max_stake_amount: float,
|
|
|
|
trade_amount: Optional[float],
|
|
|
|
):
|
2021-07-11 09:20:31 +00:00
|
|
|
if not stake_amount:
|
|
|
|
logger.debug(f"Stake amount is {stake_amount}, ignoring possible trade for {pair}.")
|
|
|
|
return 0
|
|
|
|
|
2023-01-21 07:52:10 +00:00
|
|
|
max_allowed_stake = min(max_stake_amount, self.get_available_stake_amount())
|
2022-12-23 15:09:35 +00:00
|
|
|
if trade_amount:
|
|
|
|
# if in a trade, then the resulting trade size cannot go beyond the max stake
|
|
|
|
# Otherwise we could no longer exit.
|
2023-01-21 07:52:10 +00:00
|
|
|
max_allowed_stake = min(max_allowed_stake, max_stake_amount - trade_amount)
|
2021-07-11 19:01:12 +00:00
|
|
|
|
2023-01-21 07:52:10 +00:00
|
|
|
if min_stake_amount is not None and min_stake_amount > max_allowed_stake:
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
2024-05-12 14:32:47 +00:00
|
|
|
logger.warning(
|
|
|
|
"Minimum stake amount > available balance. "
|
|
|
|
f"{min_stake_amount} > {max_allowed_stake}"
|
|
|
|
)
|
2021-07-11 19:01:12 +00:00
|
|
|
return 0
|
2021-07-11 09:20:31 +00:00
|
|
|
if min_stake_amount is not None and stake_amount < min_stake_amount:
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
2021-07-16 17:57:39 +00:00
|
|
|
logger.info(
|
|
|
|
f"Stake amount for pair {pair} is too small "
|
|
|
|
f"({stake_amount} < {min_stake_amount}), adjusting to {min_stake_amount}."
|
|
|
|
)
|
2021-11-10 05:57:22 +00:00
|
|
|
if stake_amount * 1.3 < min_stake_amount:
|
|
|
|
# Top-cap stake-amount adjustments to +30%.
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
2021-11-10 05:57:22 +00:00
|
|
|
logger.info(
|
|
|
|
f"Adjusted stake amount for pair {pair} is more than 30% bigger than "
|
2021-12-13 18:29:07 +00:00
|
|
|
f"the desired stake amount of ({stake_amount:.8f} * 1.3 = "
|
|
|
|
f"{stake_amount * 1.3:.8f}) < {min_stake_amount}), ignoring trade."
|
2021-11-10 05:57:22 +00:00
|
|
|
)
|
|
|
|
return 0
|
2021-11-07 14:41:04 +00:00
|
|
|
stake_amount = min_stake_amount
|
|
|
|
|
2023-01-21 07:52:10 +00:00
|
|
|
if stake_amount > max_allowed_stake:
|
2024-02-25 08:07:53 +00:00
|
|
|
if not self._is_backtest:
|
2021-07-16 17:57:39 +00:00
|
|
|
logger.info(
|
|
|
|
f"Stake amount for pair {pair} is too big "
|
2023-01-21 07:52:10 +00:00
|
|
|
f"({stake_amount} > {max_allowed_stake}), adjusting to {max_allowed_stake}."
|
2021-07-16 17:57:39 +00:00
|
|
|
)
|
2023-01-21 07:52:10 +00:00
|
|
|
stake_amount = max_allowed_stake
|
2021-07-11 09:20:31 +00:00
|
|
|
return stake_amount
|