2019-02-24 18:30:05 +00:00
|
|
|
""" Binance exchange subclass """
|
|
|
|
import logging
|
|
|
|
from typing import Dict
|
|
|
|
|
2019-08-25 07:50:37 +00:00
|
|
|
import ccxt
|
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, InvalidOrderException,
|
|
|
|
OperationalException, TemporaryError)
|
2019-02-24 18:30:05 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2020-06-28 09:56:29 +00:00
|
|
|
from freqtrade.exchange.common import retrier
|
2019-02-24 18:30:05 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
2019-02-24 18:30:05 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Binance(Exchange):
|
|
|
|
|
2019-02-24 19:01:20 +00:00
|
|
|
_ft_has: Dict = {
|
2019-02-24 18:30:05 +00:00
|
|
|
"stoploss_on_exchange": True,
|
2019-03-25 23:49:39 +00:00
|
|
|
"order_time_in_force": ['gtc', 'fok', 'ioc'],
|
2019-08-14 17:22:52 +00:00
|
|
|
"trades_pagination": "id",
|
|
|
|
"trades_pagination_arg": "fromId",
|
2019-02-24 18:30:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 18:27:35 +00:00
|
|
|
def fetch_l2_order_book(self, pair: str, limit: int = 100) -> dict:
|
2019-02-24 18:30:05 +00:00
|
|
|
"""
|
|
|
|
get order book level 2 from exchange
|
|
|
|
|
|
|
|
20180619: binance support limits but only on specific range
|
|
|
|
"""
|
|
|
|
limit_range = [5, 10, 20, 50, 100, 500, 1000]
|
|
|
|
# get next-higher step in the limit_range list
|
|
|
|
limit = min(list(filter(lambda x: limit <= x, limit_range)))
|
|
|
|
|
2020-05-26 18:27:35 +00:00
|
|
|
return super().fetch_l2_order_book(pair, limit)
|
2019-08-25 07:50:37 +00:00
|
|
|
|
2020-01-19 18:54:30 +00:00
|
|
|
def stoploss_adjust(self, stop_loss: float, order: Dict) -> bool:
|
|
|
|
"""
|
|
|
|
Verify stop_loss against stoploss-order value (limit or price)
|
|
|
|
Returns True if adjustment is necessary.
|
|
|
|
"""
|
|
|
|
return order['type'] == 'stop_loss_limit' and stop_loss > float(order['info']['stopPrice'])
|
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
@retrier(retries=0)
|
2020-01-19 12:30:56 +00:00
|
|
|
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
|
2019-08-25 07:50:37 +00:00
|
|
|
"""
|
|
|
|
creates a stoploss limit order.
|
|
|
|
this stoploss-limit is binance-specific.
|
|
|
|
It may work with a limited number of other exchanges, but this has not been tested yet.
|
|
|
|
"""
|
2020-01-19 12:12:28 +00:00
|
|
|
# Limit price threshold: As limit price should always be below stop-price
|
2020-02-02 09:47:44 +00:00
|
|
|
limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
|
|
|
|
rate = stop_price * limit_price_pct
|
2020-01-19 12:12:28 +00:00
|
|
|
|
2019-08-25 07:50:37 +00:00
|
|
|
ordertype = "stop_loss_limit"
|
|
|
|
|
2020-01-12 13:55:05 +00:00
|
|
|
stop_price = self.price_to_precision(pair, stop_price)
|
2019-08-25 07:50:37 +00:00
|
|
|
|
|
|
|
# Ensure rate is less than stop price
|
|
|
|
if stop_price <= rate:
|
|
|
|
raise OperationalException(
|
|
|
|
'In stoploss limit order, stop price should be more than limit price')
|
|
|
|
|
|
|
|
if self._config['dry_run']:
|
|
|
|
dry_order = self.dry_run_order(
|
|
|
|
pair, ordertype, "sell", amount, stop_price)
|
|
|
|
return dry_order
|
|
|
|
|
|
|
|
try:
|
2019-08-25 08:08:06 +00:00
|
|
|
params = self._params.copy()
|
|
|
|
params.update({'stopPrice': stop_price})
|
|
|
|
|
2020-01-12 13:55:05 +00:00
|
|
|
amount = self.amount_to_precision(pair, amount)
|
2019-08-25 07:50:37 +00:00
|
|
|
|
2020-01-12 13:55:05 +00:00
|
|
|
rate = self.price_to_precision(pair, rate)
|
2019-08-25 07:50:37 +00:00
|
|
|
|
2020-01-19 13:07:59 +00:00
|
|
|
order = self._api.create_order(symbol=pair, type=ordertype, side='sell',
|
2020-05-13 05:15:18 +00:00
|
|
|
amount=amount, price=rate, params=params)
|
2019-08-25 07:50:37 +00:00
|
|
|
logger.info('stoploss limit order added for %s. '
|
|
|
|
'stop price: %s. limit: %s', pair, stop_price, rate)
|
|
|
|
return order
|
|
|
|
except ccxt.InsufficientFunds as e:
|
2020-08-14 07:57:13 +00:00
|
|
|
raise InsufficientFundsError(
|
2020-06-13 21:35:58 +00:00
|
|
|
f'Insufficient funds to create {ordertype} sell order on market {pair}. '
|
2019-09-01 08:17:17 +00:00
|
|
|
f'Tried to sell amount {amount} at rate {rate}. '
|
2019-08-25 07:50:37 +00:00
|
|
|
f'Message: {e}') from e
|
|
|
|
except ccxt.InvalidOrder as e:
|
2019-09-01 07:08:35 +00:00
|
|
|
# Errors:
|
|
|
|
# `binance Order would trigger immediately.`
|
|
|
|
raise InvalidOrderException(
|
2019-08-25 07:50:37 +00:00
|
|
|
f'Could not create {ordertype} sell order on market {pair}. '
|
2019-09-01 08:17:17 +00:00
|
|
|
f'Tried to sell amount {amount} at rate {rate}. '
|
2019-08-25 07:50:37 +00:00
|
|
|
f'Message: {e}') from e
|
2020-06-28 09:17:06 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2019-08-25 07:50:37 +00:00
|
|
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
|
|
raise TemporaryError(
|
|
|
|
f'Could not place sell order due to {e.__class__.__name__}. Message: {e}') from e
|
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|