Skip to content

Commit c8934d1

Browse files
authored
Add GetSafeTheta to handle decimal overflow (#9348)
1 parent a11a66b commit c8934d1

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

Common/Data/Market/Greeks.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using QuantConnect.Python;
1718

1819
namespace QuantConnect.Data.Market
@@ -111,6 +112,24 @@ public virtual decimal ThetaPerDay
111112
set { Theta = value * 365m; }
112113
}
113114

115+
/// <summary>
116+
/// Calculates the annualized theta value based on a daily theta input.
117+
/// </summary>
118+
/// <param name="thetaPerDay">The theta value per day to be annualized.</param>
119+
/// <returns>The annualized theta value, calculated as the daily theta multiplied by 365. Returns decimal.MaxValue or
120+
/// decimal.MinValue if the result overflows.</returns>
121+
public static decimal GetSafeTheta(decimal thetaPerDay)
122+
{
123+
try
124+
{
125+
return thetaPerDay * 365m;
126+
}
127+
catch (OverflowException)
128+
{
129+
return thetaPerDay < 0 ? decimal.MinValue : decimal.MaxValue;
130+
}
131+
}
132+
114133
/// <summary>
115134
/// Initializes a new instance of the <see cref="Greeks"/> class.
116135
/// </summary>

Common/Data/UniverseSelection/OptionUniverse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ private class PreCalculatedGreeks : Greeks
257257

258258
public override decimal Vega => _csvLine.GetDecimalFromCsv(StartingGreeksCsvIndex + 2);
259259

260-
public override decimal Theta => _csvLine.GetDecimalFromCsv(StartingGreeksCsvIndex + 3) * 365m;
260+
public override decimal Theta => GetSafeTheta(_csvLine.GetDecimalFromCsv(StartingGreeksCsvIndex + 3));
261261

262262
public override decimal Rho => _csvLine.GetDecimalFromCsv(StartingGreeksCsvIndex + 4);
263263

Indicators/GreeksIndicators.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,7 @@ public Greeks Greeks
7878
{
7979
get
8080
{
81-
var theta = 0m;
82-
var thetaPerDay = ThetaPerDay.Current.Value;
83-
try
84-
{
85-
theta = thetaPerDay * 365m;
86-
}
87-
catch (OverflowException)
88-
{
89-
theta = thetaPerDay < 0 ? decimal.MinValue : decimal.MaxValue;
90-
}
91-
92-
return new Greeks(Delta, Gamma, Vega, theta, Rho, 0m);
81+
return new Greeks(Delta, Gamma, Vega, Greeks.GetSafeTheta(ThetaPerDay.Current.Value), Rho, 0m);
9382
}
9483
}
9584

0 commit comments

Comments
 (0)