/// <summary> /// Group list of ticks, create bar and write it in a csv file /// <param name="listTick">List of ticks to porcess</param> /// </summary> private static void WriteLeanCsvFiles(List<TmpTick> listTick) { listTick.GroupBy(t => t.SetFilename(_resolution)).ToList().ForEach(g => { var filename = _destinationDirectory + @"\" + g.Key; if (!new FileInfo(filename).Directory.Exists) new FileInfo(filename).Directory.Create(); if (g.Key.Contains("trade")) { var tradeBar = new TmpTradeBar { Time = g.First().Time.RoundDown(_span), Underlying = g.First().Underlying, OptionType = g.First().OptionType, Strike = g.First().Strike, Expiration = g.First().Expiration, TradeOpen = g.First().Price, TradeHigh = g.Max(x => x.Price), TradeLow = g.Min(x => x.Price), TradeClose = g.Last().Price, TradeVolume = g.Sum(x => x.Quantity) }; File.AppendAllText(filename, tradeBar.CsvFileOutput() + "\r\n"); } else { var quoteBar = new TmpQuoteBar { Time = g.First().Time.RoundDown(_span), Underlying = g.First().Underlying, OptionType = g.First().OptionType, Strike = g.First().Strike, Expiration = g.First().Expiration }; var a = g.Where(x => x.TickType == "ASK").Select(x => x.Price); var b = g.Where(x => x.TickType == "BID").Select(x => x.Price); if (a.Count() > 0) { quoteBar.AskOpen = a.First(); quoteBar.AskHigh = a.Max(); quoteBar.AskLow = a.Min(); quoteBar.AskClose = a.Last(); quoteBar.AvgAskSize = (long)g.Where(x => x.TickType == "ASK").Average(x => x.Quantity); } if (b.Count() > 0) { quoteBar.BidOpen = b.First(); quoteBar.BidHigh = b.Max(); quoteBar.BidLow = b.Min(); quoteBar.BidClose = b.Last(); quoteBar.AvgBidSize = (long)g.Where(x => x.TickType == "BID").Average(x => x.Quantity); } File.AppendAllText(filename, quoteBar.CsvFileOutput() + "\r\n"); } }); listTick.Clear(); }
/// <summary> /// Group list of ticks, create bar and write it in a csv file /// <param name="listTick">List of ticks to porcess</param> /// </summary> private static void WriteLeanCsvFiles(List <TmpTick> listTick) { listTick.GroupBy(t => t.SetFilename(_resolution)).ToList().ForEach(g => { var filename = _destinationDirectory + @"\" + g.Key; if (!new FileInfo(filename).Directory.Exists) { new FileInfo(filename).Directory.Create(); } if (g.Key.Contains("trade")) { var tradeBar = new TmpTradeBar { Time = g.First().Time.RoundDown(_span), Underlying = g.First().Underlying, OptionType = g.First().OptionType, Strike = g.First().Strike, Expiration = g.First().Expiration, TradeOpen = g.First().Price, TradeHigh = g.Max(x => x.Price), TradeLow = g.Min(x => x.Price), TradeClose = g.Last().Price, TradeVolume = g.Sum(x => x.Quantity) }; File.AppendAllText(filename, tradeBar.CsvFileOutput() + "\r\n"); } else { var quoteBar = new TmpQuoteBar { Time = g.First().Time.RoundDown(_span), Underlying = g.First().Underlying, OptionType = g.First().OptionType, Strike = g.First().Strike, Expiration = g.First().Expiration }; var a = g.Where(x => x.TickType == "ASK").Select(x => x.Price); var b = g.Where(x => x.TickType == "BID").Select(x => x.Price); if (a.Count() > 0) { quoteBar.AskOpen = a.First(); quoteBar.AskHigh = a.Max(); quoteBar.AskLow = a.Min(); quoteBar.AskClose = a.Last(); quoteBar.AvgAskSize = (long)g.Where(x => x.TickType == "ASK").Average(x => x.Quantity); } if (b.Count() > 0) { quoteBar.BidOpen = b.First(); quoteBar.BidHigh = b.Max(); quoteBar.BidLow = b.Min(); quoteBar.BidClose = b.Last(); quoteBar.AvgBidSize = (long)g.Where(x => x.TickType == "BID").Average(x => x.Quantity); } File.AppendAllText(filename, quoteBar.CsvFileOutput() + "\r\n"); } }); listTick.Clear(); }