public static double ImpliedVolatility(double S, double X, double t, double r, double P, EOptionType OptionType, EPutCall PutCall, EOptionPrice Method, int n, double Eps) { double num1 = 0.0; double num2 = 10.0; double num3 = 0.0; double s = 0.0; while (Math.Abs(num2 - num1) > Eps) { s = num1 + (num2 - num1) / 2.0; switch (Method) { case EOptionPrice.BlackScholes: num3 = FinMath.BS(S, X, t, s, r, PutCall); break; case EOptionPrice.Binomial: num3 = FinMath.BM(S, X, t, s, r, PutCall, n); break; case EOptionPrice.MonteCarlo: num3 = FinMath.MC(S, X, t, s, r, PutCall, n); break; } if (num3 > P) { num2 = s; } else { num1 = s; } } return(s); }
public static double p(double t, double s, int n, double r) { double num1 = FinMath.FV2(1.0, r, t / (double)n); double num2 = FinMath.u(t, s, n); double num3 = FinMath.d(t, s, n); return((num1 - num3) / (num2 - num3)); }
public static double MC(double S, double X, double t, double s, double r, EPutCall PutCall, int n) { double num1 = (r - 0.5 * s * s) * t; double num2 = s * Math.Sqrt(t); double num3 = 0.0; for (int index = 0; index < n; ++index) { num3 += FinMath.Payoff(S * Math.Exp(num1 + num2 * Random.Gaus()), X, PutCall); } return(FinMath.PV4(num3 / (double)n, r, t)); }
public static double BM(double S, double X, double t, double s, double r, EPutCall PutCall, int n) { double F = 0.0; double x1 = FinMath.u(t, s, n); double x2 = FinMath.d(t, s, n); double p = FinMath.p(t, s, n, r); for (int m = 0; m <= n; ++m) { F += FinMath.Binom(m, n, p) * FinMath.Payoff(S * Math.Pow(x1, (double)m) * Math.Pow(x2, (double)(n - m)), X, PutCall); } return(FinMath.PV4(F, r, t)); }
public static double Rho(double S, double X, double t, double s, double r, EPutCall PutCall) { switch (PutCall) { case EPutCall.Call: return(X * t * Math.Exp(-r * t) * FinMath.N(FinMath.d2(S, X, t, s, r))); case EPutCall.Put: return(-X *t *Math.Exp(-r *t) * FinMath.N(-FinMath.d2(S, X, t, s, r))); default: return(0.0); } }
public static double Theta(double S, double X, double t, double s, double r, EPutCall PutCall) { switch (PutCall) { case EPutCall.Call: return(-S *FinMath.n(FinMath.d1(S, X, t, s, r)) * s / (2.0 * Math.Sqrt(t)) - r * X * Math.Exp(-r * t) * FinMath.N(FinMath.d2(S, X, t, s, r))); case EPutCall.Put: return(-S *FinMath.n(FinMath.d1(S, X, t, s, r)) * s / (2.0 * Math.Sqrt(t)) + r * X * Math.Exp(-r * t) * FinMath.N(-FinMath.d2(S, X, t, s, r))); default: return(0.0); } }
public static double Delta(double S, double X, double t, double s, double r, EPutCall PutCall) { switch (PutCall) { case EPutCall.Call: return(FinMath.N(FinMath.d1(S, X, t, s, r))); case EPutCall.Put: return(FinMath.N(FinMath.d1(S, X, t, s, r)) - 1.0); default: return(0.0); } }
public static double Payoff(double S, double X, EPutCall PutCall) { switch (PutCall) { case EPutCall.Call: return(FinMath.Call(S, X)); case EPutCall.Put: return(FinMath.Put(S, X)); default: return(0.0); } }
public static double ImpliedVolatility(double S, double X, double t, double r, double P, EOptionType OptionType, EPutCall PutCall, EOptionPrice Method) { int n = 0; switch (Method) { case EOptionPrice.Binomial: n = 100; break; case EOptionPrice.MonteCarlo: n = 100000; break; } double Eps = 0.001; return(FinMath.ImpliedVolatility(S, X, t, r, P, OptionType, PutCall, Method, n, Eps)); }
public static double Binom(int m, int n, double p) { return(FinMath.C(m, n) * Math.Pow(p, (double)m) * Math.Pow(1.0 - p, (double)(n - m))); }
public static double C(int m, int n) { return(FinMath.Fact(n) / (FinMath.Fact(m) * FinMath.Fact(n - m))); }
public static DateTime Max(DateTime DateTime1, DateTime DateTime2, DateTime DateTime3) { return(FinMath.Max(FinMath.Max(DateTime1, DateTime2), DateTime3)); }
public static double Vega(double S, double X, double t, double s, double r) { return(S * Math.Sqrt(t) * FinMath.n(FinMath.d1(S, X, t, s, r))); }
public static double Gamma(double S, double X, double t, double s, double r) { return(FinMath.n(FinMath.d1(S, X, t, s, r)) / (S * s * Math.Sqrt(t))); }
public static double d2(double S, double X, double t, double s, double r) { return(FinMath.d1(S, X, t, s, r) - s * Math.Sqrt(t)); }
public static double MC(double S, double X, double t, double s, double r, EPutCall PutCall) { return(FinMath.MC(S, X, t, s, r, PutCall, 100000)); }