private void WriteDayFile(string fileName, HaiLiDrvDemo.RCV_HISTORY_STRUCTEx history) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fileName, true)) { //listBox1.Items.Add("交易时间:" + history.m_time.ToString() + ",最高价:" + Convert.ToString(history.m_fHigh) + ",最低价:" + Convert.ToString(history.m_fLow)); // public int m_time; //public Single m_fOpen; //开盘 //public Single m_fHigh; //最高 //public Single m_fLow; //最低 //public Single m_fClose; //收盘 //public Single m_fVolume; //量 //public Single m_fAmount; //额 //public UInt16 m_wAdvance; //涨数,仅大盘有效 //public UInt16 m_wDecline; //跌数,仅大盘有效 string strSerializeJSON = JsonConvert.SerializeObject(history); sw.WriteLine(strSerializeJSON);// 直接追加文件末尾,换行 } }
protected override void WndProc(ref System.Windows.Forms.Message m) { if (m.Msg == Demo.MY_MSG_BEGIN) { MessageBox.Show("Demo开始."); } else if (m.Msg == Demo.MY_MSG_END) { MessageBox.Show("Demo结束."); } else if (m.Msg == HaiLiDrvDemo.StockDrv.RCV_MSG_STKDATA) { //MessageBox.Show("收到实际驱动消息!"); switch (m.WParam.ToInt32()) { case HaiLiDrvDemo.StockDrv.RCV_REPORT: { HaiLiDrvDemo.RCV_DATA pHeader = (HaiLiDrvDemo.RCV_DATA)Marshal.PtrToStructure(m.LParam, typeof(HaiLiDrvDemo.RCV_DATA)); listBox1.Items.Add("收到实时行情消息!" + pHeader.m_nPacketNum.ToString()); for (int i = 0; i < pHeader.m_nPacketNum; i++) { //report就是一条实时行情数据 HaiLiDrvDemo.RCV_REPORT_STRUCTExV3 report = (HaiLiDrvDemo.RCV_REPORT_STRUCTExV3)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 158 * i), typeof(HaiLiDrvDemo.RCV_REPORT_STRUCTExV3)); //将实时数据存入自己的集合中,或者做别的数量 //System.Text.StringBuilder sb = new System.Text.StringBuilder(); //sb.Append(report.m_szName); //string codename = sb.ToString(); //注意:避免影响界面的刷新,只显示前20条记录 if (i <= 20) { listBox1.Items.Add("股票代码:" + new string(report.m_szLabel)); listBox1.Items.Add("交易时间:" + report.m_time.ToString() + ",最新价:" + Convert.ToString(report.m_fNewPrice) + ",申卖量5:" + Convert.ToString(report.m_fSellVolume5)); } //WriteFile(report, report.m_szLabel, dirData); WriteSocketMsg(report); //注意:避免影响界面的刷新,只显示前20条记录 //if (i > 20) break; } break; } case HaiLiDrvDemo.StockDrv.RCV_FILEDATA: { HaiLiDrvDemo.RCV_DATA pHeader = (HaiLiDrvDemo.RCV_DATA)Marshal.PtrToStructure(m.LParam, typeof(HaiLiDrvDemo.RCV_DATA)); switch (pHeader.m_wDataType) { // 补日线数据; case HaiLiDrvDemo.StockDrv.FILE_HISTORY_EX: DateTime dt = DateTime.Now; string dayDir = "day" + dt.ToString("yyyyMMdd"); string fileName = ""; Directory.CreateDirectory(dayDir); //行情中的商品名称等数据存入此处(即补充的数据头) for (int i = 0; i < pHeader.m_nPacketNum; i++) { //日线数据 HaiLiDrvDemo.RCV_HISTORY_STRUCTEx history = (HaiLiDrvDemo.RCV_HISTORY_STRUCTEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 32 * i), typeof(HaiLiDrvDemo.RCV_HISTORY_STRUCTEx)); HaiLiDrvDemo.RCV_EKE_HEADEx HeadEx = (HaiLiDrvDemo.RCV_EKE_HEADEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 32 * i), typeof(HaiLiDrvDemo.RCV_EKE_HEADEx)); //时间日期计算 new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified).Add(TimeSpan.FromTicks(history.m_time * TimeSpan.TicksPerSecond)); //将日线数据存入自己的集合中,或处理数据 //前四个字节,用来判断当前记录是包含股票代码的数据头还是实际历史日线数据 if ((uint)history.m_time == StockDrv.EKE_HEAD_TAG) { listBox1.Items.Add("日线数据,股票代码:" + new string(HeadEx.m_szLabel)); //创建文件 char[] dest = new char[6]; //股票代码 Array.Copy(HeadEx.m_szLabel, dest, 6); fileName = dayDir + "\\" + (HeadEx.m_wMarket == 18515 ? "1" : "2") + new string(dest) + ".txt"; } else { //listBox1.Items.Add("交易时间:" + history.m_time.ToString() + ",最高价:" + Convert.ToString(history.m_fHigh) + ",最低价:" + Convert.ToString(history.m_fLow)); WriteDayFile(fileName, history); } //注意:避免影响界面的刷新,只显示前20条记录 //if (i > 20) break; } break; // 补分时线数据; case HaiLiDrvDemo.StockDrv.FILE_MINUTE_EX: //行情中的商品名称等数据存入此处(即补充的数据头) for (int i = 0; i < pHeader.m_nPacketNum; i++) { //分时线数据 HaiLiDrvDemo.RCV_MINUTE_STRUCTEx minute = (HaiLiDrvDemo.RCV_MINUTE_STRUCTEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 16 * i), typeof(HaiLiDrvDemo.RCV_MINUTE_STRUCTEx)); HaiLiDrvDemo.RCV_EKE_HEADEx HeadEx = (HaiLiDrvDemo.RCV_EKE_HEADEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 16 * i), typeof(HaiLiDrvDemo.RCV_EKE_HEADEx)); //时间日期计算 new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified).Add(TimeSpan.FromTicks(history.m_time * TimeSpan.TicksPerSecond)); //将日线数据存入自己的集合中,或处理数据 //前四个字节,用来判断当前记录是包含股票代码的数据头还是实际历史日线数据 if ((uint)minute.m_time == StockDrv.EKE_HEAD_TAG) { listBox1.Items.Add("分时线数据,股票代码:" + new string(HeadEx.m_szLabel)); } else { listBox1.Items.Add("交易时间:" + minute.m_time.ToString() + ",成交价:" + Convert.ToString(minute.m_fPrice) + ",成交量:" + Convert.ToString(minute.m_fVolume)); } //注意:避免影响界面的刷新,只显示前20条记录 if (i > 20) { break; } } break; // 补五分钟线数据; case HaiLiDrvDemo.StockDrv.FILE_5MINUTE_EX: //行情中的商品名称等数据存入此处(即补充的数据头) for (int i = 0; i < pHeader.m_nPacketNum; i++) { //五分钟线数据 HaiLiDrvDemo.RCV_HISMINUTE_STRUCTEx history = (HaiLiDrvDemo.RCV_HISMINUTE_STRUCTEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 32 * i), typeof(HaiLiDrvDemo.RCV_HISMINUTE_STRUCTEx)); HaiLiDrvDemo.RCV_EKE_HEADEx HeadEx = (HaiLiDrvDemo.RCV_EKE_HEADEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 32 * i), typeof(HaiLiDrvDemo.RCV_EKE_HEADEx)); //时间日期计算 new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified).Add(TimeSpan.FromTicks(history.m_time * TimeSpan.TicksPerSecond)); //将日线数据存入自己的集合中,或处理数据 //前四个字节,用来判断当前记录是包含股票代码的数据头还是实际历史日线数据 if ((uint)history.m_time == StockDrv.EKE_HEAD_TAG) { listBox1.Items.Add("五分钟线数据,股票代码:" + new string(HeadEx.m_szLabel)); } else { listBox1.Items.Add("交易时间:" + history.m_time.ToString() + ",最高价:" + Convert.ToString(history.m_fHigh) + ",最低价:" + Convert.ToString(history.m_fLow)); } //注意:避免影响界面的刷新,只显示前20条记录 if (i > 20) { break; } } break; // 补除权数据; case HaiLiDrvDemo.StockDrv.FILE_POWER_EX: //行情中的商品名称等数据存入此处(即补充的数据头) for (int i = 0; i < pHeader.m_nPacketNum; i++) { //除权数据 HaiLiDrvDemo.RCV_POWER_STRUCTEx history = (HaiLiDrvDemo.RCV_POWER_STRUCTEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 20 * i), typeof(HaiLiDrvDemo.RCV_POWER_STRUCTEx)); HaiLiDrvDemo.RCV_EKE_HEADEx HeadEx = (HaiLiDrvDemo.RCV_EKE_HEADEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 20 * i), typeof(HaiLiDrvDemo.RCV_EKE_HEADEx)); //时间日期计算 new DateTime(1970, 1, 1, 8, 0, 0, DateTimeKind.Unspecified).Add(TimeSpan.FromTicks(history.m_time * TimeSpan.TicksPerSecond)); //将日线数据存入自己的集合中,或处理数据 //前四个字节,用来判断当前记录是包含股票代码的数据头还是实际历史日线数据 if ((uint)history.m_time == StockDrv.EKE_HEAD_TAG) { listBox1.Items.Add("除权数据,股票代码:" + new string(HeadEx.m_szLabel)); } else { listBox1.Items.Add("时间:" + history.m_time.ToString() + ",每股送:" + Convert.ToString(history.m_fGive) + ",每股红利:" + Convert.ToString(history.m_fProfit)); } //注意:避免影响界面的刷新,只显示前20条记录 if (i > 20) { break; } } break; // 补基本资料数据; case HaiLiDrvDemo.StockDrv.FILE_BASE_EX: listBox1.Items.Add("收到F10资料数据 长度:" + pHeader.m_File.m_dwLen.ToString()); break; // 补新闻资讯数据; case HaiLiDrvDemo.StockDrv.FILE_NEWS_EX: listBox1.Items.Add("收到公告新闻数据 长度:" + pHeader.m_File.m_dwLen.ToString()); break; default: break; } break; } case HaiLiDrvDemo.StockDrv.RCV_FENBIDATA: { //接口补充的盘口分笔数据,这个是指当天的盘口分笔 HaiLiDrvDemo.RCV_FENBI mHeader = (HaiLiDrvDemo.RCV_FENBI)Marshal.PtrToStructure(m.LParam, typeof(HaiLiDrvDemo.RCV_FENBI)); listBox1.Items.Add("当前分笔代码:" + new string(mHeader.m_szLabel)); listBox1.Items.Add("共有分笔记录数:" + mHeader.m_nCount.ToString()); //Task.Factory.StartNew((obj) => //{ Tuple <RCV_FENBI, IntPtr> tuple = Tuple.Create(mHeader, m.LParam); HaiLiDrvDemo.RCV_FENBI header = tuple.Item1; char[] dest = new char[6]; //股票代码 Array.Copy(header.m_szLabel, dest, 6); //18515=SH 23123=SZ string fileName = dirFenbi + "\\" + (header.m_wMarket == 18515 ? "1" : "2") + new string(dest) + ".btor"; WriteBinaryFile(fileName, tuple); // }, ); break; } case HaiLiDrvDemo.StockDrv.RCV_MKTTBLDATA: { HaiLiDrvDemo.HLMarketType mHeader = (HaiLiDrvDemo.HLMarketType)Marshal.PtrToStructure(m.LParam, typeof(HaiLiDrvDemo.HLMarketType)); listBox1.Items.Add("当前码表市场:" + mHeader.m_wMarket.ToString()); listBox1.Items.Add("总码表记录数:" + mHeader.m_nCount.ToString()); for (int i = 0; i < mHeader.m_nCount; i++) { HaiLiDrvDemo.RCV_TABLE_STRUCT table = (HaiLiDrvDemo.RCV_TABLE_STRUCT)Marshal.PtrToStructure(new IntPtr((int)m.LParam + 54 + 44 * i), typeof(HaiLiDrvDemo.RCV_TABLE_STRUCT)); //将实时数据存入自己的集合中,或者做别的数量 //System.Text.StringBuilder sb = new System.Text.StringBuilder(); //sb.Append(report.m_szName); //string codename = sb.ToString(); listBox1.Items.Add("股票代码:" + new string(table.m_szLabel)); listBox1.Items.Add("股票名称:" + new string(table.m_szName)); //注意:避免影响界面的刷新,只显示前20条记录 if (i > 20) { break; } } break; } case HaiLiDrvDemo.StockDrv.RCV_FINANCEDATA: { HaiLiDrvDemo.RCV_DATA pHeader = (HaiLiDrvDemo.RCV_DATA)Marshal.PtrToStructure(m.LParam, typeof(HaiLiDrvDemo.RCV_DATA)); listBox1.Items.Add("收到财务数据包,记录数:" + pHeader.m_nPacketNum.ToString()); for (int i = 0; i < pHeader.m_nPacketNum; i++) { HaiLiDrvDemo.Fin_LJF_STRUCTEx FinLjf = (HaiLiDrvDemo.Fin_LJF_STRUCTEx)Marshal.PtrToStructure(new IntPtr((int)pHeader.m_pData + 166 * i), typeof(HaiLiDrvDemo.Fin_LJF_STRUCTEx)); //将财务数据存入自己的集合中,或者做别的数量 //System.Text.StringBuilder sb = new System.Text.StringBuilder(); //sb.Append(report.m_szName); //string codename = sb.ToString(); //listBox1.Items.Add(new string(FinLjf.m_szLabel)); listBox1.Items.Add("市场类型:" + FinLjf.m_wMarket.ToString() + ",股票代码:" + new string(FinLjf.m_szLabel)); listBox1.Items.Add("数据日期:" + FinLjf.BGRQ.ToString() + ",总股本:" + Convert.ToString(FinLjf.ZGB) + ",净资收益率:" + Convert.ToString(FinLjf.JZCSYL)); //注意:避免影响界面的刷新,只显示前20条记录 if (i > 20) { break; } } break; } default: break; } return; } base.WndProc(ref m); }