viabtc / viabtc_exchange_server

A trading engine with high-speed performance and real-time notification
MIT License
2.68k stars 1.48k forks source link

"All or None" orders #164

Open jkmaina opened 5 years ago

jkmaina commented 5 years ago

I have a use case where an order should be fully matched or not at all. For example, when you place a sell order of 2 and you only want a match with a buy order of 2 or more (if lets say buy is 2.5 then it should also match also and trade the 2, with the extra 0.5 quantity remaining as a pending for the buyer.)

How can i implement this?

objectt commented 5 years ago

I will work on this. "order.put_aon"

https://www.investopedia.com/terms/a/aon.asp

jkmaina commented 5 years ago

Exactly, thank you, but with a small difference that both on both sides Qty and Price is placed.

objectt commented 5 years ago

@jkmaina

{"method": "order.put_aon", "params": [1, "BTC", 2, "20", "105", "0", "dev"], "id": 1516681174} params : [ _user_id, market, [sell/buy], amount, price, takerfee, source]

https://github.com/objectt/viabtc_exchange_server/tree/order__aon

I am not sure if this change is appropriate for a pull request. It is still under test, please let me know if you find any issues.

jkmaina commented 5 years ago

Thank you! I will test and get back you asap

jkmaina commented 5 years ago

Great work @objectt on the AON. Here is my feedback on testing:

AON Orders get posted OK but are not matching correctly . Also I am not getting AON orders in the pending order list.

{"method": "order.put_aon", "params": [6,"BCHBTC",1,"5","5291","0","api.v1"],"id":123}

response:

{ "id": 123, "error": null, "result": { "price": "5291", "mtime": 1544478597.98857, "id": 27, "market": "BCHBTC", "user": 6, "deal_fee": "0", "type": 3, "source": "api.v1", "maker_fee": "0", "side": 1, "ctime": 1544478597.98857, "deal_stock": "0", "amount": "5", "left": "5", "taker_fee": "0", "deal_money": "0" } }

However, when i post a buy order (side=2), for same quantity and same price, it does not match - deal stock/money remains zero.

{"method": "order.put_aon", "params": [3,"BCHBTC",2,"5","5291","0","api.v1"],"id":123}

response:

'{ "id": 123, "error": null, "result": { "price": "5291", "mtime": 1544478961.5650671, "id": 30, "market": "BCHBTC", "user": 3, "deal_fee": "0", "type": 3, "source": "api.v1", "maker_fee": "0", "side": 2, "ctime": 1544478961.5650671, "deal_stock": "0", "amount": "5", "left": "5", "taker_fee": "0", "deal_money": "0" } }

Also, when i check for pending orders, they don't show AON orders. Other order types still show.

{"method": "order.pending", "params": [3,"BCHBTC",0,100],"id":123} response: { "id": 123, "error": null, "result": { "limit": 100, "offset": 0, "total": 0, "records": [] } }

I haven't checked finished orders and order deals because of the first issue on matching, but could need validation also.

Could you kindly have a look and update?

Many Thanks James

objectt commented 5 years ago

@jkmaina Thanks for sparing time to test it.

They are intended behaviors. Just like a market order, AON order is not created if it is not matched immediately (Meaning there is only taker for AON orders; no makers). However, this can be adjusted if needed.

In the mean time, place some limit orders first then try corresponding AON orders.

jkmaina commented 5 years ago

@objectt When i match against existing limit orders it works perfectly - thanks.

However, in our case since the liquidity is low, AON needs to have makers and orders should last on the order book until we cancel them - essentially like a limit order.

I think current AON implementation is a "fill or kill" which has an immediate requirement. AON stay for a longer duration, see below link

https://www.investopedia.com/university/intro-to-order-types/duration.asp

Maybe you can rename it to order.put_fillorkill or order.put_fok since the feature is complete? Then add order.put_aon as required with maker and correct duration. Then we will have killed two birds with one stone :)

Best Regards James

objectt commented 5 years ago

@jkmaina Sounds good but since there is no "end of the trading session", it will not be canceled.

jkmaina commented 5 years ago

i agree. cancellation is a manual process from the UI (or automated externally). I think you don't really need to factor that in at all - this is taken care of with the order.cancel api already in place.

The difference between aon and fill or kill is the behaviour - the aon order is more like a limit order, it stays on the order book and can get matched either side of a buy/sell of an opposite aon or limit order or market order which fill it completely. The pending order stays until the exchange decides what to do with it via UI or other external process such as session management.

While the fill or kill is executed immediately like a market order as long as its fill order conditions are met. This is exactly what you have implemented.