2019-02-17 03:01:17 +00:00
|
|
|
""" Kraken exchange subclass """
|
|
|
|
import logging
|
|
|
|
from typing import Dict
|
|
|
|
|
2019-09-11 04:58:10 +00:00
|
|
|
import ccxt
|
|
|
|
|
2020-06-28 09:17:06 +00:00
|
|
|
from freqtrade.exceptions import (DependencyException, InvalidOrderException, DDosProtection,
|
2020-01-19 13:08:47 +00:00
|
|
|
OperationalException, TemporaryError)
|
2019-02-17 03:01:17 +00:00
|
|
|
from freqtrade.exchange import Exchange
|
2020-05-18 12:20:51 +00:00
|
|
|
from freqtrade.exchange.common import retrier
|
2019-02-17 03:01:17 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Kraken(Exchange):
|
|
|
|
|
2019-02-17 14:54:22 +00:00
|
|
|
_params: Dict = {"trading_agreement": "agree"}
|
2019-08-14 17:22:52 +00:00
|
|
|
_ft_has: Dict = {
|
2020-01-19 13:08:47 +00:00
|
|
|
"stoploss_on_exchange": True,
|
2019-08-14 17:22:52 +00:00
|
|
|
"trades_pagination": "id",
|
|
|
|
"trades_pagination_arg": "since",
|
|
|
|
}
|
2019-09-11 04:58:10 +00:00
|
|
|
|
|
|
|
@retrier
|
|
|
|
def get_balances(self) -> dict:
|
|
|
|
if self._config['dry_run']:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
balances = self._api.fetch_balance()
|
|
|
|
# Remove additional info from ccxt results
|
|
|
|
balances.pop("info", None)
|
|
|
|
balances.pop("free", None)
|
|
|
|
balances.pop("total", None)
|
|
|
|
balances.pop("used", None)
|
|
|
|
|
|
|
|
orders = self._api.fetch_open_orders()
|
2019-09-12 05:03:52 +00:00
|
|
|
order_list = [(x["symbol"].split("/")[0 if x["side"] == "sell" else 1],
|
|
|
|
x["remaining"],
|
|
|
|
# Don't remove the below comment, this can be important for debuggung
|
|
|
|
# x["side"], x["amount"],
|
|
|
|
) for x in orders]
|
2019-09-11 04:58:10 +00:00
|
|
|
for bal in balances:
|
|
|
|
balances[bal]['used'] = sum(order[1] for order in order_list if order[0] == bal)
|
|
|
|
balances[bal]['free'] = balances[bal]['total'] - balances[bal]['used']
|
|
|
|
|
|
|
|
return balances
|
2020-06-28 09:17:06 +00:00
|
|
|
except ccxt.DDoSProtection as e:
|
|
|
|
raise DDosProtection(e) from e
|
2019-09-11 04:58:10 +00:00
|
|
|
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
|
|
|
|
raise TemporaryError(
|
|
|
|
f'Could not get balance due to {e.__class__.__name__}. Message: {e}') from e
|
|
|
|
except ccxt.BaseError as e:
|
|
|
|
raise OperationalException(e) from e
|
2020-01-19 13:08:47 +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' and stop_loss > float(order['price'])
|
|
|
|
|
2020-01-19 13:08:47 +00:00
|
|
|
def stoploss(self, pair: str, amount: float, stop_price: float, order_types: Dict) -> Dict:
|
|
|
|
"""
|
|
|
|
Creates a stoploss market order.
|
|
|
|
Stoploss market orders is the only stoploss type supported by kraken.
|
|
|
|
"""
|
|
|
|
|
|
|
|
ordertype = "stop-loss"
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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:
|
|
|
|
raise DependencyException(
|
|
|
|
f'Insufficient funds to create {ordertype} sell order on market {pair}.'
|
|
|
|
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-01-19 13:08:47 +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
|