/// <summary> /// Запись в базу исторических данных /// </summary> /// <param name="insStoreID">Поток данных</param> /// <param name="decimals">Кол-во десятичных знаков после запятой в ценах</param> /// <param name="bars">Список баров</param> /// <param name="date1">Начальная дата блока исторических данных</param> /// <param name="date2">Конечная дата блока исторических данных</param> /// <param name="isLastDirty">Конечная дата помечается как неполный день</param> /// <param name="cancel">Токен отмены длительной операции</param> public void InsertData(int insStoreID, int decimals, IEnumerable <Bar> bars, DateTime date1, DateTime date2, bool isLastDirty, CancellationToken cancel) { var calendar = GetInsStoreCalendar(insStoreID); if (calendar == null) { return; } calendar.AppendPeriod(new InsStorePeriod(date1, date2, isLastDirty)); var freeDays = GetFreeDays(bars, date1, date2, isLastDirty); calendar.UpdateFreeDays(freeDays, date1, date2); int k = (int)Math.Pow(10, decimals); var dbBars = bars.Select(b => { DbBarHistory bh = new DbBarHistory(); bh.InsStoreID = insStoreID; bh.Time = StorageLib.ToDbTime(b.Time); int op = (int)(b.Open * k); int cp = (int)(b.Close * k); int hp = (int)(b.High * k); int lp = (int)(b.Low * k); bh.OpenPrice = op; bh.CloseDelta = CalcDelta(cp, op); bh.HighDelta = CalcDelta(hp, op); bh.LowDelta = CalcDelta(lp, op); long v = b.Volume; if (v > int.MaxValue) { v = int.MaxValue; } if (v < int.MinValue) { v = int.MinValue; } bh.Volume = (int)v; return(bh); }); _insStoreDA.InsertBars(insStoreID, dbBars, date1, date2, cancel); if (cancel.IsCancellationRequested) { return; } _insStoreDA.UpdatePeriods(insStoreID, calendar.Periods); _insStoreDA.UpdateFreeDays(insStoreID, calendar.FreeDays); }
public void InsertBars() { List <Bar> bars = new List <Bar>(); DateTime time = new DateTime(2018, 1, 1, 10, 0, 0); for (int i = 0; i < 480; i++) { Bar b = new Bar(time, Timeframes.Min); b.Open = 100; b.High = 110; b.Low = 95; b.Close = 105; b.Volume = 1000; bars.Add(b); time = time.AddMinutes(1); } time = new DateTime(2018, 1, 2, 10, 0, 0); for (int i = 0; i < 480; i++) { Bar b = new Bar(time, Timeframes.Min); b.Open = 101; b.High = 121; b.Low = 93; b.Close = 115; b.Volume = 100; bars.Add(b); time = time.AddMinutes(1); } time = new DateTime(2018, 1, 3, 10, 0, 0); for (int i = 0; i < 480; i++) { Bar b = new Bar(time, Timeframes.Min); b.Open = 101; b.High = 118; b.Low = 87; b.Close = 109; b.Volume = 537; bars.Add(b); time = time.AddMinutes(1); } int k = (int)Math.Pow(10, 2); var dbBars = bars.Select(b => { DbBarHistory bh = new DbBarHistory(); bh.InsStoreID = 1; bh.Time = StorageLib.ToDbTime(b.Time); int op = (int)(b.Open * k); int cp = (int)(b.Close * k); int hp = (int)(b.High * k); int lp = (int)(b.Low * k); bh.OpenPrice = op; bh.CloseDelta = CalcDelta(cp, op); bh.HighDelta = CalcDelta(hp, op); bh.LowDelta = CalcDelta(lp, op); long v = b.Volume; if (v > int.MaxValue) { v = int.MaxValue; } if (v < int.MinValue) { v = int.MinValue; } bh.Volume = (int)v; return(bh); }); int ins1ID = _insDA.InsertInstrum("INS1", "", "", 10, 2, 1); var insStore = _insStoreDA.GetInsStore(ins1ID, Timeframes.Min); if (insStore != null) { _insStoreDA.DeleteInsStoreByID(insStore.InsStoreID); } int insStoreID = _insStoreDA.CreateInsStore(ins1ID, Timeframes.Min, true); _insStoreDA.InsertBars(insStoreID, dbBars, new DateTime(2018, 1, 1), new DateTime(2018, 1, 3), new CancellationToken()); var bars1 = _insStoreDA.GetHistoryAsync(insStoreID, new DateTime(2018, 1, 1), new DateTime(2018, 1, 3)).Result; Assert.Equal(1440, bars1.Count()); _insStoreDA.DeleteBars(insStoreID, new DateTime(2018, 1, 1), new DateTime(2018, 1, 1)); var bars2 = _insStoreDA.GetHistoryAsync(insStoreID, new DateTime(2018, 1, 1), new DateTime(2018, 1, 3)).Result; Assert.Equal(960, bars2.Count()); _insStoreDA.DeleteBars(insStoreID, new DateTime(2018, 1, 2), new DateTime(2018, 1, 3)); var bars3 = _insStoreDA.GetHistoryAsync(insStoreID, new DateTime(2018, 1, 1), new DateTime(2018, 1, 3)).Result; Assert.Empty(bars3); //cleanup _insStoreDA.DeleteInsStoreByID(insStoreID); }