コード例 #1
0
ファイル: Form1.cs プロジェクト: fcdslz/QuantitativeTrading
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            object[] parameters   = e.Argument as object[];
            int      steps        = Convert.ToInt16(parameters[5]);
            int      trials       = Convert.ToInt32(parameters[6]);
            double   s            = Convert.ToDouble(parameters[0]);
            double   k            = Convert.ToDouble(parameters[1]);
            double   t            = Convert.ToDouble(parameters[2]);
            double   sig          = Convert.ToDouble(parameters[3]);
            double   r            = Convert.ToDouble(parameters[4]);
            string   type         = parameters[7].ToString();
            double   exoticAmount = Convert.ToDouble(parameters[8]);

            try
            {
                for (int i = 1; i <= 1; i++)
                {
                    if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
                    else
                    {
                        switch (type)
                        {
                        case "None":
                            e.Result = EuropeanOption.GetDataSet(steps, trials, s, k, t, sig, r);
                            break;

                        case "Asian":
                            e.Result = AsianOption.GetDataSet(steps, trials, s, k, t, sig, r);
                            break;

                        case "Barrier":
                            e.Result = BarrierOption.GetDataSet(steps, trials, s, k, t, sig, r, exoticAmount);
                            break;

                        case "Digital":
                            e.Result = DigitalOption.GetDataSet(steps, trials, s, k, t, sig, r, exoticAmount);
                            break;

                        case "Lookback":
                            e.Result = LookBackOption.GetDataSet(steps, trials, s, k, t, sig, r, exoticAmount);
                            break;

                        case "Range":
                            e.Result = RangeOption.GetDataSet(steps, trials, s, k, t, sig, r, exoticAmount);
                            break;

                        default:
                            break;
                        }

                        // Perform a time consuming operation and report progress.
                        Thread.Sleep(100);
                        worker.ReportProgress(i * 100);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #2
0
        private void priceTradeBookToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem i in ListTrade.Items)
            {
                Int32 tmp = Convert.ToInt32(i.Text);
                Trade tt  = (from q in AppEntities.Trades
                             where q.Id == tmp
                             select q).FirstOrDefault();


                double s   = getLatestPrice(tt.Instrument);
                double k   = Convert.ToDouble(tt.Instrument.Strike);
                double t   = (double)tt.Instrument.Tenor;
                double r   = getInterestRate(tt.Instrument);
                double sig = Convert.ToDouble(textBoxVol.Text) / 100.0;
                int    instrumentTypeId = (Int32)tt.Instrument.InstrumentTypeId;
                int    barrierTypeId    = (Int32)tt.Instrument.BarrierTypeId;
                double barrier          = (double)tt.Instrument.Barrier;
                double rebate           = (double)tt.Instrument.Rebate;

                int     steps = 100, trials = 10000;
                DataSet result = new DataSet();
                Dictionary <String, double> stockMetric = new Dictionary <string, double>();
                stockMetric.Add("Delta", 1);

                switch (instrumentTypeId)
                {
                case 1:
                    stockMetric.Add("Delta", 1);
                    break;

                case 2:
                    result = AsianOption.GetDataSet(steps, trials, s, k, t, sig, r);
                    break;

                case 3:
                    result = EuropeanOption.GetDataSet(steps, trials, s, k, t, sig, r);
                    break;

                case 4:
                    result = BarrierOption.GetDataSet(steps, trials, s, k, t, sig, r, barrier);
                    break;

                case 5:
                    result = RangeOption.GetDataSet(steps, trials, s, k, t, sig, r, rebate);
                    break;

                case 6:
                    result = LookBackOption.GetDataSet(steps, trials, s, k, t, sig, r, 0);
                    break;

                default:
                    break;
                }

                ListViewItem i2;
                i2      = new ListViewItem();
                i2.Text = tt.Id.ToString();
                i2.SubItems.Add(tt.Instrument.Ticker);
                i2.SubItems.Add(tt.Instrument.InstrumentType.TypeName);
                i2.SubItems.Add(tt.IsBuy ? "BUY" : "SELL");
                i2.SubItems.Add(tt.Quantity.ToString());
                i2.SubItems.Add(tt.Price.ToString());
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                i2.SubItems.Add("10");
                ListTrade.Items.Add(i2);
            }
        }