public static List <InstrumentStats> AddInstrumentStats(string name, List <TradeRecord> trades) { var instrumentName = trades.Where(x => x.Cmd == TradeCommand.Buy || x.Cmd == TradeCommand.Sell).Select(x => x.Symbol).Distinct().ToList(); var AccountStatsList = new List <InstrumentStats>(); foreach (TimeLineEnum timeline in Enum.GetValues(typeof(TimeLineEnum))) { foreach (var item in instrumentName.Select((Value, Index) => new { Value, Index })) { InstrumentStats instrumentStats = new InstrumentStats(); instrumentStats.TimeLineId = (int)timeline; instrumentStats.AccountStatsId = timeline; instrumentStats.BuyRate = MathCalculations.GenerateRandomNo(3); instrumentStats.CreatedBy = name; //accountStats.CreatedOn = GenerateRandomDate(); instrumentStats.InstrumentId = item.Index + 1; instrumentStats.InstrumentName = item.Value; instrumentStats.Volume = InstrumentsCalculations.GetVolumeOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.WINRate = MathCalculations.GenerateRandomNo(2); instrumentStats.NAV = MathCalculations.GenerateRandomNo(2); instrumentStats.ROI = MathCalculations.GenerateRandomNo(2); instrumentStats.Profit = InstrumentsCalculations.GetProfitOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.Loss = InstrumentsCalculations.GetLossOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.Status = true; if (instrumentStats.Volume > 0) { AccountStatsList.Add(instrumentStats); } } } return(AccountStatsList); }
private static List <InstrumentStats> AddInstrumentStatsForExistingUsers(string name, List <TradeRecord> trades, List <InstrumentStats> Instruments) { try { var instrumentName = trades.Where(x => x.Cmd == TradeCommand.Buy || x.Cmd == TradeCommand.Sell).Select(x => x.Symbol).Distinct().ToList(); var existingInstruments = Instruments.Select(x => x.InstrumentName).Distinct().ToList(); var newInstruments = trades.Where(x => (x.Cmd == TradeCommand.Buy || x.Cmd == TradeCommand.Sell) && !existingInstruments.Contains(x.Symbol)).Select(x => x.Symbol).Distinct().ToList(); var timelineIds = GetTimelinesForExistingUser(); foreach (var timeline in timelineIds) { foreach (var item in existingInstruments.Select((Value, Index) => new { Value, Index })) { InstrumentStats existinInstrument = Instruments.Where(x => x.InstrumentName == item.Value && x.TimeLineId == (int)timeline).FirstOrDefault(); if (existinInstrument != null) { existinInstrument.Volume = InstrumentsCalculations.GetVolumeOfInstrumentByTimelineId((int)timeline, trades, item.Value); existinInstrument.Profit = InstrumentsCalculations.GetProfitOfInstrumentByTimelineId((int)timeline, trades, item.Value); existinInstrument.Loss = InstrumentsCalculations.GetLossOfInstrumentByTimelineId((int)timeline, trades, item.Value); } } } if (newInstruments.Count > 0) { foreach (TimeLineEnum timeline in Enum.GetValues(typeof(TimeLineEnum))) { foreach (var item in newInstruments.Select((Value, Index) => new { Value, Index })) { InstrumentStats instrumentStats = new InstrumentStats(); instrumentStats.TimeLineId = (int)timeline; instrumentStats.AccountStatsId = timeline; instrumentStats.BuyRate = MathCalculations.GenerateRandomNo(3); instrumentStats.CreatedBy = name; //accountStats.CreatedOn = GenerateRandomDate(); instrumentStats.InstrumentId = item.Index + 1; instrumentStats.InstrumentName = item.Value; instrumentStats.Volume = InstrumentsCalculations.GetVolumeOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.WINRate = MathCalculations.GenerateRandomNo(2); instrumentStats.NAV = MathCalculations.GenerateRandomNo(2); instrumentStats.ROI = MathCalculations.GenerateRandomNo(2); instrumentStats.Profit = InstrumentsCalculations.GetProfitOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.Loss = InstrumentsCalculations.GetLossOfInstrumentByTimelineId((int)timeline, trades, item.Value); instrumentStats.Status = true; if (instrumentStats.Volume > 0) { Instruments.Add(instrumentStats); } } } } return(Instruments); } catch (Exception ex) { throw; } }