private static void ConvertCSV(string csvFile) { string[] sData = LoadCSV(csvFile); if (sData == null) { return; } bool bConvert = false; SeriesSymbolData cMSeries = null, cDSeries = null; if (logger.IsInfoEnabled) { logger.Info("[Convert] 開始轉換 CSV 期貨檔案資訊..."); } string[] sItems = sData[0].Split(','); string sSymbolId = null; bConvert = cTargetSymbols.TryGetValue(Path.GetFileNameWithoutExtension(csvFile), out sSymbolId); if (bConvert) { int iLength = sData.Length; cMSeries = CreateSeries(sSymbolId, EResolution.Minute); cMSeries.SetRange(iLength); cMSeries.AdjustSize(iLength, true); for (int i = iLength - 1; i >= 0; i--) { sItems = sData[i].Split(','); DateTime cTime = DateTime.Parse(sItems[0]); double dOpen = double.Parse(sItems[1]); double dHigh = double.Parse(sItems[2]); double dLow = double.Parse(sItems[3]); double dClose = double.Parse(sItems[4]); double dVolume = double.Parse(sItems[5]); cMSeries.AddSeries(cTime, dOpen, dHigh, dLow, dClose, dVolume, false); } cDSeries = CreateSeries(sSymbolId, EResolution.Day); cMSeries.Merge(cDSeries); FileAdapter cAdapter = new FileAdapter(Settings.GlobalSettings.Settings.DataPath, true); cAdapter.Write(cMSeries); cAdapter.Write(cDSeries); cMSeries.Dispose(); cDSeries.Dispose(); } }
private SeriesSymbolDataRand InternalGetSeries(InstrumentDataRequest dataRequest, bool useCache) { SeriesStorage cStorage = null; string sLSymbolId = dataRequest.Symbol.ToLower(); lock (__cStorages) { if (!__cStorages.TryGetValue(sLSymbolId, out cStorage)) { cStorage = new SeriesStorage(16); __cStorages.Add(sLSymbolId, cStorage); } } SeriesSymbolDataRand cSeriesRand = null; int iTotalSeconds = dataRequest.Resolution.TotalSeconds; if (useCache) { //是否使用快取 lock (cStorage) { //需要 lock 這個區塊(避免非同步讀取資料時發生問題) int iBaseSeconds = (iTotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? Resolution.MIN_BASE_TOTALSECONDS : Resolution.MAX_BASE_TOTALSECONDS; SeriesSymbolData cSeries = cStorage.GetSeries(iTotalSeconds); if (cSeries == null) { cSeries = cStorage.GetSeries(iBaseSeconds); if (cSeries == null) { DataAdapter cAdapter = LoadAdapter(ref dataRequest); cSeries = cAdapter.Series; cStorage.Add(cSeries); } } if (iBaseSeconds == iTotalSeconds) { dataRequest = cSeries.DataRequest; goto exit; } else { cSeries = cSeries.CreateSeries(dataRequest); //利用基礎周期建立其他的資料周期 cStorage.Add(cSeries); //加入至 SeriesStorage } dataRequest.Resolution = cSeries.DataRequest.Resolution; //將目標的週期結構更新至傳入的 InstrumentDataRequest 週期結構 DataRequestEvent cRequestEvent = new DataRequestEvent(dataRequest); cSeries.OnRequest(cRequestEvent); //如果已經存在則請求使用者需要的歷史資料區間(請求方法會檢查目前已下載的歷史資料區間是否足夠, 如果使用者需要的歷史資料區間比較大會向伺服器請求) dataRequest.Range.Count = cRequestEvent.Count; //將請求後的正確數量傳入至結構內 exit: cSeriesRand = new SeriesSymbolDataRand(cSeries, dataRequest); } } else { DataAdapter cAdapter = LoadAdapter(ref dataRequest, false); //重新建立新的基礎週期序列資料(不使用快取, 不保存至快取內, 使用完畢之後立即 Dispose) SeriesSymbolData cSeries = cAdapter.Series; //取得新的基礎周期序列資料 int iBaseSeconds = (iTotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? Resolution.MIN_BASE_TOTALSECONDS : Resolution.MAX_BASE_TOTALSECONDS; if (iBaseSeconds == iTotalSeconds) { dataRequest = cSeries.DataRequest; } else { SeriesSymbolData cTargetSeries = cSeries.CreateSeries(dataRequest); //使用 InstrumentDataRequest 建立新的其他週期序列資料 cSeries.Merge(cTargetSeries); //將基礎周期序列資料合併至新的其他週期序列資料 cSeries.Dispose(); //釋放基礎周期序列資料 cSeries = cTargetSeries; } cSeriesRand = new SeriesSymbolDataRand(cSeries, dataRequest); cStorage.Add(cSeries, true); //保存序列資料(存放在 SeriesStorage 內的序列資料才會自動合併最新的即時資訊報價) cAdapter.Dispose(); //釋放資料配置者類別 } return cSeriesRand; }