-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Exchanges] support spot stop losses #1396
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,39 @@ class Coinbase(exchanges.RestExchange): | |
("insufficient balance in source account", ) | ||
] | ||
|
||
# should be overridden locally to match exchange support | ||
SUPPORTED_ELEMENTS = { | ||
trading_enums.ExchangeTypes.FUTURE.value: { | ||
# order that should be self-managed by OctoBot | ||
trading_enums.ExchangeSupportedElements.UNSUPPORTED_ORDERS.value: [ | ||
trading_enums.TraderOrderType.STOP_LOSS, | ||
trading_enums.TraderOrderType.STOP_LOSS_LIMIT, | ||
trading_enums.TraderOrderType.TAKE_PROFIT, | ||
trading_enums.TraderOrderType.TAKE_PROFIT_LIMIT, | ||
trading_enums.TraderOrderType.TRAILING_STOP, | ||
trading_enums.TraderOrderType.TRAILING_STOP_LIMIT | ||
], | ||
# order that can be bundled together to create them all in one request | ||
# not supported or need custom mechanics with batch orders | ||
trading_enums.ExchangeSupportedElements.SUPPORTED_BUNDLED_ORDERS.value: {}, | ||
}, | ||
trading_enums.ExchangeTypes.SPOT.value: { | ||
# order that should be self-managed by OctoBot | ||
trading_enums.ExchangeSupportedElements.UNSUPPORTED_ORDERS.value: [ | ||
# trading_enums.TraderOrderType.STOP_LOSS, # supported on spot (as spot limit) | ||
trading_enums.TraderOrderType.STOP_LOSS_LIMIT, | ||
trading_enums.TraderOrderType.TAKE_PROFIT, | ||
trading_enums.TraderOrderType.TAKE_PROFIT_LIMIT, | ||
trading_enums.TraderOrderType.TRAILING_STOP, | ||
trading_enums.TraderOrderType.TRAILING_STOP_LIMIT | ||
], | ||
# order that can be bundled together to create them all in one request | ||
trading_enums.ExchangeSupportedElements.SUPPORTED_BUNDLED_ORDERS.value: {}, | ||
} | ||
} | ||
# stop limit price is 2% bellow trigger price to ensure instant fill | ||
STOP_LIMIT_ORDER_INSTANT_FILL_PRICE_RATIO = decimal.Decimal("0.98") | ||
|
||
@classmethod | ||
def get_name(cls): | ||
return 'coinbase' | ||
|
@@ -253,6 +286,26 @@ async def get_order(self, exchange_order_id: str, symbol: str = None, **kwargs: | |
# override for retrier | ||
return await super().get_order(exchange_order_id, symbol=symbol, **kwargs) | ||
|
||
async def _create_market_stop_loss_order(self, symbol, quantity, price, side, current_price, params=None) -> dict: | ||
# warning coinbase only supports stop limit orders, stop markets are not available | ||
stop_price = price | ||
price = float(decimal.Decimal(str(price)) * self.STOP_LIMIT_ORDER_INSTANT_FILL_PRICE_RATIO) | ||
# use limit stop loss with a "normally instantly" filled price | ||
return await self._create_limit_stop_loss_order(symbol, quantity, price, stop_price, side, params=params) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. up to use _create_limit_stop_loss_order |
||
|
||
@_coinbase_retrier | ||
async def _create_limit_stop_loss_order(self, symbol, quantity, price, stop_price, side, params=None) -> dict: | ||
params = params or {} | ||
if "stopLossPrice" not in params: | ||
params["stopLossPrice"] = stop_price # make ccxt understand that it's a stop loss | ||
order = self.connector.adapter.adapt_order( | ||
await self.connector.client.create_order( | ||
symbol, trading_enums.TradeOrderType.LIMIT.value, side, quantity, price, params=params | ||
), | ||
symbol=symbol, quantity=quantity | ||
) | ||
return order | ||
|
||
def _get_ohlcv_params(self, time_frame, input_limit, **kwargs): | ||
limit = input_limit | ||
if not input_limit or input_limit > self.MAX_PAGINATION_LIMIT: | ||
|
@@ -311,6 +364,21 @@ def _register_exchange_fees(self, order_or_trade): | |
except (KeyError, TypeError): | ||
pass | ||
|
||
def _update_stop_order_or_trade_type_and_price(self, order_or_trade: dict): | ||
if stop_price := order_or_trade.get(trading_enums.ExchangeConstantsOrderColumns.STOP_PRICE.value): | ||
# from https://bingx-api.github.io/docs/#/en-us/spot/trade-api.html#Current%20Open%20Orders | ||
limit_price = order_or_trade.get(trading_enums.ExchangeConstantsOrderColumns.PRICE.value) | ||
# use stop price as order price to parse it properly | ||
order_or_trade[trading_enums.ExchangeConstantsOrderColumns.PRICE.value] = stop_price | ||
# type is TAKE_STOP_LIMIT (not unified) | ||
if order_or_trade.get(trading_enums.ExchangeConstantsOrderColumns.TYPE.value) not in ( | ||
trading_enums.TradeOrderType.STOP_LOSS.value, trading_enums.TradeOrderType.TAKE_PROFIT.value | ||
): | ||
# Force stop loss. Add order direction parsing logic to handle take profits if necessary | ||
order_or_trade[trading_enums.ExchangeConstantsOrderColumns.TYPE.value] = ( | ||
trading_enums.TradeOrderType.STOP_LOSS.value # simulate market stop loss | ||
) | ||
|
||
def fix_order(self, raw, **kwargs): | ||
""" | ||
Handle 'order_type': 'UNKNOWN_ORDER_TYPE in coinbase order response (translated into None in ccxt order type) | ||
|
@@ -336,6 +404,7 @@ def fix_order(self, raw, **kwargs): | |
'takeProfitPrice': None, 'stopLossPrice': None, 'exchange_id': 'd7471b4e-960e-4c92-bdbf-755cb92e176b'} | ||
""" | ||
fixed = super().fix_order(raw, **kwargs) | ||
self._update_stop_order_or_trade_type_and_price(fixed) | ||
if fixed[ccxt_enums.ExchangeOrderCCXTColumns.TYPE.value] is None: | ||
if fixed[ccxt_enums.ExchangeOrderCCXTColumns.STOP_PRICE.value] is not None: | ||
# stop price set: stop order | ||
|
@@ -361,6 +430,7 @@ def fix_order(self, raw, **kwargs): | |
def fix_trades(self, raw, **kwargs): | ||
raw = super().fix_trades(raw, **kwargs) | ||
for trade in raw: | ||
self._update_stop_order_or_trade_type_and_price(trade) | ||
trade[trading_enums.ExchangeConstantsOrderColumns.STATUS.value] = trading_enums.OrderStatus.CLOSED.value | ||
try: | ||
if trade[trading_enums.ExchangeConstantsOrderColumns.AMOUNT.value] is None and \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also allow stop loss limit as it is supported?