/// <summary> /// 获取股票的实时数据 /// </summary> /// <param name="stockId">股票代码</param> /// <returns>返回股票数据</returns> public NowStockDataModel GetNowStockData(string stockId) { NowStockDataModel nowStockModel = new NowStockDataModel(); nowStockModel = nowStockDataDal.GetNowStockData(stockId); return(nowStockModel); }
/// <summary> /// 卖出股票 /// </summary> /// <param name="index">股票在datatable中的位置</param> /// <param name="count">卖出股票后剩余的数量</param> public int SellStock(int index, int count) { NowStockDataModel nowStockModel = new NowStockDataModel(); DataTable dt = new DataTable(); dt = stockTradeDal.SelectStocks(LoginInfo.loginInfo.UserName);//获取用户持有的所有股票 if (index > dt.Rows.Count - 1 || index == -1) { return(-1); } string stockId = dt.Rows[index][1].ToString().Trim(); nowStockModel = nowStockDataDal.GetNowStockData(stockId); StockTradeModel model = new StockTradeModel(); StockTradeModel availableModel = new StockTradeModel(); availableModel.UserID = LoginInfo.loginInfo.UserName; availableModel.StockID = stockId; availableModel.TradePrice = double.Parse(nowStockModel.CurrentPrice); availableModel.TradeCount = count; model.UserID = LoginInfo.loginInfo.UserName; model.StockID = stockId; model.TradePrice = double.Parse(nowStockModel.CurrentPrice); model.TradeCount = this.SelectStockCount(stockId) - count; stockTradeDal.AfterSellAvailableFund(availableModel); return(stockTradeDal.SellStock(model)); }
/// <summary> /// 更新自选股信息 /// </summary> private void UpdateOptionalStock() { DataTable dt1 = optionalBll.GetUserOptionalStock();//重新查询当前用户自选股信息 DataTable dtOptional = new DataTable(); dtOptional.Columns.Add("Number", typeof(int)); dtOptional.Columns.Add("stockId", typeof(string)); dtOptional.Columns.Add("stockName", typeof(string)); dtOptional.Columns.Add("growthRate", typeof(string)); dtOptional.Columns.Add("currentPrice", typeof(string)); dtOptional.Columns.Add("closePrice", typeof(string)); dtOptional.Columns.Add("openPrice", typeof(string)); dtOptional.Columns.Add("highestPrice", typeof(string)); dtOptional.Columns.Add("lowestPrice", typeof(string)); dtOptional.Columns.Add("buyOnePrice", typeof(string)); dtOptional.Columns.Add("buyOneCount", typeof(string)); dtOptional.Columns.Add("sellOnePrice", typeof(string)); dtOptional.Columns.Add("sellOneCount", typeof(string)); for (int i = 0; i < dt1.Rows.Count; i++) { DataRow dr = dtOptional.NewRow(); NowStockDataModel model = new NowStockDataModel(); model = stockTradeBll.GetNowStockData(dt1.Rows[i][1].ToString().Trim()); double growthRate = (double.Parse(model.CurrentPrice) - double.Parse(model.YesterdayClosePrice)) / double.Parse(model.YesterdayClosePrice) * 100; string growthRateStr; if (growthRate > 0) { growthRateStr = "+" + growthRate.ToString("0.00") + "%"; } else { growthRateStr = growthRate.ToString("0.00") + "%"; } dr[0] = i + 1; dr[1] = model.StockId; dr[2] = model.StockName; dr[3] = growthRateStr; dr[4] = model.CurrentPrice; dr[5] = model.YesterdayClosePrice; dr[6] = model.TodayOpenPrice; dr[7] = model.HighestPrice; dr[8] = model.LowestPrice; dr[9] = model.BuyOnePrice; dr[10] = model.BuyOneCount; dr[11] = model.SellOnePrice; dr[12] = model.SellOneCount; dtOptional.Rows.Add(dr); } dataGridView1.DataSource = dtOptional; if (dt1.Rows.Count != 0) { DisplayOptionalPicture(dt1.Rows[0][1].ToString().Trim()); } }
private void button1_Click(object sender, EventArgs e) { NowStockDataDal nowStockDataDal = new NowStockDataDal(); NowStockDataModel model = nowStockDataDal.GetNowStockData(textBox1.Text); if (model != null) { label1.Text = ""; label1.Text += model.StockId + " " + model.StockName + " " + model.TodayOpenPrice + " " + model.YesterdayClosePrice + " " + model.CurrentPrice + " " + model.HighestPrice + " " + model.LowestPrice + model.BuyOneCount + " " + model.BuyOnePrice + " " + model.SellOneCount + " " + model.SellOnePrice; } }
/// <summary> /// 获取当前股票的买入或卖出价格 /// </summary> /// <param name="stockId">股票代码</param> /// <returns>返回股票的买入或卖出价格</returns> public double GetCurrentStockPrice(string stockId) { double currentPrice = 0.0; NowStockDataModel nowStockModel = new NowStockDataModel(); nowStockModel = nowStockDataDal.GetNowStockData(stockId); if (nowStockModel != null) { currentPrice = double.Parse(nowStockModel.CurrentPrice); } return(currentPrice); }
/// <summary> /// 检测股票代码是否有效 /// </summary> /// <param name="stockId">股票代码</param> /// <returns>返回true表示股票代码有效</returns> public bool StockIdValid(string stockId) { bool IsStockIDValid = nowStockDataDal.CheckStockNumber(stockId); NowStockDataModel nowStockModel = new NowStockDataModel(); nowStockModel = nowStockDataDal.GetNowStockData(stockId); if (nowStockModel == null) { IsStockIDValid = false; } return(IsStockIDValid); }
/// <summary> /// 卖出股票 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSellStock_Click(object sender, EventArgs e) { if (comBoxStockName.Text == "" || textBoxSellCount.Text == "" || textBoxSellPrice.Text == "" || labSellStockTip.Visible) { return; } stockTradeBll.UpdateUserInfo(); int index = comBoxStockName.SelectedIndex; int count = int.Parse(textBoxSellCount.Text); int IsSellSuccess = stockTradeBll.SellStock(index, count); if (IsSellSuccess != -1) { string stockId = ""; stockTradeBll = new StockTradeBll(); //通过重新生成对象来刷新数据库中的数据 DataTable dt = new DataTable(); dt = stockTradeBll.GetUserStockInfo(); if (dt.Rows.Count == 0) { return; } if (dt.Rows[0][1].ToString() != "null") { stockId = dt.Rows[index][1].ToString().Trim(); int stockCount = stockTradeBll.SelectStockCount(stockId); if (stockCount == 0) //当用户的某支股票持有量为0时,删除该股票在数据库中的记录 { stockTradeBll.DeleteStockInfo(stockId); } labAvailableCount.Text = stockCount.ToString(); } double invaliable = stockTradeBll.GetUserInvaliableFund(); labAvailableFund.Text = invaliable.ToString(); MessageBox.Show("股票卖出成功", "信息提示", MessageBoxButtons.OK); NowStockDataModel model = new NowStockDataModel(); model = stockTradeBll.GetNowStockData(stockId); //string totalAsset = (LoginInfo.loginInfo.TotalAssets + double.Parse(model.CurrentPrice) * count).ToString("0.00"); string available = (LoginInfo.loginInfo.AvailableFund + double.Parse(model.CurrentPrice) * count).ToString("0.00"); MyPositionBll myPositionBll = new MyPositionBll(); myPositionBll.UpdateAvailableFund(available);//卖出股票后,更新总资产、可用资金 UpdateUserAssetStatus(); MyPosition(); } else { MessageBox.Show("股票卖出失败", "信息提示", MessageBoxButtons.OK); } }
/// <summary> /// 添加自选股 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnAddOptionalStock_Click(object sender, EventArgs e) { NowStockDataModel nowStockModel = new NowStockDataModel(); string stockId = textBoxOptionalStock.Text.Trim(); nowStockModel = stockTradeBll.GetNowStockData(stockId); if (nowStockModel == null) { MessageBox.Show("该股票代码不合法", "信息提示", MessageBoxButtons.OK); } if (nowStockModel != null)//nowStockModel不为空,说明该股票代码合法 { labOptionalValid.Visible = false; DataTable dt = new DataTable(); dt = optionalBll.GetUserOptionalStock(); if (dt != null) { bool IsOptionalStockExist = false;//股票代码为stockId的股票是否已经是自选股 for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i][1].ToString().Trim() == stockId) { IsOptionalStockExist = true; break; } } if (!IsOptionalStockExist) { optionalBll.AddOptionalStock(stockId); UpdateOptionalStock(); //更新自选股信息 MessageBox.Show("已成功添加到自选股", "信息提示", MessageBoxButtons.OK); } } else { optionalBll.AddOptionalStock(stockId); UpdateOptionalStock(); //更新自选股信息 MessageBox.Show("已成功添加到自选股", "信息提示", MessageBoxButtons.OK); } DisplayOptionalPicture(stockId); } else { labOptionalValid.Visible = true; } }
/// <summary> /// 获取当前用户持仓信息 /// </summary> /// <returns>返回当前用户持仓信息</returns> public DataTable GetUserPosition() { DataTable dt = myPosition.GetUserPositon(); if (dt == null) { return(null); } DataTable dtPositon = new DataTable(); dtPositon.Columns.Add("number", typeof(int)); dtPositon.Columns.Add("stockId", typeof(string)); dtPositon.Columns.Add("stockName", typeof(string)); dtPositon.Columns.Add("growthRate", typeof(string)); dtPositon.Columns.Add("totalEarn", typeof(string)); dtPositon.Columns.Add("buyPrice", typeof(string)); dtPositon.Columns.Add("buyCount", typeof(string)); for (int i = 0; i < dt.Rows.Count; i++) { NowStockDataModel model = new NowStockDataModel(); NowStockDataDal nowStockDal = new NowStockDataDal(); string stockId = dt.Rows[i][1].ToString().Trim(); model = nowStockDal.GetNowStockData(stockId); double growthRate = (double.Parse(model.CurrentPrice) - double.Parse(dt.Rows[i][3].ToString())) / double.Parse(dt.Rows[i][3].ToString()) * 100; double totalEarn = (double.Parse(model.CurrentPrice) - double.Parse(dt.Rows[i][3].ToString())) * int.Parse(dt.Rows[i][4].ToString()); string growthRateStr; if (growthRate > 0) { growthRateStr = "+" + growthRate.ToString("0.00") + "%"; } else { growthRateStr = growthRate.ToString("0.00") + "%"; } DataRow row = dtPositon.NewRow(); row[0] = i + 1; row[1] = stockId; row[2] = dt.Rows[i][2].ToString().Trim(); row[3] = growthRateStr; row[4] = totalEarn.ToString("0.00"); row[5] = dt.Rows[i][3].ToString().Trim(); row[6] = dt.Rows[i][4].ToString().Trim(); dtPositon.Rows.Add(row); } return(dtPositon); }
public NowStockDataModel GetNowStockData(string stockId) { model = new NowStockDataModel(); if (stockId.Length > 5) { string stock = ""; bool IsSuccess = CheckStockNumber(stockId, out stock); string url = StockNumberInput(stock); bool IsGetData = getstockdata(url); if (!IsGetData) { model = null; } return(model); } else { return(null); } }
/// <summary> /// 买入股票 /// </summary> /// <param name="StockId">股票代码</param> /// <param name="BuyCount">买入股票数量</param> /// <returns>放回true表示买入成功,返回false表示股票买入失败</returns> public bool BuyStock(string StockId, int BuyCount) { bool IsStockIDValid = nowStockDataDal.CheckStockNumber(StockId); if (!IsStockIDValid) //股票代码无效 { return(false); } StockTradeModel model = new StockTradeModel(); StockTradeModel availableModel = new StockTradeModel(); NowStockDataModel nowStockModel = new NowStockDataModel(); nowStockModel = nowStockDataDal.GetNowStockData(StockId); if (nowStockModel != null) { bool IsStockExist = false; //查看数据库中是否存在股票代码为StockId的股票 DataTable dt = new DataTable(); dt = stockTradeDal.SelectStocks(LoginInfo.loginInfo.UserName); int index = 0; if (dt.Rows.Count != 0) { for (index = 0; index < dt.Rows.Count; index++) { if (dt.Rows[index][1].ToString().Trim() == StockId) { IsStockExist = true; break; } } } model.UserID = LoginInfo.loginInfo.UserName; model.StockID = StockId; model.StockName = nowStockModel.StockName; availableModel.UserID = LoginInfo.loginInfo.UserName; availableModel.StockID = StockId; availableModel.StockName = nowStockModel.StockName; if (IsStockExist) { double lastBuyPrice = double.Parse(dt.Rows[index][3].ToString()); int lastBuyCount = int.Parse(dt.Rows[index][4].ToString()); double nowBuyPrice = double.Parse(nowStockModel.CurrentPrice); int nowBuyCount = BuyCount; availableModel.TradePrice = nowBuyPrice; availableModel.TradeCount = BuyCount; //买入的价格应该上上次的买入价格与现在买入股票价格的加权平均价格 nowBuyPrice = (lastBuyPrice * lastBuyCount + nowBuyPrice * nowBuyCount) / (nowBuyCount + lastBuyCount); nowBuyCount = lastBuyCount + nowBuyCount; model.TradePrice = nowBuyPrice; model.TradeCount = nowBuyCount; stockTradeDal.AfterBuyAvailableFund(availableModel); //更新可用资金 stockTradeDal.BuyStock(model, ""); //将买入股票的数据写入数据库(UPDATE) } else { model.TradePrice = double.Parse(nowStockModel.CurrentPrice); model.TradeCount = BuyCount; stockTradeDal.AfterBuyAvailableFund(model); //更新可用资金 stockTradeDal.BuyStock(model); //将买入股票的数据写入数据库(INSERT) } } else { return(false); } return(true); }
/// <summary> /// 焦点离开股票输入框发生事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void textBoxStockId_Leave(object sender, EventArgs e) { string stockId = textBoxStockId.Text; bool IsValid = stockTradeBll.StockIdValid(stockId); if (!IsValid) { textBoxStockId.Text = ""; labStockValid.Visible = true; } else { labStockValid.Visible = false; double currentPrice = stockTradeBll.GetCurrentStockPrice(stockId); textBoxBuyPrice.Text = currentPrice.ToString(); double invaliable = stockTradeBll.GetUserInvaliableFund(); labMaxBuyCount.Text = ((int)(invaliable / currentPrice)).ToString(); NowStockDataModel model = new NowStockDataModel(); model = stockTradeBll.GetNowStockData(stockId); labStockName.Text = model.StockName; labClosePrice.Text = double.Parse(model.YesterdayClosePrice).ToString("0.00"); labOpenPrice.Text = double.Parse(model.TodayOpenPrice).ToString("0.00"); labHighestPrice.Text = double.Parse(model.HighestPrice).ToString("0.00"); labLowestPrice.Text = double.Parse(model.LowestPrice).ToString("0.00"); labCurrentPrice.Text = double.Parse(model.CurrentPrice).ToString("0.00"); labBuyOnePrice.Text = double.Parse(model.BuyOnePrice).ToString("0.00"); labBuyOneCount.Text = model.BuyOneCount; labBuyTwoPrice.Text = double.Parse(model.BuyTwoPrice).ToString("0.00"); labBuyTwoCount.Text = model.BuyTwoCount; labBuyThreePrice.Text = double.Parse(model.BuyThreePrice).ToString("0.00"); labBuyThreeCount.Text = model.BuyThreeCount; labBuyFourPrice.Text = double.Parse(model.BuyFourPrice).ToString("0.00"); labBuyFourCount.Text = model.BuyFourCount; labBuyFivePrice.Text = double.Parse(model.BuyFivePrice).ToString("0.00"); labBuyFiveCount.Text = model.BuyFiveCount; labSellOnePrice.Text = double.Parse(model.SellOnePrice).ToString("0.00"); labSellOneCount.Text = model.SellOneCount; labSellTwoPrice.Text = double.Parse(model.SellTwoPrice).ToString("0.00"); labSellTwoCount.Text = model.SellTwoCount; labSellThreePrice.Text = double.Parse(model.SellThreePrice).ToString("0.00"); labSellThreeCount.Text = model.SellThreeCount; labSellFourPrice.Text = double.Parse(model.SellFourPrice).ToString("0.00"); labSellFourCount.Text = model.SellFourCount; labSellFivePrice.Text = double.Parse(model.SellFivePrice).ToString("0.00"); labSellFiveCount.Text = model.SellFiveCount; double growthRate = (double.Parse(model.CurrentPrice) - double.Parse(model.YesterdayClosePrice)) / double.Parse(model.YesterdayClosePrice) * 100; if (growthRate > 0) { labGrowthRate.Text = "+" + growthRate.ToString("0.00") + "%"; labGrowthRate.ForeColor = Color.Red; } else { labGrowthRate.Text = growthRate.ToString("0.00") + "%"; labGrowthRate.ForeColor = Color.Green; } double stockNum = double.Parse(stockId); if (stockNum >= 600000 && stockNum <= 603998) { stockId = "sh" + stockId; } else if (stockNum >= 000001 && stockNum <= 300489) { stockId = "sz" + stockId; } string pictureBuyOne = "http://image.sinajs.cn/newchart/min/n/" + stockId + ".gif"; string pictureBuyTwo = "http://image.sinajs.cn/newchart/daily/n/" + stockId + ".gif"; pictureBoxBuyOne.ImageLocation = pictureBuyOne; pictureBoxBuyTwo.ImageLocation = pictureBuyTwo; } }
/// <summary> /// 更新当前用户资产状况 /// </summary> private void UpdateUserAssetStatus() { labTotalAvailableFund.Text = LoginInfo.loginInfo.AvailableFund.ToString("0.00"); labStockTotalEarn.Text = (LoginInfo.loginInfo.TotalAssets - LoginInfo.loginInfo.AvailableFund).ToString("0.00"); DataTable dt = new DataTable(); dt = stockTradeBll.GetUserStockInfo(); double totalDayEarn = 0; //当前持仓日盈利额 double totalEarn = 0; //当前持仓总收益额 double totalCost = 0; //当前持仓成本 for (int i = 0; i < dt.Rows.Count; i++) { string stockId = dt.Rows[i][1].ToString().Trim(); NowStockDataModel model = new NowStockDataModel(); totalCost += double.Parse(dt.Rows[i][3].ToString()) * double.Parse(dt.Rows[i][4].ToString()); model = stockTradeBll.GetNowStockData(stockId); DateTime buyDate = Convert.ToDateTime(dt.Rows[i][5].ToString()); double dayEarn = 0.0; if (buyDate.ToShortDateString() == DateTime.Now.ToShortDateString())//该股票是今天才买的 { dayEarn = (double.Parse(model.CurrentPrice) - double.Parse(dt.Rows[i][3].ToString())) * int.Parse(dt.Rows[i][4].ToString()); } else//该股票不是今天买的 { dayEarn = (double.Parse(model.CurrentPrice) - double.Parse(model.YesterdayClosePrice)) * int.Parse(dt.Rows[i][4].ToString()); } totalDayEarn += dayEarn; double earn = (double.Parse(model.CurrentPrice) - double.Parse(dt.Rows[i][3].ToString())) * int.Parse(dt.Rows[i][4].ToString()); totalEarn += earn; } labTotalEarn.Text = (double.Parse(LoginInfo.loginInfo.TotalAssets.ToString("0.00")) + totalEarn).ToString("0.00"); double totalGrowthRate = (double.Parse(labTotalEarn.Text) - 100000.00) / 100000.00 * 100; if (totalGrowthRate > 0) { labTotalGrowthRate.Text = "+" + totalGrowthRate.ToString("0.00") + "%"; } else { labTotalGrowthRate.Text = totalGrowthRate.ToString("0.00") + "%"; } double dayGrowth = 0.0; if (totalCost != 0) { dayGrowth = totalDayEarn / totalCost * 100; } if (dayGrowth > 0) { labDayGrowth.Text = "+" + dayGrowth.ToString("0.00") + "%"; } else { labDayGrowth.Text = dayGrowth.ToString("0.00") + "%"; } labDayEarn.Text = totalDayEarn.ToString("0.00"); labTotalDayEarn.Text = totalEarn.ToString("0.00"); }
/// <summary> /// 卖出股票模块中选择卖出股票名称的组合框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comBoxStockName_SelectedIndexChanged(object sender, EventArgs e) { DataTable dt = new DataTable(); dt = stockTradeBll.GetUserStockInfo(); if (dt.Rows.Count == 0) { return; } int index = comBoxStockName.SelectedIndex; if (dt.Rows[0][1].ToString() != "null") { string stockId = dt.Rows[index][1].ToString().Trim(); stockTradeBll = new StockTradeBll(); //通过重新生成对象来刷新数据库中的数据 int stockCount = stockTradeBll.SelectStockCount(stockId); labAvailableCount.Text = stockCount.ToString(); NowStockDataModel model = new NowStockDataModel(); model = stockTradeBll.GetNowStockData(stockId); textBoxSellPrice.Text = model.CurrentPrice; labStockNameS.Text = model.StockName; labClosePriceS.Text = double.Parse(model.YesterdayClosePrice).ToString("0.00"); labOpenPriceS.Text = double.Parse(model.TodayOpenPrice).ToString("0.00"); labHighestPriceS.Text = double.Parse(model.HighestPrice).ToString("0.00"); labLowestPriceS.Text = double.Parse(model.LowestPrice).ToString("0.00"); labCurrentPriceS.Text = double.Parse(model.CurrentPrice).ToString("0.00"); labBuyOnePriceS.Text = double.Parse(model.BuyOnePrice).ToString("0.00"); labBuyOneCountS.Text = model.BuyOneCount; labBuyTwoPriceS.Text = double.Parse(model.BuyTwoPrice).ToString("0.00"); labBuyTwoCountS.Text = model.BuyTwoCount; labBuyThreePriceS.Text = double.Parse(model.BuyThreePrice).ToString("0.00"); labBuyThreeCountS.Text = model.BuyThreeCount; labBuyFourPriceS.Text = double.Parse(model.BuyFourPrice).ToString("0.00"); labBuyFourCountS.Text = model.BuyFourCount; labBuyFivePriceS.Text = double.Parse(model.BuyFivePrice).ToString("0.00"); labBuyFiveCountS.Text = model.BuyFiveCount; labSellOnePriceS.Text = double.Parse(model.SellOnePrice).ToString("0.00"); labSellOneCountS.Text = model.SellOneCount; labSellTwoPriceS.Text = double.Parse(model.SellTwoPrice).ToString("0.00"); labSellTwoCountS.Text = model.SellTwoCount; labSellThreePriceS.Text = double.Parse(model.SellThreePrice).ToString("0.00"); labSellThreeCountS.Text = model.SellThreeCount; labSellFourPriceS.Text = double.Parse(model.SellFourPrice).ToString("0.00"); labSellFourCountS.Text = model.SellFourCount; labSellFivePriceS.Text = double.Parse(model.SellFivePrice).ToString("0.00"); labSellFiveCountS.Text = model.SellFiveCount; double growthRate = (double.Parse(model.CurrentPrice) - double.Parse(model.YesterdayClosePrice)) / double.Parse(model.YesterdayClosePrice) * 100; if (growthRate > 0) { labGrowthRateS.Text = "+" + growthRate.ToString("0.00") + "%"; labGrowthRateS.ForeColor = Color.Red; } else { labGrowthRateS.Text = growthRate.ToString("0.00") + "%"; labGrowthRateS.ForeColor = Color.Green; } double stockNum = double.Parse(stockId); if (stockNum >= 600000 && stockNum <= 603998) { stockId = "sh" + stockId; } else if (stockNum >= 000001 && stockNum <= 300489) { stockId = "sz" + stockId; } string pictureSellOne = "http://image.sinajs.cn/newchart/min/n/" + stockId + ".gif"; string pictureSellTwo = "http://image.sinajs.cn/newchart/daily/n/" + stockId + ".gif"; pictureBoxSellOne.ImageLocation = pictureSellOne; pictureBoxSellTwo.ImageLocation = pictureSellTwo; } }