private List<CpTrade> CheckMultitradesBack(Ctrade ctrade, List<CpTrade> ABNtrades) { List<long> sequence = null; List<CpTrade> listBoTrades = null; if (ctrade != null) { //var sameqty = ABNtrades.Where(item => (item.BOSymbol == ctrade.symbol_id && item.Price == ctrade.price&&)); IEnumerable<CpTrade> possibletrades = ABNtrades.Where(item => (item.BOSymbol == ctrade.symbol_id && item.Price == ctrade.price)); //var accounts = possibletrades.GroupBy(x => x.).Select(g => g.First().account_id).ToList(); if (ctrade.qty > 0) { possibletrades = possibletrades.Where(item => item.Qty > 0); possibletrades = possibletrades.OrderByDescending(o => o.Qty); } else { possibletrades = possibletrades.Where(item => item.Qty < 0); possibletrades = possibletrades.OrderBy(o => o.Qty); } sequence = new List<long>(); if (possibletrades.Count() > 0) { if (ctrade.qty == possibletrades.ElementAt(0).Qty) { if (possibletrades.ElementAt(0).BOTradeNumber != null) { sequence.Add(possibletrades.ElementAt(0).FullId); listBoTrades.Add(possibletrades.ElementAt(0)); } } else { int i = 0; double qty = 0; while ((i < possibletrades.Count()) && (qty != ctrade.qty)) { if (Math.Abs((double) possibletrades.ElementAt(i).Qty) < Math.Abs((double) ctrade.qty)) { qty = (double) possibletrades.ElementAt(i).Qty; if (sequence.Count == 0) sequence.Add(i); else sequence[0] = i; qty = calculateQtyBack(ctrade.qty, qty, i + 1, possibletrades.ToList(), sequence, 1); if (qty != ctrade.qty) i++; } else i++; } if ((qty == ctrade.qty) && (sequence.Count > 0)) { listBoTrades = new List<CpTrade> {possibletrades.ElementAt((int) sequence[0])}; for (i = 1; i < sequence.Count; i++) { listBoTrades.Add(possibletrades.ElementAt((int) sequence[i])); } } } } } return listBoTrades; }
private List<long> workeithCS(Ctrade ctrade, List<CpTrade> abnTradeslist, Boolean mtytrades) { int inndexcs = ctrade.symbol_id.IndexOf(".CS/"); int mty = 1; if (inndexcs == -1) { inndexcs = ctrade.symbol_id.IndexOf(".RS/"); mty = -1; } int indexseparate = ctrade.symbol_id.IndexOf("-"); string leftside = ctrade.symbol_id.Substring(0, inndexcs + 1) + ctrade.symbol_id.Substring(inndexcs + 4, indexseparate - inndexcs - 4); string vd = getValueDate(leftside); double Cqty = (double) ctrade.qty*mty; double? spreadprice = ctrade.price*mty; string rightside = ctrade.symbol_id.Substring(0, inndexcs + 1) + ctrade.symbol_id.Substring(indexseparate + 1, ctrade.symbol_id.Length - indexseparate - 1); var leftalltrades = abnTradeslist.Where(item => ((item.BOSymbol == leftside) && (item.BOTradeNumber == null))) .Select(item => new {qty = item.Qty, price = item.Price, id = item.FullId}); if (!mtytrades) leftalltrades = leftalltrades.Where(item => (Math.Abs((double) item.qty) == Math.Abs(Cqty))); var righttalltrades = abnTradeslist.Where(item => ((item.BOSymbol == rightside) && (item.BOTradeNumber == null))) .Select(item => new {qty = item.Qty, price = item.Price, id = item.FullId}); if (!mtytrades) righttalltrades = righttalltrades.Where(item => (Math.Abs((double) item.qty) == Math.Abs(Cqty))); List<double?> pricelist = leftalltrades.Select(item => item.price).Distinct().ToList(); int indexprice = 0; bool pairfound = false; var reclist = new List<long>(); while (indexprice < pricelist.Count && !pairfound) { double? currentprice = pricelist[indexprice]; List<Trade> leftossibleletrades = leftalltrades.Where(item => (item.price == currentprice)) .Select(item => new Trade {id = item.id, qty = (double) item.qty}) .ToList(); leftossibleletrades = Samesidetrades(Cqty, leftossibleletrades); double sum = 0; foreach (Trade sumtrade in leftossibleletrades) { sum = sum + sumtrade.qty; } if (Math.Abs(sum) >= Math.Abs(Cqty)) { List<int> leftreclist = CheckMultitradesNew(Cqty, leftossibleletrades); if (leftreclist != null) { reclist.Clear(); for (int i = 0; i < leftreclist.Count; i++) { reclist.Add(leftossibleletrades[leftreclist[i]].id); } double? rightpathprice = (currentprice - spreadprice); rightpathprice = Math.Round((double) rightpathprice, 8); List<Trade> rightpossibleletrades = righttalltrades.Where(item => (item.price == rightpathprice)) .Select(item => new Trade {id = item.id, qty = (double) item.qty}) .ToList(); rightpossibleletrades = Samesidetrades(-Cqty, rightpossibleletrades); double rightsum = 0; foreach (Trade sumtrade in rightpossibleletrades) { rightsum = rightsum + sumtrade.qty; } if (Math.Abs(rightsum) >= Math.Abs(Cqty)) { double rightcty = -Cqty; List<int> rightreclist = CheckMultitradesNew(rightcty, rightpossibleletrades); if (rightreclist != null) { for (int i = 0; i < rightreclist.Count; i++) { reclist.Add(rightpossibleletrades[rightreclist[i]].id); } pairfound = true; } else { reclist.Clear(); } // var indexReclist = 0; // pairfound = true; /* while ((indexReclist < leftreclist.Count) && (pairfound)) { var testid = (int) leftreclist[indexReclist]; var CrtRecListQty = -leftossibleletrades.ElementAt((int) leftreclist[indexReclist]).qty; // var rightid = rightpossibleletrades.Where(item => (item.qty == CrtRecListQty)).Select(item => item.id).FirstOrDefault(); var rightidtrade = rightpossibleletrades.Where(item => (item.qty == CrtRecListQty)).Select(item => item).FirstOrDefault(); // var rightqty = rightidtrade.qty; var rightid = rightidtrade.id; // List<Trade> righttrade = rightpossibleletrades.Where(item => (item.id == rightid)).Select(item => item).ToList(); //new Trade { id = item.id, qty = (double)item.qty }).ToList(); double rightqty = 0; rightqty=rightidtrade.qty; if (rightid != 0) { reclist.Add(rightid); rightpossibleletrades = rightpossibleletrades.Where(item => (item.id != rightid)).Select(item => item).ToList();//new Trade { id = item.id, qty = (double)item.qty }).ToList(); // .RemoveAt(0); indexReclist++; } else { pairfound = false; } } if (!pairfound) { var rightreclist = CheckMultitradesNew(-Cqty, rightpossibleletrades); reclist.Clear(); if (rightreclist != null) { for (var i = 0; i < rightreclist.Count; i++) { reclist.Add(rightpossibleletrades[i].id); } } }*/ } else reclist.Clear(); } } indexprice++; } /* if (pairfound) { /* var templist = new List<Ctrade> {ctrade}; // var cpTrade= abnTradeslist.Where(item => (item.FullId == leftalltrades. ) // UpdateRecTrades(reclist[i], templist, db, boTradeslist, recon); var n = reclist.Count; for (var i = 0; i < n; i++) { var keysWithMatchingValues = abnTradeslist.Where(p => p.Value[0].fullid == reclist[0].fullid) .Select(p => p.Key) .FirstOrDefault(); UpdateRecTrades(cpTrade, reclist, db, boTradeslist, recon); reclist.RemoveAt(0); if (abnTradeslist[keysWithMatchingValues].Count == 1) { abnTradeslist.Remove(keysWithMatchingValues); } else { abnTradeslist[keysWithMatchingValues].RemoveAt(0); } } return reclist; }*/ return reclist; }