Пример #1
0
 public Task Start()
 {
     return(Task.Factory.StartNew(() =>
     {
         if (Token == null)
         {
             Token = new CancellationTokenSource();
         }
         AskRepositoryTask = AskRepository.Start(Token);
         OpenAsksByBuyerCommdoity.Clear();
         OpenAsks.Clear();
         var commodities = CommodityRepository.GetCommodities();
         Parallel.ForEach(commodities, c => OpenAsksByBuyerCommdoity.AddOrUpdate(c.CommodityID, new ConcurrentDictionary <int, ConcurrentDictionary <long, ThreadSafeAsk> >(), (k, o) => o));
         Parallel.ForEach(OpenAsksByBuyerCommdoity, buyCommodityDictionary =>
         {
             foreach (var commodity in commodities)
             {
                 buyCommodityDictionary.Value.AddOrUpdate(commodity.CommodityID, new ConcurrentDictionary <long, ThreadSafeAsk>(), (k, o) => o);
             }
         });
         Parallel.ForEach(AskRepository.GetAsks(), ask =>
         {
             ThreadSafeAsk tAsk = CreateAsk(new ThreadSafeAsk(ask));
             foreach (var commodityBuyID in tAsk.CommodityBuyID)
             {
                 foreach (var commoditySellID in tAsk.CommoditySellID)
                 {
                     OpenAsksByBuyerCommdoity[commodityBuyID][commoditySellID].AddOrUpdate(tAsk.AskID, tAsk, (k, o) => o);
                 }
             }
             OpenAsks.AddOrUpdate(tAsk.AskID, tAsk, (k, o) => o);
         });
     }));
 }
Пример #2
0
 public HomeController(IAskRepository askRepository = null, ICommodityRepository commodityRepository = null)
 {
     if (askRepository == null)
     {
         AskRepository = new AskRepository();
     }
     if (commodityRepository == null)
     {
         CommodityRepository = new CommodityRepository();
     }
 }
Пример #3
0
        private IEnumerable <Order> ExecuteAsk(ThreadSafeAsk ask, int maxLegDepth = 5)
        {
            long          orderQuantity;
            var           orders = GetQuote(ask, out orderQuantity, maxLegDepth, true);
            ThreadSafeAsk tAsk;

            if (ask.AskID == 0 || !OpenAsks.TryGetValue(ask.AskID, out tAsk))
            {
                this.AskRepository.AddAsk(ask.Ask);
                tAsk = CreateAsk(ask);
                this.OpenAsks[ask.AskID] = tAsk;
                foreach (var buyID in tAsk.CommodityBuyID)
                {
                    foreach (var sellID in tAsk.CommoditySellID)
                    {
                        this.OpenAsksByBuyerCommdoity[buyID][sellID][ask.AskID] = tAsk;
                    }
                }
            }
            List <Ask>    orderLegAsks = new List <Ask>();
            List <AskLeg> legs         = new List <AskLeg>();

            if (orders != null)
            {
                if (!tAsk.FillOrder(orderQuantity))
                {
                    orders = null;
                }
                tAsk.RealizeLockExecuteQuantiy();
                if (tAsk.AskQuantity <= 0)
                {
                    if (this.OpenAsks.TryRemove(tAsk.AskID, out tAsk))
                    {
                        foreach (var buyID in tAsk.CommodityBuyID)
                        {
                            foreach (var sellID in tAsk.CommoditySellID)
                            {
                                this.OpenAsksByBuyerCommdoity[buyID][sellID].TryRemove(tAsk.AskID, out tAsk);
                            }
                        }
                    }
                }

                if (orders != null)
                {
                    foreach (var order in orders)
                    {
                        order.AskID   = tAsk.AskID;
                        order.OrderID = Guid.NewGuid();
                        foreach (var orderLeg in order.OrderLegs)
                        {
                            orderLeg.OrderID    = order.OrderID;
                            orderLeg.OrderLegID = Guid.NewGuid();
                            ThreadSafeAsk orderAsk;
                            if (OpenAsks.TryGetValue(orderLeg.AskID, out orderAsk))
                            {
                                orderAsk.RealizeLock(orderAsk.IsBuy ? orderLeg.SellQuantity : orderLeg.BuyQuantity);
                                if (orderAsk.Legs.Count > 0)
                                {
                                    var leg = orderAsk.Legs.Where(l => orderAsk.IsBuy ? l.CommodityID == orderLeg.CommodityBuyID : l.CommodityID == orderLeg.CommoditySellID).Single();
                                    leg.RealizeLock(orderAsk.IsBuy ? orderLeg.BuyQuantity : orderLeg.SellQuantity);
                                    legs.Add(leg.Leg);
                                }
                                if (orderAsk.AskQuantity <= 0)
                                {
                                    if (this.OpenAsks.TryRemove(orderAsk.AskID, out orderAsk))
                                    {
                                        foreach (var buyID in tAsk.CommodityBuyID)
                                        {
                                            foreach (var sellID in tAsk.CommoditySellID)
                                            {
                                                this.OpenAsksByBuyerCommdoity[buyID][sellID].TryRemove(tAsk.AskID, out orderAsk);
                                            }
                                        }
                                    }
                                    if (orderAsk != null)
                                    {
                                        DisposeAsk(orderAsk);
                                    }
                                }
                                if (orderAsk != null)
                                {
                                    orderLegAsks.Add(orderAsk.Ask);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                tAsk.RealizeLockExecuteQuantiy();
            }
            AskRepository.ExecuteAsk(ask.Ask, orders, orderLegAsks, legs);
            return(orders);
        }