Beispiel #1
0
    public static double Delta(double S, double t0, BSOption opt)
    {
        double T       = opt.gettimeexp();
        double K       = opt.getstrike();
        double vol     = opt.getvolatility();
        double q       = opt.getyield();
        double r       = opt.getriskfreerate();
        string opttype = opt.getoption();
        double dvalue;
        double d1 = (Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0));
        double d2 = d1 - vol * Math.Sqrt(T - t0);

        if (S <= 0 || K <= 0 || vol <= 0 || T - t0 <= 0)
        {
            dvalue = 0;
        }
        if (opttype == "call")
        {
            dvalue = Math.Round(Math.Exp(-q * (T - t0)) * (RandLib.NormalCDF((Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0)))), 4);
        }
        else
        {
            dvalue = Math.Round(Math.Exp(-q * (T - t0)) * (RandLib.NormalCDF((Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0)))) - 1, 4);
        }
        return(dvalue);
    }
Beispiel #2
0
    public static double Theta(double S, double t0, BSOption opt)
    {
        double T       = opt.gettimeexp();
        double K       = opt.getstrike();
        double vol     = opt.getvolatility();
        double q       = opt.getyield();
        double r       = opt.getriskfreerate();
        string opttype = opt.getoption();
        double thetavalue;
        double d1 = (Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0));
        double d2 = d1 - vol * Math.Sqrt(T - t0);

        if (S <= 0 || K <= 0 || vol <= 0 || T - t0 <= 0)
        {
            thetavalue = 0;
        }
        if (opttype == "call")
        {
            thetavalue = Math.Round(((-S * Math.Exp(-q * (T - t0)) * RandLib.NormalPDF(d1) * 0.5 * vol / (Math.Sqrt(T - t0))) + q * S * Math.Exp(-q * (T - t0)) * RandLib.NormalCDF(d1) - r * K * Math.Exp(-r * (T - t0)) * RandLib.NormalCDF(d2)) / 365, 4);
        }
        else
        {
            thetavalue = Math.Round((-S * Math.Exp(-q * (T - t0)) * RandLib.NormalPDF(d1) * 0.5 * vol / (Math.Sqrt(T - t0)) + r * K * Math.Exp(-r * (T - t0)) * RandLib.NormalCDF(-d2) - q * S * Math.Exp(-q * (T - t0)) * RandLib.NormalCDF(-d1)) / 365, 4);
        }
        return(thetavalue);
    }
Beispiel #3
0
    static void Main()
    {
        double K;
        double S;
        double r;
        double q;
        double vol;
        double t;
        double t0;
        string optype;
        double target;
        double impvol = 0.01;

        Console.WriteLine("please enter an option strike price = ");
        K = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter a current Stock price = ");
        S = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter a risk free rate(%) = ");
        r = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter a dividend yield(%) = ");
        q = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter a stock volatility(%) = ");
        vol = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter T, expiration time = ");
        t = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter t0, start time = ");
        t0 = double.Parse(Console.ReadLine());
        Console.WriteLine("please enter the option: call or put");
        optype = Console.ReadLine();
        RandLib  rd        = new RandLib();
        BSOption option    = new BSOption(K, r, q, vol, t, optype);
        double   fairvalue = BSFairValue(S, t0, option);
        double   delta     = Delta(S, t0, option);
        double   gamma     = Gamma(S, t0, option);
        double   vega      = Vega(S, t0, option);
        double   rho       = Rho(S, t0, option);
        double   theta     = Theta(S, t0, option);

        Console.WriteLine($"The fair value of the option is {fairvalue}");
        Console.WriteLine($"The delta of the option is {delta}");
        Console.WriteLine($"The gamma of the option is {gamma}");
        Console.WriteLine($"The vega of the option is {vega}");
        Console.WriteLine($"The rho of the option is {rho}");
        Console.WriteLine($"The theta of the option is {theta}");
        Console.WriteLine("please enter the target option value to calculate implied volatility");
        target = double.Parse(Console.ReadLine());
        BSImpvol(S, t0, target, 5, 0.001, 0.001, ref impvol, option);
        Console.WriteLine($"The implied volatility of the option is {impvol}");
    }
Beispiel #4
0
    public static double Vega(double S, double t0, BSOption opt)
    {
        double T       = opt.gettimeexp();
        double K       = opt.getstrike();
        double vol     = opt.getvolatility();
        double q       = opt.getyield();
        double r       = opt.getriskfreerate();
        string opttype = opt.getoption();
        double vvalue;
        double d1 = (Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0));
        double d2 = d1 - vol * Math.Sqrt(T - t0);

        if (S <= 0 || K <= 0 || vol <= 0 || T - t0 <= 0)
        {
            vvalue = 0;
        }
        vvalue = Math.Round(.01 * S * Math.Exp(-q * (T - t0)) * Math.Sqrt(T - t0) * RandLib.NormalPDF(d1), 4);
        return(vvalue);
    }
Beispiel #5
0
    public static bool BSImpvol(double S, double t0, double target, int max_iter, double Opt_tol, double vol_tol, ref double impvol, BSOption opt)
    {
        bool   bool_value = true;
        double T          = opt.gettimeexp();
        double K          = opt.getstrike();
        double vol        = opt.getvolatility();
        double q          = opt.getyield();
        double r          = opt.getriskfreerate();
        double vol_in     = 0.4;
        double vol_f      = 0;
        string opttype    = opt.getoption();
        double V;
        double vega;
        double d1 = (Math.Log(S / K) + (r - q + 0.5 * Math.Pow(vol, 2)) * (T - t0)) / (vol * Math.Sqrt(T - t0));
        double d2 = d1 - vol * Math.Sqrt(T - t0);

        for (int i = 0; i <= max_iter; i++)
        {
            if (opttype == "call")
            {
                V = S * Math.Exp(-q * (T - t0)) * RandLib.NormalCDF(d1) - K * Math.Exp(-r * (T - t0)) * RandLib.NormalCDF(d2);
            }
            else
            {
                V = K * Math.Exp(-r * (T - t0)) * RandLib.NormalCDF(-d2) - S * Math.Exp(-q * (T - t0)) * RandLib.NormalCDF(-d1);
            }
            vega = S * Math.Exp(-q * (T - t0)) * RandLib.NormalPDF(d1) * Math.Sqrt(T - t0);
            if (i == 0)
            {
                vol_f = vol_in - (V - target) / vega;
            }
            else
            {
                if (Math.Abs(V - target) <= Opt_tol && Math.Abs((vol_f - vol_in)) <= vol_tol)
                {
                    vol_f = vol_in;
                    vol_f = vol_in - (V - target) / vega;
                }
            }
        }
        impvol = vol_f;
        return(bool_value);
    }