nodejs-order-book icon indicating copy to clipboard operation
nodejs-order-book copied to clipboard

Stop-limit, stop-market and OCO orders

Open vanodevium opened this issue 2 years ago • 6 comments

Do you have any plans to implement these types?

vanodevium avatar Mar 21 '23 12:03 vanodevium

Hi @vanodevium thanks for your feedback. Feel free to open a PR

fasenderos avatar Mar 21 '23 14:03 fasenderos

@fasenderos I have big problems with TS. Can code only using JS. Sorry. But I have implemented this functionality already. So I can describe it if you need, of course.

vanodevium avatar Mar 29 '23 20:03 vanodevium

@vanodevium in my opinion, the sole responsibility of an order book is to match buy order with sell order and vice versa. That's why I'm thinking to create a separate repo that handles, among other things, these types of orders, check the user balance, track trades and more.

fasenderos avatar Apr 05 '23 18:04 fasenderos

Anyway please share your code so it will be easy for me to add these features to the new repo. Thanks

fasenderos avatar Apr 05 '23 18:04 fasenderos

In my system, incoming signals are already objects of orders: each already has an ID, type, price.

Every incoming StopLimit or StopMarket order has property stop-price.

The matcher itself has a stopbook: it is a hashmap, where the key is the stop price, and the value is the usual queue of orders:

{
    "stop-price1": queue of stop orders,
    "stop-price2": queue of stop orders,
}

When a MARKET-LIMIT or LIMIT-LIMIT trade takes place in matcher, we know the last price of the trade.

We go to the stopbook and activate all the stop-orders that are in the queue for this stop-price. Activation is several stages:

  • remove this stop order from the stopbook
  • transform the order type (StopLimit to Limit, StopMarket to Market), remove property stop-price
  • this new order is appended into the order book if it's LIMIT, or executed if it's Marker

In my system, the result of the matcher's work is events broadcasting, so when stop orders are activated, I emit the event:

{
    "type": "activated",
    "ts": Date.now(),
    "id": "ID of activated order"
}

Feel free to ask any questions. Thanx!

vanodevium avatar Apr 05 '23 18:04 vanodevium

@vanodevium Really cool logic, I have a question regarding the hashmap: With key is the stop price "stop-price1", "stop-price2" how can you find all the keys at lower ( or greater) than a market price?

hlongvu avatar Jan 29 '24 03:01 hlongvu

@vanodevium I know it has been a long time, but I have finally managed to introduce conditional orders. At the moment, I do not yet consider this new feature stable. If you have time to test it, you would be doing me a big favor. The version is v6.1.0-beta.0.

fasenderos avatar Jul 22 '24 09:07 fasenderos

The version v6.1.0-beta.1 now also supports OCO Orders. The coverage of these new features is at 100%, but before considering them stable, I would like to test them with real-world cases. In any case, I consider this issue closed.

fasenderos avatar Jul 28 '24 00:07 fasenderos