Exchange: f-strings into use
This commit is contained in:
parent
1e1be6bc3f
commit
18e3090379
@ -59,7 +59,7 @@ def init_ccxt(exchange_config: dict) -> ccxt.Exchange:
|
|||||||
name = exchange_config['name']
|
name = exchange_config['name']
|
||||||
|
|
||||||
if name not in ccxt.exchanges:
|
if name not in ccxt.exchanges:
|
||||||
raise OperationalException('Exchange {} is not supported'.format(name))
|
raise OperationalException(f'Exchange {name} is not supported')
|
||||||
try:
|
try:
|
||||||
api = getattr(ccxt, name.lower())({
|
api = getattr(ccxt, name.lower())({
|
||||||
'apiKey': exchange_config.get('key'),
|
'apiKey': exchange_config.get('key'),
|
||||||
@ -69,7 +69,7 @@ def init_ccxt(exchange_config: dict) -> ccxt.Exchange:
|
|||||||
'enableRateLimit': True,
|
'enableRateLimit': True,
|
||||||
})
|
})
|
||||||
except (KeyError, AttributeError):
|
except (KeyError, AttributeError):
|
||||||
raise OperationalException('Exchange {} is not supported'.format(name))
|
raise OperationalException(f'Exchange {name} is not supported')
|
||||||
|
|
||||||
return api
|
return api
|
||||||
|
|
||||||
@ -118,11 +118,10 @@ def validate_pairs(pairs: List[str]) -> None:
|
|||||||
# TODO: add a support for having coins in BTC/USDT format
|
# TODO: add a support for having coins in BTC/USDT format
|
||||||
if not pair.endswith(stake_cur):
|
if not pair.endswith(stake_cur):
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
'Pair {} not compatible with stake_currency: {}'.format(pair, stake_cur)
|
f'Pair {pair} not compatible with stake_currency: {stake_cur}')
|
||||||
)
|
|
||||||
if pair not in markets:
|
if pair not in markets:
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
'Pair {} is not available at {}'.format(pair, get_name()))
|
f'Pair {pair} is not available at {get_name()}')
|
||||||
|
|
||||||
|
|
||||||
def exchange_has(endpoint: str) -> bool:
|
def exchange_has(endpoint: str) -> bool:
|
||||||
@ -138,7 +137,7 @@ def exchange_has(endpoint: str) -> bool:
|
|||||||
def buy(pair: str, rate: float, amount: float) -> Dict:
|
def buy(pair: str, rate: float, amount: float) -> Dict:
|
||||||
if _CONF['dry_run']:
|
if _CONF['dry_run']:
|
||||||
global _DRY_RUN_OPEN_ORDERS
|
global _DRY_RUN_OPEN_ORDERS
|
||||||
order_id = 'dry_run_buy_{}'.format(randint(0, 10**6))
|
order_id = f'dry_run_buy_{randint(0, 10**6)}'
|
||||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||||
'pair': pair,
|
'pair': pair,
|
||||||
'price': rate,
|
'price': rate,
|
||||||
@ -156,20 +155,17 @@ def buy(pair: str, rate: float, amount: float) -> Dict:
|
|||||||
return _API.create_limit_buy_order(pair, amount, rate)
|
return _API.create_limit_buy_order(pair, amount, rate)
|
||||||
except ccxt.InsufficientFunds as e:
|
except ccxt.InsufficientFunds as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Insufficient funds to create limit buy order on market {}.'
|
f'Insufficient funds to create limit buy order on market {pair}.'
|
||||||
'Tried to buy amount {} at rate {} (total {}).'
|
f'Tried to buy amount {amount} at rate {rate} (total {rate*amount}).'
|
||||||
'Message: {}'.format(pair, amount, rate, rate*amount, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except ccxt.InvalidOrder as e:
|
except ccxt.InvalidOrder as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Could not create limit buy order on market {}.'
|
f'Could not create limit buy order on market {pair}.'
|
||||||
'Tried to buy amount {} at rate {} (total {}).'
|
f'Tried to buy amount {amount} at rate {rate} (total {rate*amount}).'
|
||||||
'Message: {}'.format(pair, amount, rate, rate*amount, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not place buy order due to {}. Message: {}'.format(
|
f'Could not place buy order due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -177,7 +173,7 @@ def buy(pair: str, rate: float, amount: float) -> Dict:
|
|||||||
def sell(pair: str, rate: float, amount: float) -> Dict:
|
def sell(pair: str, rate: float, amount: float) -> Dict:
|
||||||
if _CONF['dry_run']:
|
if _CONF['dry_run']:
|
||||||
global _DRY_RUN_OPEN_ORDERS
|
global _DRY_RUN_OPEN_ORDERS
|
||||||
order_id = 'dry_run_sell_{}'.format(randint(0, 10**6))
|
order_id = f'dry_run_sell_{randint(0, 10**6)}'
|
||||||
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
_DRY_RUN_OPEN_ORDERS[order_id] = {
|
||||||
'pair': pair,
|
'pair': pair,
|
||||||
'price': rate,
|
'price': rate,
|
||||||
@ -194,20 +190,17 @@ def sell(pair: str, rate: float, amount: float) -> Dict:
|
|||||||
return _API.create_limit_sell_order(pair, amount, rate)
|
return _API.create_limit_sell_order(pair, amount, rate)
|
||||||
except ccxt.InsufficientFunds as e:
|
except ccxt.InsufficientFunds as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Insufficient funds to create limit sell order on market {}.'
|
f'Insufficient funds to create limit sell order on market {pair}.'
|
||||||
'Tried to sell amount {} at rate {} (total {}).'
|
f'Tried to sell amount {amount} at rate {rate} (total {rate*amount}).'
|
||||||
'Message: {}'.format(pair, amount, rate, rate*amount, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except ccxt.InvalidOrder as e:
|
except ccxt.InvalidOrder as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Could not create limit sell order on market {}.'
|
f'Could not create limit sell order on market {pair}.'
|
||||||
'Tried to sell amount {} at rate {} (total {}).'
|
f'Tried to sell amount {amount} at rate {rate} (total {rate*amount}).'
|
||||||
'Message: {}'.format(pair, amount, rate, rate*amount, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not place sell order due to {}. Message: {}'.format(
|
f'Could not place sell order due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -222,8 +215,7 @@ def get_balance(currency: str) -> float:
|
|||||||
balance = balances.get(currency)
|
balance = balances.get(currency)
|
||||||
if balance is None:
|
if balance is None:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not get {} balance due to malformed exchange response: {}'.format(
|
f'Could not get {currency} balance due to malformed exchange response: {balances}')
|
||||||
currency, balances))
|
|
||||||
return balance['free']
|
return balance['free']
|
||||||
|
|
||||||
|
|
||||||
@ -243,8 +235,7 @@ def get_balances() -> dict:
|
|||||||
return balances
|
return balances
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not get balance due to {}. Message: {}'.format(
|
f'Could not get balance due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -255,13 +246,11 @@ def get_tickers() -> Dict:
|
|||||||
return _API.fetch_tickers()
|
return _API.fetch_tickers()
|
||||||
except ccxt.NotSupported as e:
|
except ccxt.NotSupported as e:
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
'Exchange {} does not support fetching tickers in batch.'
|
f'Exchange {_API.name} does not support fetching tickers in batch.'
|
||||||
'Message: {}'.format(_API.name, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not load tickers due to {}. Message: {}'.format(
|
f'Could not load tickers due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -282,8 +271,7 @@ def get_ticker(pair: str, refresh: Optional[bool] = True) -> dict:
|
|||||||
return data
|
return data
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not load ticker history due to {}. Message: {}'.format(
|
f'Could not load ticker history due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
else:
|
else:
|
||||||
@ -327,15 +315,13 @@ def get_ticker_history(pair: str, tick_interval: str, since_ms: Optional[int] =
|
|||||||
return data
|
return data
|
||||||
except ccxt.NotSupported as e:
|
except ccxt.NotSupported as e:
|
||||||
raise OperationalException(
|
raise OperationalException(
|
||||||
'Exchange {} does not support fetching historical candlestick data.'
|
f'Exchange {_API.name} does not support fetching historical candlestick data.'
|
||||||
'Message: {}'.format(_API.name, e)
|
f'Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not load ticker history due to {}. Message: {}'.format(
|
f'Could not load ticker history due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException('Could not fetch ticker data. Msg: {}'.format(e))
|
raise OperationalException(f'Could not fetch ticker data. Msg: {e}')
|
||||||
|
|
||||||
|
|
||||||
@retrier
|
@retrier
|
||||||
@ -347,12 +333,10 @@ def cancel_order(order_id: str, pair: str) -> None:
|
|||||||
return _API.cancel_order(order_id, pair)
|
return _API.cancel_order(order_id, pair)
|
||||||
except ccxt.InvalidOrder as e:
|
except ccxt.InvalidOrder as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Could not cancel order. Message: {}'.format(e)
|
f'Could not cancel order. Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not cancel order due to {}. Message: {}'.format(
|
f'Could not cancel order due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -369,12 +353,10 @@ def get_order(order_id: str, pair: str) -> Dict:
|
|||||||
return _API.fetch_order(order_id, pair)
|
return _API.fetch_order(order_id, pair)
|
||||||
except ccxt.InvalidOrder as e:
|
except ccxt.InvalidOrder as e:
|
||||||
raise DependencyException(
|
raise DependencyException(
|
||||||
'Could not get order. Message: {}'.format(e)
|
f'Could not get order. Message: {e}')
|
||||||
)
|
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not get order due to {}. Message: {}'.format(
|
f'Could not get order due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -393,8 +375,7 @@ def get_trades_for_order(order_id: str, pair: str, since: datetime) -> List:
|
|||||||
|
|
||||||
except ccxt.NetworkError as e:
|
except ccxt.NetworkError as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not get trades due to networking error. Message: {}'.format(e)
|
f'Could not get trades due to networking error. Message: {e}')
|
||||||
)
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -416,8 +397,7 @@ def get_markets() -> List[dict]:
|
|||||||
return _API.fetch_markets()
|
return _API.fetch_markets()
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not load markets due to {}. Message: {}'.format(
|
f'Could not load markets due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
@ -442,8 +422,7 @@ def get_fee(symbol='ETH/BTC', type='', side='', amount=1,
|
|||||||
price=price, takerOrMaker=taker_or_maker)['rate']
|
price=price, takerOrMaker=taker_or_maker)['rate']
|
||||||
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
||||||
raise TemporaryError(
|
raise TemporaryError(
|
||||||
'Could not get fee info due to {}. Message: {}'.format(
|
f'Could not get fee info due to {e.__class__.__name__}. Message: {e}')
|
||||||
e.__class__.__name__, e))
|
|
||||||
except ccxt.BaseError as e:
|
except ccxt.BaseError as e:
|
||||||
raise OperationalException(e)
|
raise OperationalException(e)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user