我在寻找code的构造从接单手持订单
I'm looking for code that constructs orderbook from orders
例如,如果订单
side | price | quantity
buy 100 1
buy 101 10
buy 100 1000
buy 100 10000
然后agregated手持订单应该是:
then agregated orderbook should be:
side | price | quantity
buy 100 11001
buy 101 10
在项目生命周期的订单添加,修改或删除。在每个订单的更新,我需要快速更新手持订单。
During program lifetime orders are added, modified or deleted. On each order update I need to update OrderBook quickly.
我敢肯定,这是非常普遍的任务,所以应该有很多的实现在Internet了。
I'm sure this is very common task, so there should be a lot of implementations in Internet already.
感谢您的任何引用,我在寻找的C#实现,但我可以,如果需要另一种语言重写。
Thank you for any references, I'm looking for c# implementation, but I can rewrite it from another language if needed.
UPD 其实我应该改一下我的问题。最初,手持订单是空的。然后我收到的事件:添加订购,更改订单数量或取消订单。我应该从这次的消息重新计算手持订单。但现在它变得清楚对我来说,应该如何简单的是。下订单时加入我只添加量在这个价格水平。当订单数量发生变化,我只是需要添加改变,当订单被取消我只需要由相应的价格水平取出相应数量。唯一的问题是我应该在哪里存放最后的订单数量完全有不少订单(几千万),但不会有很多活动订单(不超过10万),并为每个活动为了我需要取得最后的量由orderId的......当然,我可以用字典,但是这将是太慢了可能。我想要的东西更快。但我不能用50 000 000项阵列。
upd Actually I should rephrase my question. Initially orderbook is empty. Then I receive events: add order, change order quantity or cancel order. I should recalculate orderBook from this messages. But now it becomes clear for me how simple it should be. When order is added I just add quantity at this price level. When order quantity is changed i just need to add "change" and when order is canceled i just need to remove corresponding quantity from corresponding price level. The only question is where should I store "last order quantity" Totally there are a lot of orders (dozens of millions), but there are not a lot of active orders (not more than 100 000) and for each active order I need to obtain "last quantity" by orderId... Of course I can use dictionary, but that would be too slow probably. I want something faster. But I can not use 50 000 000 items array.
推荐答案
下面是code在 LINQPad
Here is the code tested in LINQPad
var orders = new [] {
new {Side = "Buy", Price = 100, Quantity = 1 },
new {Side = "Buy", Price = 101, Quantity = 10 },
new {Side = "Buy", Price = 100, Quantity = 1000 },
new {Side = "Buy", Price = 100, Quantity = 10000 },
new {Side = "Sell", Price = 100, Quantity = 10000 }
};
var orderboook
= from o in (
from order in orders
group order by order.Side into sideGroup
select new {
Side = sideGroup.Key,
SideGroup =
from s in sideGroup
group s by s.Price into g
select new {
Side = sideGroup.Key,
Price = g.Key,
Quantity = g.Sum( s => s.Quantity)
}
}
)
from g in o.SideGroup
select g;
orderboook.Dump(); // .Dump() is LINQPad helper method...
结果在LINQPad是
The result in LINQPad is
相关推荐
最新文章