internal Queue <DateTime> CreateRealtimePeriods(DateTime today)
        {
            DateTime cFrom = today;

            AbstractExchange        cExchange     = ProductManager.Manager.GetExchange(__cDataRequest.Exchange);
            AbstractProductProperty cProperty     = cExchange.GetProperty(__cDataRequest.Symbol);
            IContractTime           cContractTime = cProperty.ContractRule as IContractTime;

            DateTime cExpiration = DateTime.MinValue;

            if (cContractTime == null)
            {
                cExpiration = today.AddDays(2);
            }
            else
            {
                ContractTime cContract = cContractTime.GetContractTime(today);
                cExpiration = cContract.MaturityDate;
            }

            ESymbolCategory cCategory = __cSettings.ASymbolInfo2.Category;

            if ((cCategory == ESymbolCategory.Future || cCategory == ESymbolCategory.IndexOption || cCategory == ESymbolCategory.StockOption || cCategory == ESymbolCategory.FutureOption || cCategory == ESymbolCategory.FutureRolover) && ConvertParameter.強制今日為期權到期日)
            {
                cExpiration = new DateTime(today.Year, today.Month, today.Day, cExpiration.Hour, cExpiration.Minute, cExpiration.Second);
            }

            if (__cDataRequest.Resolution.TotalSeconds == Resolution.MAX_BASE_TOTALSECONDS)
            {
                cFrom = today.AddDays(-1);
            }
            __cTimeQueue = __cDataRequest.Resolution.CalculateRealtimePeriods(this.LastBarTime, today, cExpiration);
            return(__cTimeQueue);
        }
		/// <summary>
		///   從傳入的日期來設定合約到期日
		/// </summary>
		/// <param name="time">日期時間結構</param>
		public void SetExpirationFromTime(DateTime time) {
			IContractTime cContractTime = __cProperty.ContractRule as IContractTime;
			if (cContractTime == null) {
				__cExpiration = time.AddSeconds(86400);  //如果沒有設定預設為明天到期(表示沒設定到期日, 永遠都不會到期)
			} else {
				__cExpiration = cContractTime.GetContractTime(time, __iContractIndex).MaturityDate;
			}
		}
 protected override void UpdateContractTime(DateTime date)
 {
     if (this.ContractRule != null)
     {
         IContractTime cContractTime = this.ContractRule as IContractTime;
         cContractTime.UpdateContractTime(date);
     }
 }
		private static int GetContractTimeIndex(string commodityId, int year, int month, int week = 0) {
			int iRet = -1;
			
			AbstractExchange cExchange = ProductManager.Manager.GetExchange(__sExchangeName);
			AbstractProductProperty cProperty = cExchange.GetProperty(commodityId, DataSource);
			if (cProperty != null) {
				int iYear = __cToday.Year;
				int iMod = iYear % 10;
				if (iMod == 9 && year == 0) {
					++iYear;
				} else {
					iYear = (iYear - iMod) + year;
				}
				
				int iDay = __cToday.Day;
				int iDays = DateTime.DaysInMonth(iYear, month);  //取得該月份的最大天數

				DateTime cContract = DateTime.MinValue;
				if (commodityId[2] == 'W') {  //如果是週商品(期權)
					int iFirstWeek = (int) new DateTime(iYear, month, 1).DayOfWeek;
					int iFirstWedDay = (7 - iFirstWeek > 3) ? 4 - iFirstWeek : 11 - iFirstWeek;
					int iWeekDay = iFirstWedDay + (week - 1) * 7;
					if (iDay > iWeekDay) {  //如果今日日期 > 週商品結算日
						return -1;  //返回 -1 (表示此週商品已經過期)
					}
					
					iWeekDay = (iWeekDay > iDays) ? iDays : iDay;
					cContract = new DateTime(iYear, month, iWeekDay);
				} else {
					iDay = ((iDay > iDays) ? iDays : iDay);  //如果傳進來的月份為商品代號上的月份代碼轉換的, 就必須注意是否超過此月份的最大天數, 如果超過就需要修正
					cContract = new DateTime(iYear, month, iDay);
				}

				IContractTime cRule = cProperty.ContractRule as IContractTime;
				if (cRule != null) {
					ContractTime cContractTimes = cRule.GetContractTime(cContract);
					iRet = cContractTimes.Id;
				}
			}
			return iRet;
		}
		/// <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);

			//如果沒有商品資訊就使用 request.Symbol 第一個字元當作搜尋條件來取代
			if (__cProduct == null) {
				List<Product> cProducts = cExchange.SearchProducts(new string(__sSymbolId[0], 1), false);
				foreach (Product cProduct in cProducts) {
					AbstractProductProperty cProperty = cExchange.GetProperty(cProduct.SymbolId);
					if (cProperty != null) {
						__cProduct = cProduct;
						__cProperty = cProperty;
						break;
					}
				}
			}

			__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;
		}
示例#6
0
        internal static void Convert(DateTime date, bool isDownload = true)
        {
            string[] sData = LoadRPT(date, isDownload);
            if (sData == null)
            {
                return;
            }

            double           dVolume = 0;
            bool             bConvert = false;
            string           sEDate = string.Empty;
            string           sOSymbolId = string.Empty;
            string           sDate = date.ToString("yyyyMMdd");
            SeriesSymbolData cMSeries = null, cDSeries = null;

            if (logger.IsInfoEnabled)
            {
                logger.Info("[Convert] 開始轉換期交所的期貨資訊...");
            }

            int iLength = sData.Length;

            for (int i = 1; i < iLength; i++)
            {
                string[] sItems = sData[i].Split(',');
                if (sItems.Length == 9)
                {
                    string sFutureDate = sItems[0].Trim();
                    if (!sFutureDate.Equals(sDate))                        //檢查日期是否為欲轉換的日期
                    {
                        continue;
                    }

                    string sSymbolId = sItems[1].Trim();
                    if (!sSymbolId.Equals(sOSymbolId))
                    {
                        if (bConvert)
                        {
                            FileAdapter cAdapter = new FileAdapter(Settings.GlobalSettings.Settings.DataPath, false);
                            cAdapter.Write(cMSeries);
                            cAdapter.Write(cDSeries);
                        }

                        dVolume    = 0;
                        sOSymbolId = sSymbolId;

                        string sTWSymbolId = null;
                        bConvert = cTargetSymbols.TryGetValue(sSymbolId, out sTWSymbolId);
                        if (bConvert)
                        {
                            cMSeries = CreateSeries(sTWSymbolId, EResolution.Minute, date);
                            cDSeries = CreateSeries(sTWSymbolId, EResolution.Day, date);

                            if (ConvertParameter.強制今日為期權到期日)
                            {
                                sEDate = DateTime.Now.Year.ToString() + ConvertParameter.自訂期權合約月份.ToString("0#");
                            }
                            else
                            {
                                AbstractExchange        cExchange     = ProductManager.Manager.GetExchange("TWSE");
                                AbstractProductProperty cProperty     = cExchange.GetProperty(sTWSymbolId);
                                IContractTime           cContractTime = cProperty.ContractRule as IContractTime;
                                ContractTime            cContract     = cContractTime.GetContractTime(date);

                                sEDate = cContract.MaturityDate.ToString("yyyyMM");
                            }
                        }
                    }

                    if (bConvert)
                    {
                        string sEndDate = sItems[2].Trim();
                        if (sEndDate.Length == 6 && sEndDate.Equals(sEDate))
                        {
                            Tick cTick = new Tick();
                            cTick.Time   = DateTimeParser.Parse(sItems[0], sItems[3]);
                            cTick.Price  = double.Parse(sItems[4]);
                            cTick.Single = double.Parse(sItems[5]) / 2;                              //Buy + Sell(需要除以2)
                            dVolume     += cTick.Single;
                            cTick.Volume = dVolume;

                            cMSeries.Merge(cTick);
                            cDSeries.Merge(cTick);
                        }
                    }
                }
            }

            if (bConvert)
            {
                FileAdapter cAdapter = new FileAdapter(Settings.GlobalSettings.Settings.DataPath, false);
                cAdapter.Write(cMSeries);
                cAdapter.Write(cDSeries);
            }
        }