//买入按钮 private void btn_submit_Click(object sender, EventArgs e) { if (this.buy_quantity == 0) { MessageBox.Show("请输入购买数量!"); return; } if (!this.isCanBuy) { MessageBox.Show("请输入有效的A股代码!"); return; } //MessageBox.Show(string.Format("买入价格:{0}, 买入数量:{1}",this.buy_price,this.buy_quantity)); //生成买入的委托记录 Commission model = new Commission(); model.hold_stock_info = this.hold_stock_info_select; model.commission_price = this.buy_price; model.commission_amount = this.buy_quantity; // 1.买入 2. 卖出 model.direction = 1; // 状态:1.已撤销 2.已成交 3.已提交(默认值) model.state = 3; model.remain = this.buy_quantity; model.stockholder_id = Utility.user.id; if (this.hold_stock_info_select == null) { //生成持有股票记录 (数据库中无持有此股票) Hold_Stock_Info holdStockInfo = new Hold_Stock_Info(); holdStockInfo.stock_name = this.stock_name; holdStockInfo.stock_code = this.stock_code; holdStockInfo.amount_useable = 0; holdStockInfo.hold_quantity = this.buy_quantity; holdStockInfo.cost_price = this.buy_price * this.buy_quantity; holdStockInfo.stock_holder_id = Utility.user.id; holdStockInfo.type = 1; // 1. 持有股 2. 自选股 commissionService.AddCommission(model, holdStockInfo); //委托交易成功后,减少 bankroll的金额 cost_price //TODO } else { commissionService.AddCommission(model, this.hold_stock_info_select); hold_stock_infoService.UpdateAmountUseableBuy(this.buy_quantity, this.buy_price, this.hold_stock_info_select); //委托交易成功后,减少 bankroll的金额 //TODO } this.DialogResult = DialogResult.OK; //关闭窗口 this.Close(); }
//更新数据 public void updateStockInfo(Hold_Stock_Info model) { string sql = "update t_hold_stock_info set " + "market_price = @market_price, profit_loss = @profit_loss, profit_loss_per = @profit_loss_per ," + "current_price = @current_price, stock_name = @stock_name where id = @id"; List <MySqlParameter> Paramter = new List <MySqlParameter>(); Paramter.Add(new MySqlParameter("@market_price", model.market_price)); Paramter.Add(new MySqlParameter("@profit_loss", model.profit_loss)); Paramter.Add(new MySqlParameter("@profit_loss_per", model.profit_loss_per)); Paramter.Add(new MySqlParameter("@current_price", model.current_price)); Paramter.Add(new MySqlParameter("@id", model.id)); Paramter.Add(new MySqlParameter("@stock_name", model.stock_name)); DBHelperSQL.Ins.ExecuteNonquery(sql, Paramter.ToArray()); }
//添加持有股票信息 public int AddHoldStockInfo(Hold_Stock_Info model) { string sql = "insert into t_hold_stock_info (" + "stock_holder_id, stock_name, stock_code,amount_useable,hold_quantity,cost_price,market_price,profit_loss,profit_loss_per,current_price,type) " + "values( @stock_holder_id, @stock_name, @stock_code, @amount_useable, @hold_quantity, @cost_price,@market_price,@profit_loss,@profit_loss_per,@current_price,@type) "; List <MySqlParameter> Paramter = new List <MySqlParameter>(); Paramter.Add(new MySqlParameter("@stock_holder_id", model.stock_holder_id)); Paramter.Add(new MySqlParameter("@stock_name", model.stock_name)); Paramter.Add(new MySqlParameter("@stock_code", model.stock_code)); Paramter.Add(new MySqlParameter("@amount_useable", "0")); Paramter.Add(new MySqlParameter("@hold_quantity", model.hold_quantity)); Paramter.Add(new MySqlParameter("@cost_price", model.cost_price)); Paramter.Add(new MySqlParameter("@market_price", "0")); Paramter.Add(new MySqlParameter("@profit_loss", "0")); Paramter.Add(new MySqlParameter("@profit_loss_per", "0")); Paramter.Add(new MySqlParameter("@current_price", "0")); // 1. 买入 2. 卖出 Paramter.Add(new MySqlParameter("@type", model.type)); return((int)DBHelperSQL.Ins.ExecuteLastId(sql, Paramter.ToArray())); }
//根据用户ID查询所有的委托记录 public List <Commission> GetAllCommissionById(int id) { string sql = "select * from t_commission as c right join t_hold_stock_info as h on c.hold_stock_info_id = h.id where c.stockholder_id = @id order by c.time"; List <MySqlParameter> Paramter = new List <MySqlParameter>(); Paramter.Add(new MySqlParameter("@id", id)); DataTable dt = DBHelperSQL.Ins.ExcuteDataTable(sql, Paramter.ToArray()); if (dt.Rows.Count > 0) { List <Commission> list = (List <Commission>) DataConvert <Commission> .ToList(dt); //股票信息 for (int i = 0; i < list.Count; i++) { Hold_Stock_Info holdStockInfo = new Hold_Stock_Info(); if (dt.Rows[i][11] != DBNull.Value) { holdStockInfo.stock_name = dt.Rows[i][11].ToString(); } if (dt.Rows[i][12] != DBNull.Value) { holdStockInfo.stock_code = dt.Rows[i][12].ToString(); } if (dt.Rows[i][13] != DBNull.Value) { holdStockInfo.amount_useable = int.Parse(dt.Rows[i][13].ToString()); } if (dt.Rows[i][14] != DBNull.Value) { holdStockInfo.hold_quantity = int.Parse(dt.Rows[i][14].ToString()); } if (dt.Rows[i][15] != DBNull.Value) { holdStockInfo.market_price = double.Parse(dt.Rows[i][15].ToString()); } if (dt.Rows[i][16] != DBNull.Value) { holdStockInfo.profit_loss = double.Parse(dt.Rows[i][16].ToString()); } if (dt.Rows[i][17] != DBNull.Value) { holdStockInfo.profit_loss_per = double.Parse(dt.Rows[i][17].ToString()); } if (dt.Rows[i][18] != DBNull.Value) { holdStockInfo.current_price = double.Parse(dt.Rows[i][18].ToString()); } if (dt.Rows[i][19] != DBNull.Value) { holdStockInfo.cost_price = double.Parse(dt.Rows[i][19].ToString()); } list[i].hold_stock_info = holdStockInfo; } return(list); } else { return(null); } }
//定时获取上证指数等信息 private void query(string stock_code) { string stock_data_content; //股票数据内容 WebRequest req = WebRequest.Create("http://hq.sinajs.cn/list=" + stock_code); //上证指数 WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, Encoding.Default); stock_data_content = streamReader.ReadToEnd(); //股票数据 string stock_data = stock_data_content.Substring(stock_data_content.IndexOf("\"") + 1, (stock_data_content.LastIndexOf("\"") - stock_data_content.IndexOf("\"") - 1)); string[] divide = new string[] { "," }; string[] divide_result; divide_result = stock_data.Split(divide, StringSplitOptions.None); if (divide_result.Length != 33) { //MessageBox.Show("数据信息获取错误,请输入正确的上证代码!"); return; } this.时间标签.Text = divide_result[30] + " " + divide_result[31]; //买 this.sell_1.Text = divide_result[10]; this.sell_1_price.Text = divide_result[11]; this.sell_2.Text = divide_result[12]; this.sell_2_price.Text = divide_result[13]; this.sell_3.Text = divide_result[14]; this.sell_3_price.Text = divide_result[15]; this.sell_4.Text = divide_result[16]; this.sell_4_price.Text = divide_result[17]; this.sell_5.Text = divide_result[18]; this.sell_5_price.Text = divide_result[19]; this.涨跌标签.Text = "涨跌:" + (double.Parse(divide_result[3]) - double.Parse(divide_result[2])).ToString("f2"); if (double.Parse(divide_result[3]) >= double.Parse(divide_result[2])) { this.涨跌标签.ForeColor = Color.Red; } else { this.涨跌标签.ForeColor = Color.Chartreuse; } this.lab_stock_name.Text = divide_result[0]; //计算可买股票数量 int stock_id = Utility.user.id; Stock_Holder sh = stock_HolderService.getStockHolder(stock_id); double bankroll_useable = sh.account.bankroll_useable; this.current_price = double.Parse(divide_result[3]); this.sell_price = double.Parse(divide_result[3]); //可以卖的股票数量 var datalist = sh.HoldStockInfo.Where(hsi => hsi.stock_code.Equals(stock_code)).ToList(); this.hold_stock_info_select = datalist[0]; this.canSell = hold_stock_info_select.amount_useable; this.lab_cansell.Text = canSell.ToString(); this.btn_max.Enabled = true; //买入的默认价格 this.numericUpDown_sell_price.Value = Convert.ToDecimal(divide_result[3]); this.lab_get_money.Text = Convert.ToDouble(current_price * sell_quantity).ToString(); }
//显示数据 private bool showData(string stock_data_content, string stock_k_model) { //股票数据 string stock_data = stock_data_content.Substring(stock_data_content.IndexOf("\"") + 1, (stock_data_content.LastIndexOf("\"") - stock_data_content.IndexOf("\"") - 1)); string[] divide = new string[] { "," }; string[] divide_result; divide_result = stock_data.Split(divide, StringSplitOptions.None); // K线图 this.pictureBox1.ImageLocation = stock_k_model + stock_code + ".gif"; if (divide_result.Length != 33) { this.isReachResult = false; //MessageBox.Show("数据信息获取错误,请输入正确的上证代码!"); return(false); } if (this.股票名字.Text != divide_result[0]) { this.股票名字.Text = divide_result[0]; } this.今日开盘价.Text = divide_result[1]; this.昨日收盘价.Text = divide_result[2]; this.当前价格.Text = double.Parse(divide_result[3]).ToString("f2"); for (int i = 0; i < 2; i++) { this.当前价格.ForeColor = Color.White; if (double.Parse(divide_result[3]) >= double.Parse(divide_result[2])) { this.当前价格.ForeColor = Color.Red; } else { this.当前价格.ForeColor = Color.Chartreuse; } } this.今日最高价.Text = divide_result[4]; this.今日最低价.Text = divide_result[5]; this.竞买价.Text = divide_result[6]; this.竞卖价.Text = divide_result[7]; this.成交的股票数.Text = divide_result[8]; this.成交金额.Text = divide_result[9]; this.涨跌标签.Text = "涨跌:" + (double.Parse(divide_result[3]) - double.Parse(divide_result[2])).ToString("f2"); if (double.Parse(divide_result[3]) >= double.Parse(divide_result[2])) { this.涨跌标签.ForeColor = Color.Red; } else { this.涨跌标签.ForeColor = Color.Chartreuse; } this.时间标签.Text = divide_result[30] + " " + divide_result[31]; //买 this.buy_1.Text = divide_result[10]; this.buy_1_price.Text = divide_result[11]; this.buy_2.Text = divide_result[12]; this.buy_2_price.Text = divide_result[13]; this.buy_3.Text = divide_result[14]; this.buy_3_price.Text = divide_result[15]; this.buy_4.Text = divide_result[16]; this.buy_4_price.Text = divide_result[17]; this.buy_5.Text = divide_result[18]; this.buy_5_price.Text = divide_result[19]; //卖 this.sell_1.Text = divide_result[20]; this.sell_1_price.Text = divide_result[21]; this.sell_2.Text = divide_result[22]; this.sell_2_price.Text = divide_result[23]; this.sell_3.Text = divide_result[24]; this.sell_3_price.Text = divide_result[25]; this.sell_4.Text = divide_result[26]; this.sell_4_price.Text = divide_result[27]; this.sell_5.Text = divide_result[28]; this.sell_5_price.Text = divide_result[29]; //存储股票搜索信息 Hold_Stock_Info result = new Hold_Stock_Info(); result.stock_name = divide_result[0]; this.ReachInfo = result; this.isReachResult = true; return(true); }