//submit button on the combobox click event private void button1_Click(object sender, EventArgs e) { try { int volume = Convert.ToInt32(textBox1.Text); double price = Convert.ToDouble(textBox2.Text); //make sure volume and price are 0 or greater if (volume <= 0 || price <= 0) { throw new Exception(); } //create new sellOrder, then add sell order to the respective companies sell orderlist //using the combobox selected index Class_SellOrder order = new Class_SellOrder(price, volume); realTimeData.CompanyList[comboBox1.SelectedIndex].addSellOrder(order); //notify observes realTimeData.Notify(); //clear textbox's for the next user sell input textBox1.Clear(); textBox2.Clear(); } catch { //Show an invalid error message if user entered inputs are invalid MessageBox.Show("You have entered an invalid value ", "Invalid Input"); } }
/* SELL ORDER LIST RELATED METHODS */ //add a new sell order to the sell order list public void addSellOrder(Class_SellOrder order) { sellOrdersList.Add(order); //add sell order sellOrdersList = sellOrdersList.OrderBy(x => x.getPrice()).ToList(); sellTransactionCheck(order); }
//check if we can complete any open buy orders when creating a new sell order public void sellTransactionCheck(Class_SellOrder order) { foreach (Class_Order o in buyOrdersList) { //if there is a price in buy orders greater than or equal to the selling price then a transcation can occur if (o.getPrice() >= order.getPrice()) { //if the buy order has more shares than the sell order if (o.getOrderSize() > order.getOrderSize()) { //add order shares to the current share volume, set new buy order size by minusing the seller size, and update company variables for the stock state summary this.volume += order.getOrderSize(); o.setOrderSize(o.getOrderSize() - order.getOrderSize()); UpdateCompanyHelper(order); //add sell order to the transaction list no more shares left on sell order so break transactionsList.Add(order); break; } //if the sell order has more shares than the buy order else if (o.getOrderSize() < order.getOrderSize()) { //add order shares to the current share volume, set new sell order size by minus the buy order size, and update company variables for the stock state summary this.volume += o.getOrderSize(); order.setOrderSize(order.getOrderSize() - o.getOrderSize()); UpdateCompanyHelper(o); //add buy order to the transaction list and keeping searching still shares to buy transactionsList.Add(o); } //if the buy order and sell order have matching share sizes else { //add order shares to the current share volume, and update company variables for the stock state summary this.volume += order.getOrderSize(); UpdateCompanyHelper(order); //add both buy order and sell order to the transaction list no more shares left on sell order so break transactionsList.Add(o); transactionsList.Add(order); break; } } } //removing the current pending orders since they have now got a match foreach (Class_Order temp in transactionsList) { Class_SellOrder dummy = new Class_SellOrder(); if (temp.GetType().Equals(dummy.GetType())) { sellOrdersList.Remove((Class_SellOrder)temp); } else { buyOrdersList.Remove((Class_BuyOrder)temp); } } }