2020-02-16 12:20:11 +00:00
|
|
|
""" FTX exchange subclass """
|
|
|
|
import logging
|
2021-10-17 13:06:55 +00:00
|
|
|
from typing import Any, Dict, List, Tuple
|
2020-02-16 12:20:11 +00:00
|
|
|
|
2020-03-24 19:57:12 +00:00
|
|
|
import ccxt
|
2021-09-09 07:44:35 +00:00
|
|
|
|
2022-02-01 18:53:38 +00:00
|
|
|
from freqtrade.enums import MarginMode, TradingMode
|
2020-09-28 17:39:41 +00:00
|
|
|
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, InvalidOrderException,
|
|
|
|
OperationalException, TemporaryError)
|
2021-09-08 20:50:30 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2020-08-22 15:35:42 +00:00
|
|
|
from freqtrade.exchange.common import API_FETCH_ORDER_RETRY_COUNT, retrier
|
2021-05-16 07:08:13 +00:00
|
|
|
from freqtrade.misc import safe_value_fallback2
|
2021-09-09 07:19:24 +00:00
|
|
|
|
2020-09-28 17:39:41 +00:00
|
|
|
|
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,
|
2022-01-06 13:12:00 +00:00
|
|
|
"ohlcv_volume_currency": "quote",
|
2021-11-23 09:19:59 +00:00
|
|
|
"mark_ohlcv_price": "index",
|
2021-12-05 09:01:44 +00:00
|
|
|
"mark_ohlcv_timeframe": "1h",
|
2020-02-16 12:20:11 +00:00
|
|
|
}
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2022-02-01 18:53:38 +00:00
|
|
|
_supported_trading_mode_margin_pairs: List[Tuple[TradingMode, MarginMode]] = [
|
2021-09-19 23:02:09 +00:00
|
|
|
# TradingMode.SPOT always supported and not required in this list
|
2022-02-01 18:53:38 +00:00
|
|
|
# (TradingMode.MARGIN, MarginMode.CROSS),
|
|
|
|
# (TradingMode.FUTURES, MarginMode.CROSS)
|
2021-09-19 23:02:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool:
|
2020-03-24 19:57:12 +00:00
|
|
|
"""
|
|
|
|
Verify stop_loss against stoploss-order value (limit or price)
|
|
|
|
Returns True if adjustment is necessary.
|
|
|
|
"""
|
2021-09-19 23:02:09 +00:00
|
|
|
return order['type'] == 'stop' and (
|
|
|
|
side == "sell" and stop_loss > float(order['price']) or
|
|
|
|
side == "buy" and stop_loss < float(order['price'])
|
|
|
|
)
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2020-06-28 09:56:29 +00:00
|
|
|
@retrier(retries=0)
|
2021-09-19 23:02:09 +00:00
|
|
|
def stoploss(self, pair: str, amount: float, stop_price: float,
|
|
|
|
order_types: Dict, side: str, leverage: float) -> Dict:
|
2020-03-24 19:57:12 +00:00
|
|
|
"""
|
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)
|
2021-09-19 23:02:09 +00:00
|
|
|
if side == "sell":
|
|
|
|
limit_rate = stop_price * limit_price_pct
|
|
|
|
else:
|
|
|
|
limit_rate = stop_price * (2 - 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']:
|
2021-04-10 11:50:56 +00:00
|
|
|
dry_order = self.create_dry_run_order(
|
2022-03-03 19:51:52 +00:00
|
|
|
pair, ordertype, side, amount, stop_price, leverage, stop_loss=True)
|
2020-03-24 19:57:12 +00:00
|
|
|
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
|
2022-02-02 04:23:05 +00:00
|
|
|
if self.trading_mode == TradingMode.FUTURES:
|
|
|
|
params.update({'reduceOnly': True})
|
2020-03-24 19:57:12 +00:00
|
|
|
|
2021-04-12 14:01:46 +00:00
|
|
|
params['stopPrice'] = stop_price
|
2020-03-24 19:57:12 +00:00
|
|
|
amount = self.amount_to_precision(pair, amount)
|
|
|
|
|
2022-02-02 06:28:57 +00:00
|
|
|
self._lev_prep(pair, leverage, side)
|
2021-09-19 23:02:09 +00:00
|
|
|
order = self._api.create_order(symbol=pair, type=ordertype, side=side,
|
2021-04-12 14:01:46 +00:00
|
|
|
amount=amount, params=params)
|
2021-06-10 18:09:25 +00:00
|
|
|
self._log_exchange_response('create_stoploss_order', order)
|
2020-03-24 19:57:12 +00:00
|
|
|
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(
|
2021-09-19 23:02:09 +00:00
|
|
|
f'Insufficient funds to create {ordertype} {side} 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(
|
2021-09-19 23:02:09 +00:00
|
|
|
f'Could not create {ordertype} {side} 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
|
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(
|
2021-09-19 23:02:09 +00:00
|
|
|
f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') from e
|
2020-03-24 19:57:12 +00:00
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|
2020-03-25 16:01:11 +00:00
|
|
|
|
2020-08-22 15:35:42 +00:00
|
|
|
@retrier(retries=API_FETCH_ORDER_RETRY_COUNT)
|
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']:
|
2021-06-02 09:06:32 +00:00
|
|
|
return self.fetch_dry_run_order(order_id)
|
|
|
|
|
2020-03-25 16:01:11 +00:00
|
|
|
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]
|
2021-06-10 18:09:25 +00:00
|
|
|
self._log_exchange_response('fetch_stoploss_order', order)
|
2020-03-25 16:01:11 +00:00
|
|
|
if len(order) == 1:
|
2021-06-13 09:06:34 +00:00
|
|
|
if order[0].get('status') == 'closed':
|
|
|
|
# Trigger order was triggered ...
|
|
|
|
real_order_id = order[0].get('info', {}).get('orderId')
|
2022-02-19 09:07:32 +00:00
|
|
|
# OrderId may be None for stoploss-market orders
|
|
|
|
# But contains "average" in these cases.
|
|
|
|
if real_order_id:
|
|
|
|
order1 = self._api.fetch_order(real_order_id, pair)
|
|
|
|
self._log_exchange_response('fetch_stoploss_order1', order1)
|
|
|
|
# Fake type to stop - as this was really a stop order.
|
|
|
|
order1['id_stop'] = order1['id']
|
|
|
|
order1['id'] = order_id
|
|
|
|
order1['type'] = 'stop'
|
|
|
|
order1['status_stop'] = 'triggered'
|
|
|
|
return order1
|
2021-06-13 09:06:34 +00:00
|
|
|
|
2020-03-25 16:01:11 +00:00
|
|
|
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:
|
2021-06-10 18:09:25 +00:00
|
|
|
order = self._api.cancel_order(order_id, pair, params={'type': 'stop'})
|
|
|
|
self._log_exchange_response('cancel_stoploss_order', order)
|
|
|
|
return order
|
2020-03-25 16:01:11 +00:00
|
|
|
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
|
2021-05-16 07:08:13 +00:00
|
|
|
|
|
|
|
def get_order_id_conditional(self, order: Dict[str, Any]) -> str:
|
|
|
|
if order['type'] == 'stop':
|
2021-06-13 09:06:34 +00:00
|
|
|
return safe_value_fallback2(order, order, 'id_stop', 'id')
|
2021-05-16 07:08:13 +00:00
|
|
|
return order['id']
|