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