/// <summary> /// 发送数据并且绘制正弦曲线 /// </summary> private async void DrawSin() { try { int Zoom = 15; //放大倍数 Point center = new Point(0, pbShow.Height / 2); //原点 float x1 = (float)(0 * Math.PI * Zoom / 180 + center.X); float y1 = (float)(Math.Sin(0 * Math.PI / 180) * 11 * Zoom + center.Y); grap.DrawLine(Pens.Black, 0, center.Y, pbShow.Width, center.Y); //x坐标轴 grap.DrawLine(Pens.Black, center.X, 0, center.X, pbShow.Height); //y坐标轴 LDFastMessageAdapter fastMsg = new LDFastMessageAdapter("pubL.16.5", 0); StockLevelRealTimeData stockFiled = new StockLevelRealTimeData(); for (double i = 1; i < 360 * 4; i++) //角 { if (isPause) { waitHandle.WaitOne(); } float x2 = (float)(i * Math.PI * Zoom / 180 + center.X); float y2 = (float)Math.Sin(i * Math.PI / 180) * (-1) * 11 * Zoom + center.Y; grap.DrawLine(Pens.Red, x1, y1, x2, y2); grap.Save(); this.pbShow.Invoke(new Action(() => { pbShow.Image = bmp; })); #region 针对每个券码发送一个生成并发送一个数据包 int rows = LoadStockInfo.excelData.GetLength(0); for (int j = 1; j < rows; j++) { //券码 string stockNo = LoadStockInfo.excelData[j, 1].ToString(); char[] stockNos = stockNo.ToCharArray(); if (stockNos.Length < 5 || stockNos.Length > 6) { continue; } if (stockNos.Length < 6) { var tempStocks = stockNos.ToList(); tempStocks.Add('\0'); stockNos = tempStocks.ToArray(); } //涨停价 double upPrice = Convert.ToDouble(LoadStockInfo.excelData[j, 4]); //跌停价 double downPrice = Convert.ToDouble(LoadStockInfo.excelData[j, 5]); //初始价格 double price0 = (upPrice + downPrice) / 2.00; //递增/递减价格 double price1 = (upPrice - price0) * Math.Sin(i * Math.PI / 180) + price0; double price2 = (upPrice - price0) * Math.Sin((i + 1) * Math.PI / 180) + price0; double price3 = (upPrice - price0) * Math.Sin((i + 2) * Math.PI / 180) + price0; double price4 = (upPrice - price0) * Math.Sin((i + 3) * Math.PI / 180) + price0; double price5 = (upPrice - price0) * Math.Sin((i + 4) * Math.PI / 180) + price0; fastMsg.SetInt32(LDBizTag.LDBIZ_EXCH_NO_INT, 1); fastMsg.SetString(LDBizTag.LDBIZ_STOCK_CODE_STR, stockNo); //开始组装数据包 int tmpValue = 1000; int tmpValue0 = 100; stockFiled.PriceUnit = tmpValue; stockFiled.UpPrice = (int)(upPrice * tmpValue); stockFiled.DownPrice = (int)(downPrice * tmpValue); stockFiled.FiveDayVol = 0; stockFiled.OpenPrice = (int)(price0 * tmpValue); stockFiled.PrevClose = (int)(price0 * tmpValue);; stockFiled.BuyPrice1 = (int)(price1 * tmpValue); stockFiled.BuyPrice2 = (int)(price2 * tmpValue); stockFiled.BuyPrice3 = (int)(price3 * tmpValue); stockFiled.BuyPrice4 = (int)(price4 * tmpValue); stockFiled.BuyPrice5 = (int)(price5 * tmpValue); stockFiled.BuyCount1 = (uint)(tmpValue0 * i); stockFiled.BuyCount2 = (uint)(tmpValue0 * i * 2); stockFiled.BuyCount3 = (uint)(tmpValue0 * i * 3); stockFiled.BuyCount4 = (uint)(tmpValue0 * i * 4); stockFiled.BuyCount5 = (uint)(tmpValue0 * i * 5); stockFiled.SellPrice1 = (int)(price1 * tmpValue); stockFiled.SellPrice2 = (int)(price2 * tmpValue); stockFiled.SellPrice3 = (int)(price3 * tmpValue); stockFiled.SellPrice4 = (int)(price4 * tmpValue); stockFiled.SellPrice5 = (int)(price5 * tmpValue); stockFiled.SellCount1 = (uint)(tmpValue0 * i); stockFiled.SellCount2 = (uint)(tmpValue0 * i * 2); stockFiled.SellCount3 = (uint)(tmpValue0 * i * 3); stockFiled.SellCount4 = (uint)(tmpValue0 * i * 4); stockFiled.SellCount5 = (uint)(tmpValue0 * i * 5); stockFiled.AvgPrice = (int)(price1 * tmpValue) * (uint)(tmpValue0 * i) + (int)(price2 * tmpValue) * (uint)(tmpValue0 * i * 2) + (int)(price3 * tmpValue) * (uint)(tmpValue0 * i * 3) + (int)(price4 * tmpValue) * (uint)(tmpValue0 * i * 4) + (int)(price5 * tmpValue) * (uint)(tmpValue0 * i * 5); stockFiled.MaxPrice = (int)(price5 * tmpValue); stockFiled.MinPrice = (int)(price1 * tmpValue); stockFiled.NewPrice = (int)(price3 * tmpValue); stockFiled.HandNum = 100; stockFiled.CodeType = 4361; stockFiled.Decimal = 3; stockFiled.StockCode = stockNos; stockFiled.Total = (uint)(tmpValue0 * i * 10); //将数据包转化成非托管类型 IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(stockFiled)); Marshal.StructureToPtr(stockFiled, ptr, false); fastMsg.SetRawData(LDBizTag.LDBIZ_QUOT_PRICE_INFO_STR, ptr, Marshal.SizeOf(stockFiled)); //制造阻塞,确保数据包发送之后才进行内存回收 int result = await SendPackage(fastMsg); fastMsg.FreeMsg(); Marshal.FreeHGlobal(ptr); Thread.Sleep(TimeSpan.FromMilliseconds(delay)); } #endregion x1 = x2; y1 = y2; } this.btnStart.Invoke(new Action(() => { btnStart.Enabled = true; })); } catch (Exception error) { Log.WriteLog(error.Message); MessageBox.Show(error.StackTrace + "\r\n" + error.Message); } }
/// <summary> /// 发送数据并绘制正弦曲线 /// </summary> private async void DrawSin() { try { int Zoom = 15; //放大倍数 Point center = new Point(0, 174); //原点 float x1 = (float)(0 * Math.PI * Zoom / 180 + center.X); float y1 = (float)(Math.Sin(0 * Math.PI / 180) * 11 * Zoom + center.Y); this.Dispatcher.Invoke(() => { //定义绘制的第一个点 pf.StartPoint = center; //加入到PathFigure对象中,true表示描绘线段 pf.Segments.Add(new LineSegment(center, true)); //组合绘制的线段,只要操作一次 pg.Figures.Add(pf); //作为Path对象的数据 pa.Data = pg; //加入Canvas在屏幕显示 this.cvsDraw.Children.Add(pa); }); for (int i = 1; i < 360 * 99; i++) //角 { if (isPause) //是否暂停 { waitHandle.WaitOne(); } if (token.IsCancellationRequested)//是否重置(取消当前线程) { return; } float x2 = (float)(i * Math.PI * Zoom / 180 + center.X); float y2 = (float)(Math.Sin(i * Math.PI / 180) * (-1) * 11 * Zoom + center.Y); this.Dispatcher.Invoke(() => { pf.Segments.Add(new LineSegment(new Point(x2, y2), true)); }); #region 针对每个券码发送一个生成并发送一个数据包还要将行情进行落库 int rows = LoadStockInfo.excelData.Rows.Count;//券码总条数 for (int j = 0; j < (rows < stockCountLimit ? rows : stockCountLimit); j++) { LDFastMessageAdapter fastMsg = new LDFastMessageAdapter("pubL.16.5", 0); //行情包 LDFastMessageAdapter saveMsg = new LDFastMessageAdapter("pubL.16.2", 0); //将行情落户的请求包 unsafe { if (fastMsg.Record == null || saveMsg.Record == null) { this.Dispatcher.Invoke(() => { btnStart.IsEnabled = true; MessageBox.Show("初始化数据包失败!\r\n请检查服务器是否正常", "异常", MessageBoxButton.OK, MessageBoxImage.Error); }); return; } } //券码 string stockNo = LoadStockInfo.excelData.Rows[j]["stock_code"].ToString(); if (forbidStocks.Contains(stockNo)) { continue; } char[] stockNos = stockNo.ToCharArray(); //市场 int exchNo = Convert.ToInt32(LoadStockInfo.excelData.Rows[j]["exch_no"]); //证券类型 int stockType = Convert.ToInt32(LoadStockInfo.excelData.Rows[j]["stock_type"]); if (stockNos.Length < 5 || stockNos.Length > 6) { continue; } //对5位数的券码进行数据处理 if (stockNos.Length < 6) { var tempStocks = stockNos.ToList(); tempStocks.Add('\0'); stockNos = tempStocks.ToArray(); } //昨收价格作为初始价格 double preClosePrice = Convert.ToDouble(LoadStockInfo.excelData.Rows[j]["pre_close_price"]); //涨停价 double upPrice = preClosePrice * (1 + limitPercent / 100.00); //跌停价 double downPrice = preClosePrice * (1 - limitPercent / 100.00); //精度 int precision = 0; //递增/递减价格 double price1 = CaculatePrecision((upPrice - preClosePrice) * Math.Sin(i * Math.PI / 180) + preClosePrice, exchNo, stockType, out precision); double price2 = CaculatePrecision((upPrice - preClosePrice) * Math.Sin((i + 1) * Math.PI / 180) + preClosePrice, exchNo, stockType, out precision); double price3 = CaculatePrecision((upPrice - preClosePrice) * Math.Sin((i + 2) * Math.PI / 180) + preClosePrice, exchNo, stockType, out precision); double price4 = CaculatePrecision((upPrice - preClosePrice) * Math.Sin((i + 3) * Math.PI / 180) + preClosePrice, exchNo, stockType, out precision); double price5 = CaculatePrecision((upPrice - preClosePrice) * Math.Sin((i + 4) * Math.PI / 180) + preClosePrice, exchNo, stockType, out precision); //最大最小价格Model MaxMinPriceModel MMP = null; //最大价格 double maxPrice = 0; //最小价格 double minPrice = 0; if (MaxMinPriceCollection.TryGetValue(stockNo, out MMP)) { if (price1 >= MMP.maxPrice) { maxPrice = price1; minPrice = MMP.minPrice; MaxMinPriceCollection[stockNo].maxPrice = price1; } else { maxPrice = MMP.maxPrice; //判断最小价格 if (price1 <= MMP.minPrice) { minPrice = price1; MaxMinPriceCollection[stockNo].minPrice = price1; } else { minPrice = MMP.minPrice; } } } else { MaxMinPriceCollection.Add(stockNo, new MaxMinPriceModel() { maxPrice = price1, minPrice = price1 }); } InitFastMsg.InitMsg("pubL.16.5", ref fastMsg); InitFastMsg.InitMsg("pubL.16.2", ref saveMsg); fastMsg.SetInt32(LDBizTag.LDBIZ_EXCH_NO_INT, exchNo); fastMsg.SetString(LDBizTag.LDBIZ_STOCK_CODE_STR, stockNo); //开始组装数据包 /* * 价格信息以正弦曲线的方式进行变动 price = (upPrice - price0) * Math.Sin((i + 4) * Math.PI / 180) + price0; * 数量信息呈线性增长 y = kx */ //数据基数 const int tmpValue = 1000; const int tmpValue0 = 100; IntPtr ptr; if (errorStocks.Contains(stockNo)) { #region 创建异常的行情包 WrongStockLevelRealTimeData wrongStockFiled = new WrongStockLevelRealTimeData(); wrongStockFiled.WrongData = tmpValue * 111; wrongStockFiled.PriceUnit = tmpValue; wrongStockFiled.UpPrice = (int)(downPrice * tmpValue); wrongStockFiled.DownPrice = (int)(upPrice * tmpValue); wrongStockFiled.FiveDayVol = 0; wrongStockFiled.OpenPrice = (int)(preClosePrice * tmpValue); wrongStockFiled.PrevClose = (int)(preClosePrice * tmpValue);; wrongStockFiled.BuyPrice1 = (int)(price1 * tmpValue); //买一价 wrongStockFiled.BuyPrice2 = (int)(price2 * tmpValue); //买二价 wrongStockFiled.BuyPrice3 = (int)(price3 * tmpValue); //买三价 wrongStockFiled.BuyPrice4 = (int)(price4 * tmpValue); //买四价 wrongStockFiled.BuyPrice5 = (int)(price5 * tmpValue); //买五价 wrongStockFiled.BuyCount1 = (uint)(tmpValue0 * i); wrongStockFiled.BuyCount2 = (uint)(tmpValue0 * i * 2); wrongStockFiled.BuyCount3 = (uint)(tmpValue0 * i * 3); wrongStockFiled.BuyCount4 = (uint)(tmpValue0 * i * 4); wrongStockFiled.BuyCount5 = (uint)(tmpValue0 * i * 5); wrongStockFiled.SellPrice1 = (int)(price1 * tmpValue); //卖一价 wrongStockFiled.SellPrice2 = (int)(price2 * tmpValue); //卖二价 wrongStockFiled.SellPrice3 = (int)(price3 * tmpValue); //卖三价 wrongStockFiled.SellPrice4 = (int)(price4 * tmpValue); //卖四价 wrongStockFiled.SellPrice5 = (int)(price5 * tmpValue); //卖五价 wrongStockFiled.SellCount1 = (uint)(tmpValue0 * i); wrongStockFiled.SellCount2 = (uint)(tmpValue0 * i * 2); wrongStockFiled.SellCount3 = (uint)(tmpValue0 * i * 3); wrongStockFiled.SellCount4 = (uint)(tmpValue0 * i * 4); wrongStockFiled.SellCount5 = (uint)(tmpValue0 * i * 5); //成交额 wrongStockFiled.AvgPrice = (int)(price1 * tmpValue) * (uint)(tmpValue0 * i) + (int)(price2 * tmpValue) * (uint)(tmpValue0 * i * 2) + (int)(price3 * tmpValue) * (uint)(tmpValue0 * i * 3) + (int)(price4 * tmpValue) * (uint)(tmpValue0 * i * 4) + (int)(price5 * tmpValue) * (uint)(tmpValue0 * i * 5); wrongStockFiled.MaxPrice = (int)(price5 * tmpValue); wrongStockFiled.MinPrice = (int)(price1 * tmpValue); wrongStockFiled.NewPrice = (int)(price3 * tmpValue); wrongStockFiled.HandNum = 100; wrongStockFiled.CodeType = 4361; wrongStockFiled.Decimal = 3; wrongStockFiled.StockCode = stockNos; //成交量 wrongStockFiled.Total = (uint)(tmpValue0 * i * 15); #endregion //将数据包转化成非托管类型 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(wrongStockFiled)); Marshal.StructureToPtr(wrongStockFiled, ptr, false); fastMsg.SetRawData(LDBizTag.LDBIZ_QUOT_PRICE_INFO_STR, ptr, Marshal.SizeOf(wrongStockFiled)); } else { #region 创建正常的数据包 StockLevelRealTimeData stockFiled = new StockLevelRealTimeData(); stockFiled.PriceUnit = tmpValue; stockFiled.Decimal = precision; stockFiled.UpPrice = (int)(upPrice * tmpValue); stockFiled.DownPrice = (int)(downPrice * tmpValue); stockFiled.FiveDayVol = 0; stockFiled.OpenPrice = (int)(preClosePrice * tmpValue); stockFiled.PrevClose = (int)(preClosePrice * tmpValue);; stockFiled.BuyPrice1 = (int)(price1 * tmpValue); //买一价 stockFiled.BuyPrice2 = (int)(price2 * tmpValue); //买二价 stockFiled.BuyPrice3 = (int)(price3 * tmpValue); //买三价 stockFiled.BuyPrice4 = (int)(price4 * tmpValue); //买四价 stockFiled.BuyPrice5 = (int)(price5 * tmpValue); //买五价 stockFiled.BuyCount1 = (uint)(tmpValue0 * i); stockFiled.BuyCount2 = (uint)(tmpValue0 * i * 2); stockFiled.BuyCount3 = (uint)(tmpValue0 * i * 3); stockFiled.BuyCount4 = (uint)(tmpValue0 * i * 4); stockFiled.BuyCount5 = (uint)(tmpValue0 * i * 5); stockFiled.SellPrice1 = (int)(price1 * tmpValue); //卖一价 stockFiled.SellPrice2 = (int)(price2 * tmpValue); //卖二价 stockFiled.SellPrice3 = (int)(price3 * tmpValue); //卖三价 stockFiled.SellPrice4 = (int)(price4 * tmpValue); //卖四价 stockFiled.SellPrice5 = (int)(price5 * tmpValue); //卖五价 stockFiled.SellCount1 = (uint)(tmpValue0 * i); stockFiled.SellCount2 = (uint)(tmpValue0 * i * 2); stockFiled.SellCount3 = (uint)(tmpValue0 * i * 3); stockFiled.SellCount4 = (uint)(tmpValue0 * i * 4); stockFiled.SellCount5 = (uint)(tmpValue0 * i * 5); //成交额 stockFiled.AvgPrice = (int)(price1 * tmpValue) * (uint)(tmpValue0 * i) + (int)(price2 * tmpValue) * (uint)(tmpValue0 * i * 2) + (int)(price3 * tmpValue) * (uint)(tmpValue0 * i * 3) + (int)(price4 * tmpValue) * (uint)(tmpValue0 * i * 4) + (int)(price5 * tmpValue) * (uint)(tmpValue0 * i * 5); stockFiled.MaxPrice = (int)(maxPrice * tmpValue); stockFiled.MinPrice = (int)(minPrice * tmpValue); stockFiled.NewPrice = (int)(price1 * tmpValue); stockFiled.HandNum = 100; stockFiled.CodeType = 4361; stockFiled.StockCode = stockNos; //成交量 stockFiled.Total = (uint)(tmpValue0 * i * 15); #endregion #region 构建行情落库请求包 saveMsg.SetInt32(LDBizTag.LDBIZ_EXCH_NO_INT, exchNo); saveMsg.SetString(LDBizTag.LDBIZ_STOCK_CODE_STR, stockNo); saveMsg.SetDouble(LDBizTag.LDBIZ_LAST_PRICE_FLOAT, price3); saveMsg.SetDouble(LDBizTag.LDBIZ_PRE_CLOSE_PRICE_FLOAT, preClosePrice); saveMsg.SetString(LDBizTag.LDBIZ_STOP_STATUS_STR, "0"); saveMsg.SetDouble(LDBizTag.LDBIZ_UP_LIMIT_PRICE_FLOAT, 0); saveMsg.SetDouble(LDBizTag.LDBIZ_DOWN_LIMIT_PRICE_FLOAT, 0); saveMsg.SetDouble(LDBizTag.LDBIZ_TODAY_OPEN_PRICE_FLOAT, preClosePrice); saveMsg.SetDouble(LDBizTag.LDBIZ_TODAY_CLOSE_PRICE_FLOAT, price3); saveMsg.SetDouble(LDBizTag.LDBIZ_TODAY_MAX_PRICE_FLOAT, upPrice); saveMsg.SetDouble(LDBizTag.LDBIZ_TODAY_MIN_PRICE_FLOAT, downPrice); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_PRICE_1_FLOAT, price1); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_PRICE_2_FLOAT, price2); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_PRICE_3_FLOAT, price3); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_PRICE_4_FLOAT, price4); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_PRICE_5_FLOAT, price5); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_QTY_1_FLOAT, tmpValue0 * i); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_QTY_2_FLOAT, tmpValue0 * i * 2); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_QTY_3_FLOAT, tmpValue0 * i * 3); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_QTY_4_FLOAT, tmpValue0 * i * 4); saveMsg.SetDouble(LDBizTag.LDBIZ_BUY_QTY_5_FLOAT, tmpValue0 * i * 5); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_PRICE_1_FLOAT, price1); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_PRICE_2_FLOAT, price2); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_PRICE_3_FLOAT, price3); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_PRICE_4_FLOAT, price4); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_PRICE_5_FLOAT, price5); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_QTY_1_FLOAT, tmpValue0 * i); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_QTY_2_FLOAT, tmpValue0 * i * 2); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_QTY_3_FLOAT, tmpValue0 * i * 3); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_QTY_4_FLOAT, tmpValue0 * i * 4); saveMsg.SetDouble(LDBizTag.LDBIZ_SELL_QTY_5_FLOAT, tmpValue0 * i * 5); saveMsg.SetDouble(LDBizTag.LDBIZ_STRIKE_QTY_FLOAT, tmpValue0 * i * 15); saveMsg.SetDouble(LDBizTag.LDBIZ_STRIKE_AMT_FLOAT, price1 * tmpValue0 * i + price2 * tmpValue0 * i * 2 + price3 * tmpValue0 * i * 3 + price4 * tmpValue0 * i * 4 + price5 * tmpValue0 * i * 5); #endregion //将数据包转化成非托管类型 ptr = Marshal.AllocHGlobal(Marshal.SizeOf(stockFiled)); Marshal.StructureToPtr(stockFiled, ptr, false); fastMsg.SetRawData(LDBizTag.LDBIZ_QUOT_PRICE_INFO_STR, ptr, Marshal.SizeOf(stockFiled)); } //制造阻塞,确保数据包发送之后才进行内存回收 int result = await SendPubTopicsPackage(fastMsg, "quote.realtime"); //发送行情落库请求包 await SendRequsetPackage(saveMsg, "pubL.16.2"); Marshal.FreeHGlobal(ptr);//释放非托管内存 Thread.Sleep(TimeSpan.FromMilliseconds(delay)); } #endregion } this.Dispatcher.Invoke(() => { btnStart.IsEnabled = true; }); } catch (Exception error) { this.Dispatcher.Invoke(() => { btnStart.IsEnabled = true; }); MessageBox.Show(error.Message + "\r\n" + error.StackTrace); } }