Removed leverage param from get_min_pair_stake_amount

This commit is contained in:
Sam Germain 2021-11-13 18:35:22 -06:00
parent 3f75531105
commit ef6ad0e6d7
2 changed files with 13 additions and 60 deletions

View File

@ -631,8 +631,12 @@ class Exchange:
else:
return 1 / pow(10, precision)
def get_min_pair_stake_amount(self, pair: str, price: float, stoploss: float,
leverage: Optional[float] = 1.0) -> Optional[float]:
def get_min_pair_stake_amount(
self,
pair: str,
price: float,
stoploss: float
) -> Optional[float]:
try:
market = self.markets[pair]
except KeyError:
@ -666,23 +670,11 @@ class Exchange:
# The value returned should satisfy both limits: for amount (base currency) and
# for cost (quote, stake currency), so max() is used here.
# See also #2575 at github.
return self._get_stake_amount_considering_leverage(
self._contract_size_to_amount(
pair,
max(min_stake_amounts) * amount_reserve_percent
),
leverage or 1.0
return self._contract_size_to_amount(
pair,
max(min_stake_amounts) * amount_reserve_percent
)
def _get_stake_amount_considering_leverage(self, stake_amount: float, leverage: float):
"""
Takes the minimum stake amount for a pair with no leverage and returns the minimum
stake amount when leverage is considered
: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 / leverage
# Dry-run methods
def create_dry_run_order(self, pair: str, ordertype: str, side: str, amount: float,
@ -840,15 +832,15 @@ class Exchange:
def _amount_to_contract_size(self, pair: str, amount: float):
if ('contractSize' in self._api.markets[pair]):
return amount / self._api.markets[pair]['contractSize']
if ('contractSize' in self.markets[pair]):
return amount / self.markets[pair]['contractSize']
else:
return amount
def _contract_size_to_amount(self, pair: str, amount: float):
if ('contractSize' in self._api.markets[pair]):
return amount * self._api.markets[pair]['contractSize']
if ('contractSize' in self.markets[pair]):
return amount * self.markets[pair]['contractSize']
else:
return amount

View File

@ -398,9 +398,6 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss)
expected_result = 2 * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 1, stoploss, 3.0)
assert isclose(result, expected_result/3)
# min amount is set
markets["ETH/BTC"]["limits"] = {
@ -414,9 +411,6 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = 2 * 2 * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0)
assert isclose(result, expected_result/5)
# min amount and cost are set (cost is minimal)
markets["ETH/BTC"]["limits"] = {
@ -430,9 +424,6 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = max(2, 2 * 2) * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10)
assert isclose(result, expected_result/10)
# min amount and cost are set (amount is minial)
markets["ETH/BTC"]["limits"] = {
@ -446,24 +437,15 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result = max(8, 2 * 2) * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0)
assert isclose(result, expected_result/7.0)
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4)
expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0)
assert isclose(result, expected_result/8.0)
# Really big stoploss
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1)
expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result)
# With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0)
assert isclose(result, expected_result/12)
def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None:
@ -483,8 +465,6 @@ def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None:
result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss)
expected_result = max(0.0001, 0.001 * 0.020405) * (1+0.05) / (1-abs(stoploss))
assert round(result, 8) == round(expected_result, 8)
result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss, 3.0)
assert round(result, 8) == round(expected_result/3, 8)
def test_set_sandbox(default_conf, mocker):
@ -3263,25 +3243,6 @@ def test__get_funding_fees_from_exchange(default_conf, mocker, exchange_name):
)
@pytest.mark.parametrize('exchange', ['binance', 'kraken', 'ftx'])
@pytest.mark.parametrize('stake_amount,leverage,min_stake_with_lev', [
(9.0, 3.0, 3.0),
(20.0, 5.0, 4.0),
(100.0, 100.0, 1.0)
])
def test_get_stake_amount_considering_leverage(
exchange,
stake_amount,
leverage,
min_stake_with_lev,
mocker,
default_conf
):
exchange = get_patched_exchange(mocker, default_conf, id=exchange)
assert exchange._get_stake_amount_considering_leverage(
stake_amount, leverage) == min_stake_with_lev
@pytest.mark.parametrize("exchange_name,trading_mode", [
("binance", TradingMode.FUTURES),
("ftx", TradingMode.MARGIN),