Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Oct 23, 2024
1 parent 44dd1e5 commit 565cc50
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Libs/Estimator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.0.5</Version>
<Version>1.0.6</Version>
<Description>Statistics and performance metrics in trading, CAGR, Sharpe, MAE, MFE, and others.</Description>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
Expand Down
42 changes: 21 additions & 21 deletions Libs/Services/OptionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

namespace Estimator.Services
{
public enum OptionSideEnum : byte
{
None = 0,
Put = 1,
Call = 2,
Share = 3
}

public class OptionService
{
protected enum OptionSideEnum : byte
{
None = 0,
Put = 1,
Call = 2,
Share = 3
}

/// <summary>
/// Asset or nothing
/// </summary>
Expand Down Expand Up @@ -52,17 +52,17 @@ private static double D2(double T, double sigma, double d1)
/// <param name="r">continuously compounded risk-free interest rate</param>
/// <param name="q">continuously compounded dividend yield</param>
/// <returns></returns>
public static double Premium(OptionSideEnum optionType, double S, double K, double T, double sigma, double r, double q)
public static double Premium(string optionType, double S, double K, double T, double sigma, double r, double q)
{
double d1 = D1(S, K, T, sigma, r, q);
double d2 = D2(T, sigma, d1);

switch (optionType)
{
case OptionSideEnum.Call:
case nameof(OptionSideEnum.Call):
return S * Math.Exp(-q * T) * Normal.CDF(0, 1, d1) - K * Math.Exp(-r * T) * Normal.CDF(0, 1, d2);

case OptionSideEnum.Put:
case nameof(OptionSideEnum.Put):
return K * Math.Exp(-r * T) * Normal.CDF(0, 1, -d2) - S * Math.Exp(-q * T) * Normal.CDF(0, 1, -d1);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ public static double Vega(double S, double K, double T, double sigma, double r,
/// <param name="q"></param>
/// <param name="optionMarketPrice"></param>
/// <returns></returns>
public static double IV(OptionSideEnum optionType, double S, double K, double T, double r, double q, double optionMarketPrice)
public static double IV(string optionType, double S, double K, double T, double r, double q, double optionMarketPrice)
{
Func<double, double> f = sigma => Premium(optionType, S, K, T, sigma, r, q) - optionMarketPrice;
Func<double, double> df = sigma => Vega(S, K, T, sigma, r, q);
Expand All @@ -116,14 +116,14 @@ public static double IV(OptionSideEnum optionType, double S, double K, double T,
/// <param name="r">continuously compounded risk-free interest rate</param>
/// <param name="q">continuously compounded dividend yield</param>
/// <returns></returns>
public static double Theta(OptionSideEnum optionType, double S, double K, double T, double sigma, double r, double q)
public static double Theta(string optionType, double S, double K, double T, double sigma, double r, double q)
{
double d1 = D1(S, K, T, sigma, r, q);
double d2 = D2(T, sigma, d1);

switch (optionType)
{
case OptionSideEnum.Call:
case nameof(OptionSideEnum.Call):
{
double theta = -Math.Exp(-q * T) * (S * Normal.PDF(0, 1, d1) * sigma) / (2.0 * Math.Sqrt(T))
- (r * K * Math.Exp(-r * T) * Normal.CDF(0, 1, d2))
Expand All @@ -132,7 +132,7 @@ public static double Theta(OptionSideEnum optionType, double S, double K, double
return theta / 365;
}

case OptionSideEnum.Put:
case nameof(OptionSideEnum.Put):
{
double theta = -Math.Exp(-q * T) * (S * Normal.PDF(0, 1, d1) * sigma) / (2.0 * Math.Sqrt(T))
+ (r * K * Math.Exp(-r * T) * Normal.PDF(0, 1, -d2))
Expand All @@ -157,16 +157,16 @@ public static double Theta(OptionSideEnum optionType, double S, double K, double
/// <param name="r">continuously compounded risk-free interest rate</param>
/// <param name="q">continuously compounded dividend yield</param>
/// <returns></returns>
public static double Delta(OptionSideEnum optionType, double S, double K, double T, double sigma, double r, double q)
public static double Delta(string optionType, double S, double K, double T, double sigma, double r, double q)
{
double d1 = D1(S, K, T, sigma, r, q);

switch (optionType)
{
case OptionSideEnum.Call:
case nameof(OptionSideEnum.Call):
return Math.Exp(-r * T) * Normal.CDF(0, 1, d1);

case OptionSideEnum.Put:
case nameof(OptionSideEnum.Put):
return -Math.Exp(-r * T) * Normal.CDF(0, 1, -d1);

default:
Expand Down Expand Up @@ -201,17 +201,17 @@ public static double Gamma(double S, double K, double T, double sigma, double r,
/// <param name="r">continuously compounded risk-free interest rate</param>
/// <param name="q">continuously compounded dividend yield</param>
/// <returns></returns>
public static double Rho(OptionSideEnum optionType, double S, double K, double T, double sigma, double r, double q)
public static double Rho(string optionType, double S, double K, double T, double sigma, double r, double q)
{
double d1 = D1(S, K, T, sigma, r, q);
double d2 = D2(T, sigma, d1);

switch (optionType)
{
case OptionSideEnum.Call:
case nameof(OptionSideEnum.Call):
return K * T * Math.Exp(-r * T) * Normal.CDF(0, 1, d2);

case OptionSideEnum.Put:
case nameof(OptionSideEnum.Put):
return -K * T * Math.Exp(-r * T) * Normal.CDF(0, 1, -d2);

default:
Expand Down

0 comments on commit 565cc50

Please sign in to comment.