question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unify 'stopPrice' in createOrder

See original GitHub issue

Users can set stop orders (aka conditional orders, aka trigger orders, aka algo orders) by assigning params['stopPrice'].

When type='market' it’s a stop market order, and when type='limit' it’s a stop limit order Futures/Swap exchanges should set the exchange parameter for reduce only to true

Some exchanges have separate endpoints for stoploss orders and regular orders, so make sure that fetchOrder and cancelOrder work as well


Just to reiterate:

PRs for reference:


First, there’s two types/cases of “stops” and we have to differentiate between the two:

  1. A standalone conditional stop order aka algo order – conditional orders can exist by themselves and can be placed separately from other orders.
  2. A stop loss order and/or a take profit stop order – those orders can not exist by themselves and are usually tied to a position or to another order that opens a margin position (regardless of the market type, whether it is a contract-margin market or a spot-margin market).

This issue https://github.com/ccxt/ccxt/issues/12044 about the stopPrice unification is related to type/case (1) ONLY.


The usage of type/case (1) is the following:

Suppose you want to place an order (b) if the price meets some condition. To do that, you can place a special order (a) called “stop conditional” order. A “stop conditional” order (a) is an order to place another “triggered” order (b). That triggered order (b) can be either an order to open a new contract-position, or it could be a spot-order, pretty much any kind of order. It does not care if you have a position or not. When the price condition is met, then the conditional stop order (a) will be triggered, and it will result in placing another triggered order (b) of type/amount/price that the user specifies. So, in general, to place a conditional stop order (a), the user has to supply to createOrder():

  1. conditional stopPrice – this is related to the conditional order (a) itself
  2. triggered type ('market' or 'limit') – this is related to the order (b) that will be placed by the conditional order, when triggered
  3. triggered amount – this is related to the order (b) that will be placed by the conditional order, when triggered
  4. triggered price (if 'limit') – this is related to the order (b) that will be placed by the conditional order, when triggered

So, the stopPrice conditional order (a) type/case (1) is just a way of conditionally-placing another triggered order (b) if the market meets some condition. It will be triggered when that condition is met. If the desired condition never occurs again, then the conditional order (a) will never be triggered, it will wait until you cancel it. The order (b) won’t be placed at all, if the condition is never met.


The usage of type/case (2) is the following:

Suppose you want to open a contract-margin position or spot-margin position by placing an order (a).

You also want to close that position when your desired unrealized profit meets a certain level, in percentage or in absolute prices. At that point you want to realize or “fix” your unrealized profits on that open position. You want to close a profitable position at some point with another order that would fix the profits on that position. That is called “take profit stop” order. (b)

And you also want to close that position if the market goes against your expectation, and when your losses reach a certain level, you want to stop getting further losses, or _“fix” your losses. That is called a “stop loss” order. ©

In this case, the user wants to place order (a) and attach two stop-orders (b) and © to the resulting position that will be opened by order (a). Therefore, in createOrder(), the user needs to specify a set of params for all three orders:

  1. opening order (a) amount
  2. opening order (a) type ('market' or 'limit')
  3. opening order (a) price (if 'limit')
  4. closing order (b) stopLossPrice – this is optional
  5. closing order © takeProfitPrice – this is optional

Note, that the amount is not specified for orders (b) and ©, cause those orders will usually close the entire amount of the position specified in order (a). Since both orders (b) and © are optional, the user can skip any of the two if he wants to close that position manually. Also, orders of type (b) and © can be added/attached later to an already existing open position.

So, stop-loss/take-profit orders cannot exist by themselves, cause their purpose is to fix losses and profits on existing positions (this works for both contract margin and spot margin).

There is no unification schema for type/case (2) at this time. It is yet to be designed. Probably with a pair of extra params like stopLossPrice and takeProfitPrice (as opposed to stopPrice in case (1)).


Once again, this issue https://github.com/ccxt/ccxt/issues/12044 about the stopPrice unification is related to type/case (1) ONLY.

One more note: of course, the user can cancel/fetch/edit orders (1a) and (1b) as well the user can cancel/fetch/edit orders (2a), (2b) and (2c). The difference is how you place them and to what they’re referred, applied or tied.

Start with the certified exchanges

and these are some popular exchanges it should also be unified in

  • bitfinex
  • bitfinex2
  • bitmex
  • bittrex #12904
  • bybit #13075
  • coinbase #13137
  • coinbaseprime
  • coinbasepro
  • deribit
  • kraken
  • krakenfutures
  • kucoin #12836
  • kucoinfutures
  • phemex

And then the rest of the exchanges

Once it’s unified could you check the exchange please

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:24 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
kroitorcommented, Mar 1, 2022

@DoctorSlimm for example, some exchanges don’t have market orders. For those we used to set exchange.has['createMarketOrder'] = false. That would indicate to a user, that the exchange does not support market orders. There’s a corresponding sub-family of unified helper methods createMarketOrder, createMarketBuyOrder and createMarketSellOrder, in the base classes.

We don’t have a specific flag for stop orders yet, so you can skip this exchange for now, we might add a unified helper like createStopOrder later, and then we will designate the missing stop-order functionality as exchange.has['createStopOrder'] = false or something like that.

2reactions
samgermaincommented, Feb 28, 2022

@ttodua @Dan-krm @carlosmiei @DoctorSlimm

I created a task list, once you unify stopPrice on an exchange, can you check the box for that exchange please?

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unify stopLossPrice/takeProfitPrice in createOrder
This issue is about unifying. stop loss order; take profit order. For an explanation of stopPrice (conditional or trigger orders), ...
Read more >
how to place a stop-buy order by ccxt in binance
You need to set the type argument as stop_loss_limit or take_profit_limit (depending on whether price is greater/less than stopPrice ).
Read more >
CreateOrder() - NinjaTrader 8
stopPrice. Order stop price. Use "0" should this parameter be irrelevant for the OrderType being submitted. oco. A string representing the OCO ID...
Read more >
Exchanges — ccxt 2.4.71 documentation
The Unified CCXT API | │ . | | loadMarkets . fetchBalance | | fetchMarkets . createOrder | | fetchCurrencies . cancelOrder |...
Read more >
Buy-Side Request/Response Service
The first step is for the user to use CreateOrder request to create the orders ... down the stop price, set the content...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found