private void createStockMatch(stocks_action sell, stocks_action buy, float i_Average)
 {
     if (buy.quantity >= sell.quantity)
     {
         UpdateActionLog(sell, buy, sell.quantity, i_Average);
         updatePrice(buy, sell.quantity, i_Average);
         updatePrice(sell, sell.quantity, i_Average);
         buy.quantity     -= sell.quantity;
         sell.quantity     = 0;
         sell.status       = StockStatusGetter.GetDescription(eStatus.Done);
         sell.is_updatable = 0;
         if (buy.quantity == 0)
         {
             buy.status       = StockStatusGetter.GetDescription(eStatus.Done);
             buy.is_updatable = 0;
         }
     }
     else
     {
         UpdateActionLog(sell, buy, buy.quantity, i_Average);
         updatePrice(buy, buy.quantity, i_Average);
         updatePrice(sell, buy.quantity, i_Average);
         sell.quantity   -= buy.quantity;
         buy.quantity     = 0;
         buy.status       = StockStatusGetter.GetDescription(eStatus.Done);
         buy.is_updatable = 0;
     }
     UpdateStockAsMatch(sell);
     UpdateStockAsMatch(buy);
 }
 public void AddNewStockAction(stocks_action i_StocksDataManager)
 {
     using (Entities _context = new Entities())
     {
         i_StocksDataManager.status = StockStatusGetter.GetDescription(eStatus.InProgress);
         _context.stocks_action.Add(i_StocksDataManager);
         _context.SaveChanges();
     }
 }
 private void RemoveNotReleventStocks(ConcurrentDictionary <string, List <stocks_action> > remove)
 {
     foreach (var item in remove)
     {
         remove[item.Key].RemoveAll(
             y =>
             y.status == StockStatusGetter.GetDescription(eStatus.Deleted) ||
             y.status == StockStatusGetter.GetDescription(eStatus.Done));
     }
 }
 private void checkStocksThread(Object stateInfo)
 {
     try
     {
         var marketStocksStatus = _xmlStockSerializer.XmlDarkPoolStockModel;
         foreach (var stockGroup in _sell)
         {
             var marketStockStatus = marketStocksStatus.FirstOrDefault(x => x.Symbol == stockGroup.Key);
             if (marketStockStatus == null)
             {
                 continue;
             }
             float marketAverage = (marketStockStatus.Bid + marketStockStatus.Ask) / 2;
             foreach (var sell in stockGroup.Value)
             {
                 float  sellPrice;
                 double buyPrice = getBuyNumberMatch(marketStockStatus, sell, marketAverage, out sellPrice);
                 if (_buy.ContainsKey(stockGroup.Key))
                 {
                     var buyMatches = _buy[stockGroup.Key].Where(x => x.user_id != sell.user_id &&
                                                                 x.limit <= buyPrice && x.limit >= sellPrice &&
                                                                 x.status == StockStatusGetter.GetDescription(eStatus.InProgress)).ToList();
                     if (sellPrice <= marketAverage)
                     {
                         buyMatches.AddRange(_buy[stockGroup.Key]
                                             .Where(x => x.user_id != sell.user_id && x.market_limit == 1 &&
                                                    x.status == StockStatusGetter.GetDescription(eStatus.InProgress))
                                             .ToList());
                     }
                     buyMatches = buyMatches.OrderBy(x => x.date_time).ToList();
                     foreach (var buyMatch in buyMatches)
                     {
                         float average = GetAvragePrice(sellPrice, buyMatch, marketAverage, marketStockStatus);
                         createStockMatch(sell, buyMatch, average);
                         if (sell.status == StockStatusGetter.GetDescription(eStatus.Done))
                         {
                             break;
                         }
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Log.ErrorFormat("Error : {0}", ex.Message);
     }
     RemoveNotReleventStocks(_sell);
     RemoveNotReleventStocks(_buy);
 }
 public stocks_action DeleteStockAction(string i_Id)
 {
     using (Entities _context = new Entities())
     {
         var stockToDelete = _context.stocks_action.FirstOrDefault(x => x.Id == i_Id);
         if (stockToDelete == null)
         {
             throw new Exception("Stock Action not found");
         }
         stockToDelete.status       = StockStatusGetter.GetDescription(eStatus.Deleted);
         stockToDelete.is_updatable = 0;
         _context.SaveChanges();
         return(stockToDelete);
     }
 }
 private void deleteStockFromDictionray(stocks_action i_StockToDelete)
 {
     if (i_StockToDelete.sell_action == 1)
     {
         if (_sell.ContainsKey(i_StockToDelete.stock_name))
         {
             var stock = _sell[i_StockToDelete.stock_name].FirstOrDefault(x => x.Id == i_StockToDelete.Id);
             stock.status = StockStatusGetter.GetDescription(eStatus.Deleted);
         }
     }
     else
     {
         if (_buy.ContainsKey(i_StockToDelete.stock_name))
         {
             var stock = _buy[i_StockToDelete.stock_name].FirstOrDefault(x => x.Id == i_StockToDelete.Id);
             stock.status = StockStatusGetter.GetDescription(eStatus.Deleted);
         }
     }
 }
        public void StartFindStocksMatches()
        {
            Log.Error("start the thread of finding stock that match");
            Entities _context = new Entities();

            var stockslst = _context.stocks_action.AsEnumerable()
                            .Where(x => x.status == StockStatusGetter.GetDescription(eStatus.InProgress)).ToList();
            var sellLst = stockslst.Where(x => x.sell_action == 1);
            var buyLst  = stockslst.Where(x => x.sell_action == 0);
            var grpSell = sellLst.GroupBy(x => x.stock_name);
            var grpbuy  = buyLst.GroupBy(x => x.stock_name);

            foreach (var grp in grpSell)
            {
                addToDictionary(_sell, grp.Key, grp.OrderBy(x => x.date_time).ToList());
            }
            foreach (var grp in grpbuy)
            {
                addToDictionary(_buy, grp.Key, grp.OrderBy(x => x.date_time).ToList());
            }
            _timer = new System.Threading.Timer(new TimerCallback(checkStocksThread), null, 0, _intervalTime);
        }