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> /// <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); }
/// <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); }