public IKLineData GetKLineData(string code, int startDate, int endDate, KLinePeriod period) { ITradingDayReader openDateReader = dataReaderFactory.OpenDateReader; IList <int> openDates = openDateReader.GetOpenDates(startDate, endDate); int prevOpenDate = openDateReader.GetPrevOpenDate(startDate); IKLineData lastKLineData = dataReaderFactory.KLineDataReader.GetData(code, prevOpenDate, prevOpenDate, period); List <IKLineData> klineDataList = new List <IKLineData>(); for (int i = 0; i < openDates.Count; i++) { int openDate = openDates[i]; TickData tickdata = dataReaderFactory.TickDataReader.GetTickData(code, openDate); if (tickdata != null) { List <double[]> openTimes = dataProvider.GetOpenTime(code, openDate); OpenTimeUtilsArgs args = new OpenTimeUtilsArgs(openDate, openDateReader, openTimes, period); KLineData klineData = DataTransfer_Tick2KLine2.Transfer(tickdata, lastKLineData, args); klineDataList.Add(klineData); } } if (klineDataList.Count == 0) { return(null); } return(KLineData.Merge(klineDataList)); }
private void TestKLineDataStore_Append(string uri) { string code = "m1005"; KLinePeriod period = KLinePeriod.KLinePeriod_1Minute; IDataStore dataStore = DataStoreFactory.CreateDataStore(uri); IKLineDataStore klineDataStore = dataStore.CreateKLineDataStore(); try { IKLineData klineData = MockDataLoader.GetKLineData(code, 20100107, 20100114, period); IKLineData klineData2 = MockDataLoader.GetKLineData(code, 20100115, 20100120, period); List <IKLineData> ks = new List <IKLineData>(); ks.Add(klineData); ks.Add(klineData2); IKLineData klineData_Merge = KLineData.Merge(ks); klineDataStore.Save(code, period, klineData); klineDataStore.Append(code, period, klineData2); IKLineData klineData_Merge2 = klineDataStore.LoadAll(code, period); AssertUtils.AssertEqual_KLineData(klineData_Merge, klineData_Merge2); } finally { klineDataStore.Delete(code, period); } }
public KLineData CalcKLineData() { lock (lockObj) { if (this.klineData != null) { return(klineData); } this.klineData = new KLineData(this.klineTimes.Count); int startTickIndex = 0; int endTickIndex = 0; KLineBar klineBar; for (int i = 0; i < klineTimes.Count - 1; i++) { endTickIndex = CalcCurrentTickEndIndex(startTickIndex, klineTimes[i + 1]); klineBar = CalcKLineBar(i, startTickIndex, endTickIndex); klineBar.Copy2KLineData(klineData, i); startTickIndex = endTickIndex + 1; } klineBar = CalcKLineBar(klineData.Length - 1, startTickIndex, tickData.Length - 1); klineBar.Copy2KLineData(klineData, klineData.Length - 1); return(this.klineData); } }
public void TestKLineDataIndex_DoIndex_Normal() { KLineData data_ = ResourceLoader.GetKLineData_1Min(); IKLineData data = data_.GetRange(0, 449); MockDataProvider provider = new MockDataProvider(); provider.DataPathDir = "testindex"; String targetPath = provider.GetDataPath() + "\\testindex.kline"; KLineDataStore store = new KLineDataStore(targetPath); store.Save(data); KLineDataIndexer indexer = new KLineDataIndexer(targetPath); indexer.DoIndex(); KLineDataIndexResult result = indexer.GetIndexResult(); Assert.AreEqual(2, result.DateList.Count); Assert.AreEqual(20131202, result.DateList[0]); Assert.AreEqual(20131203, result.DateList[1]); data = data_.GetRange(450, data_.Length - 1); store.Append(data); indexer.DoIndex(); result = indexer.GetIndexResult(); Assert.AreEqual(10, result.DateList.Count); Directory.Delete(provider.GetDataPath(), true); }
private void TestKLineDataStore_LoadByDate(string uri) { string code = "m1005"; KLinePeriod klinePeriod = KLinePeriod.KLinePeriod_1Minute; IDataStore dataStore = DataStoreFactory.CreateDataStore(uri); IKLineDataStore klineDataStore = dataStore.CreateKLineDataStore(); try { IKLineData data = MockDataLoader.GetKLineData(code, 20100107, 20100120, klinePeriod); klineDataStore.Save(code, klinePeriod, data); IKLineData klineData = klineDataStore.Load(code, 20100107, 20100111, klinePeriod); IKLineData klineData2 = klineDataStore.Load(code, 20100112, 20100120, klinePeriod); List <IKLineData> ks = new List <IKLineData>(); ks.Add(klineData); ks.Add(klineData2); IKLineData klineData_Merge = KLineData.Merge(ks); //AssertUtils.PrintKLineData(data); //AssertUtils.PrintKLineData(klineData_Merge); AssertUtils.PrintKLineData(klineData); //AssertUtils.PrintKLineData(klineData_Merge); AssertUtils.AssertEqual_KLineData(data, klineData_Merge); } finally { klineDataStore.Delete(code, klinePeriod); } }
private static IKLineData Transfer_SrcIs1Minute(IKLineData data, KLinePeriod targetPeriod) { KLinePeriod sourcePeriod = data.Period; if (sourcePeriod.PeriodType != targetPeriod.PeriodType) return Transfer_DifferentPeriod(data, targetPeriod); List<KLineBar> charts = new List<KLineBar>(); int period = targetPeriod.Period; int startIndex = 0; int endIndex = startIndex + period - 1; endIndex = FindRealLastIndex_1Minute(data, startIndex, endIndex); while (startIndex < data.Length && endIndex < data.Length) { charts.Add(GetChart(data, startIndex, endIndex)); startIndex = endIndex + 1; endIndex = startIndex + period - 1; endIndex = FindRealLastIndex_1Minute(data, startIndex, endIndex); } KLineData klineData = GetKLineData(charts); klineData.Period = targetPeriod; return klineData; }
private void TestKLineDataStore_SaveLoad(string uri) { string code = "m1005"; KLinePeriod period = KLinePeriod.KLinePeriod_1Minute; IDataStore dataStore = DataStoreFactory.CreateDataStore(uri); IKLineDataStore klineDataStore = dataStore.CreateKLineDataStore(); try { IKLineData klineData = MockDataLoader.GetKLineData(code, 20100107, 20100120, period); klineDataStore.Save(code, period, klineData); KLineData klineData2 = klineDataStore.LoadAll(code, period); AssertUtils.AssertEqual_KLineData(klineData, klineData2); Assert.AreEqual(20100107, klineDataStore.GetFirstTradingDay(code, period)); Assert.AreEqual(20100120, klineDataStore.GetLastTradingDay(code, period)); Console.WriteLine(klineDataStore.GetLastTradingTime(code, period)); } finally { klineDataStore.Delete(code, period); } }
public IKLineData GetKLineDataByTick(string code, DataReaderFactory dataReaderFactory, KLinePeriod period, IList <int> dates) { IKLineData lastKLineData = null; float lastPrice = -1; List <IKLineData> klineDataList = new List <IKLineData>(); for (int i = 0; i < dates.Count; i++) { int openDate = dates[i]; TickData tickdata = dataReaderFactory.TickDataReader.GetTickData(code, openDate); List <double[]> openTimes = dataProvider.GetOpenTime(code, openDate); KLineData klineData; if (tickdata != null) { klineData = DataTransfer_Tick2KLine.Transfer(tickdata, period, openTimes, lastPrice); klineDataList.Add(klineData); lastPrice = klineData.arr_end[klineData.Length - 1]; } else { klineData = GetEmptyDayKLineData(code, openDate, openTimes, dataReaderFactory.OpenDateReader, lastKLineData); klineDataList.Add(klineData); } lastKLineData = klineData; } if (klineDataList.Count == 0) { return(null); } IKLineData data = KLineData.Merge(klineDataList); return(data); }
private IKLineData GetKLineData(string code, KLinePeriod klinePeriod, IList <int> openDates, float lastEndPrice, int lastEndHold) { List <IKLineData> klineDataList = new List <IKLineData>(); for (int i = 0; i < openDates.Count; i++) { int openDate = openDates[i]; IKLineData klineData = GetKLineData(code, openDate, klinePeriod); if (klineData != null) { klineDataList.Add(klineData); lastEndPrice = klineData.Arr_End[klineData.Length - 1]; lastEndHold = klineData.Arr_Hold[klineData.Length - 1]; } else { IList <double[]> tradingTime = GetTradingTime(code, openDate); IList <double[]> klineTimes = TradingTimeUtils.GetKLineTimeList(tradingTime, klinePeriod); klineData = DataTransfer_Tick2KLine.GetEmptyKLineData(klineTimes, lastEndPrice, lastEndHold); klineDataList.Add(klineData); } } return(KLineData.Merge(klineDataList)); }
/// <summary> /// 装载所有的K线数据 /// </summary> /// <param name="code"></param> /// <param name="klinePeriod"></param> /// <returns></returns> public KLineData LoadAll(string code, KLinePeriod klinePeriod) { string path = dataPathUtils.GetKLineDataPath(code, klinePeriod); KLineData klineData = (KLineData)(new KLineDataStore_File_Single(path)).LoadAll(); klineData.Period = klinePeriod; return(klineData); }
private void Init(DataReaderFactory factory, string code, KLinePeriod period, double time, int startDate, int endDate) { KLineData data = (KLineData)factory.KLineDataReader.GetData(code, startDate, endDate, period); this.klineData = new KLineData_RealTime(data); DataCacheFactory cacheFac = factory.CacheFactory; this.klineChartBuilder = new RealTimeDataBuilder_KLine(klineData, cacheFac.CreateCache_Code(code, startDate, endDate), time); this.CurrentTime = time; }
public IKLineData GetAllData(string code, KLinePeriod period) { KLineData klineData = klineDataStore.LoadAll(code, period); if (klineData == null) { return(null); } klineData.Code = code; klineData.Period = period; return(klineData); }
private IKLineData LoadKLineData(string code, int startDate, int endDate, KLinePeriod period) { KLineData klineData = klineDataStore.Load(code, startDate, endDate, period); if (klineData == null) { return(null); } klineData.Code = code; klineData.Period = period; return(klineData); }
/// <summary> /// 装载K线数据 /// </summary> /// <param name="startDate"></param> /// <param name="endDate"></param> /// <returns></returns> public KLineData Load(string code, int startDate, int endDate, KLinePeriod klinePeriod) { string path = dataPathUtils.GetKLineDataPath(code, klinePeriod); KLineData klineData = (KLineData)(new KLineDataStore_File_Single(path)).Load(startDate, endDate); if (klineData == null) { return(null); } klineData.Period = klinePeriod; return(klineData); }
private static void GetEmptyChart(KLineData klineData, int currentIndex, double time, IKLineData lastKLineData) { klineData.arr_time[currentIndex] = time; float lastPrice = currentIndex == 0 ? lastKLineData.Arr_End[lastKLineData.Length - 1] : klineData.Arr_End[currentIndex - 1]; int lastHold = currentIndex == 0 ? lastKLineData.Arr_Hold[lastKLineData.Length - 1] : klineData.Arr_Hold[currentIndex - 1]; klineData.arr_start[currentIndex] = lastPrice; klineData.arr_high[currentIndex] = lastPrice; klineData.arr_low[currentIndex] = lastPrice; klineData.arr_end[currentIndex] = lastPrice; klineData.arr_mount[currentIndex] = 0; klineData.arr_money[currentIndex] = 0; klineData.arr_hold[currentIndex] = lastHold; }
public void TestKLineDataStore_SaveLoad() { IKLineData klineData = MockDataLoader.GetKLineData("m1005", 20100107, 20100120, KLinePeriod.KLinePeriod_1Minute); String path = TestCaseManager.GetTestCasePath(GetType(), "output_20100107_20100120"); KLineDataStore_File_Single store = new KLineDataStore_File_Single(path); store.Save(klineData); KLineDataStore_File_Single store2 = new KLineDataStore_File_Single(path); KLineData klineData2 = store.LoadAll(); AssertUtils.AssertEqual_KLineData(klineData, klineData2); File.Delete(path); }
public static IKLineData Transfer(List <TickData> data, KLinePeriod targetPeriod, List <double[]> opentime) { List <IKLineData> klineDataList = new List <IKLineData>(); KLineData lastData = null; for (int i = 0; i < data.Count; i++) { float lastPrice = lastData == null ? -1 : lastData.arr_end[lastData.Length - 1]; KLineData klinedata = Transfer(data[i], targetPeriod, opentime, lastPrice); klineDataList.Add(klinedata); lastData = klinedata; } //DataTransfer_Tick2KLine transfer = new DataTransfer_Tick2KLine(data, targetPeriod, opentime, yesterdayEndPrice); //return transfer.GetKLineData(); return(KLineData.Merge(klineDataList)); }
/// <summary> /// 将k线转换成日线 /// </summary> /// <param name="data"></param> /// <param name="targetPeriod"></param> /// <param name="timeSplit"></param> /// <returns></returns> public static IKLineData Transfer_Day(IKLineData data, KLinePeriod targetPeriod) { List<SplitterResult> results = DaySplitter.Split(data); List<KLineBar> charts = new List<KLineBar>(results.Count); for (int i = 0; i < results.Count; i++) { int startIndex = results[i].Index; int endIndex = (i == results.Count - 1) ? data.Length - 1 : results[i + 1].Index - 1; charts.Add(GetChart_Day(data, startIndex, endIndex)); } KLineData klineData = GetKLineData(charts); klineData.Period = targetPeriod; return klineData; }
public IKLineData GetKLineData_Second(string code, int startDate, int endDate, KLinePeriod period) { IList <int> dates = this.dataReader.TradingDayReader.GetTradingDays(startDate, endDate); List <IKLineData> klineDataList = new List <IKLineData>(); for (int i = 0; i < dates.Count; i++) { int date = dates[i]; IKLineData klineData = LoadKLineData_Second(code, date, period); if (klineData != null) { klineDataList.Add(klineData); } } return(KLineData.Merge(klineDataList)); }
/// <summary> /// 得到一个空的K线数据 /// </summary> /// <param name="klineTimes"></param> /// <param name="price"></param> /// <param name="hold"></param> /// <returns></returns> public static KLineData GetEmptyKLineData(IList <double[]> klineTimes, float price, int hold) { KLineData klineData = new KLineData(klineTimes.Count); for (int i = 0; i < klineTimes.Count; i++) { klineData.arr_time[i] = klineTimes[i][0]; klineData.arr_start[i] = price; klineData.arr_high[i] = price; klineData.arr_low[i] = price; klineData.arr_end[i] = price; klineData.arr_mount[i] = 0; klineData.arr_money[i] = 0; klineData.arr_hold[i] = hold; } return(klineData); }
private static KLineData GetKLineData(List<KLineBar> charts) { KLineData data = new KLineData(charts.Count); data.Code = charts[0].Code; for (int i = 0; i < charts.Count; i++) { KLineBar chart = charts[i]; data.arr_time[i] = chart.Time; data.arr_start[i] = chart.Start; data.arr_high[i] = chart.High; data.arr_low[i] = chart.Low; data.arr_end[i] = chart.End; data.arr_mount[i] = chart.Mount; data.arr_money[i] = chart.Money; data.arr_hold[i] = chart.Hold; } return data; }
public static IKLineData Transfer(int date, TickData data, TradingDayReader openDateReader, List <double[]> opentime, KLinePeriod targetPeriod, KLineData lastKLineData) { List <double> klineTimePeriods = OpenTimeUtils.GetKLineTimeList(date, openDateReader, opentime, targetPeriod); KLineData klineData = new KLineData(klineTimePeriods.Count); for (int i = 0; i < klineTimePeriods.Count; i++) { klineData.arr_time[i] = klineTimePeriods[i]; } int currentTickindex = 0; for (int i = 0; i < klineData.Length; i++) { } return(klineData); }
public static IKLineData LoadByLines(string[] lines) { KLineData data = new KLineData(lines.Length); for (int i = 0; i < lines.Length; i++) { String line = lines[i].Trim(); String[] dataArr = line.Split(','); data.arr_time[i] = double.Parse(dataArr[0]); data.arr_start[i] = float.Parse(dataArr[1]); data.arr_high[i] = float.Parse(dataArr[2]); data.arr_low[i] = float.Parse(dataArr[3]); data.arr_end[i] = float.Parse(dataArr[4]); data.arr_mount[i] = (int)float.Parse(dataArr[5]); data.arr_money[i] = float.Parse(dataArr[6]); data.arr_hold[i] = int.Parse(dataArr[7]); } return(data); }
public KLineData CalcKLineData() { lock (lockObj) { if (this.klineData != null) { return(klineData); } if (this.tickData == null) { this.klineData = GetEmptyKLineData(this.klineTimes, lastEndPrice, lastEndHold); return(this.klineData); } this.klineData = new KLineData(this.klineTimes.Count); int startTickIndex = 0; int endTickIndex = 0; KLineBar klineBar; for (int i = 0; i < klineTimes.Count; i++) { double endTime = klineTimes[i][1]; double nextStartTime = -1; if (i != klineTimes.Count - 1) { nextStartTime = klineTimes[i + 1][0]; } if (i == klineTimes.Count - 1) { endTickIndex = tickData.Length - 1; } else { endTickIndex = CalcCurrentTickEndIndex(startTickIndex, endTime, nextStartTime); } klineBar = CalcKLineBar(i, startTickIndex, endTickIndex); klineBar.Copy2KLineData(klineData, i); startTickIndex = endTickIndex + 1; } return(this.klineData); } }
private KLineData GetEmptyDayKLineData(string code, int date, List <double[]> openTimes, ITradingDayReader openDateReader, IKLineData lastKLineData) { float lastPrice = lastKLineData.Arr_End[lastKLineData.Length - 1]; int lastHold = lastKLineData.Arr_Hold[lastKLineData.Length - 1]; List <double> openTimeList = OpenTimeUtils.GetKLineTimeList(date, openDateReader, openTimes, KLinePeriod.KLinePeriod_1Minute); KLineData klineData = new KLineData(openTimeList.Count); for (int i = 0; i < klineData.Length; i++) { klineData.arr_time[i] = openTimeList[i]; klineData.arr_start[i] = lastPrice; klineData.arr_high[i] = lastPrice; klineData.arr_low[i] = lastPrice; klineData.arr_end[i] = lastPrice; klineData.arr_mount[i] = 0; klineData.arr_money[i] = 0; klineData.arr_hold[i] = lastHold; } return(klineData); }
private IKLineData UpdateByTick(string code, DataReaderFactory dataReaderFactory, KLinePeriod period) { String path = utils.GetKLineDataPath(code, period); KLineDataStore store = new KLineDataStore(path); int lastDate = (int)store.GetLastTime(); List <int> openDates = dataProvider.GetOpenDates(); int lastIndex; if (lastDate < 0) { lastIndex = -1; } else { lastIndex = openDates.IndexOf(lastDate); } float lastPrice = -1; List <IKLineData> klineDataList = new List <IKLineData>(); for (int i = lastIndex + 1; i < openDates.Count; i++) { int openDate = openDates[i]; TickData tickdata = dataReaderFactory.TickDataReader.GetTickData(code, openDate); if (tickdata != null) { List <double[]> openTimes = dataProvider.GetOpenTime(code, openDate); KLineData klineData = DataTransfer_Tick2KLine.Transfer(tickdata, period, openTimes, lastPrice); klineDataList.Add(klineData); lastPrice = klineData.arr_end[klineData.Length - 1]; } } if (klineDataList.Count == 0) { return(null); } IKLineData data = KLineData.Merge(klineDataList); store.Append(data); return(data); }
public void Change(string code, double time, KLinePeriod period) { if (this.code == code && this.period.Equals(period) && IsInCurrentKLineData(time)) { ChangeTime(time); return; } dataCache_Code = dataCacheFac.CreateCache_Code(code); int extendBefore = 30; int extendAfter = 30; if (period.PeriodType == KLineTimeType.MINUTE) { extendBefore = 30; extendAfter = 30; } else if (period.PeriodType == KLineTimeType.HOUR) { extendBefore = 100; extendAfter = 100; } else if (period.PeriodType == KLineTimeType.DAY) { extendBefore = 2000; extendAfter = 2000; } int date = (int)time; int start = (int)TimeUtils.AddDays(date, -extendBefore); int end = (int)TimeUtils.AddDays(date, extendAfter); KLineData data = (KLineData)dataReaderFac.KLineDataReader.GetData(code, start, end, period); this.klineData = new KLineData_RealTime(data); this.code = code; this.period = period; ChangeTime(time); }
public void TestKLineDataStore_Append() { IKLineData klineData = MockDataLoader.GetKLineData("m1005", 20100107, 20100114, KLinePeriod.KLinePeriod_1Minute); IKLineData klineData2 = MockDataLoader.GetKLineData("m1005", 20100115, 20100120, KLinePeriod.KLinePeriod_1Minute); List <IKLineData> ks = new List <IKLineData>(); ks.Add(klineData); ks.Add(klineData2); IKLineData klineData_Merge = KLineData.Merge(ks); String path = TestCaseManager.GetTestCasePath(GetType(), "output_append"); KLineDataStore_File_Single store = new KLineDataStore_File_Single(path); store.Save(klineData); store.Append(klineData2); IKLineData klineData_Merge2 = store.LoadAll(); AssertUtils.AssertEqual_KLineData(klineData_Merge, klineData_Merge2); File.Delete(path); }
public void TestKLineDataIndex_DoIndex_HasNight() { KLineData klineData = ResourceLoader.GetKLineData(Resources.KLineData_M05_20130101_20151231_1Minute); String targetPath = GetTestOutputPath() + "M05_20130101_20151231_1Minute.kline"; KLineDataStore store = new KLineDataStore(targetPath); store.Save(klineData); KLineDataIndexer indexer = new KLineDataIndexer(targetPath); indexer.DoIndex(); string indexPath = targetPath + ".index"; string[] indexLines = File.ReadAllLines(indexPath); string[] lines = Resources.KLineData_M05_20130101_20151231_1Minute_Index.Split('\r'); Assert.AreEqual(lines.Length, indexLines.Length); for (int i = 0; i < lines.Length; i++) { Assert.AreEqual(lines[i].Trim(), indexLines[i].Trim()); } Directory.Delete(GetTestOutputPath(), true); }
public static KLineData FromBytes(byte[] bs, int start, int len) { int size = LEN_EVERYKLINE; int dataLength = len / size; KLineData data = new KLineData(dataLength); for (int i = 0; i < dataLength; i++) { int offset = i * size + start; data.arr_time[i] = BitConverter.ToDouble(bs, offset); offset += 8; data.arr_start[i] = BitConverter.ToSingle(bs, offset); offset += 4; data.arr_high[i] = BitConverter.ToSingle(bs, offset); offset += 4; data.arr_low[i] = BitConverter.ToSingle(bs, offset); offset += 4; data.arr_end[i] = BitConverter.ToSingle(bs, offset); offset += 4; data.arr_mount[i] = BitConverter.ToInt32(bs, offset); offset += 4; data.arr_money[i] = BitConverter.ToSingle(bs, offset); offset += 4; data.arr_hold[i] = BitConverter.ToInt32(bs, offset); offset += 4; } return(data); }