Stop-limit, stop-market and OCO orders
Do you have any plans to implement these types?
Hi @vanodevium thanks for your feedback. Feel free to open a PR
@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 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.
Anyway please share your code so it will be easy for me to add these features to the new repo. Thanks
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 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?
@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.
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.