Skip to content

Commit

Permalink
Improve exception message when generating Annual Performance Statistic (
Browse files Browse the repository at this point in the history
#8022)

* Improve exception message

* Nit changes
  • Loading branch information
Marinovsky committed May 15, 2024
1 parent c82573a commit cc3c97a
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Common/Statistics/PortfolioStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using MathNet.Numerics.Distributions;
using MathNet.Numerics.Statistics;
using Newtonsoft.Json;
using QLNet;
using QuantConnect.Data;
using QuantConnect.Util;

Expand Down Expand Up @@ -349,7 +348,29 @@ private static decimal DrawdownPercent(SortedDictionary<DateTime, decimal> equit
/// <returns>Double annual performance percentage</returns>
private static decimal GetAnnualPerformance(List<double> performance, int tradingDaysPerYear)
{
return Statistics.AnnualPerformance(performance, tradingDaysPerYear).SafeDecimalCast();
try
{
return Statistics.AnnualPerformance(performance, tradingDaysPerYear).SafeDecimalCast();
}
catch (ArgumentException ex)
{
var partialSums = 0.0;
var points = 0;
double troublePoint = default;
foreach(var point in performance)
{
points++;
partialSums += point;
if (Math.Pow(partialSums / points, tradingDaysPerYear).IsNaNOrInfinity())
{
troublePoint = point;
break;
}
}

throw new ArgumentException($"PortfolioStatistics.GetAnnualPerformance(): An exception was thrown when trying to cast the annual performance value due to the following performance point: {troublePoint}. " +
$"The exception thrown was the following: {ex.Message}.");
}
}

private static decimal GetValueAtRisk(
Expand Down

0 comments on commit cc3c97a

Please sign in to comment.