Integrate leverage() to freqtradebot

This commit is contained in:
Matthias 2021-11-19 19:23:48 +01:00
parent 6247608cc6
commit 9aed76ba17
2 changed files with 18 additions and 4 deletions

View File

@ -43,6 +43,7 @@ By default, loop runs every few seconds (`internals.process_throttle_secs`) and
* Determine buy-price based on `bid_strategy` configuration setting, or by using the `custom_entry_price()` callback. * Determine buy-price based on `bid_strategy` configuration setting, or by using the `custom_entry_price()` callback.
* Determine stake size by calling the `custom_stake_amount()` callback. * Determine stake size by calling the `custom_stake_amount()` callback.
* Before a buy order is placed, `confirm_trade_entry()` strategy callback is called. * Before a buy order is placed, `confirm_trade_entry()` strategy callback is called.
* In Margin and Futures mode, `leverage()` strategy callback is called to determine the desired leverage.
This loop will be repeated again and again until the bot is stopped. This loop will be repeated again and again until the bot is stopped.

View File

@ -576,7 +576,6 @@ class FreqtradeBot(LoggingMixin):
stake_amount: float, stake_amount: float,
price: Optional[float] = None, price: Optional[float] = None,
forcebuy: bool = False, forcebuy: bool = False,
leverage: float = 1.0,
is_short: bool = False, is_short: bool = False,
enter_tag: Optional[str] = None enter_tag: Optional[str] = None
) -> bool: ) -> bool:
@ -590,6 +589,7 @@ class FreqtradeBot(LoggingMixin):
time_in_force = self.strategy.order_time_in_force['buy'] time_in_force = self.strategy.order_time_in_force['buy']
[side, name] = ['sell', 'Short'] if is_short else ['buy', 'Long'] [side, name] = ['sell', 'Short'] if is_short else ['buy', 'Long']
trade_side = 'short' if is_short else 'long'
if price: if price:
enter_limit_requested = price enter_limit_requested = price
@ -610,7 +610,8 @@ class FreqtradeBot(LoggingMixin):
pair, pair,
enter_limit_requested, enter_limit_requested,
self.strategy.stoploss, self.strategy.stoploss,
leverage=leverage # TODO-lev: This is problematic... we need stake-amount to determine max_leverage
# leverage=leverage
) )
if not self.edge: if not self.edge:
@ -620,7 +621,7 @@ class FreqtradeBot(LoggingMixin):
pair=pair, current_time=datetime.now(timezone.utc), pair=pair, current_time=datetime.now(timezone.utc),
current_rate=enter_limit_requested, proposed_stake=stake_amount, current_rate=enter_limit_requested, proposed_stake=stake_amount,
min_stake=min_stake_amount, max_stake=max_stake_amount, min_stake=min_stake_amount, max_stake=max_stake_amount,
side='short' if is_short else 'long' side=trade_side
) )
stake_amount = self.wallets.validate_stake_amount(pair, stake_amount, min_stake_amount) stake_amount = self.wallets.validate_stake_amount(pair, stake_amount, min_stake_amount)
@ -628,6 +629,18 @@ class FreqtradeBot(LoggingMixin):
if not stake_amount: if not stake_amount:
return False return False
max_leverage = self.exchange.get_max_leverage(pair, stake_amount)
leverage = strategy_safe_wrapper(self.strategy.leverage, default_retval=1.0)(
pair=pair,
current_time=datetime.now(timezone.utc),
current_rate=enter_limit_requested,
proposed_leverage=1.0,
max_leverage=max_leverage,
side=trade_side,
) if self.trading_mode != TradingMode.SPOT else 1.0
# Cap leverage between 1.0 and max_leverage.
leverage = min(max(leverage, 1.0), max_leverage)
logger.info( logger.info(
f"{name} signal found: about create a new trade for {pair} with stake_amount: " f"{name} signal found: about create a new trade for {pair} with stake_amount: "
f"{stake_amount} ..." f"{stake_amount} ..."
@ -644,7 +657,7 @@ class FreqtradeBot(LoggingMixin):
if not strategy_safe_wrapper(self.strategy.confirm_trade_entry, default_retval=True)( if not strategy_safe_wrapper(self.strategy.confirm_trade_entry, default_retval=True)(
pair=pair, order_type=order_type, amount=amount, rate=enter_limit_requested, pair=pair, order_type=order_type, amount=amount, rate=enter_limit_requested,
time_in_force=time_in_force, current_time=datetime.now(timezone.utc), time_in_force=time_in_force, current_time=datetime.now(timezone.utc),
side='short' if is_short else 'long' side=trade_side
): ):
logger.info(f"User requested abortion of buying {pair}") logger.info(f"User requested abortion of buying {pair}")
return False return False