public static double GetLossOfInstrumentByTimelineId(int timelineId, List <TradeRecord> trades, string instrumentName) { if (trades.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } if (Timelines.timelinesTillToday.Contains(timelineId)) { endDate = DateTime.Today.AddDays(-1).Date; } else { endDate = Dates.GetEndDateByTimeLineID(timelineId, startDate); } var startSeconds = (uint)(Int32)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endSeconds = (uint)(Int32)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var loss = trades.Where(x => x.OpenTime >= startSeconds && x.OpenTime < endSeconds && (x.Cmd == TradeCommand.Buy || x.Cmd == TradeCommand.Sell) && x.Symbol == instrumentName && x.Profit < 0).Sum(x => x.Profit); return(loss); } else { return(0); } }
public static double GetWithdrawnByTimeline(int timelineId, DailyEquity dailyEquity, List <TradeRecord> trades) { if (trades.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } if (Timelines.timelinesTillToday.Contains(timelineId)) { endDate = DateTime.Today.AddDays(-1).Date; } else { endDate = Dates.GetEndDateByTimeLineID(timelineId, startDate); } var startSeconds = (uint)(Int32)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endSeconds = (uint)(Int32)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var withdrawn = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && (x.Cmd == TradeCommand.Balance || x.Cmd == TradeCommand.Credit) && x.Profit < 0).Sum(x => x.Profit); return(withdrawn); } else { return(0); } }
public static string CalculateWorstPLForTimeline(int timelineId, List <TradeRecord> trades) { if (trades.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } if (Timelines.timelinesTillToday.Contains(timelineId)) { endDate = DateTime.Today.AddDays(-1).Date; } else { endDate = Dates.GetEndDateByTimeLineID(timelineId, startDate); } var startSeconds = (uint)(Int32)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endSeconds = (uint)(Int32)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var worstBuyTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Buy).OrderBy(x => x.Profit).FirstOrDefault(); var worstSellTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Sell).OrderBy(x => x.Profit).FirstOrDefault(); if (worstBuyTrades == null && worstSellTrades == null) { return("0"); } else if (worstBuyTrades == null && worstSellTrades != null) { return(Math.Round(worstSellTrades.Profit, 2).ToString()); } else if (worstBuyTrades != null && worstSellTrades == null) { return(Math.Round(worstBuyTrades.Profit, 2).ToString()); } else { return(worstBuyTrades.Profit > worstSellTrades.Profit ? Math.Round(worstSellTrades.Profit, 2).ToString() : Math.Round(worstBuyTrades.Profit, 2).ToString()); } } else { return("0"); } }
public static double CalculateWINForTimeline(int timelineId, List <TradeRecord> trades) { if (trades.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } if (Timelines.timelinesTillToday.Contains(timelineId)) { endDate = DateTime.Today.AddDays(-1).Date; } else { endDate = Dates.GetEndDateByTimeLineID(timelineId, startDate); } var startSeconds = (uint)(Int32)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endSeconds = (uint)(Int32)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var buyTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Buy).Count(); var sellTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Sell).Count(); var positiveBuyTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Buy && x.Profit > 0).Count(); var positiveSellTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Sell && x.Profit > 0).Count(); var winRate = new double(); if (buyTrades == 0 && sellTrades == 0) { winRate = 0; } else { winRate = Math.Round((Convert.ToDouble(positiveSellTrades + positiveBuyTrades) / Convert.ToDouble(buyTrades + sellTrades)) * 100, 2); } return(winRate); } else { return(0); } }
public static double CalculateAvgTradesForTimeline(int timelineId, List <TradeRecord> trades) { if (trades.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } if (Timelines.timelinesTillToday.Contains(timelineId)) { endDate = DateTime.Today.AddDays(-1).Date; } else { endDate = Dates.GetEndDateByTimeLineID(timelineId, startDate); } var startSeconds = (uint)(Int32)(startDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var endSeconds = (uint)(Int32)(endDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; var buyTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Buy).Count(); var sellTrades = trades.Where(x => (x.OpenTime >= startSeconds && x.OpenTime < endSeconds) && x.Cmd == TradeCommand.Sell).Count(); var numberOfDays = (endDate - startDate).TotalDays; var avgTrades = Math.Round(((buyTrades + sellTrades) / numberOfDays), 2); return(avgTrades); } else { return(0); } }
public static double GetStartingBalanceByTimeline(int timelineId, DailyEquity dailyEquity, List <TradeRecord> trades) { if (dailyEquity != null && dailyEquity.EquityByDate.Count > 0) { var startDate = new DateTime(); var endDate = new DateTime(); if (timelineId == (int)TimeLineEnum.Overall) //timelineid= 45 { startDate = Dates.SecondsToDate(trades[0].OpenTime).Date; } else { startDate = Dates.GetStartDateByTimeLineID(timelineId, DateTime.Today.AddDays(-1).Date); } var startingBalance = dailyEquity.EquityByDate.Where(x => x.date == startDate.ToString()).Select(x => x.Equity).FirstOrDefault(); return(startingBalance); } else { return(0); } }