Skip to content
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

Ability to set custom stoploss before entry #9233

Open
Axel-CH opened this issue Sep 25, 2023 · 3 comments
Open

Ability to set custom stoploss before entry #9233

Axel-CH opened this issue Sep 25, 2023 · 3 comments
Labels
Enhancement Enhancements to the bot. Get lower priority than bugs by default. Stoploss Related to stoploss

Comments

@Axel-CH
Copy link
Contributor

Axel-CH commented Sep 25, 2023

Context

Using custom entry or exit price, user can wait postpone his entry or exit when the market reach a price level.
It is a great tool to enter at key levels or market structure (last period low, last period high, Fibonacci retracement / extension, etc...)
However, for an entry case, if the market reach the desired price, but violently move deeper, there is a big loss potential, as the stop loss is not yet placed on exchange.

Describe the enhancement

Being able to take advantage of the possibility to create entry stop loss directly within entry order could mitigate greatly that risk.

Why?

  1. It will allow an increased risk protection on the position in case of sudden price change, as there is no gap in time between entry and stop-loss on exchange activation.

  2. It will allow more precise stop loss based on user defined market structure.

Explain the enhancement you would like
Ability to set custom stop-loss in entry order

Note
This feature will be particularly useful if user want to enter at custom price, and exit immediately if a price level is not held.
Tested with ccxt and bybit with this sample code:

# SL set at order creation existing order
symbol = 'LOOM/USDT:USDT'
type = 'limit'  # or 'market'
side = 'buy'
amount = 11  # your amount
price = 0.10  # your price
params = {
    'stopLoss': {
        'type': 'limit',  # or 'market', this field is not necessary if limit price is specified
        'price': 0.09,  # limit price for a limit stop loss order
        'triggerPrice': 0.095,
    }
}
order = exchange.create_order (symbol, type, side, amount, price, params)

On exchange Order:
entry_order_with_sl_lq

Implementation idea:
As custom_stoploss require an active Trade object, I would add a new callback named initial_custom_stoploss or entry_custom_stoploss

@xmatthias xmatthias added the Question Questions - will be closed after some period of inactivity. label Sep 26, 2023
@xmatthias
Copy link
Member

I don't think that this type of order placement has wide-spread support (so far, i've only seen it on bybit futures).

I only see this as worth the time if it's supported by multiple exchanges, in which case it can provide benefits to a wider userbase - so please let me know which of binance/okx/gate do support this (obviously using the same ccxt interface - and on which markets this support exists (spot, futures)).

Also, please investigate how this needs to be handled after the fact (like - how do we get the order-id for these additional orders - across the supported exchanges)?

i've briefly looked at this for bybit, and it seems that while the placement of the initial orders is pretty simple - you only get one order id back, so you essentially can't track what happens to the "other" orders once the initial order fills.


From an interface perspective - that's the wront point in time for this.
first investigate the above points - which will lead to a decision about the "if" - then we can have a look the "how".

I've marked this as question for now - as i'm pretty sure it'll be a bybit only feature - but proof me wrong, and it may be a nice enhancement.

@Axel-CH
Copy link
Contributor Author

Axel-CH commented Sep 26, 2023

You are right on the fact that this type of order is not a standard and It could be complicated to track the order once created within an entry.
I think we can achieve the same goal of having a stop-loss order on exchange before entry via different way:
Using a distinct stop market order or limit.
A majority of exchanges accept such order, as it is not a "real" order in order-book, but an instruction to add it if possible and at certain conditions.

You can see on majority of exchange, for perpetual contracts, I can put theses trigger order before having an actual position, even an open order. This way we can have a separate "orderid" that will allow us to manage the order normally.
We just have to make sure to be in "reduceOnly" mode at each time.

Exemples:
OKX
trigger_exit_order_okx
Kucoin
trigger_exit_order_kucoin
Bybit
trigger_exit_order_bybit

@Axel-CH Axel-CH changed the title Ability to set custom stoploss directly in entry order Ability to set custom stoploss before entry Sep 26, 2023
@xmatthias
Copy link
Member

it's a possibility eventually in the future - but you need to be VERY cautious to ensure orders are properly canceled alongside other orders / deleting the trade - which is all logic we currently don't need/have.
It'll also need logic / handling for markets / exchanges that don't support this (e.g. binance spot, where a stop order does consume the amount, and fails to place if you don't own it or it's tied up in another order).

i don't see the need for anything new though from an interface perspective.

The way i see the process:

  • place the entry order
  • trade object is created
  • initial "stop" call is made
  • stop order is created

that's a gap of milliseconds (depending on the exchange) - if you place your order to "not fill immediately" - it shouldn't be a problem
if it does - it's also not a realistic problem, as then you'd have to be "filled immediately, and market crashes within 1 second".

It'll however not happen soon ... things like stoploss_order_id will make this cumbersome and difficult to handle.

@xmatthias xmatthias added Enhancement Enhancements to the bot. Get lower priority than bugs by default. and removed Question Questions - will be closed after some period of inactivity. labels Sep 27, 2023
@xmatthias xmatthias added the Stoploss Related to stoploss label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Enhancements to the bot. Get lower priority than bugs by default. Stoploss Related to stoploss
Projects
None yet
Development

No branches or pull requests

2 participants