Пример #1
0
        protected void gvStockBuyOrder_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            lblBuyMessage.Visible = false;
            // Get the index of the row that was clicked, the order reference number and the stock code.
            int         index           = Convert.ToInt32(e.CommandArgument);
            GridViewRow row             = gvStockBuyOrder.Rows[index];
            int         referenceNumber = Convert.ToInt32(row.Cells[6].Text.Trim());
            string      stockCode       = row.Cells[7].Text.Trim();

            if (e.CommandName == "ExecuteOrder")
            {
                // Get required values from the order.
                decimal limitPrice  = 0;
                decimal stopPrice   = 0;
                string  price       = ((System.Web.UI.WebControls.TextBox)row.FindControl("txtExecutePrice")).Text.Trim();
                string  buyAmount   = ((System.Web.UI.WebControls.TextBox)row.FindControl("txtExecuteAmount")).Text.Trim();
                decimal orderAmount = Convert.ToDecimal(row.Cells[9].Text);
                string  orderType   = row.Cells[10].Text.Trim();
                if (orderType == "limit" || orderType == "stop limit")
                {
                    limitPrice = Convert.ToDecimal(row.Cells[13].Text.Trim());
                }
                if (orderType == "stop" || orderType == "stop limit")
                {
                    stopPrice = Convert.ToDecimal(row.Cells[14].Text.Trim());
                }

                // Check if inputs are valid.
                if (!executionIsValid("buy", orderType, price, buyAmount, orderAmount, limitPrice, stopPrice))
                {
                    return;
                }

                // Calculate the number of shares to buy, the number of shares remaining to buy and set the status accordingly.
                decimal buyShares       = Convert.ToDecimal(buyAmount) / Convert.ToDecimal(price);
                decimal remainingAmount = orderAmount - Convert.ToDecimal(buyAmount);
                string  orderStatus     = "partial";
                if (remainingAmount == 0)
                {
                    orderStatus = "completed";
                }

                // Create a transaction for the buy order, change the buy order status, update the security price and refresh the buy orders.
                SqlTransaction trans = myExternalData.beginTransaction();
                myExternalData.setData("insert into [Transaction]([referenceNumber], [executeDate], [executeShares], [executePrice]) values (" +
                                       referenceNumber + ", '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "', " + buyShares + ", '" + price + "')", trans);
                myExternalData.setData("update [Order] set [status]='" + orderStatus + "', [amount]=" + remainingAmount + " where [referenceNumber]=" + referenceNumber, trans);
                myExternalData.setData("update [Stock] set [close]='" + price + "' where [code]='" + stockCode + "'", trans);
                myExternalData.commitTransaction(trans);
                getStockBuyOrders();
            }

            if (e.CommandName == "CancelOrder")
            {
                SqlTransaction trans = myExternalData.beginTransaction();
                myExternalData.setData("update [Order] set [status]='cancelled' where [referenceNumber]=" + referenceNumber, trans);
                myExternalData.commitTransaction(trans);
                getStockBuyOrders();
            }

            if (e.CommandName == "GetPrice")
            {
                string currentPrice = myExternalFunctions.getSecuritiesPrice("stock", stockCode).ToString();
                showMessage("buy", "The current price of stock " + stockCode + " is " + currentPrice + ".", "info");
            }
        }