Example #1
0
        /// <summary>
        ///   取得週合約到期日資訊(適用於周選擇權商品)
        /// </summary>
        /// <param name="date">日期</param>
        /// <returns>返回值: 合約到期日資訊</returns>
        internal static ContractTime GetWeekMaturityDate(DateTime date)
        {
            ContractTime cContractTime = new ContractTime();
            DateTime     cMaturity     = GetMonthMaturityDate(date, false).MaturityDate;

            for (int i = 0; i < 7; i++)
            {
                if (date.DayOfWeek == DayOfWeek.Wednesday)
                {
                    int iDay      = date.Day;
                    int iEndDay   = cMaturity.Day;
                    int iStartDay = iEndDay - 6;
                    if (iDay >= iStartDay && iDay <= iEndDay)                        //如果是第三個星期三就跳過這個區間
                    {
                        date = cMaturity.AddSeconds(604800);                         //一次加上一星期的秒數
                    }

                    cContractTime.ContractYear  = date.Year;
                    cContractTime.ContractMonth = date.Month;
                    cContractTime.MaturityDate  = date;
                    break;
                }
                date = date.AddSeconds(86400);                  //一次加上一天 86400 秒
            }
            return(cContractTime);
        }
        public ContractTime GetContractTime(DateTime date, int index = 0)
        {
            ContractTime cContractTime = null;

            date = new DateTime(date.Year, date.Month, date.Day, __iCloseHour, __iCloseMinute, __iCloseSecond);
            int iCount = __cContractTimes.Count;

            for (int i = index; i < iCount; i++)
            {
                ContractTime cContractTemp = __cContractTimes[i];
                double       dTotals       = (cContractTemp.MaturityDate - date).TotalSeconds;
                if (dTotals >= 0 && dTotals <= 604800)
                {
                    cContractTime = cContractTemp;
                    break;
                }
            }

            if (cContractTime == null)
            {
                if (index > 0)
                {
                    date = date.AddSeconds(index * 604800);
                }
                cContractTime = MaturityDateUtil.GetWeekMaturityDate(date);
            }
            return(cContractTime);
        }
        private void CalcContractTime(DateTime today)
        {
            //先計算出現月
            ContractTime cCurrentContractTime = MaturityDateUtil.GetMonthMaturityDate(today, true);

            AddContractTime(0, cCurrentContractTime);

            //由現月計算出其他月份
            int iMonth     = cCurrentContractTime.ContractMonth;
            int iBaseMonth = CalcBaseMonth(iMonth);

            int[] iMonthPeriods = new int[5];
            iMonthPeriods[0] = 1;
            iMonthPeriods[1] = 2;
            for (int i = 0; i < 3; i++)
            {
                iMonthPeriods[i + 2] = (iBaseMonth + (3 * i)) - iMonth;
            }

            //計算其他月份的交易日與到期日
            int iLength = iMonthPeriods.Length;

            for (int i = 0; i < iLength; i++)
            {
                ContractTime cContractTime = AddContractMonths(cCurrentContractTime, iMonthPeriods[i]);
                AddContractTime(i + 1, cContractTime);
            }
        }
Example #4
0
        /// <summary>
        ///   取得合約到期日資訊(適用於月選擇權與期貨商品)
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="isAutoNextMonth">是否自動換月(如果不自動換月,不會超過本月)</param>
        /// <returns>返回值: 合約到期日資訊</returns>
        internal static ContractTime GetMonthMaturityDate(DateTime date, bool isAutoNextMonth)
        {
            int          iYear         = date.Year;
            int          iMonth        = date.Month;
            ContractTime cContractTime = new ContractTime();

            for (int i = 15; i <= 21; i++)
            {
                DateTime cDate = new DateTime(iYear, iMonth, i, date.Hour, date.Minute, date.Second);
                if (cDate.DayOfWeek == DayOfWeek.Wednesday)
                {
                    if (isAutoNextMonth && date > cDate)
                    {
                        i = 14;
                        ++iMonth;
                        if (iMonth > 12)
                        {
                            iMonth = 1;
                            ++iYear;
                        }
                        continue;
                    }

                    cContractTime.ContractYear  = iYear;
                    cContractTime.ContractMonth = iMonth;
                    cContractTime.MaturityDate  = cDate;
                    break;
                }
            }
            return(cContractTime);
        }
        public ContractTime GetContractTime(DateTime date, int index = 0)
        {
            ContractTime cContractTime = null;

            int iYear = date.Year, iMonth = date.Month;
            int iIndex = 0, iKey = iYear * 100 + iMonth;

            if (__cIndexs.TryGetValue(iKey, out iIndex))
            {
                cContractTime = __cContractTimes[(index > 0) ? index : iIndex];
            }
            else
            {
                date          = new DateTime(iYear, iMonth, date.Day, __iCloseHour, __iCloseMinute, __iCloseSecond);
                cContractTime = MaturityDateUtil.GetMonthMaturityDate(date, true);

                if (index > 0)
                {
                    int iCMonth      = cContractTime.ContractMonth;
                    int iBaseMonth   = CalcBaseMonth(iMonth);
                    int iMonthPeriod = (index > 1) ? (iBaseMonth + (3 * (index - 2))) - iCMonth : 1;
                    cContractTime = AddContractMonths(cContractTime, iMonthPeriod);
                }
            }
            return(cContractTime);
        }
        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);
        }
        private void AddContractTime(int id, ContractTime contractTime)
        {
            int iKey = contractTime.ContractYear * 100 + contractTime.ContractMonth;

            if (!__cIndexs.ContainsKey(iKey))
            {
                contractTime.Id = id;

                __cIndexs.Add(iKey, id);
                __cContractTimes.Add(contractTime);
            }
        }
        private ContractTime AddContractMonths(ContractTime contract, int month)
        {
            int iYear  = contract.ContractYear;
            int iMonth = contract.ContractMonth + month;

            if (iMonth > 12)
            {
                --iMonth;
                iMonth %= 12;
                ++iMonth;
                ++iYear;
            }
            return(MaturityDateUtil.GetMonthMaturityDate(new DateTime(iYear, iMonth, 1, __iCloseHour, __iCloseMinute, __iCloseSecond), false));
        }
Example #9
0
        private ContractTime AddContractMonths(ContractTime contract, int month)
        {
            int iYear  = contract.ContractYear;
            int iMonth = contract.ContractMonth + month;

            if (iMonth > 12)
            {
                --iMonth;
                iMonth %= 12;
                ++iMonth;
                ++iYear;
            }
            return(MaturityDateUtil.GetMonthMaturityDate((new DateTime(iYear, iMonth, 1)).AddSeconds(__cCloseTime.TotalSeconds), false));
        }
        private void CalcContractTime(DateTime today)
        {
            //先計算出目前此週到期日
            ContractTime cCurrentContractTime = MaturityDateUtil.GetWeekMaturityDate(today);

            AddContractTime(0, cCurrentContractTime);

            DateTime cDate = cCurrentContractTime.MaturityDate.AddSeconds(604800);

            for (int i = 1; i < 3; i++)
            {
                ContractTime cContractTime = MaturityDateUtil.GetWeekMaturityDate(cDate);
                AddContractTime(i, cContractTime);

                cDate = cContractTime.MaturityDate.AddSeconds(604800);
            }
        }
		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;
		}
 private void AddContractTime(int id, ContractTime contractTime)
 {
     contractTime.Id = id;
     __cContractTimes.Add(contractTime);
 }
Example #13
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);
            }
        }