ArjunVachhani / order-matcher

simple, fast and feature rich order matching engine supports limit, market, stop-loss, iceberg, IOC, FOK, GTD orders
MIT License
137 stars 70 forks source link

ITradeListener.OnTrade possible better to have event for many restingOrderIds? #44

Closed P9avel closed 1 year ago

P9avel commented 2 years ago

Hi, thank you for new release. In common case one incomingOrder can closed many restingOrder. I am planning to use NoSql dbms MongoDb and want to get trade with many mached orders. Possible can you to rewrite ITradeListener.OnTrade or add new event ITradeListener.OnTradeComplete for example?

ArjunVachhani commented 2 years ago

This can be handled in ITradeListener implementation.

P9avel commented 2 years ago

I am draw my idea ![Uploading 123.png…]()

P9avel commented 2 years ago

123

ArjunVachhani commented 2 years ago

It can be done in MatchingEngine, but to me it seems that having one call per trade is simple design and will be used most.

We may achieve this in ITradeListener implementation, below is sample code.

class MyTradeListener : ITradeListener
    {
        private OrderId? lastIncomingOrderId;
        private List<Fill> fills = new();

        public void OnAccept(OrderId orderId)
        {
            FlushCombinedTradeForIncomingOrder();
            //Handle Accept;
        }

        public void OnCancel(OrderId orderId, Quantity remainingQuantity, Quantity cost, Quantity fee, CancelReason cancelReason)
        {
            FlushCombinedTradeForIncomingOrder();
            //Handle Cancel;
        }

        public void OnOrderTriggered(OrderId orderId)
        {
            FlushCombinedTradeForIncomingOrder();
            //Handle Trigger;
        }

        public void OnTrade(OrderId incomingOrderId, OrderId restingOrderId, Price matchPrice, Quantity matchQuantiy, Quantity? askRemainingQuantity, Quantity? askFee, Quantity? bidCost, Quantity? bidFee)
        {
            if (lastIncomingOrderId == incomingOrderId)
            {
                AddFills(incomingOrderId, restingOrderId, matchPrice, matchQuantiy, askRemainingQuantity, askFee, bidCost, bidFee);
            }
            else
            {
                FlushCombinedTradeForIncomingOrder();
                AddFills(incomingOrderId, restingOrderId, matchPrice, matchQuantiy, askRemainingQuantity, askFee, bidCost, bidFee);
            }
        }

        private void AddFills(OrderId incomingOrderId, OrderId restingOrderId, Price matchPrice, Quantity matchQuantiy, Quantity? askRemainingQuantity, Quantity? askFee, Quantity? bidCost, Quantity? bidFee)
        {
            fills.Add(new Fill
            {
                TakerOrderId = incomingOrderId,
                MakerOrderId = restingOrderId,
                //TODO Rest of the fields
            });
            lastIncomingOrderId = incomingOrderId;
        }

        private void FlushCombinedTradeForIncomingOrder()
        {
            //TODO process multiple fills for same orderId
            fills.Clear();
            lastIncomingOrderId = null;
        }
    }

Your thoughts?

P9avel commented 2 years ago

Yes, i am understood you. But in case if (lastIncomingOrderId == incomingOrderId) i am always not recived last trade. For your code need wait new input Order for recive prior trade. And your code set delay between trading and reciveng trade info. My suggetion give last trades when filling complete. My idea in pseudo code

OnTrading(inputOrder);

while(true) { Onrade(inputOrder, completedOrder); }

OnTraded(inputOrder, IList completedOrders);

Advantages: 1) I am always recived all trades and last trade 2) I am recived trade info without delay

ArjunVachhani commented 2 years ago

Hmm, if you are getting lots of order every seconds then for humans it would be hard to notice it. But If you are not getting that much orders then you set a delay/timer to trigger it if no fills received for 100ms then flush fills.

P9avel commented 2 years ago

You are right. But i think my solution is simpler.....

ArjunVachhani commented 2 years ago

I would wait and see if there is demand for combined fills API, then I would implement it.

P9avel commented 2 years ago

Ok, no problem. please look again closed https://github.com/ArjunVachhani/order-matcher/issues/38