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()); } }
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; }); }); }