/// <summary> /// 插入資訊 /// </summary> /// <param name="series">SeriesSymbolData 類別</param> /// <param name="date">欲插入的日期(資料會被插入至該指定的日期前)</param> internal void Insert(SeriesSymbolData series, DateTime date) { try { string sFile = string.Format("{0}\\{1}\\{2}", __sPath, (series.DataRequest.Resolution.TotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? "mins" : "days", series.DataRequest.Symbol); using (FileStream cStream = new FileStream(sFile, (__bCreate) ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { bool bFind = false; if (!__bCreate) { long lCount = cStream.Length / MAX_BLOCK_SIZE; bFind = FileSearchUtil.BinaryNearSearch(cStream, lCount, MAX_BLOCK_SIZE, date); } if (bFind) { long lCurrentP = cStream.Position; //先保留後面的資料以便插入時不會被覆蓋 long lRevSize = cStream.Length - lCurrentP; ZBuffer cTemp = new ZBuffer((int)lRevSize); cStream.Read(cTemp.Data, 0, (int)lRevSize); cTemp.Length = (int)lRevSize; cStream.SetLength(lCurrentP + 1); cStream.Position = lCurrentP; //移動置固定位置 ZBuffer cBuffer = new ZBuffer(64); int iHistoryIndex = series.Indexer.HistoryIndex; int iCount = series.Count; for (int i = 0; i < iCount; i++) { int iIndex = iHistoryIndex + i; cBuffer.Length = 0; cBuffer.Add(series.Time[iIndex]); cBuffer.Add(series.Open[iIndex]); cBuffer.Add(series.High[iIndex]); cBuffer.Add(series.Low[iIndex]); cBuffer.Add(series.Close[iIndex]); cBuffer.Add(series.Volume[iIndex]); cStream.Write(cBuffer.Data, 0, cBuffer.Length); } cStream.Write(cTemp.Data, 0, cTemp.Length); //再將後面的資料合併 } } if (logger.IsInfoEnabled) { logger.InfoFormat("[FileWrite] {0} insert completed... count={1}", sFile, series.Count); } } catch (Exception __errExcep) { if (logger.IsErrorEnabled) { logger.ErrorFormat("[FileWrite] '{0}' insert error...", series.DataRequest.Symbol, series.Count); } if (logger.IsErrorEnabled) { logger.ErrorFormat("{0}/r/n{1}", __errExcep.Message, __errExcep.StackTrace); } } }
private void Dispose(bool disposing) { if (!this.__bDisposed) { __bDisposed = true; if (disposing) { __cExchange = null; __cSymbolBuffer = null; } } }
/// <summary> /// 讀取Response資料 /// </summary> /// <returns>返回值:網頁內容</returns> internal string ReadText() { string sText = string.Empty; ZBuffer cBuffer = this.Read(); if (cBuffer != null) { sText = Encoding.GetEncoding(this.Charset).GetString(cBuffer.Data, 0, cBuffer.Length); } return(sText); }
/// <summary> /// 請求商品歷史資料 /// </summary> /// <param name="e">資料請求事件</param> protected internal override void Request(DataRequestEvent e) { InstrumentDataRequest cDataRequest = this.DataRequest; DateTime[] cRanges = e.Ranges; ZRequest cRequest = new ZRequest(); cRequest.Method = "POST"; cRequest.Url = __sHttpUrl; cRequest.Parameters = string.Format("exchange={0}&symbolId={1}&timeFrame={2}&position={3}&startDate={4}&endDate={5}&count={6}", cDataRequest.Exchange, cDataRequest.Symbol, cDataRequest.Resolution.TotalSeconds, this.Position, cRanges[0].ToString("yyyy-MM-dd"), cRanges[1].ToString("yyyy-MM-dd"), e.Count); cRequest.CookieContainer = __cCookies; int iRet = cRequest.Request(); if (iRet == 0) { ZReader cReader = new ZReader(cRequest.Response); e.Result = cReader.Result; if (cReader.Result == 0) { int iCount = e.Count = cReader.Count; this.AdjustSize(iCount); ZBuffer cBuffer = cReader.Read(); while (--iCount >= 0) { cBuffer.Position = iCount * DATA_BLOCK_SIZE; DateTime cDate = cBuffer.GetDateTime(); double dOpen = cBuffer.GetDouble(); double dHigh = cBuffer.GetDouble(); double dLow = cBuffer.GetDouble(); double dClose = cBuffer.GetDouble(); double dVolume = cBuffer.GetDouble(); this.AddSeries(cDate, dOpen, dHigh, dLow, dClose, dVolume); } this.Position = cReader.Position; e.Ranges[0] = cReader.BeginDate; e.Ranges[1] = cReader.EndDate; if (this.Position == 0) { e.IsAlreadyRequestAllData = true; } } } }
/// <summary> /// 讀取Response資料 /// </summary> /// <returns>返回值:ZBuffer類別(null=讀取失敗)</returns> internal ZBuffer Read() { if (__cResponse == null) { return(null); } try { int iLength = 0; Stream cStream = __cResponse.GetResponseStream(); if (__cResponse.ContentEncoding.Equals("gzip")) { if (logger.IsDebugEnabled) { logger.Debug("ZReader.Read: 封包資料有使用gzip壓縮編碼,建立GZip解碼物件..."); } cStream = new GZipStream(cStream, CompressionMode.Decompress); } //建立緩衝區 byte[] cTemps = new byte[8192]; ZBuffer cBuffer = new ZBuffer(__iDataSize + 2); do { iLength = cStream.Read(cTemps, 0, 8192); if (iLength > 0) { cBuffer.Add(cTemps, 0, iLength); } } while (iLength != 0); cStream.Close(); //關閉資料流 cStream.Dispose(); //釋放資源 __cResponse = null; //釋放資源 if (logger.IsDebugEnabled) { logger.DebugFormat("ZReader.Read: dataLength={0}", cBuffer.Length); } return(cBuffer); } catch (System.Exception __errExcep) { logger.ErrorFormat("{0}\r\n{1}", __errExcep.StackTrace, __errExcep.Message); return(null); } }
internal void Write(SeriesSymbolData series) { try { string sFile = string.Format("{0}\\{1}\\{2}", __sPath, (series.DataRequest.Resolution.TotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? "mins" : "days", series.DataRequest.Symbol); using (FileStream cStream = new FileStream(sFile, (__bCreate) ? FileMode.Create : FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { if (!__bCreate) { long lCount = cStream.Length / MAX_BLOCK_SIZE; if (lCount > 0) { FileSearchUtil.BinarySearch(cStream, lCount, MAX_BLOCK_SIZE, series.Time[0]); } } ZBuffer cBuffer = new ZBuffer(64); int iHistoryIndex = series.Indexer.HistoryIndex; int iCount = series.Count; for (int i = 0; i < iCount; i++) { int iIndex = iHistoryIndex + i; cBuffer.Length = 0; cBuffer.Add(series.Time[iIndex]); cBuffer.Add(series.Open[iIndex]); cBuffer.Add(series.High[iIndex]); cBuffer.Add(series.Low[iIndex]); cBuffer.Add(series.Close[iIndex]); cBuffer.Add(series.Volume[iIndex]); cStream.Write(cBuffer.Data, 0, cBuffer.Length); } } if (logger.IsInfoEnabled) { logger.InfoFormat("[FileWrite] {0} write completed... count={1}", sFile, series.Count); } } catch (Exception __errExcep) { if (logger.IsErrorEnabled) { logger.ErrorFormat("[FileWrite] '{0}' write error...", series.DataRequest.Symbol, series.Count); } if (logger.IsErrorEnabled) { logger.ErrorFormat("{0}/r/n{1}", __errExcep.Message, __errExcep.StackTrace); } } }
/// <summary> /// 讀取Response資料 /// </summary> /// <returns>返回值:ZBuffer類別(null=讀取失敗)</returns> internal ZBuffer Read() { if (__cResponse == null) { return null; } try { int iLength = 0; Stream cStream = __cResponse.GetResponseStream(); if (__cResponse.ContentEncoding.Equals("gzip")) { if (logger.IsDebugEnabled) logger.Debug("ZReader.Read: 封包資料有使用gzip壓縮編碼,建立GZip解碼物件..."); cStream = new GZipStream(cStream, CompressionMode.Decompress); } //建立緩衝區 byte[] cTemps = new byte[8192]; ZBuffer cBuffer = new ZBuffer(__iDataSize + 2); do { iLength = cStream.Read(cTemps, 0, 8192); if (iLength > 0) { cBuffer.Add(cTemps, 0, iLength); } } while (iLength != 0); cStream.Close(); //關閉資料流 cStream.Dispose(); //釋放資源 __cResponse = null; //釋放資源 if (logger.IsDebugEnabled) logger.DebugFormat("ZReader.Read: dataLength={0}", cBuffer.Length); return cBuffer; } catch (System.Exception __errExcep) { logger.ErrorFormat("{0}\r\n{1}", __errExcep.StackTrace, __errExcep.Message); return null; } }
/// <summary> /// 加入新封包 /// </summary> /// <param name="buffer">來源ZBuffer封包</param> /// <returns>返回值:ture=成功 false=失敗</returns> public bool Add(ZBuffer buffer) { return(Add(buffer.Data, 0, buffer.Length)); }
/// <summary> /// ZBuffer建構子 /// </summary> /// <param name="buffer">ZBuffer緩衝區</param> public ZBuffer(ZBuffer buffer) { __iLength = buffer.Length; __iSize = buffer.Size; this.Data = buffer.Data; }
/// <summary> /// 加入新封包 /// </summary> /// <param name="buffer">來源ZBuffer封包</param> /// <returns>返回值:ture=成功 false=失敗</returns> public bool Add(ZBuffer buffer) { return Add(buffer.Data, 0, buffer.Length); }
internal MitakeSourceAdapter() { __cSymbolBuffer = new ZBuffer(1048576 * 5); __cExchange = ProductManager.Manager.GetExchange("TWSE"); }