public OpenInterestData(DateTime date, ZBuffer buffer) { __cDate = date.Date; __cInterest = new Dictionary<string, int>(128); while (buffer.Position < buffer.Length) { __cInterest.Add(buffer.GetString(), buffer.GetInt32()); } }
private static ZBuffer Decode(string content) { ZBuffer cBuffer = null; string[] sDatas = content.Split(new char[] { '\r', '\n' }); if (sDatas[0].Length == 0) { return cBuffer; } int iLength = sDatas.Length; if (iLength > 1) { cBuffer = new ZBuffer(iLength * 20); int iIndexM = -1, iIndexW = -1; bool bWeek = false; string sSymbolId = null; string sSymbol = string.Empty; string sContract = string.Empty; for (int i = 1; i < iLength; i++) { string sData = sDatas[i]; if (sData.Length > 0) { string[] sItems = sData.Split(','); int iInterest = int.Parse(sItems[11]); if (iInterest > 0) { bWeek = sItems[2][6] == 'W'; float fTarget = float.Parse(sItems[3]); char chCallOrPut = sItems[4].Equals("買權") ? 'C' : 'P'; if (!sSymbol.Equals(sItems[1])) { iIndexW = -1; iIndexM = -1; sSymbol = sItems[1]; sContract = string.Empty; } if (!sContract.Equals(sItems[2])) { if (bWeek) { ++iIndexW; } else { ++iIndexM; } sContract = sItems[2]; } sSymbolId = string.Format("{0}{1}{2}{3}", (bWeek) ? "TXW" : sSymbol, (bWeek) ? iIndexW : iIndexM, chCallOrPut, fTarget); cBuffer.Add(sSymbolId); cBuffer.Add(iInterest); } } } } return cBuffer; }
public static OpenInterestData Load(DateTime date) { OpenInterestData cData = null; string sFile = string.Format("{0}OI\\{1}.dat", GlobalSettings.Paths.DatabasePath, date.ToString("yyyyMMdd")); if (File.Exists(sFile)) { byte[] bArray = File.ReadAllBytes(sFile); ZBuffer cBuffer = new ZBuffer(); cBuffer.Data = bArray; cBuffer.Length = bArray.Length; cData = new OpenInterestData(date, cBuffer); } return cData; }
public static ForeignInvestmentData Create(ZBuffer buffer) { ForeignInvestmentData cData = new ForeignInvestmentData(); cData.自營商多方交易口數 = buffer.GetInt32(); cData.自營商空方交易口數 = buffer.GetInt32(); cData.自營商多方未平倉口數 = buffer.GetInt32(); cData.自營商空方未平倉口數 = buffer.GetInt32(); cData.投信多方交易口數 = buffer.GetInt32(); cData.投信空方交易口數 = buffer.GetInt32(); cData.投信多方未平倉口數 = buffer.GetInt32(); cData.投信空方未平倉口數 = buffer.GetInt32(); cData.外資多方交易口數 = buffer.GetInt32(); cData.外資空方交易口數 = buffer.GetInt32(); cData.外資多方未平倉口數 = buffer.GetInt32(); cData.外資空方未平倉口數 = buffer.GetInt32(); return cData; }
public static ForeignInvestmentGroup Load(DateTime date) { ForeignInvestmentGroup cData = new ForeignInvestmentGroup(); string sFile = string.Format("{0}FI\\{1}.dat", GlobalSettings.Paths.DatabasePath, date.ToString("yyyyMMdd")); if (File.Exists(sFile)) { byte[] bArray = File.ReadAllBytes(sFile); ZBuffer cBuffer = new ZBuffer(); cBuffer.Data = bArray; cBuffer.Length = bArray.Length; for (int i = 0; i < 4; i++) { string sSymbolId = cBuffer.GetString(); cData.Add(sSymbolId, ForeignInvestmentData.Create(cBuffer)); } } return cData; }
/// <summary> /// 讀取Response資料 /// </summary> /// <returns>返回值:ZBuffer類別(null=讀取失敗)</returns> public 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("封包資料有使用gzip壓縮編碼,建立GZip解碼物件..."); cStream = new GZipStream(cStream, CompressionMode.Decompress); } //建立緩衝區 byte[] cTemps = new byte[8192]; MemoryStream cMemoryStream = new MemoryStream(32 * 1024); do { iLength = cStream.Read(cTemps, 0, 8192); cMemoryStream.Write(cTemps, 0, iLength); } while (iLength != 0); cStream.Close(); //關閉資料流 cStream.Dispose(); //釋放資源 int iSize = (int)cMemoryStream.Length; ZBuffer cBuffer = new ZBuffer(iSize); cBuffer.Length = iSize; cMemoryStream.Position = 0; cMemoryStream.Read(cBuffer.Data, 0, iSize); cMemoryStream.Close(); cMemoryStream.Dispose(); __cResponse = null; //釋放資源 if (logger.IsDebugEnabled) logger.DebugFormat("[Response]資料資訊 Length:{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="stream">檔案串流類別</param> /// <param name="count">資料最大 Block 區塊個數(以 blockSize 為一個單位區塊)</param> /// <param name="blockSize">Block 區塊大小</param> /// <param name="time">要搜尋的時間</param> /// <returns>返回值:true=逼近, false=已經有目標</returns> internal static bool BinaryNearSearch(FileStream stream, long count, int blockSize, DateTime time) { if (count == 0) { return(false); } byte[] bDate = new byte[4]; long lLeft = 0, lMiddle = 0, lRight = count; ZBuffer cBuffer = new ZBuffer(16); while (lLeft <= lRight) { lMiddle = (lLeft + lRight) / 2; cBuffer.Position = 0; stream.Position = lMiddle * blockSize; stream.Read(cBuffer.Data, 0, 8); DateTime cTime = cBuffer.GetDateTime(); int iCompare = cTime.CompareTo(time); if (iCompare == 0) { stream.Seek(-8, SeekOrigin.Current); //因為已經讀取的時間日期, 所以 Position 會被移動 8bytes 要在移動回去至日期處, 才是資料 Block 的起點 return(false); } else if (iCompare < 0) { lLeft = lMiddle + 1; } else { lRight = lMiddle - 1; } } if (lLeft > count) { --lLeft; } stream.Position = lLeft * blockSize; return(true); }
internal static void Save(string symbolId, bool isMinute, string file, DateTime targetDate) { try { string sFile = string.Format("{0}\\{1}\\{2}", Settings.GlobalSettings.Settings.DataPath, (isMinute) ? "mins" : "days", symbolId); if (File.Exists(sFile)) { DateTime cEndDate = targetDate.AddSeconds(86400); using (FileStream cStream = new FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { ZBuffer cBuffer = new ZBuffer(64); while (cStream.Read(cBuffer.Data, 0, FileAdapter.MAX_BLOCK_SIZE) > 0) { cBuffer.Position = 0; cBuffer.Length = FileAdapter.MAX_BLOCK_SIZE; DateTime cTime = cBuffer.GetDateTime(); if (cTime >= targetDate && cTime <= cEndDate) { double dOpen = cBuffer.GetDouble(); double dHigh = cBuffer.GetDouble(); double dLow = cBuffer.GetDouble(); double dClose = cBuffer.GetDouble(); double dVolume = cBuffer.GetDouble(); File.AppendAllText(file, string.Format("{0},{1},{2},{3},{4},{5}\r\n", cTime.ToString("yyyy-MM-dd HH:mm:ss"), dOpen, dHigh, dLow, dClose, dVolume), Encoding.UTF8); System.Console.WriteLine("{0} {1,8:0.00} {2,8:0.00} {3,8:0.00} {4,8:0.00} {5,7}", cTime.ToString("yyyyMMdd HHmmss"), dOpen, dHigh, dLow, dClose, dVolume); if (cTime >= cEndDate) { break; } } } } } } catch (Exception __errExcep) { if (logger.IsErrorEnabled) { logger.ErrorFormat("{0}/r/n{1}", __errExcep.Message, __errExcep.StackTrace); } } }
internal static void SearchNextDate(FileStream stream, int blockSize, DateTime time) { byte[] bDate = new byte[4]; ZBuffer cBuffer = new ZBuffer(16); int iSize = 0; long lCurrent = stream.Position / blockSize; while ((iSize = stream.Read(cBuffer.Data, 0, 8)) > 0) { cBuffer.Position = 0; cBuffer.Length = iSize; DateTime cTime = cBuffer.GetDateTime(); if ((cTime - time).TotalSeconds >= 86400) { stream.Seek(-8, SeekOrigin.Current); //因為已經讀取的時間日期, 所以 Position 會被移動 8bytes 要在移動回去至日期處, 才是資料 Block 的起點 break; } ++lCurrent; stream.Position = lCurrent * blockSize; } }
private static ZBuffer Decode(string content) { ZBuffer cBuffer = null; string[] sDatas = content.Split(new char[] { '\r', '\n' }); if (sDatas[0][0] == '<') { return cBuffer; } int iLength = sDatas.Length; if (iLength > 1) { int iCount = 0; cBuffer = new ZBuffer(iLength * 20); for (int i = 1; i < iLength; i++) { string sData = sDatas[i]; if (sData.Length > 0) { string sSymbolId = null; string[] sItems = sData.Split(','); if (__cSymbols.TryGetValue(sItems[1], out sSymbolId)) { if (iCount == 0) { cBuffer.Add(sSymbolId); } cBuffer.Add(int.Parse(sItems[3])); //多方交易量 cBuffer.Add(int.Parse(sItems[5])); //空方交易量 cBuffer.Add(int.Parse(sItems[9])); //多方留倉口 cBuffer.Add(int.Parse(sItems[11])); //空方留倉口 if (++iCount == 3) { iCount = 0; } } } } } return cBuffer; }
private static void Write(string file, ZBuffer buffer) { using (FileStream cStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.Read)) { cStream.Write(buffer.Data, 0, buffer.Length); } }
/// <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); }