コード例 #1
0
ファイル: Form1.cs プロジェクト: fcdslz/QuantitativeTrading
        private void optionTypeComboBox_SelectedIndexChanted(object sender, EventArgs e)
        {
            int index = optionTypeComboBox.SelectedIndex;

            switch (index)
            {
            case 2:
                amountLabel.Text      = "Barrier Price";
                amountTextBox.Visible = true;
                outputDataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                outputDataGridView.DataSource          = BarrierOption.SetDataTable();
                break;

            case 3:
                amountLabel.Text              = "Rebate Price";
                amountTextBox.Visible         = true;
                outputDataGridView.DataSource = DigitalOption.SetDataTable();
                break;

            case 5:
                outputDataGridView.DataSource = RangeOption.SetDataTable();
                break;

            default:
                amountLabel.Text              = "";
                amountTextBox.Visible         = false;
                outputDataGridView.DataSource = EuropeanOption.SetDataTable();
                break;
            }
        }
コード例 #2
0
        private static DigitalOption GetOption(bool isCall,
                                               bool isAssetSettled,
                                               double strike = 105)
        {
            var expiryDate = new DateTime(2018, 5, 31);
            var option     = new DigitalOption("testAsset", expiryDate, strike, isCall, "testCurve", isAssetSettled);

            return(option);
        }
コード例 #3
0
        private void add2_Click(object sender, EventArgs e)
        {
            try
            {
                InstType inst = new InstType();
                if (insttype2.Text != "" && underlying2.Text != "")
                {
                    inst.TypeName = insttype2.Text.ToString();
                    inst.Underlying = underlying2.Text.ToString();
                    portfolio.InstTypes.Add(inst);
                    portfolio.SaveChanges();
                    IQueryable<InstType> var = from p in portfolio.InstTypes
                                               orderby p.Id descending
                                               select p;
                    inst.Id = var.First().Id;//find the latest insttype you add
                    if (inst.TypeName.ToUpper() == "BARRIEROPION")//if you add barrieroption, you should also add wheather it is in out up down and its barrierprice
                    {
                        barrier2.ReadOnly = false;
                        up_in2.Enabled = true;
                        up_out2.Enabled = true;
                        down_in2.Enabled = true;
                        down_out2.Enabled = true;
                        BarrierOption bar = new BarrierOption();
                        bar.barrier = Convert.ToDouble(barrier2.Value);
                        bar.InstTypeId = inst.Id;
                        if (up_in2.Checked == true)
                        {
                            bar.IsIn = true;
                            bar.IsUp = true;
                        }
                        else if (down_in2.Checked == true)
                        {
                            bar.IsIn = true;
                            bar.IsUp = false;
                        }
                        else if (up_out2.Checked == true)
                        {
                            bar.IsIn = false;
                            bar.IsUp = true;
                        }
                        else if (down_out2.Checked == true)
                        {
                            bar.IsIn = false;
                            bar.IsUp = false;
                        }
                        portfolio.BarrierOptions.Add(bar);
                        portfolio.SaveChanges();
                    }
                    else if (insttype2.Text.ToUpper() == "DIGITALOPTION")//if you add digitaloption, you should also add its rebate price
                    {
                        rebate2.ReadOnly = false;
                        DigitalOption dig = new DigitalOption();
                        dig.rebate = Convert.ToDouble(rebate2.Value);
                        dig.InstTypeId = inst.Id;
                        portfolio.DigitalOptions.Add(dig);
                        portfolio.SaveChanges();
                    }
                    else if (insttype2.Text.ToUpper() == "LOOKBACKOPTION")//if you add lookbackoption, you should choose fixing or floating for payoff function
                    {
                        fix2.Enabled = true;
                        floating2.Enabled = true;
                        LookBackOption look = new LookBackOption();
                        if (fix2.Checked == true)
                            look.IsFixed = true;
                        else
                            look.IsFixed = false;
                        look.InstTypeId = inst.Id;
                        portfolio.LookBackOptions.Add(look);
                        portfolio.SaveChanges();
                    }
                    MessageBox.Show("Data added successfully", "Notice");
                    this.Close();

                }
                else
                    MessageBox.Show("Please input the TypeName and Underlying", "Notice");
            }
            catch { MessageBox.Show("Something wrong, please check wheather the inputs are correct."); }
        }
コード例 #4
0
        private void calculate_Click(object sender, RoutedEventArgs e)
        {
            this.sw.Reset(); this.sw.Start();
            graphPlot             = new GraphPlotting();
            this.DataContext      = graphPlot;
            this.graphs.IsEnabled = true;
            if (this.simulationNumber > 500 && this.steps > 500)
            {
                this.graphs.ToolTip = "Graph may take time to load ";
            }
            else
            {
                this.graphs.ToolTip = "";
            }
            ISecurity underlying = new Stock(this.underlyingPrice);
            Options   option     = null;

            switch (this.kind)
            {
            case OptionKind.ASIAN:
                option = new AsianOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind);
                break;

            case OptionKind.BARRIER:
                option = new BarrierOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind, this.rebate, this.barrierOptiont);
                break;

            case OptionKind.DIGITAL:
                option = new DigitalOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind, this.rebate);
                break;

            case OptionKind.EUROPEAN:
                option = new EuropeanOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind);
                break;

            case OptionKind.LOOKBACK:
                option = new LookbackOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind);
                break;

            case OptionKind.RANGE:
                option = new RangeOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind);
                break;

            default:
                option = new EuropeanOption(underlying.Symbol, underlying, this.maturityDate, this.strike, this.vol, this.type, this.kind);
                break;
            }
            try
            {
                Task work = Task.Factory.StartNew(() =>
                {
                    this.Dispatcher.Invoke(() => this.progress.IsActive = true);
                    option.calulateOptionPriceAndGreeks(this.simulationNumber, this.rate, this.steps, this.anitheticReductionEnabled, this.controlVariateEnabled, this.multithreadingEnabled, plot: this.graphPlot);
                    this.sw.Stop();
                    this.Dispatcher.Invoke(() => { display(option); this.progress.IsActive = false; });
                });
            }
            catch (Exception message)
            {
                MessageBox.Show(message.Message.ToString() + "\n" + message.StackTrace.ToString());
            }
        }
コード例 #5
0
        private void bPriceOptionBook_Click(object sender, RoutedEventArgs e)
        {
            graphPlot          = new GraphPlotting();
            graphs.DataContext = graphPlot;
            Task work = Task.Factory.StartNew(() =>
            {
                var orderBook = model.OrderBookDBs.ToList();
                foreach (var trade in orderBook)
                {
                    if (trade.InstrumentsDB.SecurityTypeDB.TypeName.Equals("Stocks"))
                    {
                        var instrument   = model.StockDBs.Where(x => x.Symbol == trade.InstrumentsDB.Symbol).First();
                        trade.FairPrice  = trade.Price;
                        trade.Delta      = trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity;
                        trade.ProfitLoss = 0;
                        trade.Theta      = 0;
                        trade.Gamma      = 0;
                        trade.Vega       = 0;
                        trade.Rho        = 0;
                        lock (lck)
                        {
                            model.SaveChanges();
                        }
                    }
                    else if (trade.InstrumentsDB.SecurityTypeDB.TypeName.Equals("Options"))
                    {
                        var instrument       = model.OptionsDBs.Where(x => x.Symbol == trade.InstrumentsDB.Symbol).First();
                        Options option       = null;
                        ISecurity underlying = new Stock(instrument.StockDB.LastTradedPrice);
                        switch (instrument.OptionKindDB.OptionKindName)
                        {
                        case "Asian Option":
                            option = new AsianOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.ASIAN);
                            break;

                        case "Barrier Option":
                            option = new BarrierOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.BARRIER, (Double)instrument.Barrier, (instrument.BarrierOptionType == "downout" ? BarrierOptionType.DOWNOUT : (instrument.BarrierOptionType == "downin" ? BarrierOptionType.DOWNIN : (instrument.BarrierOptionType == "upout" ? BarrierOptionType.UPOUT : BarrierOptionType.UPIN))));
                            break;

                        case "Digital Option":
                            option = new DigitalOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.DIGITAL, (Double)instrument.Rebate);
                            break;

                        case "European Option":
                            option = new EuropeanOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.EUROPEAN);
                            break;

                        case "Lookback Option":
                            option = new LookbackOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.LOOKBACK);
                            break;

                        case "Range Option":
                            option = new RangeOption(instrument.Symbol, underlying, instrument.MaturityDate, (Double)instrument.StrikePrice, instrument.StockDB.HistoricalVolatility, instrument.OptionType == "CALL" ? OptionType.CALL : OptionType.PUT, OptionKind.RANGE);
                            break;

                        default:
                            break;
                        }
                        try
                        {
                            option.calulateOptionPriceAndGreeks(1000, 0.05, (option.ExpiryDate - DateTime.Now).Days, true, option.OptionKind == OptionKind.EUROPEAN ? true : false, true, plot: graphPlot);
                            trade.FairPrice  = Math.Round(option.Price, 4);
                            trade.ProfitLoss = Math.Round((Double)trade.FairPrice - (Double)trade.Price, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            trade.Delta      = Math.Round(option.Greeks.Delta, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            trade.Theta      = Math.Round(option.Greeks.Theta, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            trade.Gamma      = Math.Round(option.Greeks.Gamma, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            trade.Vega       = Math.Round(option.Greeks.Vega, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            trade.Rho        = Math.Round(option.Greeks.Rho, 4) * (trade.Position == "BUY" ? trade.Quantity : -1 * trade.Quantity);
                            lock (lck)
                            {
                                model.SaveChanges();
                            }
                        }
                        catch (Exception message)
                        {
                            MessageBox.Show(message.Message.ToString() + "\n" + message.StackTrace.ToString());
                        }
                    }
                }
                this.Dispatcher.Invoke(() => {
                    dataGrid.DataContext = orderBook;
                });
            });
        }
コード例 #6
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);
            }
        }
コード例 #7
0
        private void testdata()
        {
            InstType eur = new InstType();
            eur.TypeName = "EuropeanOption";
            eur.Underlying = "MSFT";
            portfolio.InstTypes.Add(eur);
            portfolio.SaveChanges();
            InstType asi = new InstType();
            asi.TypeName = "AsianOption";
            asi.Underlying = "GOOG";
            portfolio.InstTypes.Add(asi);
            portfolio.SaveChanges();
            InstType bar = new InstType();
            bar.TypeName = "BarrierOption";
            bar.Underlying = "SINA";
            portfolio.InstTypes.Add(bar);
            portfolio.SaveChanges();
            var v1 = (from i in portfolio.InstTypes
                      where i.TypeName == "BarrierOption"
                      select i.Id).First();
            BarrierOption b = new BarrierOption();
            b.InstTypeId = v1;
            b.IsIn = true;
            b.IsUp = true;
            b.barrier = 60;
            portfolio.BarrierOptions.Add(b);
            portfolio.SaveChanges();
            InstType dig = new InstType();
            dig.TypeName = "DigitalOption";
            dig.Underlying = "WEBO";
            portfolio.InstTypes.Add(dig);
            portfolio.SaveChanges();
            var v2 = (from i in portfolio.InstTypes
                      where i.TypeName == "DigitalOption"
                      select i.Id).First();
            DigitalOption d = new DigitalOption();
            d.InstTypeId = v2;
            d.rebate = 50;
            portfolio.DigitalOptions.Add(d);
            portfolio.SaveChanges();
            InstType look = new InstType();
            look.TypeName = "LookBackOption";
            look.Underlying = "BAIDU";
            portfolio.InstTypes.Add(look);
            portfolio.SaveChanges();
            var v3 = (from i in portfolio.InstTypes
                      where i.TypeName == "LookBackOption"
                      select i.Id).First();
            LookBackOption l = new LookBackOption();
            l.InstTypeId = v3;
            l.IsFixed = true;
            portfolio.LookBackOptions.Add(l);
            portfolio.SaveChanges();
            InstType ran = new InstType();
            ran.TypeName = "RangeOption";
            ran.Underlying = "SOHU";
            portfolio.InstTypes.Add(ran);
            portfolio.SaveChanges();

            InstType sto = new InstType();
            sto.TypeName = "Stock";
            sto.Underlying = "MSFT";
            portfolio.InstTypes.Add(sto);
            portfolio.SaveChanges();
        }