/// <summary> /// Gets Portfolio Statistics from a pandas.DataFrame with equity and benchmark values /// </summary> /// <param name="dataFrame">pandas.DataFrame with the information required to compute the Portfolio statistics</param> /// <returns><see cref="PortfolioStatistics"/> object wrapped in a <see cref="PyDict"/> with the portfolio statistics.</returns> public PyDict GetPortfolioStatistics(PyObject dataFrame) { var dictBenchmark = new SortedDictionary <DateTime, double>(); var dictEquity = new SortedDictionary <DateTime, double>(); var dictPL = new SortedDictionary <DateTime, double>(); using (Py.GIL()) { var result = new PyDict(); try { // Converts the data from pandas.DataFrame into dictionaries keyed by time var df = ((dynamic)dataFrame).dropna(); dictBenchmark = GetDictionaryFromSeries((PyObject)df["benchmark"]); dictEquity = GetDictionaryFromSeries((PyObject)df["equity"]); dictPL = GetDictionaryFromSeries((PyObject)df["equity"].pct_change()); } catch (PythonException e) { result.SetItem("Runtime Error", e.Message.ToPython()); return(result); } // Convert the double into decimal var equity = new SortedDictionary <DateTime, decimal>(dictEquity.ToDictionary(kvp => kvp.Key, kvp => (decimal)kvp.Value)); var profitLoss = new SortedDictionary <DateTime, decimal>(dictPL.ToDictionary(kvp => kvp.Key, kvp => double.IsNaN(kvp.Value) ? 0 : (decimal)kvp.Value)); // Gets the last value of the day of the benchmark and equity var listBenchmark = CalculateDailyRateOfChange(dictBenchmark); var listPerformance = CalculateDailyRateOfChange(dictEquity); // Gets the startting capital var startingCapital = Convert.ToDecimal(dictEquity.FirstOrDefault().Value); // Compute portfolio statistics var stats = new PortfolioStatistics(profitLoss, equity, listPerformance, listBenchmark, startingCapital); result.SetItem("Average Win (%)", Convert.ToDouble(stats.AverageWinRate * 100).ToPython()); result.SetItem("Average Loss (%)", Convert.ToDouble(stats.AverageLossRate * 100).ToPython()); result.SetItem("Compounding Annual Return (%)", Convert.ToDouble(stats.CompoundingAnnualReturn * 100m).ToPython()); result.SetItem("Drawdown (%)", Convert.ToDouble(stats.Drawdown * 100).ToPython()); result.SetItem("Expectancy", Convert.ToDouble(stats.Expectancy).ToPython()); result.SetItem("Net Profit (%)", Convert.ToDouble(stats.TotalNetProfit * 100).ToPython()); result.SetItem("Sharpe Ratio", Convert.ToDouble(stats.SharpeRatio).ToPython()); result.SetItem("Win Rate (%)", Convert.ToDouble(stats.WinRate * 100).ToPython()); result.SetItem("Loss Rate (%)", Convert.ToDouble(stats.LossRate * 100).ToPython()); result.SetItem("Profit-Loss Ratio", Convert.ToDouble(stats.ProfitLossRatio).ToPython()); result.SetItem("Alpha", Convert.ToDouble(stats.Alpha).ToPython()); result.SetItem("Beta", Convert.ToDouble(stats.Beta).ToPython()); result.SetItem("Annual Standard Deviation", Convert.ToDouble(stats.AnnualStandardDeviation).ToPython()); result.SetItem("Annual Variance", Convert.ToDouble(stats.AnnualVariance).ToPython()); result.SetItem("Information Ratio", Convert.ToDouble(stats.InformationRatio).ToPython()); result.SetItem("Tracking Error", Convert.ToDouble(stats.TrackingError).ToPython()); result.SetItem("Treynor Ratio", Convert.ToDouble(stats.TreynorRatio).ToPython()); return(result); } }
private void CheckCalculated() { if (!this.IsCalculated || this.statistics == null) { this.statistics = new PortfolioStatistics(this.portfolio); this.statistics.Calculate(); this.IsCalculated = true; } }
private void CheckCalculated() { if (this.IsCalculated && this.statistics != null) { return; } this.statistics = new PortfolioStatistics(this.portfolio); this.statistics.Calculate(); this.IsCalculated = true; }
private TimeSeries GetTimeSeries(PortfolioStatistics portfolioStatistics) { foreach (PortfolioStatisticsItem portfolioStatisticsItem in portfolioStatistics.Items) { if (portfolioStatisticsItem.Name == this.statisticsName) { return(portfolioStatisticsItem.TotalValues); } } return((TimeSeries)null); }
public OptimizationStatistics(PortfolioStatistics statistics) { if (statistics == null) { return; } NetProfit = statistics.Items[PortfolioStatisticsType.NetProfit - 1].TotalValue; MaxDDPct = statistics.Items[8].TotalValue; ProfitFactor = statistics.Items[9].TotalValue; Trades = Convert.ToInt32(statistics.Items[11].TotalValue); ProfitablePct = statistics.Items[14].TotalValue; AvgTrade = statistics.Items[18].TotalValue; PayoffRatio = statistics.Items[21].TotalValue; AvgDailyReturnPct = statistics.Items[37].TotalValue; AvgAnnualReturnPct = statistics.Items[38].TotalValue; SharpeRatio = statistics.Items[43].TotalValue; }
public void MinMaxValueDeserializesSuccessfuly() { var portfolioStatistics = new PortfolioStatistics { AverageWinRate = decimal.MaxValue, AverageLossRate = decimal.MinValue, CompoundingAnnualReturn = decimal.MaxValue }; var serializedValue = JsonConvert.SerializeObject(portfolioStatistics); var deserializedValue = JsonConvert.DeserializeObject <PortfolioStatistics>(serializedValue); Assert.AreEqual(portfolioStatistics.AverageWinRate, deserializedValue.AverageWinRate); Assert.AreEqual(portfolioStatistics.AverageLossRate, deserializedValue.AverageLossRate); Assert.AreEqual(portfolioStatistics.CompoundingAnnualReturn, deserializedValue.CompoundingAnnualReturn); Assert.AreEqual(portfolioStatistics.AnnualStandardDeviation, deserializedValue.AnnualStandardDeviation); Assert.AreEqual(portfolioStatistics.Expectancy, deserializedValue.Expectancy); }
private void CheckCalculated() { if (!this.IsCalculated || this.statistics == null) { this.statistics = new PortfolioStatistics(this.portfolio); this.statistics.Calculate(); this.IsCalculated = true; } }
public OptimizationResult(OptimizationParameterSet parameterSet, PortfolioStatistics statistics) { OptimizationParameterSet = parameterSet; OptimizationStatistics = new OptimizationStatistics(statistics); }