Merge branch 'lev-exchange' into lev-freqtradebot
This commit is contained in:
@@ -112,9 +112,6 @@ class Binance(Exchange):
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e) from e
|
||||
|
||||
def _apply_leverage_to_stake_amount(self, stake_amount: float, leverage: float):
|
||||
return stake_amount / leverage
|
||||
|
||||
@retrier
|
||||
def fill_leverage_brackets(self):
|
||||
"""
|
||||
@@ -154,3 +151,27 @@ class Binance(Exchange):
|
||||
if nominal_value >= min_amount:
|
||||
max_lev = 1/margin_req
|
||||
return max_lev
|
||||
|
||||
@retrier
|
||||
def _set_leverage(
|
||||
self,
|
||||
leverage: float,
|
||||
pair: Optional[str] = None,
|
||||
trading_mode: Optional[TradingMode] = None
|
||||
):
|
||||
"""
|
||||
Set's the leverage before making a trade, in order to not
|
||||
have the same leverage on every trade
|
||||
"""
|
||||
trading_mode = trading_mode or self.trading_mode
|
||||
|
||||
try:
|
||||
if trading_mode == TradingMode.FUTURES:
|
||||
self._api.set_leverage(symbol=pair, leverage=leverage)
|
||||
except ccxt.DDoSProtection as e:
|
||||
raise DDosProtection(e) from e
|
||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||
raise TemporaryError(
|
||||
f'Could not set leverage due to {e.__class__.__name__}. Message: {e}') from e
|
||||
except ccxt.BaseError as e:
|
||||
raise OperationalException(e) from e
|
||||
|
@@ -145,7 +145,7 @@ class Exchange:
|
||||
self._api_async = self._init_ccxt(
|
||||
exchange_config, ccxt_async, ccxt_kwargs=ccxt_async_config)
|
||||
|
||||
trading_mode: TradingMode = (
|
||||
self.trading_mode: TradingMode = (
|
||||
TradingMode(config.get('trading_mode'))
|
||||
if config.get('trading_mode')
|
||||
else TradingMode.SPOT
|
||||
@@ -156,7 +156,7 @@ class Exchange:
|
||||
else None
|
||||
)
|
||||
|
||||
if trading_mode != TradingMode.SPOT:
|
||||
if self.trading_mode != TradingMode.SPOT:
|
||||
self.fill_leverage_brackets()
|
||||
|
||||
logger.info('Using Exchange "%s"', self.name)
|
||||
@@ -176,7 +176,7 @@ class Exchange:
|
||||
self.validate_order_time_in_force(config.get('order_time_in_force', {}))
|
||||
self.validate_required_startup_candles(config.get('startup_candle_count', 0),
|
||||
config.get('timeframe', ''))
|
||||
self.validate_trading_mode_and_collateral(trading_mode, collateral)
|
||||
self.validate_trading_mode_and_collateral(self.trading_mode, collateral)
|
||||
# Converts the interval provided in minutes in config to seconds
|
||||
self.markets_refresh_interval: int = exchange_config.get(
|
||||
"markets_refresh_interval", 60) * 60
|
||||
@@ -630,7 +630,7 @@ class Exchange:
|
||||
:param stake_amount: The stake amount for a pair before leverage is considered
|
||||
:param leverage: The amount of leverage being used on the current trade
|
||||
"""
|
||||
return stake_amount
|
||||
return stake_amount / leverage
|
||||
|
||||
# Dry-run methods
|
||||
|
||||
@@ -771,12 +771,14 @@ class Exchange:
|
||||
# Order handling
|
||||
|
||||
def create_order(self, pair: str, ordertype: str, side: str, amount: float,
|
||||
rate: float, time_in_force: str = 'gtc') -> Dict:
|
||||
rate: float, time_in_force: str = 'gtc', leverage=1.0) -> Dict:
|
||||
|
||||
if self._config['dry_run']:
|
||||
dry_order = self.create_dry_run_order(pair, ordertype, side, amount, rate)
|
||||
return dry_order
|
||||
|
||||
if self.trading_mode != TradingMode.SPOT:
|
||||
self._set_leverage(leverage, pair)
|
||||
params = self._params.copy()
|
||||
if time_in_force != 'gtc' and ordertype != 'market':
|
||||
param = self._ft_has.get('time_in_force_parameter', '')
|
||||
@@ -1600,7 +1602,12 @@ class Exchange:
|
||||
return 1.0
|
||||
|
||||
@retrier
|
||||
def set_leverage(self, leverage: float, pair: Optional[str]):
|
||||
def _set_leverage(
|
||||
self,
|
||||
leverage: float,
|
||||
pair: Optional[str] = None,
|
||||
trading_mode: Optional[TradingMode] = None
|
||||
):
|
||||
"""
|
||||
Set's the leverage before making a trade, in order to not
|
||||
have the same leverage on every trade
|
||||
|
@@ -146,6 +146,7 @@ class Kraken(Exchange):
|
||||
leverages = {}
|
||||
|
||||
for pair, market in self.markets.items():
|
||||
leverages[pair] = [1]
|
||||
info = market['info']
|
||||
leverage_buy = info.get('leverage_buy', [])
|
||||
leverage_sell = info.get('leverage_sell', [])
|
||||
@@ -155,12 +156,12 @@ class Kraken(Exchange):
|
||||
f"The buy({leverage_buy}) and sell({leverage_sell}) leverage are not equal"
|
||||
"for {pair}. Please notify freqtrade because this has never happened before"
|
||||
)
|
||||
if max(leverage_buy) < max(leverage_sell):
|
||||
leverages[pair] = leverage_buy
|
||||
if max(leverage_buy) <= max(leverage_sell):
|
||||
leverages[pair] += [int(lev) for lev in leverage_buy]
|
||||
else:
|
||||
leverages[pair] = leverage_sell
|
||||
leverages[pair] += [int(lev) for lev in leverage_sell]
|
||||
else:
|
||||
leverages[pair] = leverage_buy
|
||||
leverages[pair] += [int(lev) for lev in leverage_buy]
|
||||
self._leverage_brackets = leverages
|
||||
|
||||
def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float:
|
||||
@@ -171,9 +172,18 @@ class Kraken(Exchange):
|
||||
"""
|
||||
return float(max(self._leverage_brackets[pair]))
|
||||
|
||||
def set_leverage(self, pair, leverage):
|
||||
def _set_leverage(
|
||||
self,
|
||||
leverage: float,
|
||||
pair: Optional[str] = None,
|
||||
trading_mode: Optional[TradingMode] = None
|
||||
):
|
||||
"""
|
||||
Kraken set's the leverage as an option in the order object, so it doesn't do
|
||||
anything in this function
|
||||
Kraken set's the leverage as an option in the order object, so we need to
|
||||
add it to params
|
||||
"""
|
||||
return
|
||||
if leverage > 1.0:
|
||||
self._params['leverage'] = leverage
|
||||
else:
|
||||
if 'leverage' in self._params:
|
||||
del self._params['leverage']
|
||||
|
Reference in New Issue
Block a user