private USeKLine ParseKLine(string line, ImportFileEntity entity) { string[] items = line.Split(new char[] { ',' }); USeKLine kLine = new USeKLine(); //文件名带过来的 kLine.InstrumentCode = entity.InstrumentCode; //搜索匹配过来的 kLine.Market = FromCodeToMarket(entity.InstrumentCode); //entity根据文件名获取过来的 kLine.Cycle = entity.Cycle; //line[0]和line[1]合成...开高低收 string dateRegion = items[0]; string[] dateArray = items[0].Split('.'); string dateProcess = dateArray[0] + "-" + dateArray[1] + "-" + dateArray[2] + " " + items[1]; kLine.DateTime = Convert.ToDateTime(dateProcess); kLine.Open = Convert.ToDecimal(items[2]); kLine.High = Convert.ToDecimal(items[3]); kLine.Low = Convert.ToDecimal(items[4]); kLine.Close = Convert.ToDecimal(items[5]); kLine.OpenInterest = Convert.ToDecimal(items[6]); kLine.Volumn = Convert.ToInt32(items[7]); kLine.Turnover = Convert.ToDecimal(items[8]) * 10000; return(kLine); }
public void ImportDayKLine(ImportFileEntity entity) { USeKLine kLine = ReadDayKLineFile(entity.FilePath); if (kLine != null) { //日线先更新,影响条目为0,则Insert try { using (MySqlConnection connection = new MySqlConnection(m_dbConStr)) { connection.Open(); string cmdUpdateText = CreateKLineUpdateSql(kLine); MySqlCommand updateCommand = new MySqlCommand(cmdUpdateText, connection); updateCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); updateCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); updateCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); updateCommand.Parameters.AddWithValue("@price_open", kLine.Open); updateCommand.Parameters.AddWithValue("@price_high", kLine.High); updateCommand.Parameters.AddWithValue("@price_low", kLine.Low); updateCommand.Parameters.AddWithValue("@price_close", kLine.Close); updateCommand.Parameters.AddWithValue("@volumn", kLine.Volumn); updateCommand.Parameters.AddWithValue("@turnover", kLine.Turnover); updateCommand.Parameters.AddWithValue("@openinterest", kLine.OpenInterest); updateCommand.Parameters.AddWithValue("@pre_settlement_price", kLine.PreSettlementPrice); updateCommand.Parameters.AddWithValue("@settlement_price", kLine.SettlementPrice); updateCommand.Parameters.AddWithValue("@ask_volumn", kLine.AskVolumn); updateCommand.Parameters.AddWithValue("@bid_volumn", kLine.BidVolumn); int updateResult = updateCommand.ExecuteNonQuery(); if (updateResult <= 0) { string cmdInsertText = CreateKLineUpdateSql(kLine); MySqlCommand insertCommand = new MySqlCommand(cmdInsertText, connection); insertCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); insertCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); insertCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); insertCommand.Parameters.AddWithValue("@price_open", kLine.Open); insertCommand.Parameters.AddWithValue("@price_high", kLine.High); insertCommand.Parameters.AddWithValue("@price_low", kLine.Low); insertCommand.Parameters.AddWithValue("@price_close", kLine.Close); insertCommand.Parameters.AddWithValue("@volumn", kLine.Volumn); insertCommand.Parameters.AddWithValue("@turnover", kLine.Turnover); insertCommand.Parameters.AddWithValue("@openinterest", kLine.OpenInterest); insertCommand.Parameters.AddWithValue("@pre_settlement_price", kLine.PreSettlementPrice); insertCommand.Parameters.AddWithValue("@settlement_price", kLine.SettlementPrice); insertCommand.Parameters.AddWithValue("@ask_volumn", kLine.AskVolumn); insertCommand.Parameters.AddWithValue("@bid_volumn", kLine.BidVolumn); int insertResult = insertCommand.ExecuteNonQuery(); } } } catch (Exception ex) { throw new Exception("ImportDayKLine异常:" + ex.Message); } } }
/// <summary> /// 读取分钟文件所有的数据List /// </summary> /// <param name="fileName"></param> /// <returns></returns> private List <USeKLine> ReadMin1KLineFile(ImportFileEntity entity) { List <USeKLine> list = new List <USeKLine>(); string[] lines = File.ReadAllLines(entity.FilePath); if (lines != null && lines.Length > 0) { foreach (string line in lines) { list.Add(ParseKLine(line, entity)); } } return(list); }
private List <ImportFileEntity> GetAllImportFile() { List <ImportFileEntity> list = new List <ImportFileEntity>(); DirectoryInfo directory = new DirectoryInfo(m_folderPath); FileInfo[] fileList = directory.GetFiles(); foreach (FileInfo file in fileList) { string[] fileNameInfo = file.Name.Split(new char[] { '_', '.' }); ImportFileEntity entity = new ImportFileEntity(); entity.FilePath = file.FullName; entity.Cycle = (USeCycleType)(Enum.Parse(typeof(USeCycleType), fileNameInfo[0])); entity.InstrumentCode = fileNameInfo[1]; } return(list); }
private List <ImportFileEntity> GetAllImportFile() { List <ImportFileEntity> list = new List <ImportFileEntity>(); DirectoryInfo directory = new DirectoryInfo(m_folderPath); FileInfo[] fileList = directory.GetFiles(); foreach (FileInfo file in fileList) { string[] fileNameInfo = file.Name.Split(new char[] { '-', '.' }); ImportFileEntity entity = new ImportFileEntity(); entity.FilePath = file.FullName; if (fileNameInfo[0].Equals("1day")) { entity.Cycle = USeCycleType.Day; } else { entity.Cycle = USeCycleType.Min1; } entity.InstrumentCode = fileNameInfo[1]; list.Add(entity); } List <ImportFileEntity> tempList = new List <ImportFileEntity>(); for (int i = list.Count() - 1; i >= 0; i--) { tempList.Add(list[i]); } return(tempList); }
private void ImportMin1KLine(ImportFileEntity entity) { int count = 0; List <USeKLine> kLineList = ReadMin1KLineFile(entity); if (kLineList != null) { try { using (MySqlConnection connection = new MySqlConnection(m_dbConStr)) { connection.Open(); foreach (USeKLine kLine in kLineList) { string cmdUpdateText = CreateKLineUpdateSql(kLine); MySqlCommand updateCommand = new MySqlCommand(cmdUpdateText, connection); updateCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); updateCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); updateCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); updateCommand.Parameters.AddWithValue("@price_open", kLine.Open); updateCommand.Parameters.AddWithValue("@price_high", kLine.High); updateCommand.Parameters.AddWithValue("@price_low", kLine.Low); updateCommand.Parameters.AddWithValue("@price_close", kLine.Close); updateCommand.Parameters.AddWithValue("@volumn", kLine.Volumn); updateCommand.Parameters.AddWithValue("@turnover", kLine.Turnover); updateCommand.Parameters.AddWithValue("@openinterest", kLine.OpenInterest); int updateResult = updateCommand.ExecuteNonQuery(); if (updateResult <= 0) { string cmdInsertText = CreateKLineInsertSql(kLine); MySqlCommand insertCommand = new MySqlCommand(cmdInsertText, connection); insertCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); insertCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); insertCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); insertCommand.Parameters.AddWithValue("@price_open", kLine.Open); insertCommand.Parameters.AddWithValue("@price_high", kLine.High); insertCommand.Parameters.AddWithValue("@price_low", kLine.Low); insertCommand.Parameters.AddWithValue("@price_close", kLine.Close); insertCommand.Parameters.AddWithValue("@volumn", kLine.Volumn); insertCommand.Parameters.AddWithValue("@turnover", kLine.Turnover); insertCommand.Parameters.AddWithValue("@openinterest", kLine.OpenInterest); int insertResult = insertCommand.ExecuteNonQuery(); } count++; Debug.WriteLine("该文件共{0}个条目,目前:{1},正在处理的文件:{2},", kLineList.Count(), count, entity.FilePath); OnProcessDataEvent("Inner", kLineList.Count(), count, entity.FilePath); } } } catch (Exception ex) { throw new Exception("ImportDayKLine异常:" + ex.Message); } } }
private void ImportMin1KLine(ImportFileEntity entity) { List <USeKLine> klineList = ReadMin1KLineFile(entity.FilePath); if (klineList != null) { DateTime maxTime = klineList[klineList.Count - 1].DateTime; DateTime minTime = klineList[0].DateTime; //数据库delete这个范围 foreach (USeKLine kLine in klineList) { try { using (MySqlConnection connection = new MySqlConnection(m_dbConStr)) { connection.Open(); string cmdDeleteText = CreateKLineDeleteSql(kLine); MySqlCommand deleteCommand = new MySqlCommand(cmdDeleteText, connection); deleteCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); deleteCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); deleteCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); int deleteResult = deleteCommand.ExecuteNonQuery(); } } catch (Exception ex) { throw new Exception("ImportMinKLine删除分钟数据异常:" + ex.Message); } } //分钟线删除完毕开始Insert foreach (USeKLine kLine in klineList) { try { using (MySqlConnection connection = new MySqlConnection(m_dbConStr)) { connection.Open(); string cmdInsertText = CreateKLineInsertSql(kLine); MySqlCommand insertCommand = new MySqlCommand(cmdInsertText, connection); insertCommand.Parameters.AddWithValue("@contract", kLine.InstrumentCode); insertCommand.Parameters.AddWithValue("@exchange", kLine.Market.ToString()); insertCommand.Parameters.AddWithValue("@date_time", kLine.DateTime); insertCommand.Parameters.AddWithValue("@price_open", kLine.Open); insertCommand.Parameters.AddWithValue("@price_high", kLine.High); insertCommand.Parameters.AddWithValue("@price_low", kLine.Low); insertCommand.Parameters.AddWithValue("@price_close", kLine.Close); insertCommand.Parameters.AddWithValue("@volumn", kLine.Volumn); insertCommand.Parameters.AddWithValue("@turnover", kLine.Turnover); insertCommand.Parameters.AddWithValue("@openinterest", kLine.OpenInterest); insertCommand.Parameters.AddWithValue("@pre_settlement_price", kLine.PreSettlementPrice); insertCommand.Parameters.AddWithValue("@settlement_price", kLine.SettlementPrice); insertCommand.Parameters.AddWithValue("@ask_volumn", kLine.AskVolumn); insertCommand.Parameters.AddWithValue("@bid_volumn", kLine.BidVolumn); int insertResult = insertCommand.ExecuteNonQuery(); Debug.Assert(insertResult == 1); } } catch (Exception ex) { throw new Exception("ImportMinKLine添加分钟数据异常:" + ex.Message); } } } }