Example #1
0
 private void button_Click(object sender, RoutedEventArgs e)
 {
     try                                                                 //error handle
     {
         double       stockprice = Convert.ToDouble(textBox_S.Text);     //get stock price from textbox
         double       strike     = Convert.ToDouble(textBox_K.Text);     // get strike price from textbox
         double       time       = Convert.ToDouble(textBox_T.Text);     //get tenor from textbox
         double       sigma      = Convert.ToDouble(textBox_sigma.Text); //get volitality from textbox
         double       r          = Convert.ToDouble(textBox_r.Text);     //get interest rate from textbox
         int          step       = Convert.ToInt32(textBox_step.Text);   //get num of steps of one simulation
         int          trial      = Convert.ToInt32(textBox_trial.Text);  //get num of trials
         option       a          = new option();
         randomnumber b          = new randomnumber();
         a.Strike          = strike;
         a.Stockprice      = stockprice;
         a.Step            = step;
         a.Trial           = trial;
         a.Time            = time;
         a.Vol             = sigma;
         a.Rate            = r;
         double[,] rmatrix = b.randommatrixgenerator(step, trial); //get random matrix
         a.Randommatrix    = rmatrix;
         if (radioButton.IsChecked == true)                        //get the option type if option is a call
         {
             double[] list1 = a.callgreek();                       //get result
             //output result to GUI
             textBox_delta.Text = Convert.ToString(list1[0]);
             textBox_gamma.Text = Convert.ToString(list1[1]);
             textBox_vega.Text  = Convert.ToString(list1[2]);
             textBox_rho.Text   = Convert.ToString(list1[3]);
             textBox_theta.Text = Convert.ToString(list1[4]);
             textBox_Price.Text = Convert.ToString(list1[5]);
             textBox_std.Text   = Convert.ToString(list1[6]);
         }
         else if (radioButton1.IsChecked == true)  //if option is a put
         {
             double[] list1 = a.putgreek();
             //output result to GUI
             textBox_delta.Text = Convert.ToString(list1[0]);
             textBox_gamma.Text = Convert.ToString(list1[1]);
             textBox_vega.Text  = Convert.ToString(list1[2]);
             textBox_rho.Text   = Convert.ToString(list1[3]);
             textBox_theta.Text = Convert.ToString(list1[4]);
             textBox_Price.Text = Convert.ToString(list1[5]);
             textBox_std.Text   = Convert.ToString(list1[6]);
         }
         else  //if the optiontype is not determined
         {
             MessageBox.Show("optiontype can't be null");
         }
     }
     catch (Exception x)
     {
         MessageBox.Show("Error:" + x);
     }
 }
Example #2
0
        }                                                                //get core

        //use threads method to generate every part of thread to compute stock price process
        public void threads(double strike, double stockprice, double rate, double vol, int step, int trial, int beginnumber, double time, bool antithetic, bool cvmethod, bool calloption)
        {
            randomnumber r = new randomnumber();
            option       o = new option();

            double[,] randommatrix = new double[trial, step];
            //use bool antithetic to decide random matrix
            if (antithetic)
            {
                randommatrix = r.antitheticgenerator(step, trial);
            }
            else
            {
                randommatrix = r.randommatrixgenerator(step, trial);
            }

            if (cvmethod)
            {
                double[,] p1 = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);
                //get option value using cvmethod
                double[] optionlist = CVmethod(strike, stockprice, rate, vol, step, trial, time, p1, calloption);
                for (int i = 0; i < p1.GetLength(0); i++)
                {
                    for (int j = 0; j < p1.GetLength(1) - 1; j++)
                    {
                        _matrix[beginnumber + i, j] = p1[i, j];
                    }
                    _matrix[beginnumber + i, p1.GetLength(1) - 1] = optionlist[i];
                }
            }
            else //if not control variate
            {
                double[,] p1 = pricematrix(strike, stockprice, rate, vol, step, trial, time, randommatrix);

                for (int i = 0; i < p1.GetLength(0); i++)
                {
                    for (int j = 0; j < p1.GetLength(1); j++)
                    {
                        _matrix[beginnumber + i, j] = p1[i, j];
                    }
                }
            }
        }
 private void button_Click(object sender, RoutedEventArgs e)
 {
     try     //error handle
     {
         progress = 0;
         watch.Reset();
         watch.Start();
         Thread t1         = new Thread(new ThreadStart(dowork)); t1.Start();
         double stockprice = Convert.ToDouble(textBox_S.Text);     //get stock price from textbox
         double strike     = Convert.ToDouble(textBox_K.Text);     // get strike price from textbox
         double time       = Convert.ToDouble(textBox_T.Text);     //get tenor from textbox
         double sigma      = Convert.ToDouble(textBox_sigma.Text); //get volitality from textbox
         double r          = Convert.ToDouble(textBox_r.Text);     //get interest rate from textbox
         int    step       = Convert.ToInt32(textBox_step.Text);   //get num of steps of one simulation
         int    trial      = Convert.ToInt32(textBox_trial.Text);  //get num of trials
         int    core       = System.Environment.ProcessorCount;    //get num of core
         textBox_time_core.Text = Convert.ToString(core);
         option       a = new option();
         randomnumber b = new randomnumber();
         a.Strike          = strike;
         a.Stockprice      = stockprice;
         a.Step            = step;
         a.Trial           = trial;
         a.Time            = time;
         a.Vol             = sigma;
         a.Rate            = r;
         double[,] rmatrix = new double[step, trial];
         if (mutiple.IsChecked == true) //use mutiple thread
         {
             mutiplethread thread = new mutiplethread();
             thread.Strike     = strike;
             thread.Stockprice = stockprice;
             thread.Step       = step;
             thread.Trial      = trial;
             thread.Time       = time;
             thread.Vol        = sigma;
             thread.Rate       = r;
             thread.getlength();
             bool boolantithetic = (antithetic.IsChecked == true);
             bool boolcvmethod   = (control_variate.IsChecked == true);
             bool boolcall       = true;
             if (callButton.IsChecked == true)
             {
                 boolcall = (callButton.IsChecked == true);
             }
             else if (putButton1.IsChecked == true)
             {
                 boolcall = (putButton1.IsChecked == false);
             }
             else
             {
                 MessageBox.Show("optiontype can't be null");
             }
             //create a list1 to save output
             double[] list1 = thread.threadpractice(strike, stockprice, r, sigma, step, trial, core, time, boolantithetic, boolcvmethod, boolcall);
             textBox_delta.Text = Convert.ToString(list1[0]);
             textBox_gamma.Text = Convert.ToString(list1[1]);
             textBox_vega.Text  = Convert.ToString(list1[2]);
             textBox_rho.Text   = Convert.ToString(list1[3]);
             textBox_theta.Text = Convert.ToString(list1[4]);
             textBox_Price.Text = Convert.ToString(list1[5]);
             textBox_se.Text    = Convert.ToString(list1[6]);
         }
         else //not use mutiple thread
         {
             if (antithetic.IsChecked == true)
             {
                 rmatrix = b.antitheticgenerator(step, trial);
             }                                                //use antithetic method to get random matrix
             else
             {
                 rmatrix = b.randommatrixgenerator(step, trial);
             }                                           //get random matrix
             a.Randommatrix = rmatrix;
             if (callButton.IsChecked == true)           //get the option type if option is a call
             {
                 if (control_variate.IsChecked == false) //if not use cv method
                 {
                     double[] list1 = a.callgreek();     //get result
                                                         //output result to GUI
                     textBox_delta.Text = Convert.ToString(list1[0]);
                     textBox_gamma.Text = Convert.ToString(list1[1]);
                     textBox_vega.Text  = Convert.ToString(list1[2]);
                     textBox_rho.Text   = Convert.ToString(list1[3]);
                     textBox_theta.Text = Convert.ToString(list1[4]);
                     textBox_Price.Text = Convert.ToString(list1[5]);
                     if (antithetic.IsChecked == true) //if use antithetic variance reduction
                     {
                         textBox_se.Text = Convert.ToString(list1[7]);
                     }
                     else
                     {
                         textBox_se.Text = Convert.ToString(list1[6]);//if use traditional way to produce random number
                     }
                 }
                 else //use the cv method
                 {
                     double[] list1 = a.CVcallgreek();  //get result
                                                        //output result to GUI
                     textBox_delta.Text = Convert.ToString(list1[0]);
                     textBox_gamma.Text = Convert.ToString(list1[1]);
                     textBox_vega.Text  = Convert.ToString(list1[2]);
                     textBox_rho.Text   = Convert.ToString(list1[3]);
                     textBox_theta.Text = Convert.ToString(list1[4]);
                     textBox_Price.Text = Convert.ToString(list1[5]);
                     if (antithetic.IsChecked == true) //if use antithetic variance reduction
                     {
                         textBox_se.Text = Convert.ToString(list1[7]);
                     }
                     else
                     {
                         textBox_se.Text = Convert.ToString(list1[6]);//if use traditional way to produce random number
                     }
                 }
             }
             else if (putButton1.IsChecked == true)      //if option is a put
             {
                 if (control_variate.IsChecked == false) //if not use the cv method
                 {
                     double[] list1 = a.putgreek();
                     //output result to GUI
                     textBox_delta.Text = Convert.ToString(list1[0]);
                     textBox_gamma.Text = Convert.ToString(list1[1]);
                     textBox_vega.Text  = Convert.ToString(list1[2]);
                     textBox_rho.Text   = Convert.ToString(list1[3]);
                     textBox_theta.Text = Convert.ToString(list1[4]);
                     textBox_Price.Text = Convert.ToString(list1[5]);
                     if (antithetic.IsChecked == true)
                     {
                         textBox_se.Text = Convert.ToString(list1[7]);
                     }
                     else
                     {
                         textBox_se.Text = Convert.ToString(list1[6]);
                     }
                 }
                 else  //if use the cv method
                 {
                     double[] list1 = a.CVputgreek();
                     //output result to GUI
                     textBox_delta.Text = Convert.ToString(list1[0]);
                     textBox_gamma.Text = Convert.ToString(list1[1]);
                     textBox_vega.Text  = Convert.ToString(list1[2]);
                     textBox_rho.Text   = Convert.ToString(list1[3]);
                     textBox_theta.Text = Convert.ToString(list1[4]);
                     textBox_Price.Text = Convert.ToString(list1[5]);
                     if (antithetic.IsChecked == true)
                     {
                         textBox_se.Text = Convert.ToString(list1[7]);
                     }
                     else
                     {
                         textBox_se.Text = Convert.ToString(list1[6]);
                     }
                 }
             }
             else  //if the optiontype is not determined
             {
                 MessageBox.Show("optiontype can't be null");
             }
         }
     }
     catch (Exception x)
     {
         MessageBox.Show("Error:" + x);
     }
     watch.Stop(); //add timer
     textBox_time.Text = watch.Elapsed.Hours.ToString() + ":" + watch.Elapsed.Minutes.ToString() + ":" + watch.Elapsed.Seconds.ToString() + ":" + watch.Elapsed.Milliseconds.ToString();
 }
 private void button_Click(object sender, RoutedEventArgs e)
 {
     try     //error handle
     {
         watch.Reset();
         watch.Start();
         double       stockprice = Convert.ToDouble(textBox_S.Text);     //get stock price from textbox
         double       strike     = Convert.ToDouble(textBox_K.Text);     // get strike price from textbox
         double       time       = Convert.ToDouble(textBox_T.Text);     //get tenor from textbox
         double       sigma      = Convert.ToDouble(textBox_sigma.Text); //get volitality from textbox
         double       r          = Convert.ToDouble(textBox_r.Text);     //get interest rate from textbox
         int          step       = Convert.ToInt32(textBox_step.Text);   //get num of steps of one simulation
         int          trial      = Convert.ToInt32(textBox_trial.Text);  //get num of trials
         option       a          = new option();
         randomnumber b          = new randomnumber();
         a.Strike          = strike;
         a.Stockprice      = stockprice;
         a.Step            = step;
         a.Trial           = trial;
         a.Time            = time;
         a.Vol             = sigma;
         a.Rate            = r;
         double[,] rmatrix = new double[step, trial];
         if (antithetic.IsChecked == true)
         {
             rmatrix = b.antitheticgenerator(step, trial);
         }                                                //use antithetic method to get random matrix
         else
         {
             rmatrix = b.randommatrixgenerator(step, trial);
         }                                           //get random matrix
         a.Randommatrix = rmatrix;
         if (callButton.IsChecked == true)           //get the option type if option is a call
         {
             if (control_variate.IsChecked == false) //if not use cv method
             {
                 double[] list1 = a.callgreek();     //get result
                                                     //output result to GUI
                 textBox_delta.Text = Convert.ToString(list1[0]);
                 textBox_gamma.Text = Convert.ToString(list1[1]);
                 textBox_vega.Text  = Convert.ToString(list1[2]);
                 textBox_rho.Text   = Convert.ToString(list1[3]);
                 textBox_theta.Text = Convert.ToString(list1[4]);
                 textBox_Price.Text = Convert.ToString(list1[5]);
                 if (antithetic.IsChecked == true) //if use antithetic variance reduction
                 {
                     textBox_se.Text = Convert.ToString(list1[7]);
                 }
                 else
                 {
                     textBox_se.Text = Convert.ToString(list1[6]);//if use traditional way to produce random number
                 }
             }
             else //use the cv method
             {
                 double[] list1 = a.CVcallgreek();  //get result
                                                    //output result to GUI
                 textBox_delta.Text = Convert.ToString(list1[0]);
                 textBox_gamma.Text = Convert.ToString(list1[1]);
                 textBox_vega.Text  = Convert.ToString(list1[2]);
                 textBox_rho.Text   = Convert.ToString(list1[3]);
                 textBox_theta.Text = Convert.ToString(list1[4]);
                 textBox_Price.Text = Convert.ToString(list1[5]);
                 if (antithetic.IsChecked == true) //if use antithetic variance reduction
                 {
                     textBox_se.Text = Convert.ToString(list1[7]);
                 }
                 else
                 {
                     textBox_se.Text = Convert.ToString(list1[6]);//if use traditional way to produce random number
                 }
             }
         }
         else if (putButton1.IsChecked == true)      //if option is a put
         {
             if (control_variate.IsChecked == false) //if not use the cv method
             {
                 double[] list1 = a.putgreek();
                 //output result to GUI
                 textBox_delta.Text = Convert.ToString(list1[0]);
                 textBox_gamma.Text = Convert.ToString(list1[1]);
                 textBox_vega.Text  = Convert.ToString(list1[2]);
                 textBox_rho.Text   = Convert.ToString(list1[3]);
                 textBox_theta.Text = Convert.ToString(list1[4]);
                 textBox_Price.Text = Convert.ToString(list1[5]);
                 if (antithetic.IsChecked == true)
                 {
                     textBox_se.Text = Convert.ToString(list1[7]);
                 }
                 else
                 {
                     textBox_se.Text = Convert.ToString(list1[6]);
                 }
             }
             else  //if use the cv method
             {
                 double[] list1 = a.CVputgreek();
                 //output result to GUI
                 textBox_delta.Text = Convert.ToString(list1[0]);
                 textBox_gamma.Text = Convert.ToString(list1[1]);
                 textBox_vega.Text  = Convert.ToString(list1[2]);
                 textBox_rho.Text   = Convert.ToString(list1[3]);
                 textBox_theta.Text = Convert.ToString(list1[4]);
                 textBox_Price.Text = Convert.ToString(list1[5]);
                 if (antithetic.IsChecked == true)
                 {
                     textBox_se.Text = Convert.ToString(list1[7]);
                 }
                 else
                 {
                     textBox_se.Text = Convert.ToString(list1[6]);
                 }
             }
         }
         else  //if the optiontype is not determined
         {
             MessageBox.Show("optiontype can't be null");
         }
     }
     catch (Exception x)
     {
         MessageBox.Show("Error:" + x);
     }
     watch.Stop(); //add timer
     textBox_time.Text = watch.Elapsed.Hours.ToString() + ":" + watch.Elapsed.Minutes.ToString() + ":" + watch.Elapsed.Seconds.ToString() + ":" + watch.Elapsed.Milliseconds.ToString();
 }