Example #1
0
        private void CalculateData()
        {
            //read data: S,K,r,Sigma,T,Trials,steps
            label_bar.Text = "Reading data";
            inprogress(10);
            //S means underlying
            double S = Convert.ToDouble(TextBox_S.Text);
            //K means strike price
            double K = Convert.ToDouble(TextBox_K.Text);
            //r means the interest rate
            double r = Convert.ToDouble(TextBox_r.Text);
            //Sigma means volatility
            double Sigma = Convert.ToDouble(TextBox_Sigma.Text);
            //T means tenor
            double T = Convert.ToDouble(TextBox_T.Text);
            //Trials means the trials of Mento Carlo Simulations
            int Trials = Convert.ToInt32(TextBox_Sims.Text);
            //steps means the steps to calculate the option price
            int  steps          = Convert.ToInt32(TextBox_Steps.Text);
            bool Type           = Convert.ToBoolean(Radio_Call.Checked);
            bool AntVar         = Convert.ToBoolean(checkbox_AntVar.Checked);
            bool CV             = Convert.ToBoolean(checkbox_CV.Checked);
            bool Multithreading = Convert.ToBoolean(checkBox_MT.Checked);

            //European
            if (comboBox1.SelectedIndex == 0)
            {
                //store the data
                Option Euroption = new European(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, 0, 0, 0);
                //option price
                label_bar.Text = "Calculating Price";
                inprogress(30);
                TextBox_Price.Text = Convert.ToString(Euroption.OptionPrice()[0]);
                //delta
                label_bar.Text = "Calculating Delta";
                inprogress(40);
                TextBox_Delta.Text = Convert.ToString(Euroption.Delta());
                //gamma
                label_bar.Text = "Calculating Gamma";
                inprogress(50);
                TextBox_Gamma.Text = Convert.ToString(Euroption.Gamma());
                //vega
                label_bar.Text = "Calculating Vega";
                inprogress(60);
                TextBox_Vega.Text = Convert.ToString(Euroption.Vega());
                //theta
                label_bar.Text = "Calculating Theta";
                inprogress(70);
                TextBox_Theta.Text = Convert.ToString(Euroption.Theta());
                //rho
                label_bar.Text = "Calculating Rho";
                inprogress(80);
                TextBox_Rho.Text = Convert.ToString(Euroption.Rho());
                //standard error
                label_bar.Text = "Calculating Standard Error";
                inprogress(90);
                TextBox_Std.Text   = Convert.ToString(Euroption.OptionPrice()[1]);
                TextBox_Cores.Text = Convert.ToString(Euroption.core_num());
                label_bar.Text     = "Done.";
                inprogress(100);
            }
            //Asian
            if (comboBox1.SelectedIndex == 1)
            {
                //store the data
                Option AsianOption = new Asian(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, 0, 0, 0);
                //option price
                label_bar.Text = "Calculating Price";
                inprogress(30);
                TextBox_Price.Text = Convert.ToString(AsianOption.OptionPrice()[0]);
                //delta
                label_bar.Text = "Calculating Delta";
                inprogress(40);
                TextBox_Delta.Text = Convert.ToString(AsianOption.Delta());
                //gamma
                label_bar.Text = "Calculating Gamma";
                inprogress(50);
                TextBox_Gamma.Text = Convert.ToString(AsianOption.Gamma());
                //vega
                label_bar.Text = "Calculating Vega";
                inprogress(60);
                TextBox_Vega.Text = Convert.ToString(AsianOption.Vega());
                //theta
                label_bar.Text = "Calculating Theta";
                inprogress(70);
                TextBox_Theta.Text = Convert.ToString(AsianOption.Theta());
                //rho
                label_bar.Text = "Calculating Rho";
                inprogress(80);
                TextBox_Rho.Text = Convert.ToString(AsianOption.Rho());
                //standard error
                label_bar.Text = "Calculating Standard Error";
                inprogress(90);
                TextBox_Std.Text   = Convert.ToString(AsianOption.OptionPrice()[1]);
                TextBox_Cores.Text = Convert.ToString(AsianOption.core_num());
                label_bar.Text     = "Done.";
                inprogress(100);
            }
            //Digital
            if (comboBox1.SelectedIndex == 2)
            {
                //store the data
                if (TextBox_Rebate.Text == string.Empty)
                {
                    label_bar.Text = "Missing some inputs";
                }
                else
                {
                    double Rebate        = Convert.ToDouble(TextBox_Rebate.Text);
                    Option DigitalOption = new Digital(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, Rebate, 0, 0);
                    //option price
                    label_bar.Text = "Calculating Price";
                    inprogress(30);
                    TextBox_Price.Text = Convert.ToString(DigitalOption.OptionPrice()[0]);
                    //delta
                    label_bar.Text = "Calculating Delta";
                    inprogress(40);
                    TextBox_Delta.Text = Convert.ToString(DigitalOption.Delta());
                    //gamma
                    label_bar.Text = "Calculating Gamma";
                    inprogress(50);
                    TextBox_Gamma.Text = Convert.ToString(DigitalOption.Gamma());
                    //vega
                    label_bar.Text = "Calculating Vega";
                    inprogress(60);
                    TextBox_Vega.Text = Convert.ToString(DigitalOption.Vega());
                    //theta
                    label_bar.Text = "Calculating Theta";
                    inprogress(70);
                    TextBox_Theta.Text = Convert.ToString(DigitalOption.Theta());
                    //rho
                    label_bar.Text = "Calculating Rho";
                    inprogress(80);
                    TextBox_Rho.Text = Convert.ToString(DigitalOption.Rho());
                    //standard error
                    label_bar.Text = "Calculating Standard Error";
                    inprogress(90);
                    TextBox_Std.Text   = Convert.ToString(DigitalOption.OptionPrice()[1]);
                    TextBox_Cores.Text = Convert.ToString(DigitalOption.core_num());
                    label_bar.Text     = "Done.";
                    inprogress(100);
                }
            }
            //Barrier
            if (comboBox1.SelectedIndex == 3)
            {
                //store the data
                if (TextBox_Barrier.Text == string.Empty)
                {
                    label_bar.Text = "Missing some inputs";
                }
                else
                {
                    double Barrier       = Convert.ToDouble(TextBox_Barrier.Text);
                    int    barrtype      = Convert.ToInt32(comboBox2.SelectedIndex);
                    Option BarrierOption = new Barrier(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, 0, Barrier, barrtype);
                    //option price
                    label_bar.Text = "Calculating Price";
                    inprogress(30);
                    TextBox_Price.Text = Convert.ToString(BarrierOption.OptionPrice()[0]);
                    //delta
                    label_bar.Text = "Calculating Delta";
                    inprogress(40);
                    TextBox_Delta.Text = Convert.ToString(BarrierOption.Delta());
                    //gamma
                    label_bar.Text = "Calculating Gamma";
                    inprogress(50);
                    TextBox_Gamma.Text = Convert.ToString(BarrierOption.Gamma());
                    //vega
                    label_bar.Text = "Calculating Vega";
                    inprogress(60);
                    TextBox_Vega.Text = Convert.ToString(BarrierOption.Vega());
                    //theta
                    label_bar.Text = "Calculating Theta";
                    inprogress(70);
                    TextBox_Theta.Text = Convert.ToString(BarrierOption.Theta());
                    //rho
                    label_bar.Text = "Calculating Rho";
                    inprogress(80);
                    TextBox_Rho.Text = Convert.ToString(BarrierOption.Rho());
                    //standard error
                    label_bar.Text = "Calculating Standard Error";
                    inprogress(90);
                    TextBox_Std.Text   = Convert.ToString(BarrierOption.OptionPrice()[1]);
                    TextBox_Cores.Text = Convert.ToString(BarrierOption.core_num());
                    label_bar.Text     = "Done.";
                    inprogress(100);
                }
            }
            //Lookback
            if (comboBox1.SelectedIndex == 4)
            {
                //store the data
                Option LookbackOption = new Lookback(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, 0, 0, 0);
                //option price
                label_bar.Text = "Calculating Price";
                inprogress(30);
                TextBox_Price.Text = Convert.ToString(LookbackOption.OptionPrice()[0]);
                //delta
                label_bar.Text = "Calculating Delta";
                inprogress(40);
                TextBox_Delta.Text = Convert.ToString(LookbackOption.Delta());
                //gamma
                label_bar.Text = "Calculating Gamma";
                inprogress(50);
                TextBox_Gamma.Text = Convert.ToString(LookbackOption.Gamma());
                //vega
                label_bar.Text = "Calculating Vega";
                inprogress(60);
                TextBox_Vega.Text = Convert.ToString(LookbackOption.Vega());
                //theta
                label_bar.Text = "Calculating Theta";
                inprogress(70);
                TextBox_Theta.Text = Convert.ToString(LookbackOption.Theta());
                //rho
                label_bar.Text = "Calculating Rho";
                inprogress(80);
                TextBox_Rho.Text = Convert.ToString(LookbackOption.Rho());
                //standard error
                label_bar.Text = "Calculating Standard Error";
                inprogress(90);
                TextBox_Std.Text   = Convert.ToString(LookbackOption.OptionPrice()[1]);
                TextBox_Cores.Text = Convert.ToString(LookbackOption.core_num());
                label_bar.Text     = "Done.";
                inprogress(100);
            }
            //Range
            if (comboBox1.SelectedIndex == 5)
            {
                //store the data
                Option RangeOption = new Range(S, K, r, Sigma, T, Trials, steps, Type, AntVar, CV, Multithreading, 0, 0, 0);
                //option price
                label_bar.Text = "Calculating Price";
                inprogress(30);
                TextBox_Price.Text = Convert.ToString(RangeOption.OptionPrice()[0]);
                //delta
                label_bar.Text = "Calculating Delta";
                inprogress(40);
                TextBox_Delta.Text = Convert.ToString(RangeOption.Delta());
                //gamma
                label_bar.Text = "Calculating Gamma";
                inprogress(50);
                TextBox_Gamma.Text = Convert.ToString(RangeOption.Gamma());
                //vega
                label_bar.Text = "Calculating Vega";
                inprogress(60);
                TextBox_Vega.Text = Convert.ToString(RangeOption.Vega());
                //theta
                label_bar.Text = "Calculating Theta";
                inprogress(70);
                TextBox_Theta.Text = Convert.ToString(RangeOption.Theta());
                //rho
                label_bar.Text = "Calculating Rho";
                inprogress(80);
                TextBox_Rho.Text = Convert.ToString(RangeOption.Rho());
                //standard error
                label_bar.Text = "Calculating Standard Error";
                inprogress(90);
                TextBox_Std.Text   = Convert.ToString(RangeOption.OptionPrice()[1]);
                TextBox_Cores.Text = Convert.ToString(RangeOption.core_num());
                label_bar.Text     = "Done.";
                inprogress(100);
            }
        }
Example #2
0
        private void priceBookUsingSimulationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            double vol    = Convert.ToDouble(textBox_vol.Text) / 100;
            var    query1 = from i in Program.PMC.Prices select i;

            //if just 2 rates
            if ((from i in Program.PMC.InterestRates select i.Rate).Count() < 3)
            {
                MessageBox.Show("Please add more interest rate!");
            }
            else
            {
                // if the prices is not enough
                if ((query1.Count() == 0) || (query1.Count() < (from i in Program.PMC.Instruments where i.InstType.TypeName == "Stock" select i).Count()))
                {
                    MessageBox.Show("Please add more historical prices!");
                }
                else
                {
                    listView_Alltrades.BeginUpdate();
                    foreach (ListViewItem i in listView_Alltrades.Items)
                    {
                        int   id    = Convert.ToInt32(i.SubItems[0].Text);
                        Trade trade = Program.PMC.Trades.SingleOrDefault(j => j.Id == id);
                        if (id > 0)
                        {
                            int direction;
                            if (i.SubItems[1].Text == "BUY")
                            {
                                direction = 1;
                            }
                            else
                            {
                                direction = -1;
                            }
                            int    quantity    = Convert.ToInt32(i.SubItems[2].Text);
                            string instrument  = i.SubItems[3].Text;
                            string instrtype   = i.SubItems[4].Text;
                            double marketprice = Convert.ToDouble(i.SubItems[5].Text);
                            //get the id of instrument
                            int        Instid      = GetData.InstrumentID(instrument);
                            Instrument instrument1 = GetData.instrument(Instid);
                            double     S           = GetData.S(id);
                            double     K           = GetData.strike(Instid);
                            double     t           = GetData.tenor(Instid);
                            double     r           = GetData.rate(t, Instid);
                            double     rebate      = Convert.ToDouble(instrument1.Rebate);
                            double     barrier     = Convert.ToDouble(instrument1.Barrier);
                            int        barriertype = 0;
                            if (instrument1.BarrierType == "Down and out")
                            {
                                barriertype = 1;
                            }
                            if (instrument1.BarrierType == "Up and out")
                            {
                                barriertype = 2;
                            }
                            if (instrument1.BarrierType == "Down and in")
                            {
                                barriertype = 3;
                            }
                            if (instrument1.BarrierType == "Up and in")
                            {
                                barriertype = 4;
                            }
                            int optiontype = 0;
                            if (instrtype == "Stock")
                            {
                                optiontype = 0;
                            }
                            if (instrtype == "EuropeanOption")
                            {
                                optiontype = 1;
                            }
                            if (instrtype == "AsianOption")
                            {
                                optiontype = 2;
                            }
                            if (instrtype == "DigitalOption")
                            {
                                optiontype = 3;
                            }
                            if (instrtype == "BarrierOption")
                            {
                                optiontype = 4;
                            }
                            if (instrtype == "LookbackOption")
                            {
                                optiontype = 5;
                            }
                            if (instrtype == "RangeOption")
                            {
                                optiontype = 6;
                            }

                            bool Iscall;
                            if (instrument1.IsCall == true)
                            {
                                Iscall = true;
                            }
                            else
                            {
                                Iscall = false;
                            }
                            if (optiontype == 0)
                            {
                                //stock
                                //market price
                                i.SubItems.Add(Convert.ToString(S));
                                //P&L
                                i.SubItems.Add(((S - marketprice) * direction * quantity).ToString());
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity));
                                //gamma
                                i.SubItems.Add(Convert.ToString(0));
                                //vega
                                i.SubItems.Add(Convert.ToString(0));
                                //rho
                                i.SubItems.Add(Convert.ToString(0));
                                //theta
                                i.SubItems.Add(Convert.ToString(0));
                            }
                            //European
                            if (optiontype == 1)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option European = new MonteCarloSim_ExoticOptions.European(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, 0, 0, 0);
                                //market price
                                i.SubItems.Add(Convert.ToString(European.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((European.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * European.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * European.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * European.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * European.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * European.Rho()));
                            }
                            //Asian
                            if (optiontype == 2)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option AisanOption = new MonteCarloSim_ExoticOptions.Asian(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, 0, 0, 0);
                                //market price
                                i.SubItems.Add(Convert.ToString(AisanOption.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((AisanOption.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * AisanOption.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * AisanOption.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * AisanOption.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * AisanOption.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * AisanOption.Rho()));
                            }
                            //Digital
                            if (optiontype == 3)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option DigitalOption = new MonteCarloSim_ExoticOptions.Digital(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, rebate, 0, 0);
                                //market price
                                i.SubItems.Add(Convert.ToString(DigitalOption.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((DigitalOption.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * DigitalOption.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * DigitalOption.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * DigitalOption.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * DigitalOption.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * DigitalOption.Rho()));
                            }
                            //Barrier
                            if (optiontype == 4)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option BarrierOption = new MonteCarloSim_ExoticOptions.Barrier(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, 0, barrier, barriertype);
                                //market price
                                i.SubItems.Add(Convert.ToString(BarrierOption.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((BarrierOption.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * BarrierOption.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * BarrierOption.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * BarrierOption.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * BarrierOption.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * BarrierOption.Rho()));
                            }
                            //Lookback
                            if (optiontype == 5)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option LookbackOption = new MonteCarloSim_ExoticOptions.Lookback(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, 0, 0, 0);
                                //market price
                                i.SubItems.Add(Convert.ToString(LookbackOption.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((LookbackOption.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * LookbackOption.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * LookbackOption.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * LookbackOption.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * LookbackOption.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * LookbackOption.Rho()));
                            }
                            //Range
                            if (optiontype == 6)
                            {
                                //option
                                MonteCarloSim_ExoticOptions.Option RangeOption = new MonteCarloSim_ExoticOptions.Range(S, K, r, vol, t, 10000, 50, Iscall, true, false, true, 0, 0, 0);
                                //market price
                                i.SubItems.Add(Convert.ToString(RangeOption.OptionPrice()[0]));
                                //P&L
                                i.SubItems.Add(Convert.ToString((RangeOption.OptionPrice()[0] - marketprice) * direction * quantity));
                                //delta
                                i.SubItems.Add(Convert.ToString(direction * quantity * RangeOption.Delta()));
                                //gamma
                                i.SubItems.Add(Convert.ToString(direction * quantity * RangeOption.Gamma()));
                                //vega
                                i.SubItems.Add(Convert.ToString(direction * quantity * RangeOption.Vega()));
                                //theta
                                i.SubItems.Add(Convert.ToString(direction * quantity * RangeOption.Theta()));
                                //rho
                                i.SubItems.Add(Convert.ToString(direction * quantity * RangeOption.Rho()));
                            }
                        }
                    }
                }
            }
            listView_Alltrades.EndUpdate();
        }