exchange._contracts_to_amount and exchange._amount_to_contracts safe checks

This commit is contained in:
Sam Germain 2021-12-31 05:28:25 -06:00
parent 6ab0e870c2
commit f92d47a16b
2 changed files with 36 additions and 4 deletions

View File

@ -875,15 +875,23 @@ class Exchange:
def _amount_to_contracts(self, pair: str, amount: float): def _amount_to_contracts(self, pair: str, amount: float):
contract_size = None
if ('contractSize' in self.markets[pair]): if ('contractSize' in self.markets[pair]):
return amount / self.markets[pair]['contractSize'] contract_size = self.markets[pair]['contractSize']
if (contract_size and self.trading_mode == TradingMode.FUTURES):
return amount / contract_size
else: else:
return amount return amount
def _contracts_to_amount(self, pair: str, num_contracts: float): def _contracts_to_amount(self, pair: str, num_contracts: float):
contract_size = None
if ('contractSize' in self.markets[pair]): if ('contractSize' in self.markets[pair]):
return num_contracts * self.markets[pair]['contractSize'] contract_size = self.markets[pair]['contractSize']
if (contract_size and self.trading_mode == TradingMode.FUTURES):
return num_contracts * contract_size
else: else:
return num_contracts return num_contracts

View File

@ -3897,6 +3897,7 @@ def test__trades_contracts_to_amount(
@pytest.mark.parametrize('pair,param_amount,param_size', [ @pytest.mark.parametrize('pair,param_amount,param_size', [
('XLTCUSDT', 40, 4000), ('XLTCUSDT', 40, 4000),
('LTC/ETH', 30, 30), ('LTC/ETH', 30, 30),
('LTC/USD', 30, 30),
('ETH/USDT:USDT', 10, 1), ('ETH/USDT:USDT', 10, 1),
]) ])
def test__amount_to_contracts( def test__amount_to_contracts(
@ -3908,9 +3909,32 @@ def test__amount_to_contracts(
param_size param_size
): ):
api_mock = MagicMock() api_mock = MagicMock()
default_conf['trading_mode'] = 'futures' default_conf['trading_mode'] = 'spot'
default_conf['collateral'] = 'isolated' default_conf['collateral'] = 'isolated'
mocker.patch('freqtrade.exchange.Exchange.markets', markets) mocker.patch('freqtrade.exchange.Exchange.markets', {
'LTC/USD': {
'symbol': 'LTC/USD',
'contractSize': None,
},
'XLTCUSDT': {
'symbol': 'XLTCUSDT',
'contractSize': 0.01,
},
'LTC/ETH': {
'symbol': 'LTC/ETH',
},
'ETH/USDT:USDT': {
'symbol': 'ETH/USDT:USDT',
'contractSize': 10,
}
})
exchange = get_patched_exchange(mocker, default_conf, api_mock)
result_size = exchange._amount_to_contracts(pair, param_amount)
assert result_size == param_amount
result_amount = exchange._contracts_to_amount(pair, param_size)
assert result_amount == param_size
default_conf['trading_mode'] = 'futures'
exchange = get_patched_exchange(mocker, default_conf, api_mock) exchange = get_patched_exchange(mocker, default_conf, api_mock)
result_size = exchange._amount_to_contracts(pair, param_amount) result_size = exchange._amount_to_contracts(pair, param_amount)
assert result_size == param_size assert result_size == param_size