Black Scholes模型的主要方法问题

时间:2015-07-12 19:38:59

标签: c#

此代码出现以下错误:...\ConsoleApplication1.exe does not contain a static 'Main' method suitable for an entry point.

我该如何解决这个问题?

namespace BlackScholes
{
    public class BS
    {
        public static double BlackScholes(double x)
        {
            double s = 100;
            double t = 1;
            double r = 0.10;
            double v = 0.10;
            double y = (float)Math.Pow(v, 2);
            double d1 = 0.0;
            double d2 = 0.0;
            double model = 0.0;


            d1 = ((float)Math.Log(s / x) + (r + 0.5 * y) * t) / v * (float)Math.Sqrt(t);
            d2 = d1 - v * (float)Math.Sqrt(t);

            model = s * cnd(d1) - x * (float)Math.Exp(-r * t) * cnd(d2);

            return model;
        }
        public static double cnd(double x)
        {
            double L = 0.0;
            double K = 0.0;
            double dCND = 0.0;
            const double a1 = 0.31938153;
            const double a2 = -0.356563782;
            const double a3 = 1.781477937;
            const double a4 = -1.821255978;
            const double a5 = 1.330274429;
            L = Math.Abs(x);
            K = 1.0 / (1.0 + 0.2316419 * L);
            dCND = 1.0 - 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) *
            Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) +
            a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0));
            if (x < 0)
            {
                return 1.0 - dCND;
            }
            else
            {
                return dCND;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您删除了Main方法。但该程序始终从static Main方法开始。

public class BS
{
     static void Main() // this method is missing
     {
          //TODO : just an example. Do your main operations here. (Usually input/output)
          double input = Convert.ToDouble(Console.ReadLine());
          Console.WriteLine(BlackScholes(x)); // outputs the result 
     }
    public static double BlackScholes(double x)
    {
        double s = 100;
        double t = 1;
        double r = 0.10;
        double v = 0.10;
        double y = (float)Math.Pow(v, 2);
        double d1 = 0.0;
        double d2 = 0.0;
        double model = 0.0;


        d1 = ((float)Math.Log(s / x) + (r + 0.5 * y) * t) / v * (float)Math.Sqrt(t);
        d2 = d1 - v * (float)Math.Sqrt(t);

        model = s * cnd(d1) - x * (float)Math.Exp(-r * t) * cnd(d2);

        return model;
    }
    public static double cnd(double x)
    {
        double L = 0.0;
        double K = 0.0;
        double dCND = 0.0;
        const double a1 = 0.31938153;
        const double a2 = -0.356563782;
        const double a3 = 1.781477937;
        const double a4 = -1.821255978;
        const double a5 = 1.330274429;
        L = Math.Abs(x);
        K = 1.0 / (1.0 + 0.2316419 * L);
        dCND = 1.0 - 1.0 / Math.Sqrt(2 * Convert.ToDouble(Math.PI.ToString())) *
        Math.Exp(-L * L / 2.0) * (a1 * K + a2 * K * K + a3 * Math.Pow(K, 3.0) +
        a4 * Math.Pow(K, 4.0) + a5 * Math.Pow(K, 5.0));
        if (x < 0)
        {
            return 1.0 - dCND;
        }
        else
        {
            return dCND;
        }
    }
}

详细了解Main()方法。

如果您要创建库(.dll),则必须将输出类型更改为ClassLibrary。在项目设置

答案 1 :(得分:0)

您已经创建了一个类,其中有两种方法可以帮助您使用Black Scholes公式计算看涨期权和看跌期权价格。

enter image description here

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OptionCalculater
{
    public class CallPutOptionPrice
    {
        double S, K, r, q, T, v, Call;
        public CallPutOptionPrice(string Is, string Ik, string Ir, string Iq, string It, string Iv)
        {
            S = double.Parse(Is);
            K = double.Parse(Ik);
            r = Math.Round(double.Parse(Ir) / 100, 4);
            q = double.Parse(Iq);
            T = Math.Round((double.Parse(It) - 1) / 365, 6);
            v = Math.Round(double.Parse(Iv) / 100, 4);
        }
        public string callOptionPrice()
        {
            double d1 = (Math.Log(S / K) + (r - q + v * v / 2.0) * T) / v / Math.Sqrt(T);
            double d2 = d1 - v * Math.Sqrt(T);

            double b0 = 0.2316419;
            double b1 = 0.319381530;
            double b2 = -0.356563782;
            double b3 = 1.781477937;
            double b4 = -1.821255978;
            double b5 = 1.330274429;

            double pi = Math.PI;
            double phid1 = Math.Exp(-d1 * d1 / 2.0) / Math.Sqrt(2.0 * pi);
            double td1, cd1;
            double Nd1 = 0.5;
            if (d1 > 0.0)
            {
                td1 = 1.0 / (1.0 + b0 * d1);
                Nd1 = 1.0 - phid1 * (b1 * td1 + b2 * Math.Pow(td1, 2) + b3 * Math.Pow(td1, 3) + b4 * Math.Pow(td1, 4) + b5 * Math.Pow(td1, 5));
            }
            else if (d1 < 0.0)
            {
                d1 = -d1;
                td1 = 1.0 / (1.0 + b0 * d1);
                cd1 = 1.0 - phid1 * (b1 * td1 + b2 * Math.Pow(td1, 2) + b3 * Math.Pow(td1, 3) + b4 * Math.Pow(td1, 4) + b5 * Math.Pow(td1, 5));
                Nd1 = 1.0 - cd1;
            }
            double phid2 = Math.Exp(-d2 * d2 / 2.0) / Math.Sqrt(2.0 * pi);
            double td2, cd2;
            double Nd2 = 0.5;
            if (d2 > 0.0)
            {
                td2 = 1.0 / (1.0 + b0 * d2);
                Nd2 = 1.0 - phid2 * (b1 * td2 + b2 * Math.Pow(td2, 2) + b3 * Math.Pow(td2, 3) + b4 * Math.Pow(td2, 4) + b5 * Math.Pow(td2, 5));
            }
            else if (d2 < 0.0)
            {
                d2 = -d2;
                td2 = 1.0 / (1.0 + b0 * d2);
                cd2 = 1.0 - phid2 * (b1 * td2 + b2 * Math.Pow(td2, 2) + b3 * Math.Pow(td2, 3) + b4 * Math.Pow(td2, 4) + b5 * Math.Pow(td2, 5));
                Nd2 = 1.0 - cd2;
            }
            Call = S * Math.Exp(-q * T) * Nd1 - K * Math.Exp(-r * T) * Nd2;
            return Call.ToString();
        }

        public string putOptionPrice()
        {
            double d1 = (Math.Log(S / K) + (r - q + v * v / 2.0) * T) / v / Math.Sqrt(T);
            double d2 = d1 - v * Math.Sqrt(T);

            d1 = -d1;
            d2 = -d2;

            double b0 = 0.2316419;
            double b1 = 0.319381530;
            double b2 = -0.356563782;
            double b3 = 1.781477937;
            double b4 = -1.821255978;
            double b5 = 1.330274429;

            double pi = Math.PI;
            double phid1 = Math.Exp(-d1 * d1 / 2.0) / Math.Sqrt(2.0 * pi);
            double td1, cd1;
            double Nd1 = 0.5;
            if (d1 > 0.0)
            {
                td1 = 1.0 / (1.0 + b0 * d1);
                Nd1 = 1.0 - phid1 * (b1 * td1 + b2 * Math.Pow(td1, 2) + b3 * Math.Pow(td1, 3) + b4 * Math.Pow(td1, 4) + b5 * Math.Pow(td1, 5));
            }
            else if (d1 < 0.0)
            {
                d1 = -d1;
                td1 = 1.0 / (1.0 + b0 * d1);
                cd1 = 1.0 - phid1 * (b1 * td1 + b2 * Math.Pow(td1, 2) + b3 * Math.Pow(td1, 3) + b4 * Math.Pow(td1, 4) + b5 * Math.Pow(td1, 5));
                Nd1 = 1.0 - cd1;
            }
            double phid2 = Math.Exp(-d2 * d2 / 2.0) / Math.Sqrt(2.0 * pi);
            double td2, cd2;
            double Nd2 = 0.5;
            if (d2 > 0.0)
            {
                td2 = 1.0 / (1.0 + b0 * d2);
                Nd2 = 1.0 - phid2 * (b1 * td2 + b2 * Math.Pow(td2, 2) + b3 * Math.Pow(td2, 3) + b4 * Math.Pow(td2, 4) + b5 * Math.Pow(td2, 5));
            }
            else if (d2 < 0.0)
            {
                d2 = -d2;
                td2 = 1.0 / (1.0 + b0 * d2);
                cd2 = 1.0 - phid2 * (b1 * td2 + b2 * Math.Pow(td2, 2) + b3 * Math.Pow(td2, 3) + b4 * Math.Pow(td2, 4) + b5 * Math.Pow(td2, 5));
                Nd2 = 1.0 - cd2;
            }

            Call = K * Math.Exp(-r * T) * Nd2 - S * Math.Exp(-q * T) * Nd1;
            return Call.ToString();
        }
    }
}