Extract contracts_to_amount and amount_to_contracts to standalone functions

This commit is contained in:
Matthias
2022-08-22 20:28:33 +02:00
parent 1b0f37a93c
commit 6036018f35
3 changed files with 39 additions and 18 deletions

View File

@@ -408,7 +408,7 @@ class Exchange:
else:
return DataFrame()
def _get_contract_size(self, pair: str) -> float:
def get_contract_size(self, pair: str) -> float:
if self.trading_mode == TradingMode.FUTURES:
market = self.markets[pair]
contract_size: float = 1.0
@@ -421,7 +421,7 @@ class Exchange:
def _trades_contracts_to_amount(self, trades: List) -> List:
if len(trades) > 0 and 'symbol' in trades[0]:
contract_size = self._get_contract_size(trades[0]['symbol'])
contract_size = self.get_contract_size(trades[0]['symbol'])
if contract_size != 1:
for trade in trades:
trade['amount'] = trade['amount'] * contract_size
@@ -429,7 +429,7 @@ class Exchange:
def _order_contracts_to_amount(self, order: Dict) -> Dict:
if 'symbol' in order and order['symbol'] is not None:
contract_size = self._get_contract_size(order['symbol'])
contract_size = self.get_contract_size(order['symbol'])
if contract_size != 1:
for prop in self._ft_has.get('order_props_in_contracts', []):
if prop in order and order[prop] is not None:
@@ -438,19 +438,13 @@ class Exchange:
def _amount_to_contracts(self, pair: str, amount: float) -> float:
contract_size = self._get_contract_size(pair)
if contract_size and contract_size != 1:
return amount / contract_size
else:
return amount
contract_size = self.get_contract_size(pair)
return amount_to_contracts(amount, contract_size)
def _contracts_to_amount(self, pair: str, num_contracts: float) -> float:
contract_size = self._get_contract_size(pair)
if contract_size and contract_size != 1:
return num_contracts * contract_size
else:
return num_contracts
contract_size = self.get_contract_size(pair)
return contracts_to_amount(num_contracts, contract_size)
def set_sandbox(self, api: ccxt.Exchange, exchange_config: dict, name: str) -> None:
if exchange_config.get('sandbox'):
@@ -2898,6 +2892,33 @@ def market_is_active(market: Dict) -> bool:
return market.get('active', True) is not False
def amount_to_contracts(amount: float, contract_size: float) -> float:
"""
Convert amount to contracts.
:param amount: amount to convert
:param contract_size: contract size - taken from exchange.get_contract_size(pair)
:return: num-contracts
"""
if contract_size and contract_size != 1:
return amount / contract_size
else:
return amount
def contracts_to_amount(num_contracts: float, contract_size: float) -> float:
"""
Takes num-contracts and converts it to contract size
:param num_contracts: number of contracts
:param contract_size: contract size - taken from exchange.get_contract_size(pair)
:return: Amount
"""
if contract_size and contract_size != 1:
return num_contracts * contract_size
else:
return num_contracts
def amount_to_precision(amount: float, amount_precision: Optional[float],
precisionMode: Optional[int]) -> float:
"""