private void BulkLoadStockMinuteToSqlFromSource(string code, DateTime currentTime) { IdentifyOrCreateDBAndTable(currentTime); var latestTime = GetLatestTimeFromSql(code, currentTime); latestTime = latestTime == default(DateTime) ? new DateTime(currentTime.Year, currentTime.Month, 1) : latestTime.AddMinutes(1); if (latestTime.TimeOfDay >= new TimeSpan(14, 59, 00)) { latestTime = latestTime.Date.AddDays(1); } var endTime = GetEndTime(currentTime); if (latestTime < endTime) { var dataTable = tickRepo.GetStockTransaction(code, latestTime, endTime); var days = dateTimeRepo.GetStockTransactionDate(latestTime.Date, endTime.Date); var timelist = DataTimeStampExtension.GetStockMinuteTimeList(); List <StockMinuteTransaction> minuteAll = new List <StockMinuteTransaction>(); foreach (var day in days) { //Console.WriteLine("date:{0} start!!", day); List <StockMinuteTransaction> minuteNow = new List <StockMinuteTransaction>(); var dtToday = (from data in dataTable where data.TransactionDateTime.Date == day.Date select data).ToList(); if (dtToday[dtToday.Count() - 1].Volume <= 0) { continue; } if (dtToday.Count() != 0) { //处理开盘前的价格和成交量 double totalVolume = 0; double totalAmount = 0; double high = 0; double low = 0; double close = 0; double open = 0; var preOpen = (from data in dtToday where (data.TransactionDateTime.TimeOfDay < timelist[0]) select data).ToList(); if (preOpen.Count != 0) { var last = preOpen[preOpen.Count() - 1]; totalVolume = last.Volume; totalAmount = last.Amount; close = last.LastPrice; open = close; high = close; low = close; } for (int i = 0; i < timelist.Count() - 1; i++) { var dtNow = (from data in dtToday where (data.TransactionDateTime.TimeOfDay >= timelist[i]) && (data.TransactionDateTime.TimeOfDay <= timelist[i + 1]) select data).ToList(); if (dtNow.Count() != 0) { high = dtNow.Max(x => x.LastPrice); low = dtNow.Min(x => x.LastPrice); var listNow = dtNow.ToList(); var startData = listNow[0]; var endData = listNow[listNow.Count() - 1]; open = startData.LastPrice; close = endData.LastPrice; double volumeNew = endData.Volume; double amountNew = endData.Amount; double volume = volumeNew - totalVolume; double amount = amountNew - totalAmount; totalAmount = amountNew; totalVolume = volumeNew; StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = amount; KLine.Volume = volume; KLine.Open = open; KLine.Close = close; KLine.High = high; KLine.Low = low; KLine.Code = code; KLine.DateTime = day.Date + timelist[i]; minuteNow.Add(KLine); } else { StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = 0; KLine.Volume = 0; KLine.Open = close; KLine.Close = close; KLine.High = close; KLine.Low = close; KLine.Code = code; KLine.DateTime = day.Date + timelist[i]; minuteNow.Add(KLine); } } var nearClose = (from data in dtToday where (data.TransactionDateTime.TimeOfDay > timelist[timelist.Count() - 1]) select data).ToList(); if (nearClose.Count() != 0) { high = nearClose.Max(x => x.LastPrice); low = nearClose.Min(x => x.LastPrice); var listNow = nearClose.ToList(); var startData = listNow[0]; var endData = listNow[listNow.Count() - 1]; open = startData.LastPrice; close = endData.LastPrice; double volumeNew = endData.Volume; double amountNew = endData.Amount; double volume = volumeNew - totalVolume; double amount = amountNew - totalAmount; totalAmount = amountNew; totalVolume = volumeNew; StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = amount; KLine.Volume = volume; KLine.Open = open; KLine.Close = close; KLine.High = high; KLine.Low = low; KLine.Code = code; KLine.DateTime = day.Date + timelist[timelist.Count() - 1]; minuteNow.Add(KLine); } else { StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = 0; KLine.Volume = 0; KLine.Open = close; KLine.Close = close; KLine.High = close; KLine.Low = close; KLine.Code = code; KLine.DateTime = day.Date + timelist[timelist.Count() - 1]; minuteNow.Add(KLine); } } else { //for (int i = 0; i < timelist.Count(); i++) //{ // StockMinuteTransaction KLine = new StockMinuteTransaction(); // KLine.Amount = 0; // KLine.Volume = 0; // KLine.Open =0; // KLine.Close = 0; // KLine.High = 0; // KLine.Low = 0; // KLine.Code = code; // KLine.DateTime = day.Date + timelist[i]; // minuteNow.Add(KLine); //} Console.WriteLine("date:{0},no tickData!!!", day); } minuteAll.AddRange(minuteNow); } if (minuteAll.Count() != 0) { var dt = transactionListToDataTable(minuteAll); WriteToSql(dt); } } }
private List <StockMinuteTransaction> LoadStockMinuteToSqlFromSouceDaily(string code, DateTime currentTime, bool record = true) { List <StockMinuteTransaction> list = new List <StockMinuteTransaction>(); IdentifyOrCreateDBAndTable(currentTime); var dtToday = tickRepo.GetStockTransaction(code, currentTime, currentTime, record); dtToday = (from data in dtToday where (data.TransactionDateTime.TimeOfDay <= new TimeSpan(15, 0, 1)) select data).ToList(); var timelist = DataTimeStampExtension.GetStockMinuteTimeList(); List <StockMinuteTransaction> minuteNow = new List <StockMinuteTransaction>(); bool exists = true; if (dtToday.Count() == 0 || dtToday[dtToday.Count() - 1].Volume <= 0) { exists = false; } if (exists == true) { //处理开盘前的价格和成交量 var day = currentTime.Date; double totalVolume = 0; double totalAmount = 0; double high = 0; double low = 0; double close = 0; double open = 0; var preOpen = (from data in dtToday where (data.TransactionDateTime.TimeOfDay < timelist[0]) select data).ToList(); if (preOpen.Count != 0) { var last = preOpen[preOpen.Count() - 1]; totalVolume = last.Volume; totalAmount = last.Amount; close = last.LastPrice; open = close; high = close; low = close; } for (int i = 0; i < timelist.Count() - 1; i++) { var dtNow = (from data in dtToday where (data.TransactionDateTime.TimeOfDay >= timelist[i]) && (data.TransactionDateTime.TimeOfDay <= timelist[i + 1]) select data).ToList(); if (dtNow.Count() != 0) { high = dtNow.Max(x => x.LastPrice); low = dtNow.Min(x => x.LastPrice); var listNow = dtNow.ToList(); var startData = listNow[0]; var endData = listNow[listNow.Count() - 1]; open = startData.LastPrice; close = endData.LastPrice; double volumeNew = endData.Volume; double amountNew = endData.Amount; double volume = volumeNew - totalVolume; double amount = amountNew - totalAmount; totalAmount = amountNew; totalVolume = volumeNew; StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = amount; KLine.Volume = volume; KLine.Open = open; KLine.Close = close; KLine.High = high; KLine.Low = low; KLine.Code = code; KLine.DateTime = day.Date + timelist[i]; minuteNow.Add(KLine); } else { StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = 0; KLine.Volume = 0; KLine.Open = close; KLine.Close = close; KLine.High = close; KLine.Low = close; KLine.Code = code; KLine.DateTime = day.Date + timelist[i]; minuteNow.Add(KLine); } } var nearClose = (from data in dtToday where (data.TransactionDateTime.TimeOfDay > timelist[timelist.Count() - 1]) select data).ToList(); if (nearClose.Count() != 0) { high = nearClose.Max(x => x.LastPrice); low = nearClose.Min(x => x.LastPrice); var listNow = nearClose.ToList(); var startData = listNow[0]; var endData = listNow[listNow.Count() - 1]; open = startData.LastPrice; close = endData.LastPrice; double volumeNew = endData.Volume; double amountNew = endData.Amount; double volume = volumeNew - totalVolume; double amount = amountNew - totalAmount; totalAmount = amountNew; totalVolume = volumeNew; StockMinuteTransaction KLine = new StockMinuteTransaction { Amount = amount, Volume = volume, Open = open, Close = close, High = high, Low = low, Code = code, DateTime = day.Date + timelist[timelist.Count() - 1] }; minuteNow.Add(KLine); } else { StockMinuteTransaction KLine = new StockMinuteTransaction(); KLine.Amount = 0; KLine.Volume = 0; KLine.Open = close; KLine.Close = close; KLine.High = close; KLine.Low = close; KLine.Code = code; KLine.DateTime = day.Date + timelist[timelist.Count() - 1]; minuteNow.Add(KLine); } list = minuteNow; } else { // Console.WriteLine("date:{0},no tickData!!!", currentTime); } return(list); }