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); }
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); } }
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)); }
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)); }