/// <summary> /// 將設定檔轉換為 InstrumentDataRequest 列表的格式 /// </summary> /// <param name="requests">RequestSetting 陣列</param> /// <returns>返回值: InstrumentDataRequest 列表</returns> public static List<InstrumentDataRequest> Convert(RequestSetting[] requests) { List<InstrumentDataRequest> cResult = new List<InstrumentDataRequest>(); foreach (RequestSetting cRequest in requests) { string[] sPeriods = cRequest.DataPeriod.Split(','); int iPeriodSize = int.Parse(sPeriods[0]); EResolution cResolution = (EResolution) Enum.Parse(typeof(EResolution), sPeriods[1]); InstrumentDataRequest cDataRequest = new InstrumentDataRequest() { Exchange = cRequest.Exchange, DataFeed = cRequest.DataFeed, Resolution = new Resolution(cResolution, iPeriodSize), Symbol = cRequest.SymbolId }; string[] sParams = cRequest.Range.Split(','); string sMode = sParams[0]; string[] sArgs = sParams[1].Split(';'); switch (sMode) { case "barsBack": cDataRequest.Range = DataRequest.CreateBarsBack(DateTime.Parse(sArgs[0]), int.Parse(sArgs[1])); break; case "daysBack": cDataRequest.Range = DataRequest.CreateDaysBack(DateTime.Parse(sArgs[0]), int.Parse(sArgs[1])); break; case "fromTo": cDataRequest.Range = DataRequest.CreateFromTo(DateTime.Parse(sArgs[0]), DateTime.Parse(sArgs[1])); break; } cResult.Add(cDataRequest); } return cResult; }
internal DataAdapter(InstrumentDataRequest request) { __cCookies = new CookieContainer(); int iCount = request.Range.Count; __cSeries = new SeriesSymbolData(request); __cSeries.onRequest += SeriesSymbolData_onRequest; __cSeries.OnRequest(new DataRequestEvent(iCount, iCount, 1)); //請求歷史資料 __cSeries.MergeTicks(); //合併即時Tick資訊 }
/// <summary> /// 建立指定的時間週期 SeriesSymbolData 列表 /// </summary> /// <param name="dataRequest">InstrumentDataRequest 類別</param> /// <returns>返回值: SeriesSymbolData 類別</returns> internal SeriesSymbolData Create(InstrumentDataRequest dataRequest) { int iTotalSeconds = dataRequest.Resolution.TotalSeconds; SeriesSymbolData cBaseSeries = GetSeries(((iTotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? Resolution.MIN_BASE_TOTALSECONDS : Resolution.MAX_BASE_TOTALSECONDS)); SeriesSymbolData cTargetSeries = cBaseSeries.CreateSeries(dataRequest); this.Add(cTargetSeries); int iCount = dataRequest.Range.Count; cTargetSeries.OnRequest(new DataRequestEvent(iCount, iCount, cTargetSeries.DataRequest.Resolution.Rate)); return cTargetSeries; }
/// <summary> /// 取得開高低收資訊列表 /// </summary> /// <param name="dataRequest">資料請求結構</param> /// <returns>返回值: SeriesSymbolData 類別</returns> public SeriesSymbolData GetSeries(InstrumentDataRequest dataRequest) { SeriesStorage cStorage = null; string sLSymbolId = dataRequest.Symbol.ToLower(); lock (__cStorages) { if (!__cStorages.TryGetValue(sLSymbolId, out cStorage)) { DataAdapter cAdapter = LoadAdapter(dataRequest); if (cAdapter.Series == null) { return null; //表示沒有檔案 } else { cStorage = new SeriesStorage(16); cStorage.Add(cAdapter.Series); __cStorages.Add(sLSymbolId, cStorage); } } } int iTotalSeconds = dataRequest.Resolution.TotalSeconds; SeriesSymbolData cSeries = cStorage.GetSeries(iTotalSeconds); if (cSeries == null) { int iBaseSeconds = (iTotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) ? Resolution.MIN_BASE_TOTALSECONDS : Resolution.MAX_BASE_TOTALSECONDS; cSeries = cStorage.GetSeries(iBaseSeconds); if (cSeries == null) { DataAdapter cAdapter = LoadAdapter(dataRequest); if (cAdapter.Series != null) { cSeries = cAdapter.Series; cStorage.Add(cSeries); } } if (iBaseSeconds != iTotalSeconds) { cSeries = cStorage.Create(dataRequest); } } return cSeries; }
private DataAdapter LoadAdapter(InstrumentDataRequest dataRequest) { dataRequest.Resolution = Resolution.GetBaseValue(dataRequest.Resolution); DataAdapter cAdapter = new DataAdapter(dataRequest); SeriesSymbolData cSeries = cAdapter.Series; if (cSeries == null) { return null; } if (!cSeries.DataRequest.Range.IsAlreadyRequestAllData) { cAdapter.onCompleted += DataAdapter_onCompleted; string sLSymbolId = dataRequest.Symbol.ToLower(); int iTotalSeconds = cSeries.DataRequest.Resolution.TotalSeconds; if (iTotalSeconds < Resolution.MAX_BASE_TOTALSECONDS) { lock (__cMinBases) { if (!__cMinBases.ContainsKey(sLSymbolId)) { __cMinBases.Add(sLSymbolId, cAdapter); } } } else { lock (__cDayBases) { if (!__cDayBases.ContainsKey(sLSymbolId)) { __cDayBases.Add(sLSymbolId, cAdapter); } } } } return cAdapter; }
internal SeriesSymbolData CreateSeries(InstrumentDataRequest dataRequest) { return new SeriesSymbolData(dataRequest, __cSettings); }
internal SeriesSymbolData(InstrumentDataRequest dataRequest, InstrumentSettings settings = null) { this.Indexer = new SeriesIndexer(); __cDataRequest = dataRequest; __cSettings = ((settings == null) ? new InstrumentSettings(ref __cDataRequest) : settings.Create(ref __cDataRequest)); __cUpdateTime = DateTime.UtcNow.AddHours(__cSettings.TimeZone); SessionObject cSession = __cSettings.GetSessionFromToday(); __iRealtimeCount = (int) ((cSession.GetCloseTimeForDaylight() - cSession.GetStartTimeForDaylight()).TotalSeconds / dataRequest.Resolution.TotalSeconds + 1); Indexer.HistoryIndex = 0; Indexer.RealtimeIndex = -1; __cOpens = new Series<double>(__iRealtimeCount); __cOpens.onRequest += Series_onRequest; __cHighs = new Series<double>(__iRealtimeCount); __cHighs.onRequest += Series_onRequest; __cLows = new Series<double>(__iRealtimeCount); __cLows.onRequest += Series_onRequest; __cCloses = new Series<double>(__iRealtimeCount); __cCloses.onRequest += Series_onRequest; __cTimes = new Series<DateTime>(__iRealtimeCount); __cTimes.onRequest += Series_onRequest; __cVolumes = new Series<double>(__iRealtimeCount); __cVolumes.onRequest += Series_onRequest; __cDataRequest.Range.Count = 0; //將資料筆數設定為0(因為一開始沒有請求任何資訊) }
/// <summary> /// 根據設定資訊調整 InstrumentDataRequest 設定並回傳目前的設定類別 /// </summary> /// <param name="request">InstrumentDataRequest類別</param> /// <returns>回傳值: 目前的 InstrumentSettings類別</returns> public InstrumentSettings Create(ref InstrumentDataRequest request) { SessionObject cSession = GetSessionFromToday(); DataRequestType cRequestType = request.Range.RequestType; request.Resolution.CalculateRate(cSession.GetStartTimeForDaylight(), cSession.GetCloseTimeForDaylight(), __cProperty.Sessions.Count); if (cRequestType == DataRequestType.DaysBack || cRequestType == DataRequestType.FromTo) { request.Range.Count = request.Resolution.ConvertDaysToBars(request.Range.Count); } return this; }
/// <summary> /// 建構子 /// </summary> /// <param name="request">InstrumentDataRequest類別</param> public InstrumentSettings(ref InstrumentDataRequest request) { AbstractExchange cExchange = ProductManager.Manager.GetExchange(request.Exchange); __sSymbolId = request.Symbol; __sExchange = request.Exchange; __sDataSource = request.DataFeed; __dTimeZone = cExchange.TimeZone; __cProduct = cExchange.GetProduct(__sSymbolId); __cProperty = cExchange.GetProperty(__sSymbolId, __sDataSource); if (__cProperty != null) { __cOptionType = __cProperty.GetCallOrPut(__cProduct); //取得選擇權 Call or Put 類型 __dStrikePrice = __cProperty.GetStrikePrice(__cProduct); //取得選擇權履約價格 //取得合約到期日索引值 IContractTime cContractTime = __cProperty.ContractRule as IContractTime; if (cContractTime != null) { __iContractIndex = cContractTime.GetContractIndex(__sSymbolId); } } this.Create(ref request); __cResolution = request.Resolution; }