/// <summary> /// Strategy long/short exposure by asset class /// </summary> /// <param name="equityCurve">Equity curve</param> /// <param name="orders">Orders of the strategy</param> /// <param name="direction">Long or short</param> /// <returns> /// Frame keyed by <see cref="SecurityType"/> and <see cref="OrderDirection"/>. /// Returns a Frame of exposure per asset per direction over time /// </returns> public static Frame <DateTime, Tuple <SecurityType, OrderDirection> > Exposure(Series <DateTime, double> equityCurve, List <Order> orders, OrderDirection direction) { if (equityCurve.IsEmpty || orders.Count == 0) { return(Frame.CreateEmpty <DateTime, Tuple <SecurityType, OrderDirection> >()); } return(Exposure(PortfolioLooper.FromOrders(equityCurve, orders).ToList(), direction)); }
/// <summary> /// Calculates the portfolio's asset allocation percentage over time. The series used to call this extension function should /// be the equity curve with the associated <see cref="Order"/> objects that go along with it. /// </summary> /// <param name="equityCurve">Equity curve series</param> /// <param name="orders">Orders associated with the equity curve</param> /// <returns></returns> public static Series <Symbol, double> AssetAllocations(Series <DateTime, double> equityCurve, List <Order> orders) { if (equityCurve.IsEmpty || orders.Count == 0) { return(new Series <Symbol, double>(new Symbol[] { }, new double[] { })); } // Convert PointInTimePortfolios to List because for some reason our AbsoluteHoldingsValue is multiplied by two whenever we GroupBy on the raw IEnumerable return(AssetAllocations(PortfolioLooper.FromOrders(equityCurve, orders).ToList())); }
/// <summary> /// Calculates the leverage used from trades. The series used to call this extension function should /// be the equity curve with the associated <see cref="Order"/> objects that go along with it. /// </summary> /// <param name="equityCurve">Equity curve series</param> /// <param name="orders">Orders associated with the equity curve</param> /// <returns>Leverage utilization over time</returns> public static Series <DateTime, double> LeverageUtilization(Series <DateTime, double> equityCurve, List <Order> orders) { if (equityCurve.IsEmpty || orders.Count == 0) { return(new Series <DateTime, double>(new DateTime[] { }, new double[] { })); } var pointInTimePortfolios = PortfolioLooper.FromOrders(equityCurve, orders) .ToList(); // Required because for some reason our AbsoluteHoldingsValue is multiplied by two whenever we GroupBy on the raw IEnumerable return(LeverageUtilization(pointInTimePortfolios)); }
/// <summary> /// Create beautiful HTML and PDF Reports based on backtest and live data. /// </summary> /// <param name="name">Name of the strategy</param> /// <param name="description">Description of the strategy</param> /// <param name="version">Version number of the strategy</param> /// <param name="backtest">Backtest result object</param> /// <param name="live">Live result object</param> public Report(string name, string description, string version, BacktestResult backtest, LiveResult live) { var backtestCurve = new Series <DateTime, double>(ResultsUtil.EquityPoints(backtest)); var liveCurve = new Series <DateTime, double>(ResultsUtil.EquityPoints(live)); var backtestOrders = backtest?.Orders?.Values.ToList() ?? new List <Order>(); var liveOrders = live?.Orders?.Values.ToList() ?? new List <Order>(); Log.Trace($"QuantConnect.Report.Report(): Processing backtesting orders"); var backtestPortfolioInTime = PortfolioLooper.FromOrders(backtestCurve, backtestOrders).ToList(); Log.Trace($"QuantConnect.Report.Report(): Processing live orders"); var livePortfolioInTime = PortfolioLooper.FromOrders(liveCurve, liveOrders, liveSeries: true).ToList(); _elements = new List <IReportElement> { //Basics new TextReportElement("strategy name", ReportKey.StrategyName, name), new TextReportElement("description", ReportKey.StrategyDescription, description), new TextReportElement("version", ReportKey.StrategyVersion, version), new TextReportElement("stylesheet", ReportKey.Stylesheet, File.ReadAllText("css/report.css")), new TextReportElement("live marker key", ReportKey.LiveMarker, live == null ? string.Empty : "Live "), //KPI's Backtest: new DaysLiveReportElement("days live kpi", ReportKey.DaysLive, live), new CAGRReportElement("cagr kpi", ReportKey.CAGR, backtest, live), new TurnoverReportElement("turnover kpi", ReportKey.Turnover, backtest, live), new MaxDrawdownReportElement("max drawdown kpi", ReportKey.MaxDrawdown, backtest, live), new KellyEstimateReportElement("kelly estimate kpi", ReportKey.KellyEstimate, backtest, live), new SharpeRatioReportElement("sharpe kpi", ReportKey.SharpeRatio, backtest, live), new PSRReportElement("psr kpi", ReportKey.PSR, backtest, live), new InformationRatioReportElement("ir kpi", ReportKey.InformationRatio, backtest, live), new MarketsReportElement("markets kpi", ReportKey.Markets, backtest, live), new TradesPerDayReportElement("trades per day kpi", ReportKey.TradesPerDay, backtest, live), // Generate and insert plots MonthlyReturnsReportElement new MonthlyReturnsReportElement("monthly return plot", ReportKey.MonthlyReturns, backtest, live), new CumulativeReturnsReportElement("cumulative returns", ReportKey.CumulativeReturns, backtest, live), new AnnualReturnsReportElement("annual returns", ReportKey.AnnualReturns, backtest, live), new ReturnsPerTradeReportElement("returns per trade", ReportKey.ReturnsPerTrade, backtest, live), new AssetAllocationReportElement("asset allocation over time pie chart", ReportKey.AssetAllocation, backtest, live, backtestPortfolioInTime, livePortfolioInTime), new DrawdownReportElement("drawdown plot", ReportKey.Drawdown, backtest, live), //new DailyReturnsReportElement("daily returns plot", ReportKey.DailyReturns, backtest, live), //new RollingPortfolioBetaReportElement("rolling beta to equities plot", ReportKey.RollingBeta, backtest, live), //new RollingSharpeReportElement("rolling sharpe ratio plot", ReportKey.RollingSharpe, backtest, live), //new LeverageUtilizationReportElement("leverage plot", ReportKey.LeverageUtilization, backtest, live, backtestPortfolioInTime, livePortfolioInTime), //new ExposureReportElement("exposure plot", ReportKey.Exposure, backtest, live, backtestPortfolioInTime, livePortfolioInTime), // Array of Crisis Plots: new CrisisReportElement("crisis page", ReportKey.CrisisPageStyle, backtest, live), new CrisisReportElement("crisis plots", ReportKey.CrisisPlots, backtest, live) }; }
/// <summary> /// Create beautiful HTML and PDF Reports based on backtest and live data. /// </summary> /// <param name="name">Name of the strategy</param> /// <param name="description">Description of the strategy</param> /// <param name="version">Version number of the strategy</param> /// <param name="backtest">Backtest result object</param> /// <param name="live">Live result object</param> /// <param name="pointInTimePortfolioDestination">Point in time portfolio json output base filename</param> public Report(string name, string description, string version, BacktestResult backtest, LiveResult live, string pointInTimePortfolioDestination = null) { var backtestCurve = new Series <DateTime, double>(ResultsUtil.EquityPoints(backtest)); var liveCurve = new Series <DateTime, double>(ResultsUtil.EquityPoints(live)); var backtestOrders = backtest?.Orders?.Values.ToList() ?? new List <Order>(); var liveOrders = live?.Orders?.Values.ToList() ?? new List <Order>(); Log.Trace($"QuantConnect.Report.Report(): Processing backtesting orders"); var backtestPortfolioInTime = PortfolioLooper.FromOrders(backtestCurve, backtestOrders).ToList(); Log.Trace($"QuantConnect.Report.Report(): Processing live orders"); var livePortfolioInTime = PortfolioLooper.FromOrders(liveCurve, liveOrders, liveSeries: true).ToList(); var destination = pointInTimePortfolioDestination ?? Config.Get("report-destination"); if (!string.IsNullOrWhiteSpace(destination)) { if (backtestPortfolioInTime.Count != 0) { var dailyBacktestPortfolioInTime = backtestPortfolioInTime .Select(x => new PointInTimePortfolio(x, x.Time.Date).NoEmptyHoldings()) .GroupBy(x => x.Time.Date) .Select(kvp => kvp.Last()) .OrderBy(x => x.Time) .ToList(); var outputFile = destination.Replace(".html", string.Empty) + "-backtesting-portfolio.json"; Log.Trace($"Report.Report(): Writing backtest point-in-time portfolios to JSON file: {outputFile}"); var backtestPortfolioOutput = JsonConvert.SerializeObject(dailyBacktestPortfolioInTime); File.WriteAllText(outputFile, backtestPortfolioOutput); } if (livePortfolioInTime.Count != 0) { var dailyLivePortfolioInTime = livePortfolioInTime .Select(x => new PointInTimePortfolio(x, x.Time.Date).NoEmptyHoldings()) .GroupBy(x => x.Time.Date) .Select(kvp => kvp.Last()) .OrderBy(x => x.Time) .ToList(); var outputFile = destination.Replace(".html", string.Empty) + "-live-portfolio.json"; Log.Trace($"Report.Report(): Writing live point-in-time portfolios to JSON file: {outputFile}"); var livePortfolioOutput = JsonConvert.SerializeObject(dailyLivePortfolioInTime); File.WriteAllText(outputFile, livePortfolioOutput); } } _elements = new List <IReportElement> { //Basics new TextReportElement("strategy name", ReportKey.StrategyName, name), new TextReportElement("description", ReportKey.StrategyDescription, description), new TextReportElement("version", ReportKey.StrategyVersion, version), new TextReportElement("stylesheet", ReportKey.Stylesheet, File.ReadAllText("css/report.css")), new TextReportElement("live marker key", ReportKey.LiveMarker, live == null ? string.Empty : "Live "), //KPI's Backtest: new DaysLiveReportElement("days live kpi", ReportKey.DaysLive, live), new CAGRReportElement("cagr kpi", ReportKey.CAGR, backtest, live), new TurnoverReportElement("turnover kpi", ReportKey.Turnover, backtest, live), new MaxDrawdownReportElement("max drawdown kpi", ReportKey.MaxDrawdown, backtest, live), new KellyEstimateReportElement("kelly estimate kpi", ReportKey.KellyEstimate, backtest, live), new SharpeRatioReportElement("sharpe kpi", ReportKey.SharpeRatio, backtest, live), new PSRReportElement("psr kpi", ReportKey.PSR, backtest, live), new InformationRatioReportElement("ir kpi", ReportKey.InformationRatio, backtest, live), new MarketsReportElement("markets kpi", ReportKey.Markets, backtest, live), new TradesPerDayReportElement("trades per day kpi", ReportKey.TradesPerDay, backtest, live), // Generate and insert plots MonthlyReturnsReportElement new MonthlyReturnsReportElement("monthly return plot", ReportKey.MonthlyReturns, backtest, live), new CumulativeReturnsReportElement("cumulative returns", ReportKey.CumulativeReturns, backtest, live), new AnnualReturnsReportElement("annual returns", ReportKey.AnnualReturns, backtest, live), new ReturnsPerTradeReportElement("returns per trade", ReportKey.ReturnsPerTrade, backtest, live), new AssetAllocationReportElement("asset allocation over time pie chart", ReportKey.AssetAllocation, backtest, live, backtestPortfolioInTime, livePortfolioInTime), new DrawdownReportElement("drawdown plot", ReportKey.Drawdown, backtest, live), new DailyReturnsReportElement("daily returns plot", ReportKey.DailyReturns, backtest, live), new RollingPortfolioBetaReportElement("rolling beta to equities plot", ReportKey.RollingBeta, backtest, live), new RollingSharpeReportElement("rolling sharpe ratio plot", ReportKey.RollingSharpe, backtest, live), new LeverageUtilizationReportElement("leverage plot", ReportKey.LeverageUtilization, backtest, live, backtestPortfolioInTime, livePortfolioInTime), new ExposureReportElement("exposure plot", ReportKey.Exposure, backtest, live, backtestPortfolioInTime, livePortfolioInTime), // Array of Crisis Plots: new CrisisReportElement("crisis page", ReportKey.CrisisPageStyle, backtest, live), new CrisisReportElement("crisis plots", ReportKey.CrisisPlots, backtest, live) }; }