/// <summary> /// 改变代码 /// </summary> /// <param name="code"></param> public void ChangeSecurity(String code) { if (currentCode != code) { currentCode = code; } InitControl(); if (minuteMode) { minuteDatasPos = 0; minuteDatas.Clear(); minuteDatas = GetSecurityMinuteDatas(Application.StartupPath + "\\SH600000_M.txt"); UpdateDataToGraphMinute(minuteDatas, true); } else { lastData = new SecurityLatestData(); try { List <SecurityData> datas = ChartExtend.GetSinaHistoryDatasByStr(code, cycle); UpdateDataToGraph(datas, true); datas.Clear(); } catch (Exception ex) { } } }
/// <summary> /// 秒表事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { SecurityLatestData newData = new SecurityLatestData(); SecurityService.GetLatestData(securityCode, ref newData); if (!newData.equal(lastData)) { lastData = newData; Invalidate(); } }
/// <summary> /// 获取最新数据 /// </summary> /// <param name="code">代码</param> /// <param name="latestData">最新数据</param> /// <returns>状态</returns> public static int GetLatestData(String code, ref SecurityLatestData latestData) { int state = 0; lock (latestDatasCache) { if (latestDatasCache.ContainsKey(code)) { latestData.copy(latestDatasCache[code]); state = 1; } } return(state); }
/// <summary> /// 根据字符串获取新浪最新数据 /// </summary> /// <param name="str">数据字符串</param> /// <param name="formatType">格式</param> /// <param name="datas">最新数据</param> /// <returns>状态</returns> public static int GetLatestDatasBySinaStr(String str, int formatType, List <SecurityLatestData> datas) { String[] strs = str.Split(new String[] { ";\n" }, StringSplitOptions.RemoveEmptyEntries); int strLen = strs.Length; for (int i = 0; i < strLen; i++) { SecurityLatestData latestData = new SecurityLatestData(); String dataStr = strs[i]; GetLatestDataBySinaStr(strs[i], formatType, ref latestData); if (latestData.m_date > 0) { datas.Add(latestData); } } return(1); }
/// <summary> /// 单元格点击事件 /// </summary> /// <param name="cell"></param> /// <param name="e"></param> public override void OnCellClick(GridCell cell, System.Windows.Forms.MouseEventArgs e) { base.OnCellClick(cell, e); if (e.Clicks == 1) { String code = cell.Row.GetCell("colP1").GetString(); Security security = new Security(); SecurityLatestData latestData = new SecurityLatestData(); if (SecurityService.GetSecurityByCode(code, ref security) > 0 && SecurityService.GetLatestData(code, ref latestData) > 0) { MainForm.instance.latestDiv.SecurityCode = code; MainForm.instance.latestDiv.Invalidate(); MainForm.instance.chartExtend.ChangeSecurity(code); } Console.WriteLine("1"); } }
/// <summary> /// 复制数据 /// </summary> /// <param name="data">数据</param> public void copy(SecurityLatestData data) { if (data == null) { return; } m_amount = data.m_amount; m_allBuyVol = data.m_allBuyVol; m_allSellVol = data.m_allSellVol; m_avgBuyPrice = data.m_avgBuyPrice; m_avgSellPrice = data.m_avgSellPrice; m_buyVolume1 = data.m_buyVolume1; m_buyVolume2 = data.m_buyVolume2; m_buyVolume3 = data.m_buyVolume3; m_buyVolume4 = data.m_buyVolume4; m_buyVolume5 = data.m_buyVolume5; m_buyPrice1 = data.m_buyPrice1; m_buyPrice2 = data.m_buyPrice2; m_buyPrice3 = data.m_buyPrice3; m_buyPrice4 = data.m_buyPrice4; m_buyPrice5 = data.m_buyPrice5; m_close = data.m_close; m_date = data.m_date; m_high = data.m_high; m_innerVol = data.m_innerVol; m_lastClose = data.m_lastClose; m_low = data.m_low; m_open = data.m_open; m_openInterest = data.m_openInterest; m_outerVol = data.m_outerVol; m_code = data.m_code; m_sellVolume1 = data.m_sellVolume1; m_sellVolume2 = data.m_sellVolume2; m_sellVolume3 = data.m_sellVolume3; m_sellVolume4 = data.m_sellVolume4; m_sellVolume5 = data.m_sellVolume5; m_sellPrice1 = data.m_sellPrice1; m_sellPrice2 = data.m_sellPrice2; m_sellPrice3 = data.m_sellPrice3; m_sellPrice4 = data.m_sellPrice4; m_sellPrice5 = data.m_sellPrice5; m_settlePrice = data.m_settlePrice; m_settlePrice = data.m_settlePrice; m_turnoverRate = data.m_turnoverRate; m_volume = data.m_volume; }
/// <summary> /// 比较是否相同 /// </summary> /// <param name="data">数据</param> /// <returns>是否相同</returns> public bool equal(SecurityLatestData data) { if (data == null) { return(false); } if (m_amount == data.m_amount && m_buyVolume1 == data.m_buyVolume1 && m_buyVolume2 == data.m_buyVolume2 && m_buyVolume3 == data.m_buyVolume3 && m_buyVolume4 == data.m_buyVolume4 && m_buyVolume5 == data.m_buyVolume5 && m_buyPrice1 == data.m_buyPrice1 && m_buyPrice2 == data.m_buyPrice2 && m_buyPrice3 == data.m_buyPrice3 && m_buyPrice4 == data.m_buyPrice4 && m_buyPrice5 == data.m_buyPrice5 && m_close == data.m_close && m_date == data.m_date && m_high == data.m_high && m_innerVol == data.m_innerVol && m_lastClose == data.m_lastClose && m_low == data.m_low && m_open == data.m_open && m_openInterest == data.m_openInterest && m_outerVol == data.m_outerVol && m_code == data.m_code && m_sellVolume1 == data.m_sellVolume1 && m_sellVolume2 == data.m_sellVolume2 && m_sellVolume3 == data.m_sellVolume3 && m_sellVolume4 == data.m_sellVolume4 && m_sellVolume5 == data.m_sellVolume5 && m_sellPrice1 == data.m_sellPrice1 && m_sellPrice2 == data.m_sellPrice2 && m_sellPrice3 == data.m_sellPrice3 && m_sellPrice4 == data.m_sellPrice4 && m_sellPrice5 == data.m_sellPrice5 && m_settlePrice == data.m_settlePrice && m_turnoverRate == data.m_turnoverRate && m_volume == data.m_volume) { return(true); } return(false); }
/// <summary> /// 键盘按下事件 /// </summary> /// <param name="e"></param> protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.KeyCode == Keys.Enter) { if (SelectedRows.Count > 0) { String code = SelectedRows[0].GetCell("colP1").GetString(); Security security = new Security(); SecurityLatestData latestData = new SecurityLatestData(); if (SecurityService.GetSecurityByCode(code, ref security) > 0 && SecurityService.GetLatestData(code, ref latestData) > 0) { MainForm.instance.latestDiv.SecurityCode = code; MainForm.instance.latestDiv.Invalidate(); MainForm.instance.chartExtend.ChangeSecurity(code); } } } }
/// <summary> /// 重绘方法 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = null; BufferedGraphics myBuffer = null; BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; myBuffer = currentContext.Allocate(pe.Graphics, DisplayRectangle); g = myBuffer.Graphics; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; if (!blackOrWhite) { g.Clear(Color.White); } SecurityLatestData latestData = new SecurityLatestData(); Security security = new Security(); SecurityService.GetSecurityByCode(SecurityCode, ref security); SecurityService.GetLatestData(SecurityCode, ref latestData); String securityName = security.m_name; int width = Width - 1; int height = Height - 1; if (width > 0 && height > 0) { Font font = new Font("微软雅黑", 12); Font lfont = new Font("微软雅黑", 12); Color wordColor = Color.FromArgb(100, 100, 100); int top = 32, step = 22; //画买卖盘 DrawText(g, "卖", wordColor, font, 1, 47); DrawText(g, "盘", wordColor, font, 1, 100); DrawText(g, "买", wordColor, font, 1, 157); DrawText(g, "盘", wordColor, font, 1, 210); String buySellStr = "5,4,3,2,1,1,2,3,4,5"; String[] buySellStrs = buySellStr.Split(','); int strsSize = buySellStrs.Length; for (int i = 0; i < strsSize; i++) { DrawText(g, buySellStrs[i], wordColor, font, 25, top); top += step; } font = new Font("微软雅黑", 12); top = 260; DrawText(g, "最新", wordColor, font, 1, top); DrawText(g, "升跌", wordColor, font, 1, top + 20); DrawText(g, "幅度", wordColor, font, 1, top + 40); DrawText(g, "总手", wordColor, font, 1, top + 60); DrawText(g, "涨停", wordColor, font, 1, top + 80); DrawText(g, "外盘", wordColor, font, 1, top + 100); DrawText(g, "开盘", wordColor, font, 110, top); DrawText(g, "最高", wordColor, font, 110, top + 20); DrawText(g, "最低", wordColor, font, 110, top + 40); DrawText(g, "换手", wordColor, font, 110, top + 60); DrawText(g, "跌停", wordColor, font, 110, top + 80); DrawText(g, "内盘", wordColor, font, 110, top + 100); font = new Font("微软雅黑", 14, FontStyle.Bold); //画股票代码 Color yellowColor = Color.FromArgb(255, 255, 80); if (!blackOrWhite) { yellowColor = Color.Black; } if (latestData.m_code != null && latestData.m_code.Length > 0) { double close = latestData.m_close, open = latestData.m_open, high = latestData.m_high, low = latestData.m_low, lastClose = latestData.m_lastClose; if (close == 0) { if (latestData.m_buyPrice1 > 0) { close = latestData.m_buyPrice1; open = latestData.m_buyPrice1; high = latestData.m_buyPrice1; low = latestData.m_buyPrice1; } else if (latestData.m_sellPrice1 > 0) { close = latestData.m_sellPrice1; open = latestData.m_sellPrice1; high = latestData.m_sellPrice1; low = latestData.m_sellPrice1; } } if (lastClose == 0) { lastClose = close; } List <double> plist = new List <double>(); List <double> vlist = new List <double>(); plist.Add(latestData.m_sellPrice5); plist.Add(latestData.m_sellPrice4); plist.Add(latestData.m_sellPrice3); plist.Add(latestData.m_sellPrice2); plist.Add(latestData.m_sellPrice1); vlist.Add(latestData.m_sellVolume5); vlist.Add(latestData.m_sellVolume4); vlist.Add(latestData.m_sellVolume3); vlist.Add(latestData.m_sellVolume2); vlist.Add(latestData.m_sellVolume1); plist.Add(latestData.m_buyPrice1); plist.Add(latestData.m_buyPrice2); plist.Add(latestData.m_buyPrice3); plist.Add(latestData.m_buyPrice4); plist.Add(latestData.m_buyPrice5); vlist.Add(latestData.m_buyVolume1); vlist.Add(latestData.m_buyVolume2); vlist.Add(latestData.m_buyVolume3); vlist.Add(latestData.m_buyVolume4); vlist.Add(latestData.m_buyVolume5); Color color = Color.Empty; double mx = Max(vlist); font = new Font("微软雅黑", 12); if (mx > 0) { //绘制买卖盘 int pLength = plist.Count; top = 32; for (int i = 0; i < pLength; i++) { color = GetPriceColor(plist[i], lastClose); DrawUnderLineNum(g, plist[i], digit, font, color, true, 60, top); DrawUnderLineNum(g, vlist[i], 0, font, yellowColor, false, 110, top); Brush sBrush = new SolidBrush(color); int dWidth = (int)(vlist[i] / mx * 60); if (dWidth < 2) { dWidth = 2; } g.FillRectangle(sBrush, new Rectangle(width - dWidth, top + step / 2 - 2, dWidth, 4)); sBrush.Dispose(); top += step; } } vlist.Clear(); plist.Clear(); top = 260; //成交 color = GetPriceColor(close, lastClose); DrawUnderLineNum(g, close, digit, font, color, true, 45, top); //升跌 double sub = 0; if (close == 0) { sub = latestData.m_buyPrice1 - lastClose; double rate = 100 * (latestData.m_buyPrice1 - lastClose) / lastClose; int pleft = DrawUnderLineNum(g, rate, 2, font, color, false, 45, top + 40); DrawText(g, "%", color, font, pleft + 47, top + 40); } else { sub = close - latestData.m_lastClose; double rate = 100 * (close - lastClose) / lastClose; int pleft = DrawUnderLineNum(g, rate, 2, font, color, false, 45, top + 40); DrawText(g, "%", color, font, pleft + 47, top + 40); } DrawUnderLineNum(g, sub, digit, font, color, false, 45, top + 20); double volume = latestData.m_volume / 100; String unit = ""; if (volume > 100000000) { volume /= 100000000; unit = "亿"; } else if (volume > 10000) { volume /= 10000; unit = "万"; } //总手 int cleft = DrawUnderLineNum(g, volume, unit.Length > 0 ? digit : 0, font, yellowColor, true, 45, top + 60); if (unit.Length > 0) { DrawText(g, unit, yellowColor, font, cleft + 47, top + 60); } //换手 double turnoverRate = latestData.m_turnoverRate; cleft = DrawUnderLineNum(g, turnoverRate, 2, font, yellowColor, true, 155, top + 60); if (turnoverRate > 0) { DrawText(g, "%", yellowColor, font, cleft + 157, top + 60); } //开盘 color = GetPriceColor(open, lastClose); DrawUnderLineNum(g, open, digit, font, color, true, 155, top); //最高 color = GetPriceColor(high, lastClose); DrawUnderLineNum(g, high, digit, font, color, true, 155, top + 20); //最低 color = GetPriceColor(low, lastClose); DrawUnderLineNum(g, low, digit, font, color, true, 155, top + 40); //涨停 double upPrice = lastClose * 1.1; if (securityName != null && securityName.Length > 0) { if (securityName.StartsWith("ST") || securityName.StartsWith("*ST")) { upPrice = lastClose * 1.05; } } if (blackOrWhite) { DrawUnderLineNum(g, upPrice, digit, font, Color.FromArgb(255, 80, 80), true, 45, top + 80); } else { DrawUnderLineNum(g, upPrice, digit, font, Color.FromArgb(0, 0, 0), true, 45, top + 80); } //跌停 double downPrice = lastClose * 0.9; if (securityName != null && securityName.Length > 0) { if (securityName.StartsWith("ST") || securityName.StartsWith("*ST")) { downPrice = lastClose * 0.95; } } if (blackOrWhite) { DrawUnderLineNum(g, downPrice, digit, font, Color.FromArgb(80, 255, 80), true, 155, top + 80); } else { DrawUnderLineNum(g, downPrice, digit, font, Color.FromArgb(0, 0, 0), true, 155, top + 80); } //外盘 double outerVol = latestData.m_outerVol; unit = ""; if (outerVol > 100000000) { outerVol /= 100000000; unit = "亿"; } else if (outerVol > 10000) { outerVol /= 10000; unit = "万"; } if (blackOrWhite) { cleft = DrawUnderLineNum(g, outerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(255, 80, 80), false, 45, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(255, 80, 80), font, cleft + 47, top + 100); } } else { cleft = DrawUnderLineNum(g, outerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(0, 0, 0), false, 45, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(0, 0, 0), font, cleft + 47, top + 100); } } unit = ""; double innerVol = latestData.m_innerVol; if (innerVol > 100000000) { innerVol /= 100000000; unit = "亿"; } else if (innerVol > 10000) { innerVol /= 10000; unit = "万"; } if (blackOrWhite) { //内盘 cleft = DrawUnderLineNum(g, innerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(80, 255, 80), true, 155, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(80, 255, 80), font, cleft + 157, top + 100); } } else { //内盘 cleft = DrawUnderLineNum(g, innerVol, unit.Length > 0 ? digit : 0, font, Color.FromArgb(0, 0, 0), true, 155, top + 100); if (unit.Length > 0) { DrawText(g, unit, Color.FromArgb(0, 0, 0), font, cleft + 157, top + 100); } } } font = new Font("微软雅黑", 14); if (blackOrWhite) { //股票代码 if (securityCode != null && securityCode.Length > 0) { DrawText(g, securityCode, Color.FromArgb(255, 255, 255), font, 2, 4); } //股票名称 if (securityName != null && securityName.Length > 0) { DrawText(g, securityName, Color.FromArgb(80, 255, 255), font, 110, 3); } } else { //股票代码 if (securityCode != null && securityCode.Length > 0) { DrawText(g, securityCode, Color.FromArgb(0, 0, 0), font, 2, 4); } //股票名称 if (securityName != null && securityName.Length > 0) { DrawText(g, securityName, Color.FromArgb(0, 0, 0), font, 110, 3); } } //画边框 Color frameColor = Color.FromArgb(150, 0, 0); if (!blackOrWhite) { frameColor = Color.Black; } Pen framePen = new Pen(frameColor); g.DrawLine(framePen, 0, 0, 0, height); g.DrawLine(framePen, 0, 30, width, 30); g.DrawLine(framePen, 24, 30, 24, top - 2); g.DrawLine(framePen, 0, 140, width, 140); g.DrawLine(framePen, 0, top - 2, width, top - 2); g.DrawLine(framePen, width, 0, width, height); g.DrawLine(framePen, 0, top + 130, width, top + 130); framePen.Dispose(); } myBuffer.Render(); myBuffer.Dispose(); g.Dispose(); }
/// <summary> /// 秒表事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void thisTimer_Tick(object sender, EventArgs e) { if (minuteMode) { if (minuteDatasPos < minuteDatas.Count) { minuteDatasPos++; UpdateDataToGraphMinute(minuteDatas, false); Invalidate(); } } else { SecurityLatestData newData = new SecurityLatestData(); SecurityService.GetLatestData(currentCode, ref newData); if (!newData.equal(lastData) && newData.m_volume > 0) { double close = newData.m_close; double dVolume = 0; if (lastData.m_code.Length > 0) { dVolume = newData.m_volume - lastData.m_volume; } SecurityData securityData = new SecurityData(); securityData.date = (double)((long)newData.m_date / (cycle * 60) * (cycle * 60)); if (cycle != 1440) { securityData.date += (cycle * 60); } securityData.close = close; if (DataSource.RowsCount > 0) { if (DataSource.GetXValue(DataSource.RowsCount - 1) == securityData.date) { if (securityData.close > DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH)) { securityData.high = close; } else { securityData.high = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_HIGH); } if (securityData.close < DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW)) { securityData.low = close; } else { securityData.low = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_LOW); } securityData.open = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_OPEN); double oldVolume = DataSource.Get2(DataSource.RowsCount - 1, COLUMN_VOLUME); oldVolume += dVolume; securityData.volume = oldVolume; } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } } else { securityData.high = close; securityData.low = close; securityData.open = close; securityData.volume = dVolume; } List <SecurityData> datas = new List <SecurityData>(); datas.Add(securityData); UpdateDataToGraph(datas, false); datas.Clear(); lastData = newData; } } }
/// <summary> /// 重绘方法 /// </summary> /// <param name="pe"></param> protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); Graphics g = null; BufferedGraphics myBuffer = null; BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; myBuffer = currentContext.Allocate(pe.Graphics, DisplayRectangle); g = myBuffer.Graphics; g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; int width = Width; int height = Height; if (width > 0 && height > 0) { SecurityLatestData ssLatestData = new SecurityLatestData(); SecurityLatestData szLatestData = new SecurityLatestData(); SecurityLatestData cyLatestData = new SecurityLatestData(); if (SecurityService.GetLatestData("000001.SH", ref ssLatestData) > 0 && SecurityService.GetLatestData("399001.SZ", ref szLatestData) > 0 && SecurityService.GetLatestData("399006.SZ", ref cyLatestData) > 0) { } Color titleColor = Color.FromArgb(255, 255, 80); Font font = new Font("微软雅黑", 12); Font indexFont = new Font("微软雅黑", 12); Color grayColor = Color.FromArgb(200, 200, 200); //上证指数 Color indexColor = GetPriceColor(ssLatestData.m_close, ssLatestData.m_lastClose); int left = 1; DrawText(g, "上证", titleColor, font, left, 3); left += 40; Pen grayPen = new Pen(grayColor); g.DrawLine(grayPen, left, 0, left, height); String amount = (ssLatestData.m_amount / 100000000).ToString("0.0") + "亿"; Size amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width / 3 - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; int length = DrawUnderLineNum(g, ssLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += length + (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, ssLatestData.m_close - ssLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); //深证指数 left = width / 3; g.DrawLine(grayPen, left, 0, left, height); indexColor = GetPriceColor(szLatestData.m_close, szLatestData.m_lastClose); DrawText(g, "深证", titleColor, font, left, 3); left += 40; g.DrawLine(grayPen, left, 0, left, height); amount = (szLatestData.m_amount / 100000000).ToString("0.0") + "亿"; amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width * 2 / 3 - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, szLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += length + (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, szLatestData.m_close - szLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); //创业指数 left = width * 2 / 3; g.DrawLine(grayPen, left, 0, left, height); indexColor = GetPriceColor(cyLatestData.m_close, cyLatestData.m_lastClose); DrawText(g, "创业", titleColor, font, left, 3); left += 40; g.DrawLine(grayPen, left, 0, left, height); amount = (cyLatestData.m_amount / 100000000).ToString("0.0") + "亿"; amountSize = g.MeasureString(amount, indexFont).ToSize(); DrawText(g, amount, titleColor, indexFont, width - amountSize.Width, 3); left += (width / 3 - 40 - amountSize.Width) / 4; length = DrawUnderLineNum(g, cyLatestData.m_close, 2, indexFont, indexColor, false, left, 3); left += (width / 3 - 40 - amountSize.Width) / 4 + length; length = DrawUnderLineNum(g, cyLatestData.m_close - cyLatestData.m_lastClose, 2, indexFont, indexColor, false, left, 3); g.DrawRectangle(grayPen, new Rectangle(0, 0, width - 1, height - 1)); grayPen.Dispose(); } myBuffer.Render(); myBuffer.Dispose(); g.Dispose(); }
/// <summary> /// 根据字符串获取新浪的最新数据 /// </summary> /// <param name="str">数据字符串</param> /// <param name="formatType">格式</param> /// <param name="data">最新数据</param> /// <returns>状态</returns> public static int GetLatestDataBySinaStr(String str, int formatType, ref SecurityLatestData data) { //分析数据 String date = ""; String[] strs2 = str.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); int strLen2 = strs2.Length; bool szIndex = false; for (int j = 0; j < strLen2; j++) { String str2 = strs2[j]; switch (j) { case 0: data.m_code = FCStrEx.convertSinaCodeToDBCode(str2); if (data.m_code.StartsWith("399")) { szIndex = true; } break; case 1: { data.m_open = Convert.ToDouble(str2); break; } case 2: { data.m_lastClose = Convert.ToDouble(str2); break; } case 3: { data.m_close = Convert.ToDouble(str2); break; } case 4: { data.m_high = Convert.ToDouble(str2); break; } case 5: { data.m_low = Convert.ToDouble(str2); break; } case 8: { data.m_volume = Convert.ToDouble(str2); if (szIndex) { data.m_volume /= 100; } break; } case 9: { data.m_amount = Convert.ToDouble(str2); break; } case 10: { if (formatType == 0) { data.m_buyVolume1 = (int)Convert.ToDouble(str2); } break; } case 11: { if (formatType == 0) { data.m_buyPrice1 = Convert.ToDouble(str2); } break; } case 12: { if (formatType == 0) { data.m_buyVolume2 = (int)Convert.ToDouble(str2); } break; } case 13: { if (formatType == 0) { data.m_buyPrice2 = Convert.ToDouble(str2); } break; } case 14: { if (formatType == 0) { data.m_buyVolume3 = (int)Convert.ToDouble(str2); } break; } case 15: { if (formatType == 0) { data.m_buyPrice3 = Convert.ToDouble(str2); } break; } case 16: { if (formatType == 0) { data.m_buyVolume4 = (int)Convert.ToDouble(str2); } break; } case 17: { if (formatType == 0) { data.m_buyPrice4 = Convert.ToDouble(str2); } break; } case 18: { if (formatType == 0) { data.m_buyVolume5 = (int)Convert.ToDouble(str2); } break; } case 19: { if (formatType == 0) { data.m_buyPrice5 = Convert.ToDouble(str2); } break; } case 20: { if (formatType == 0) { data.m_sellVolume1 = (int)Convert.ToDouble(str2); } break; } case 21: { if (formatType == 0) { data.m_sellPrice1 = Convert.ToDouble(str2); } break; } case 22: { if (formatType == 0) { data.m_sellVolume2 = (int)Convert.ToDouble(str2); } break; } case 23: { if (formatType == 0) { data.m_sellPrice2 = Convert.ToDouble(str2); } break; } case 24: { if (formatType == 0) { data.m_sellVolume3 = (int)Convert.ToDouble(str2); } break; } case 25: { if (formatType == 0) { data.m_sellPrice3 = Convert.ToDouble(str2); } break; } case 26: { if (formatType == 0) { data.m_sellVolume4 = (int)Convert.ToDouble(str2); } break; } case 27: { if (formatType == 0) { data.m_sellPrice4 = Convert.ToDouble(str2); } break; } case 28: { if (formatType == 0) { data.m_sellVolume5 = (int)Convert.ToDouble(str2); } break; } case 29: { if (formatType == 0) { data.m_sellPrice5 = Convert.ToDouble(str2); } break; } case 30: date = str2; break; case 31: date += " " + str2; break; } } //获取时间 if (date != null && date.Length > 0) { DateTime dateTime = Convert.ToDateTime(date); data.m_date = (dateTime - new DateTime(1970, 1, 1)).TotalSeconds; //data.m_date = FCTran.GetDateNum(dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, 0); } //价格修正 if (data.m_close != 0) { if (data.m_open == 0) { data.m_open = data.m_close; } if (data.m_high == 0) { data.m_high = data.m_close; } if (data.m_low == 0) { data.m_low = data.m_close; } } return(0); }
/// <summary> /// 开始工作 /// </summary> private static void RunWork() { while (true) { if (codesTextCache != null && codesTextCache.Length > 0) { if (codesTextCache.EndsWith(",")) { codesTextCache.Remove(codesTextCache.Length - 1); } String[] strCodes = codesTextCache.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries); int codesSize = strCodes.Length; String latestCodes = ""; for (int i = 0; i < codesSize; i++) { latestCodes += strCodes[i]; if (i == codesSize - 1 || (i > 0 && i % 50 == 0)) { String latestDatasResult = GetSinaLatestDatasStrByCodes(latestCodes); if (latestDatasResult != null && latestDatasResult.Length > 0) { List <SecurityLatestData> latestDatas = new List <SecurityLatestData>(); GetLatestDatasBySinaStr(latestDatasResult, 0, latestDatas); String[] subStrs = latestDatasResult.Split(new String[] { ";\n" }, StringSplitOptions.RemoveEmptyEntries); int latestDatasSize = latestDatas.Count; for (int j = 0; j < latestDatasSize; j++) { SecurityLatestData latestData = latestDatas[j]; if (latestData.m_close == 0) { latestData.m_close = latestData.m_buyPrice1; } if (latestData.m_close == 0) { latestData.m_close = latestData.m_sellPrice1; } lock (latestDatasCache) { bool newData = false; if (!latestDatasCache.ContainsKey(latestData.m_code)) { latestDatasCache[latestData.m_code] = latestData; newData = true; } else { if (!latestDatasCache[latestData.m_code].equal(latestData)) { latestDatasCache[latestData.m_code].copy(latestData); newData = true; } } if (newData) { if (m_listener != null) { m_listener.LatestDataCallBack(latestData.m_code); } } if (latestData.m_code == "000001.SH") { shTradeTime = latestData.m_date; } } } latestDatas.Clear(); } latestCodes = ""; } else { latestCodes += ","; } } } Thread.Sleep(1); } }
/// <summary> /// 秒表方法 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_Tick(object sender, EventArgs e) { List <String> refreshCodes = new List <string>(); lock (newDataCodes) { if (newDataCodes.Count > 0) { foreach (String key in newDataCodes.Keys) { refreshCodes.Add(key); } newDataCodes.Clear(); } } int refreshCodesSize = refreshCodes.Count; if (refreshCodes.Count > 0) { if (updateCells.Count > 0) { if (updateCells.Count > 10) { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } else { for (int i = 0; i < updateCells.Count; i++) { GridCell updateCell = updateCells[i]; updateCell.Style.BackColor = Color.Empty; } } updateCells.Clear(); } for (int i = 0; i < refreshCodesSize; i++) { GridRow row = null; String code = refreshCodes[i]; if (rowsMap.ContainsKey(code)) { row = rowsMap[code]; SecurityLatestData lastestData = new SecurityLatestData(); SecurityService.GetLatestData(code, ref lastestData); double lastClose = lastestData.m_lastClose; double diff = 0, diffRange = 0; diff = lastestData.m_close - lastestData.m_lastClose; if (lastestData.m_lastClose != 0) { diffRange = diff / lastestData.m_lastClose; } GridCell cell3 = row.GetCell("colP3"); if (lastestData.m_close != cell3.GetDouble()) { cell3.SetDouble(lastestData.m_close); cell3.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell3); } GridCell cell4 = row.GetCell("colP4"); if (diff != cell4.GetDouble()) { cell4.SetDouble(Convert.ToDouble(LbCommon.GetValueByDigit(diff, 2, true))); cell4.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell4); } GridCell cell5 = row.GetCell("colP5"); if (diffRange != cell5.GetDouble()) { cell5.SetDouble(diffRange); cell5.Style.TextColor = GetPriceColor(lastestData.m_close, lastClose); updateCells.Add(cell5); } GridCell cell6 = row.GetCell("colP6"); if (lastestData.m_high != cell6.GetDouble()) { cell6.SetDouble(lastestData.m_high); cell6.Style.TextColor = GetPriceColor(lastestData.m_high, lastClose); updateCells.Add(cell6); } GridCell cell7 = row.GetCell("colP7"); if (lastestData.m_low != cell7.GetDouble()) { cell7.SetDouble(lastestData.m_low); cell7.Style.TextColor = GetPriceColor(lastestData.m_low, lastClose); updateCells.Add(cell7); } GridCell cell8 = row.GetCell("colP8"); if (lastestData.m_open != cell8.GetDouble()) { cell8.SetDouble(lastestData.m_open); cell8.Style.TextColor = GetPriceColor(lastestData.m_open, lastClose); updateCells.Add(cell8); } GridCell cell9 = row.GetCell("colP9"); if (lastestData.m_volume != cell9.GetDouble()) { cell9.SetDouble(lastestData.m_volume); updateCells.Add(cell9); } GridCell cell10 = row.GetCell("colP10"); if (lastestData.m_amount != cell10.GetDouble()) { cell10.SetDouble(lastestData.m_amount); updateCells.Add(cell10); } GridCell cell11 = row.GetCell("colP11"); if (lastestData.m_buyPrice1 != cell11.GetDouble()) { cell11.SetDouble(lastestData.m_buyPrice1); cell11.Style.TextColor = GetPriceColor(lastestData.m_buyPrice1, lastClose); updateCells.Add(cell11); } GridCell cell12 = row.GetCell("colP12"); if (lastestData.m_buyVolume1 != cell12.GetDouble()) { cell12.SetDouble(lastestData.m_buyVolume1); updateCells.Add(cell12); } GridCell cell13 = row.GetCell("colP13"); if (lastestData.m_sellPrice1 != cell13.GetDouble()) { cell13.SetDouble(lastestData.m_sellPrice1); cell13.Style.TextColor = GetPriceColor(lastestData.m_sellPrice1, lastClose); updateCells.Add(cell13); } GridCell cell14 = row.GetCell("colP14"); if (lastestData.m_sellVolume1 != cell14.GetDouble()) { cell14.SetDouble(lastestData.m_sellVolume1); updateCells.Add(cell14); } } } if (updateCells.Count > 0) { for (int i = 0; i < updateCells.Count; i++) { updateCells[i].Style.BackColor = Color.FromArgb(50, 255, 255, 255); } UpdateGrid(); Invalidate(); } } }