示例#1
0
        //use the object o as inputs and outputs, for method 1
        private void calc2(object o)
        {
            multithreading m = (multithreading)o;
            Greeks gre = new Greeks(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial);
            Monitor.Enter(m);//Ensure random number will generate before path.
            Thread.Sleep(5);//wait the random number start generate first

            if (m.type == "European")
            {
                European eur = new European(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial, m.cv);
                m.price = eur.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, eur.Payoff);//calculate the greeks
            }
            else if (m.type == "Asian")
            {
                Asian asi = new Asian(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial, m.cv);
                m.price = asi.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, asi.Payoff);//calculate the greeks
            }
            else if (m.type == "Barrier")
            {
                    Barrier bar = new Barrier(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial, m.barrier, m.udio, m.cv);
                    m.price = bar.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                    m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, bar.Payoff);//calculate the greeks

            }
            else if (m.type == "Digital")
            {
                Digital dig = new Digital(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial, m.rebate, m.cv);
                m.price = dig.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, dig.Payoff);//calculate the greeks
            }
            else if (m.type == "Lookback")
            {
                LookBack look = new LookBack(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial, m.lookback, m.cv);
                m.price = look.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, look.Payoff);//calculate the greeks
            }
            else if (m.type == "Range")
            {
                Range ran = new Range(m.underlyings.S, m.underlyings.K, m.underlyings.r, m.underlyings.e, m.underlyings.T, m.underlyings.d, m.underlyings.step, m.underlyings.trial);
                m.price = ran.Payoff(m.underlyings, m.rand, m.anti, m.s);//calculate the option price
                m.greeks = gre.calculate(m.underlyings, m.rand, m.s, m.anti, ran.Payoff);//calculate the greeks
            }
            Monitor.Exit(m);
        }
示例#2
0
        //Monte Carlo Simulation
        private void montecarlo()
        {
            try
            {
                Form1 form1 = new Form1();
                double e = Convert.ToDouble(form1.volatility1.Value), d = Convert.ToDouble(form1.dividend1.Value);
                List<int> row = new List<int>();
                var inst = from i in portfolio.Trades
                           select i.Id;
                foreach (var i in inst)
                {
                    row.Add(i);
                }
                List<Basicvalue> basicvalue = new List<Basicvalue>();
                Basicvalue basic = new Basicvalue();
                for (int i = 0; i < row.Count; i++)//first find all the inputs I need from different table
                {
                    int oo = row[i];
                    var type = (from q in portfolio.InstTypes
                                join j in portfolio.Instruments on q.Id equals j.InstTypeId
                                join k in portfolio.Trades on j.Id equals k.InstrumentId
                                where k.Id == oo
                                select q.TypeName).First();//find the insttype
                    if (type.ToUpper() == "STOCK")//stock do not need to do simulation
                        break;
                    int n = row[i];
                    var inst2 = (from i2 in portfolio.Trades
                                 where i2.Id == n
                                 select i2).First();//find the historical price for this trade
                    basic.tradeprice = inst2.Price;
                    basic.S = Convert.ToDouble(inst2.UnderlyingP);
                    basic.instrumentid = inst2.InstrumentId;
                    basic.timestamp = inst2.Timestamp.ToString();
                    basic.quantity = Convert.ToDouble(inst2.Quantity);
                    var inst3 = (from i3 in portfolio.Instruments
                                 where i3.Id == basic.instrumentid
                                 select i3).First();//find the strike tenor call or put for the trade
                    basic.K = Convert.ToDouble(inst3.Strike);
                    basic.T = Convert.ToDouble(inst3.Tenor);
                    basic.iscall = Convert.ToInt16(inst3.IsCall);
                    basic.insttypeid = Convert.ToInt16(inst3.InstTypeId);

                    double ratetemp = new double();//store the risk free rate
                    List<double> rate = new List<double>();
                    var inst4 = from i4 in portfolio.InterestRates
                                orderby i4.Tenor ascending
                                select i4;
                    List<InterestRate> ir = new List<InterestRate>();
                    foreach (var g in inst4)
                    {
                        ir.Add(g);
                    }
                    int h = 0;
                    while (basic.T < ir[h].Tenor)//find out the interval of the T, and compute the rate
                        h++;
                    if (h == 0)
                    {
                        double r = Convert.ToDouble(ir[h].Rate);
                        double t = Convert.ToDouble(ir[h].Tenor);
                        ratetemp = basic.T / t * Convert.ToDouble(r);
                    }
                    else
                    {
                        double r1 = Convert.ToDouble(ir[h - 1].Rate), r2 = Convert.ToDouble(ir[h].Rate); ;
                        double t1 = Convert.ToDouble(ir[h - 1].Tenor), t2 = Convert.ToDouble(ir[h].Tenor);
                        ratetemp = r2 + (r2 - r1) / (t2 - t1) * (basic.T - t1);
                    }

                    basic.r = Convert.ToDouble(ratetemp);

                    var inst5 = (from i5 in portfolio.InstTypes
                                 where i5.Id == basic.insttypeid
                                 select i5).First();
                    basic.insttype = inst5.TypeName;
                    basic.underlying = inst5.Underlying;

                    if (basic.insttype == "BarrierOption")
                    {
                        var inst6 = (from i6 in portfolio.BarrierOptions
                                     where i6.InstTypeId == basic.insttypeid
                                     select i6).First();
                        basic.isup = Convert.ToBoolean(inst6.IsUp);
                        basic.isin = Convert.ToBoolean(inst6.IsIn);
                        basic.barrier = Convert.ToDouble(inst6.barrier);
                    }
                    else if (basic.insttype == "DigitalOption")
                    {
                        var inst7 = (from i7 in portfolio.DigitalOptions
                                     where i7.InstTypeId == basic.insttypeid
                                     select i7).First();
                        basic.rebate = Convert.ToDouble(inst7.rebate);
                    }
                    else if (basic.insttype == "LookBackOption")
                    {
                        var inst8 = (from i8 in portfolio.LookBackOptions
                                     where i8.InstTypeId == basic.insttypeid
                                     select i8).First();
                        basic.isfix = Convert.ToBoolean(inst8.IsFixed);
                    }

                    Value temp = new Value(basic.S, basic.K, basic.r, e, basic.T, d, Convert.ToInt64(step11.Value), Convert.ToInt64(trail11.Value));
                    Simulator sim = new Simulator(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial);
                    Option opt = new Option(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial);
                    Greeks gre = new Greeks(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial);
                    bool isant = true;
                    string s = null; ;//store the choice call or put option

                    string s3 = null;//up,down,in,out
                    string s4 = null;//fixed, floating

                    double[,] randmatrix = new double[temp.trial, temp.step];// store the random matrix
                    double[] origin_price = new double[2];//store the price and standard deviation
                    double[] greeks;//store the greeks

                    if (antithetic.Checked == true)
                        isant = true;
                    else
                        isant = false;

                    randmatrix = sim.generateregular();//generate the random matrix

                    if (basic.iscall == 0)
                        s = "call";
                    else if (basic.iscall == 1)
                        s = "put";
                    else
                        s = "neither";

                    if (basic.isup == true && basic.isin == true)
                        s3 = "up_in";
                    else if (basic.isup == false && basic.isin == false)
                        s3 = "up_out";
                    else if (basic.isin == true && basic.isup == false)
                        s3 = "down_in";
                    else if (basic.isup == false && basic.isin == false)
                        s3 = "down_out";

                    if (basic.isfix == true)
                        s4 = "fix";
                    else
                        s4 = "floating";

                    switch (basic.insttype)
                    {
                        case "EuropeanOption":
                            European eur = new European(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial, dcv.Checked);
                            origin_price = eur.Payoff(temp, randmatrix, isant, s);//calculate the option price
                            greeks = gre.calculate(temp, randmatrix, s, isant, eur.Payoff);//calculate the greeks
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                        case "AsianOption":
                            Asian asi = new Asian(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial, dcv.Checked);
                            origin_price = asi.Payoff(temp, randmatrix, isant, s);//calculate the option price
                            greeks = gre.calculate(temp, randmatrix, s, isant, asi.Payoff);//calculate the greeks
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                        case "BarrierOption":
                            Barrier bar = new Barrier(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial, basic.barrier, s3, dcv.Checked);
                            origin_price = bar.Payoff(temp, randmatrix, isant, s);
                            greeks = gre.calculate(temp, randmatrix, s, isant, bar.Payoff);
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                        case "DigitalOption":
                            Digital dig = new Digital(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial, basic.rebate, dcv.Checked);
                            origin_price = dig.Payoff(temp, randmatrix, isant, s);
                            greeks = gre.calculate(temp, randmatrix, s, isant, dig.Payoff);
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                        case "LookbackOption":
                            LookBack look = new LookBack(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial, s4, dcv.Checked);
                            origin_price = look.Payoff(temp, randmatrix, isant, s);
                            greeks = gre.calculate(temp, randmatrix, s, isant, look.Payoff);
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                        case "RangeOption":
                            Range rang = new Range(temp.S, temp.K, temp.r, temp.e, temp.T, temp.d, temp.step, temp.trial);
                            origin_price = rang.Payoff(temp, randmatrix, isant, s);
                            greeks = gre.calculate(temp, randmatrix, s, isant, rang.Payoff);
                            basic.mprice = origin_price[0];
                            basic.greek = greeks;
                            break;
                    }
                    basic.pl = (basic.tradeprice - basic.mprice) * basic.quantity;
                    var update = (from u in portfolio.Trades
                                  where u.Id == n
                                  select u).First();//update the price and greeks in trade
                    update.MarketPrice = basic.mprice;
                    update.PL = basic.pl;
                    update.Delta = basic.greek[0];
                    update.Gamma = basic.greek[1];
                    update.Vega = basic.greek[2];
                    update.Theta = basic.greek[3];
                    update.Rho = basic.greek[4];

                    portfolio.SaveChanges();

                    basic = new Basicvalue();
                }
                this.Close();
                MessageBox.Show("Done! Please refresh the database again", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch { MessageBox.Show("Something wrong, please check wheather the inputs are correct."); }
        }