2020-02-16 12:20:11 +00:00
|
|
|
""" FTX exchange subclass """
|
|
|
|
import logging
|
|
|
|
from typing import Dict
|
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
import ccxt
|
|
|
|
|
2020-08-14 09:31:02 +00:00
|
|
|
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError,
|
2020-06-28 09:17:06 +00:00
|
|
|
InvalidOrderException, OperationalException,
|
|
|
|
TemporaryError)
|
2020-02-16 12:20:11 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2020-03-25 16:01:11 +00:00
|
|
|
from freqtrade.exchange.common import retrier
|
2020-02-16 12:20:11 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Ftx(Exchange):
|
|
|
|
|
|
|
|
_ft_has: Dict = {
|
2020-03-24 19:57:12 +00:00
|
|
|
"stoploss_on_exchange": True,
|
2020-02-16 12:20:11 +00:00
|
|
|
"ohlcv_candle_limit": 1500,
|
|
|
|
}
|
2020-03-24 19:57:12 +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' and stop_loss > float(order['price'])
|
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
@retrier(retries=0)
|
2020-03-24 19:57:12 +00:00
|
|
|
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
|
|
|
|
"""
|
2020-06-01 09:33:40 +00:00
|
|
|
Creates a stoploss order.
|
|
|
|
depending on order_types.stoploss configuration, uses 'market' or limit order.
|
|
|
|
|
|
|
|
Limit orders are defined by having orderPrice set, otherwise a market order is used.
|
2020-03-24 19:57:12 +00:00
|
|
|
"""
|
2020-06-01 09:33:40 +00:00
|
|
|
limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
|
|
|
|
limit_rate = stop_price * limit_price_pct
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
ordertype = "stop"
|
|
|
|
|
|
|
|
stop_price = self.price_to_precision(pair, stop_price)
|
|
|
|
|
|
|
|
if self._config['dry_run']:
|
|
|
|
dry_order = self.dry_run_order(
|
|
|
|
pair, ordertype, "sell", amount, stop_price)
|
|
|
|
return dry_order
|
|
|
|
|
|
|
|
try:
|
|
|
|
params = self._params.copy()
|
2020-06-01 09:33:40 +00:00
|
|
|
if order_types.get('stoploss', 'market') == 'limit':
|
|
|
|
# set orderPrice to place limit order, otherwise it's a market order
|
|
|
|
params['orderPrice'] = limit_rate
|
2020-03-24 19:57:12 +00:00
|
|
|
|
|
|
|
amount = self.amount_to_precision(pair, amount)
|
|
|
|
|
|
|
|
order = self._api.create_order(symbol=pair, type=ordertype, side='sell',
|
|
|
|
amount=amount, price=stop_price, params=params)
|
|
|
|
logger.info('stoploss order added for %s. '
|
|
|
|
'stop price: %s.', pair, stop_price)
|
|
|
|
return order
|
|
|
|
except ccxt.InsufficientFunds as e:
|
2020-08-14 07:57:13 +00:00
|
|
|
raise InsufficientFundsError(
|
2020-06-14 04:31:05 +00:00
|
|
|
f'Insufficient funds to create {ordertype} sell order on market {pair}. '
|
2020-03-24 19:57:12 +00:00
|
|
|
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
|
|
|
|
f'Message: {e}') from e
|
|
|
|
except ccxt.InvalidOrder as e:
|
|
|
|
raise InvalidOrderException(
|
|
|
|
f'Could not create {ordertype} sell order on market {pair}. '
|
|
|
|
f'Tried to create stoploss with amount {amount} at stoploss {stop_price}. '
|
|
|
|
f'Message: {e}') from e
|
2020-06-28 09:17:06 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2020-03-24 19:57:12 +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
|
2020-03-25 16:01:11 +00:00
|
|
|
|
2020-08-02 08:24:10 +00:00
|
|
|
@retrier(retries=5)
|
2020-06-28 14:30:24 +00:00
|
|
|
def fetch_stoploss_order(self, order_id: str, pair: str) -> Dict:
|
2020-03-25 16:01:11 +00:00
|
|
|
if self._config['dry_run']:
|
|
|
|
try:
|
|
|
|
order = self._dry_run_open_orders[order_id]
|
|
|
|
return order
|
|
|
|
except KeyError as e:
|
|
|
|
# Gracefully handle errors with dry-run orders.
|
|
|
|
raise InvalidOrderException(
|
|
|
|
f'Tried to get an invalid dry-run-order (id: {order_id}). Message: {e}') from e
|
|
|
|
try:
|
2020-06-01 08:05:14 +00:00
|
|
|
orders = self._api.fetch_orders(pair, None, params={'type': 'stop'})
|
2020-03-25 16:01:11 +00:00
|
|
|
|
|
|
|
order = [order for order in orders if order['id'] == order_id]
|
|
|
|
if len(order) == 1:
|
|
|
|
return order[0]
|
|
|
|
else:
|
2020-06-14 04:31:05 +00:00
|
|
|
raise InvalidOrderException(f"Could not get stoploss order for id {order_id}")
|
2020-03-25 16:01:11 +00:00
|
|
|
|
|
|
|
except ccxt.InvalidOrder as e:
|
|
|
|
raise InvalidOrderException(
|
|
|
|
f'Tried to get an invalid order (id: {order_id}). Message: {e}') from e
|
2020-06-28 09:17:06 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2020-03-25 16:01:11 +00:00
|
|
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
|
|
raise TemporaryError(
|
|
|
|
f'Could not get order due to {e.__class__.__name__}. Message: {e}') from e
|
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|
|
|
|
|
|
|
|
@retrier
|
2020-04-18 17:52:21 +00:00
|
|
|
def cancel_stoploss_order(self, order_id: str, pair: str) -> Dict:
|
2020-03-25 16:01:11 +00:00
|
|
|
if self._config['dry_run']:
|
2020-04-18 17:52:21 +00:00
|
|
|
return {}
|
2020-03-25 16:01:11 +00:00
|
|
|
try:
|
|
|
|
return self._api.cancel_order(order_id, pair, params={'type': 'stop'})
|
|
|
|
except ccxt.InvalidOrder as e:
|
|
|
|
raise InvalidOrderException(
|
|
|
|
f'Could not cancel order. Message: {e}') from e
|
2020-06-28 09:17:06 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2020-03-25 16:01:11 +00:00
|
|
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
|
|
raise TemporaryError(
|
|
|
|
f'Could not cancel order due to {e.__class__.__name__}. Message: {e}') from e
|
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|