/// <summary> /// Retrieves available call and put options for a given ticker. /// </summary> /// <param name="mdq">The market data query.</param> /// <param name="marketData">The requested market data.</param> /// <returns>The result of the query.</returns> private RefreshStatus GetCallPriceMarketData(MarketDataQuery mdq, out IMarketData marketData) { marketData = null; Fairmat.MarketData.CallPriceMarketData data = new Fairmat.MarketData.CallPriceMarketData(); List <MEFFHistoricalQuote> options = MEFFAPI.GetOptions(mdq.Ticker, mdq.Date); foreach (MEFFHistoricalQuote q in options) { Console.WriteLine(q.ContractCode + "\t" + q.StrikePrice + "\t" + q.MaturityDate.ToShortDateString() + "\t" + q.SettlPrice); } var status = OptionQuotesUtility.GetCallPriceMarketData(this, mdq, options.ConvertAll(x => (OptionQuotes.IOptionQuote)x), data); if (status.HasErrors) { return(status); } marketData = data; Console.WriteLine(data); return(status); }
/// <summary> /// Retrieves available call and put options for a given ticker. /// </summary> /// <param name="mdq">The market data query.</param> /// <param name="marketData">The requested market data.</param> /// <returns>A <see cref="RefreshStatus"/> with the status of the result.</returns> private RefreshStatus GetCallPriceMarketData(MarketDataQuery mdq, out IMarketData marketData) { marketData = null; //Check if the market data is in cache string cachedName = Path.Combine(Path.GetTempPath(), "CallPrices" + mdq.Ticker + mdq.Date.Year + mdq.Date.Month + mdq.Date.Day); List <YahooOptionChain> optionChains = null; if (System.IO.File.Exists(cachedName)) { try { optionChains = (List <YahooOptionChain>)DVPLI.ObjectSerialization.ReadFromFile(cachedName); } catch { //Failed to read from cache } } //if not found in cache try to get from the Yahoo service if (optionChains == null) { //Yahoo returns only last traded options hence, we assume that the only //valid dates are Today and Yesterday DateTime tMax = DateTime.Today; DateTime tMin = tMax.AddDays(-1); if (tMax.DayOfWeek == DayOfWeek.Monday) { tMin = tMax.AddDays(-3); } if (mdq.Date.Date < tMin || mdq.Date.Date > tMax) { return(new RefreshStatus("Options are not available for the requested period. Set the valuation date to Yesterday")); } // Request options does not seems to give the option effectively // tradaded at a given date but the options that are still being traded. optionChains = YahooFinanceAPI.RequestOptions(mdq.Ticker); try { DVPLI.ObjectSerialization.WriteToFile(cachedName, optionChains); } catch { } } Fairmat.MarketData.CallPriceMarketData data = new Fairmat.MarketData.CallPriceMarketData(); // Extract a list of YahooOption from the YahooOptionChain List. List <YahooOption> options = new List <YahooOption>(); foreach (YahooOptionChain q in optionChains) { Console.WriteLine(q.Symbol + " " + q.Expiration); foreach (YahooOption o in q.Options) { // Loads into YahooOption the needed information. o.Maturity = q.Expiration; options.Add(o); } } // Populate the CallPriceMarketData data structure var status = OptionQuotesUtility.GetCallPriceMarketData(this, mdq, options.ConvertAll(x => (IOptionQuote)x), data); if (status.HasErrors) { return(status); } marketData = data; Console.WriteLine(data); return(status); }