private void button4_Click(object sender, EventArgs e) //search a selected rate { try { int id = Convert.ToInt32(textBox_rateID.Text); using (var db = new MyEntityModelContainer()) { var data = (from tt in db.Entity_rate where tt.Id == id select new { id = tt.Id, tenor = tt.Tenor, insterest_rate = tt.Interest_rate }).ToList(); if (data.FirstOrDefault() == null) { MessageBox.Show("Selected ID does not exist!"); //if not return result } else { foreach (var item in data) { textBox_rateID.Text = item.id.ToString(); textBox_tenor.Text = item.tenor.ToString(); textBox_rate.Text = item.insterest_rate.ToString(); } } } } catch (Exception x) { MessageBox.Show("Error:" + x); } }
private void button_search_Click(object sender, EventArgs e) { try { int id = Convert.ToInt32(textBox_id.Text); using (var db = new MyEntityModelContainer()) { var data = (from instrument in db.Entity_instrument where instrument.Id == id select new { id = instrument.Id, ticker = instrument.Ticker, exchange = instrument.Exchange, companyname = instrument.CompanyName, underlying = instrument.Underlying, rateid = instrument.Entity_rateId, strike = instrument.Strike, tenor = instrument.Tenor, rebate = instrument.Rebate, barrier = instrument.Barrier, type = instrument.Type, instype = instrument.Instype }).ToList(); if (data.FirstOrDefault() == null) { MessageBox.Show("Selected ID does not exist!"); //if not return result } else { foreach (var q in data) { textBox_exchange.Text = q.exchange.ToString(); textBox_companyname.Text = q.companyname.ToString(); textBox_Ticker.Text = q.ticker.ToString(); textBox_strike.Text = q.strike.ToString(); textBox_barrier.Text = q.barrier.ToString(); textBox_rateid.Text = q.rateid.ToString(); textBox_tenor.Text = q.tenor.ToString(); textBox_Underlying.Text = q.underlying.ToString(); textBox_rebate.Text = q.rebate.ToString(); comboBox_istype.SelectedItem = q.instype.ToString(); comboBox_type.SelectedItem = q.type.ToString(); } } } } catch (Exception x) { MessageBox.Show("Error:" + x); } }
} //edit history price private void refreshTradesFromDatabaseToolStripMenuItem_Click(object sender, EventArgs e) //display all trades in database { try { using (var db = new MyEntityModelContainer()) { var data = (from trade in db.Entity_Trade join inst in db.Entity_instrument on trade.Entity_instrumentId equals inst.Id select new { id = trade.Id, direction = trade.Direction, quantity = trade.Quantity, instrumentid = inst.Id, type = inst.Type, instrumenttype = inst.Instype, tradeprice = trade.Tradeprice }).ToList(); Listview_Alltrades.Items.Clear(); //clear listview foreach (var q in data) //display listview in listview_alltrades { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); if (q.type == null) { lv.SubItems.Add("Neither call nor put"); } else { lv.SubItems.Add(q.type.ToString()); } lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); Listview_Alltrades.Items.Add(lv); } MessageBox.Show("Refresh successfully!"); } } catch (Exception x) { MessageBox.Show("Error:" + x); } }
private void button4_Click(object sender, EventArgs e) //search selected trade { try { int id = Convert.ToInt32(textBox_tradeID.Text); using (var db = new MyEntityModelContainer()) { var data = (from tt in db.Entity_Trade //search trade with selected id where tt.Id == id select new { id = tt.Id, direction = tt.Direction, quantity = tt.Quantity, instrumentid = tt.Entity_instrumentId, tradeprice = tt.Tradeprice }).ToList(); if (data.FirstOrDefault() == null) { MessageBox.Show("Selected ID does not exist!"); //if not return result } else { foreach (var item in data) //display data { textBox_quantity.Text = item.quantity.ToString(); textBox_instid.Text = item.instrumentid.ToString(); textBox_tradeprice.Text = item.tradeprice.ToString(); if (item.direction == 0) { textBox_direction.Text = "Sell"; } else { textBox_direction.Text = "Buy"; } } } } } catch (Exception x) { MessageBox.Show("Error:" + x); } }
private void button4_Click(object sender, EventArgs e) //search selected history price { try { int id = Convert.ToInt32(textBox_id.Text); using (var db = new MyEntityModelContainer()) { var data = (from tt in db.Entity_Historyprice where tt.Id == id select new { id = tt.Id, companyname = tt.CompanyName, ticker = tt.Ticker, closeprice = tt.ClosePrice, date = tt.Date }).ToList(); if (data.FirstOrDefault() == null) { MessageBox.Show("Selected ID does not exist!"); //if not return result } else { foreach (var item in data) { textBox_name.Text = item.companyname.ToString(); textBox_companyticker.Text = item.ticker.ToString(); textBox_date.Text = item.date.ToString(); textBox_histprice.Text = item.closeprice.ToString(); } } } } catch (Exception x) { MessageBox.Show("Error:" + x); } }
//compute greek and mark price in very trade private void priceBookUsingSimulationToolStripMenuItem_Click(object sender, EventArgs e) { try { if (textBox_vol.Text == "") { MessageBox.Show("You need input a volatility!"); } using (var db = new MyEntityModelContainer()) { double vol = Convert.ToDouble(textBox_vol.Text); //get vol int core = System.Environment.ProcessorCount; //get num of core var data = (from trade in db.Entity_Trade //get data from database join inst in db.Entity_instrument on trade.Entity_instrumentId equals inst.Id join r in db.Entity_rate on inst.Entity_rateId equals r.Id join price in db.Entity_Historyprice on inst.Ticker equals price.Ticker select new //get enough parameter to compute greek and simulation price { id = trade.Id, direction = trade.Direction, quantity = trade.Quantity, instrumentid = trade.Entity_instrumentId, type = inst.Type, instrumenttype = inst.Instype, tradeprice = trade.Tradeprice, underlying = inst.Underlying, strike = inst.Strike, tenor = inst.Tenor, rate = r.Interest_rate, rebate = inst.Rebate, barrier = inst.Barrier, histprice = price.ClosePrice }).ToList(); Listview_Alltrades.Items.Clear(); Listview_totals.Items.Clear(); foreach (var q in data) //output data in listview { string instrumentype = q.instrumenttype; double delta; double rho; double gamma; double vega; double theta; double PL; if (instrumentype == "Stock") //if instrument is stock { ListViewItem lv = new ListViewItem(q.id.ToString()); //get an instrument line if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add("neither call nor put"); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); lv.SubItems.Add(q.histprice.ToString()); //close price as mark price PL = (q.histprice - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; //compute p&l delta = (q.direction - 0.5) * 2 * q.quantity; vega = 0; rho = 0; theta = 0; gamma = 0; lv.SubItems.Add(PL.ToString()); lv.SubItems.Add(delta.ToString()); lv.SubItems.Add(gamma.ToString()); lv.SubItems.Add(vega.ToString()); lv.SubItems.Add(theta.ToString()); lv.SubItems.Add(rho.ToString()); Listview_Alltrades.Items.Add(lv); } else if (instrumentype == "European") //if instrument type is european option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread thread = new multiplethread(); double strike = Convert.ToDouble(q.strike); bool iscall = q.type == "Call"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = 1; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } else if (instrumentype == "Asian") //if instrument type is asian option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread_asian thread = new multiplethread_asian(); double strike = Convert.ToDouble(q.strike); bool iscall = q.type == "Call"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = tenor; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } else if (instrumentype == "Lookback")//if instrument type is lookback option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread_lookback thread = new multiplethread_lookback(); double strike = Convert.ToDouble(q.strike); bool iscall = q.type == "Call"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = tenor; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } else if (instrumentype == "Digital")//if instrument type is digital option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread_digital thread = new multiplethread_digital(); double strike = Convert.ToDouble(q.strike); double rebate = Convert.ToDouble(q.rebate); thread.Rebate = rebate; bool iscall = q.type == "Call"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = tenor; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } else if (instrumentype == "Range")//if instrument type is range option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread_range thread = new multiplethread_range(); double strike = 0; double rebate = Convert.ToDouble(q.rebate); bool iscall = q.type == "Call"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = tenor; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } else //if instrument type is barrier option { ListViewItem lv = new ListViewItem(q.id.ToString()); if (q.direction == 0) { lv.SubItems.Add("Sell"); } else { lv.SubItems.Add("Buy"); } lv.SubItems.Add(q.quantity.ToString()); lv.SubItems.Add(q.instrumentid.ToString()); lv.SubItems.Add(q.type.ToString()); lv.SubItems.Add(q.instrumenttype.ToString()); lv.SubItems.Add(q.tradeprice.ToString()); multiplethread_barrier thread = new multiplethread_barrier(); double strike = 0; double rebate = Convert.ToDouble(q.rebate); bool iscall = q.type == "Call"; double barrier = Convert.ToDouble(q.barrier); thread.Bar = barrier;//input barrier thread.IsOut = q.instrumenttype == "Barrier(Down out)" || q.instrumenttype == "Barrier(Up out)"; thread.IsDown = q.instrumenttype == "Barrier(Down out)" || q.instrumenttype == "Barrier(Down in)"; double tenor = Convert.ToDouble(q.tenor); thread.Strike = strike; thread.Stockprice = q.underlying; thread.Time = tenor; thread.Step = 200; thread.Trial = 10000; thread.Vol = vol; thread.Rate = q.rate; thread.getlength(); double[] list1 = thread.threadpractice(strike, q.underlying, q.rate, vol, 200, 10000, core, tenor, false, false, iscall); PL = (list1[5] - q.tradeprice) * (q.direction - 0.5) * 2 * q.quantity; delta = (q.direction - 0.5) * 2 * q.quantity * list1[0]; vega = list1[2] * (q.direction - 0.5) * 2 * q.quantity; rho = list1[3] * (q.direction - 0.5) * 2 * q.quantity; theta = list1[4] * (q.direction - 0.5) * 2 * q.quantity; gamma = list1[1] * (q.direction - 0.5) * 2 * q.quantity; lv.SubItems.Add(list1[5].ToString()); //simulation price lv.SubItems.Add(PL.ToString()); //P and L lv.SubItems.Add(delta.ToString()); //delta lv.SubItems.Add(gamma.ToString()); //gamma lv.SubItems.Add(vega.ToString()); //vega lv.SubItems.Add(theta.ToString()); //theta lv.SubItems.Add(rho.ToString()); //rho Listview_Alltrades.Items.Add(lv); } } } MessageBox.Show("Simulation has been done!");//finish simulation } catch (Exception x) { MessageBox.Show("Error:" + x); } }