Exemple #1
0
        /// <summary>
        /// Searches the futures chain for the next matching futures contract, and resolves the underlying
        /// as the closest future we can find during or after the contract month.
        /// </summary>
        /// <param name="canonicalFutureSymbol">Canonical future Symbol</param>
        /// <param name="futureOptionContractMonth">Future option contract month. Note that this is not the expiry of the Future Option.</param>
        /// <param name="lookupDate">The date that we'll be using to look at the Future chain</param>
        /// <returns>The underlying future's contract month, or null if no closest contract was found</returns>
        private static DateTime?ContractMonthSerialLookupRule(Symbol canonicalFutureSymbol, DateTime futureOptionContractMonth, DateTime lookupDate)
        {
            var futureChain = FuturesListings.ListedContracts(canonicalFutureSymbol.ID.Symbol, lookupDate);

            if (futureChain == null)
            {
                // No matching contract listing rules entry was found
                return(null);
            }

            foreach (var future in futureChain.OrderBy(s => s.ID.Date))
            {
                // Normalize by date first, normalize to a contract month date, then we want to get the contract
                // month of the Future contract so we normalize by getting the delta between the expiration
                // and the contract month.
                var futureContractMonth = future.ID.Date.Date
                                          .AddDays(-future.ID.Date.Day + 1)
                                          .AddMonths(FuturesExpiryUtilityFunctions.GetDeltaBetweenContractMonthAndContractExpiry(future.ID.Symbol, future.ID.Date));

                // We want a contract that is either the same as the contract month or greater
                if (futureContractMonth < futureOptionContractMonth)
                {
                    continue;
                }

                return(futureContractMonth);
            }

            // No matching/closest contract was found in the futures chain.
            return(null);
        }
Exemple #2
0
        public void ListedContractsMatchesCME(string ticker, string market, DateTime[] expectedListedExpiries)
        {
            // Test was created on 2021-01-11, we're using CME's data here to validate the test, hence the fixed date.
            var contractsListed  = FuturesListings.ListedContracts(ticker, new DateTime(2021, 1, 11));
            var contractsMissing = new HashSet <Symbol>();

            foreach (var expectedExpiry in expectedListedExpiries)
            {
                var expectedFuture = Symbol.CreateFuture(ticker, market, expectedExpiry);
                if (!contractsListed.Contains(expectedFuture))
                {
                    contractsMissing.Add(expectedFuture);
                }
            }

            var missingContractsMessage = $"The following contracts are missing from the listed contracts: {string.Join("\n", contractsMissing.Select(s => "  " + s.Value + " " + s.ID.Date.ToStringInvariant("yyyy-MM-dd")))}";

            Assert.AreEqual(0, contractsMissing.Count, missingContractsMessage);
            Assert.AreEqual(expectedListedExpiries.Length, contractsListed.Count, $"The length of expected listed contracts does not match the returned contract count.");
        }