stable/freqtrade/exchange/kraken.py

192 lines
7.9 KiB
Python
Raw Normal View History

2019-02-17 03:01:17 +00:00
""" Kraken exchange subclass """
import logging
from typing import Any, Dict, List, Optional, Tuple
2019-02-17 03:01:17 +00:00
import ccxt
from freqtrade.enums import Collateral, TradingMode
2020-09-28 17:39:41 +00:00
from freqtrade.exceptions import (DDosProtection, InsufficientFundsError, InvalidOrderException,
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
2020-09-28 17:39:41 +00:00
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,
2020-12-20 10:44:50 +00:00
"ohlcv_candle_limit": 720,
2019-08-14 17:22:52 +00:00
"trades_pagination": "id",
"trades_pagination_arg": "since",
}
_supported_trading_mode_collateral_pairs: List[Tuple[TradingMode, Collateral]] = [
# TradingMode.SPOT always supported and not required in this list
# (TradingMode.MARGIN, Collateral.CROSS), # TODO-lev: Uncomment once supported
# (TradingMode.FUTURES, Collateral.CROSS) # TODO-lev: No CCXT support
]
def market_is_tradable(self, market: Dict[str, Any]) -> bool:
"""
Check if the market symbol is tradable by Freqtrade.
Default checks + check if pair is darkpool pair.
"""
parent_check = super().market_is_tradable(market)
return (parent_check and
market.get('darkpool', False) is False)
2021-09-09 19:42:43 +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()
order_list = [(x["symbol"].split("/")[0 if x["side"] == "sell" else 1],
x["remaining"] if x["side"] == "sell" else x["remaining"] * x["price"],
# Don't remove the below comment, this can be important for debugging
# x["side"], x["amount"],
) for x in orders]
for bal in balances:
if not isinstance(balances[bal], dict):
continue
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
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
2021-07-26 06:01:57 +00:00
def stoploss_adjust(self, stop_loss: float, order: Dict, side: str) -> bool:
"""
Verify stop_loss against stoploss-order value (limit or price)
Returns True if adjustment is necessary.
"""
return (order['type'] in ('stop-loss', 'stop-loss-limit') and (
(side == "sell" and stop_loss > float(order['price'])) or
(side == "buy" and stop_loss < float(order['price']))
))
2021-09-09 19:42:43 +00:00
@retrier(retries=0)
2021-09-17 05:05:13 +00:00
def stoploss(self, pair: str, amount: float, stop_price: float,
order_types: Dict, side: str, leverage: float) -> Dict:
2020-01-19 13:08:47 +00:00
"""
Creates a stoploss market order.
Stoploss market orders is the only stoploss type supported by kraken.
"""
2020-11-25 15:27:27 +00:00
params = self._params.copy()
2020-01-19 13:08:47 +00:00
2020-11-25 15:27:27 +00:00
if order_types.get('stoploss', 'market') == 'limit':
ordertype = "stop-loss-limit"
limit_price_pct = order_types.get('stoploss_on_exchange_limit_ratio', 0.99)
if side == "sell":
limit_rate = stop_price * limit_price_pct
else:
limit_rate = stop_price * (2 - limit_price_pct)
2020-11-25 15:27:27 +00:00
params['price2'] = self.price_to_precision(pair, limit_rate)
else:
ordertype = "stop-loss"
2020-01-19 13:08:47 +00:00
stop_price = self.price_to_precision(pair, stop_price)
if self._config['dry_run']:
dry_order = self.create_dry_run_order(
2021-09-17 05:05:13 +00:00
pair, ordertype, side, amount, stop_price, leverage)
2020-01-19 13:08:47 +00:00
return dry_order
try:
amount = self.amount_to_precision(pair, amount)
order = self._api.create_order(symbol=pair, type=ordertype, side=side,
amount=amount, price=stop_price, params=params)
2021-06-10 18:09:25 +00:00
self._log_exchange_response('create_stoploss_order', order)
2020-01-19 13:08:47 +00:00
logger.info('stoploss order added for %s. '
'stop price: %s.', pair, stop_price)
return order
except ccxt.InsufficientFunds as e:
raise InsufficientFundsError(
f'Insufficient funds to create {ordertype} {side} order on market {pair}. '
2020-01-19 13:08:47 +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} {side} order on market {pair}. '
2020-01-19 13:08:47 +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-01-19 13:08:47 +00:00
except (ccxt.NetworkError, ccxt.ExchangeError) as e:
raise TemporaryError(
f'Could not place {side} order due to {e.__class__.__name__}. Message: {e}') from e
2020-01-19 13:08:47 +00:00
except ccxt.BaseError as e:
raise OperationalException(e) from e
def fill_leverage_brackets(self):
"""
Assigns property _leverage_brackets to a dictionary of information about the leverage
allowed on each pair
"""
leverages = {}
for pair, market in self.markets.items():
leverages[pair] = [1]
info = market['info']
leverage_buy = info.get('leverage_buy', [])
leverage_sell = info.get('leverage_sell', [])
if len(leverage_buy) > 0 or len(leverage_sell) > 0:
if leverage_buy != leverage_sell:
logger.warning(
f"The buy({leverage_buy}) and sell({leverage_sell}) leverage are not equal"
"for {pair}. Please notify freqtrade because this has never happened before"
)
if max(leverage_buy) <= max(leverage_sell):
leverages[pair] += [int(lev) for lev in leverage_buy]
else:
leverages[pair] += [int(lev) for lev in leverage_sell]
else:
leverages[pair] += [int(lev) for lev in leverage_buy]
self._leverage_brackets = leverages
def get_max_leverage(self, pair: Optional[str], nominal_value: Optional[float]) -> float:
"""
Returns the maximum leverage that a pair can be traded at
:param pair: The base/quote currency pair being traded
:nominal_value: Here for super class, not needed on Kraken
"""
return float(max(self._leverage_brackets[pair]))
def _set_leverage(
self,
leverage: float,
2021-09-12 09:09:51 +00:00
pair: Optional[str] = None,
trading_mode: Optional[TradingMode] = None
):
"""
Kraken set's the leverage as an option in the order object, so we need to
add it to params
"""
2021-09-16 03:55:19 +00:00
return
def _get_params(self, ordertype: str, leverage: float, time_in_force: str = 'gtc') -> Dict:
params = super()._get_params(ordertype, leverage, time_in_force)
if leverage > 1.0:
2021-09-16 03:55:19 +00:00
params['leverage'] = leverage
return params