コード例 #1
0
        public void TestRequestMultipleEntry()
        {
            global::MEFFIntegration.MEFFIntegration wrapper = new global::MEFFIntegration.MEFFIntegration();
            IMarketData[]   data;
            DateTime[]      dates;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "GRF";
            query.Date           = new DateTime(2013, 6, 3);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "close";

            Status status = wrapper.GetTimeSeries(query, new DateTime(2013, 6, 4), out dates, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(2, dates.Length);

            Assert.AreEqual(new DateTime(2013, 6, 3), data[1].TimeStamp);
            Assert.AreEqual(new DateTime(2013, 6, 4), data[0].TimeStamp);
            Assert.That(data[0] is Scalar);
            Assert.That(data[1] is Scalar);

            Assert.AreEqual(28, (data[1] as Scalar).Value, 1);
            Assert.AreEqual(29, (data[0] as Scalar).Value, 1);
        }
コード例 #2
0
        public void TestRequestOneEntry()
        {
            global::YahooFinanceIntegration.YahooFinanceIntegration wrapper = new global::YahooFinanceIntegration.YahooFinanceIntegration();
            IMarketData     data;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "GOOG";
            query.Date           = new DateTime(2011, 1, 31);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "open";

            Status status = wrapper.GetMarketData(query, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(data.TimeStamp, new DateTime(2011, 1, 31));
            Assert.That(data is Scalar);
            Assert.AreEqual((data as Scalar).Value, 603, 1);

            query.Field = "close";

            status = wrapper.GetMarketData(query, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(data.TimeStamp, new DateTime(2011, 1, 31));
            Assert.That(data is Scalar);
            Assert.AreEqual((data as Scalar).Value, 600, 1);
        }
コード例 #3
0
        public void TestRequestOneEntry()
        {
            global::EuropeanCentralBankIntegration.EuropeanCentralBankIntegration wrapper = new global::EuropeanCentralBankIntegration.EuropeanCentralBankIntegration();
            IMarketData     data;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "EUCFZAR";
            query.Date           = new DateTime(2011, 1, 31);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "close";

            Status status = wrapper.GetMarketData(query, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(new DateTime(2011, 1, 31), data.TimeStamp);
            Assert.That(data is Scalar);
            Assert.AreEqual(9.8458, (data as Scalar).Value, 0.0001);

            query.Ticker = "EURZAR";

            status = wrapper.GetMarketData(query, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(new DateTime(2011, 1, 31), data.TimeStamp);
            Assert.That(data is Scalar);
            Assert.AreEqual(9.8458, (data as Scalar).Value, 0.0001);
        }
コード例 #4
0
        public void TestGetCallPriceMarketData()
        {
            var wrapper = new global::YahooFinanceIntegration.YahooFinanceIntegration();

            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "GOOG";
            query.Date           = new DateTime(2011, 1, 31);
            query.MarketDataType = typeof(Fairmat.MarketData.CallPriceMarketData).ToString();
            query.Field          = "close";

            IMarketData data;
            var         status = wrapper.GetMarketData(query, out data);

            Assert.IsFalse(status.HasErrors);
        }
コード例 #5
0
        public void TestGetCallPriceMarketData()
        {
            global::MEFFIntegration.MEFFIntegration wrapper = new global::MEFFIntegration.MEFFIntegration();

            MarketDataQuery mdq = new MarketDataQuery();

            mdq.Ticker         = "BBVA";
            mdq.Date           = new DateTime(2013, 07, 01);
            mdq.Market         = "EU";
            mdq.Field          = "close";
            mdq.MarketDataType = typeof(Fairmat.MarketData.CallPriceMarketData).ToString();
            IMarketData marketData;
            var         status = wrapper.GetMarketData(mdq, out marketData);

            Assert.IsFalse(status.HasErrors);
        }
コード例 #6
0
        public void TestRequestOneEntry()
        {
            global::MEFFIntegration.MEFFIntegration wrapper = new global::MEFFIntegration.MEFFIntegration();
            IMarketData     data;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "GRF";
            query.Date           = new DateTime(2013, 6, 3);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "close";

            Status status = wrapper.GetMarketData(query, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(new DateTime(2013, 6, 3), data.TimeStamp);
            Assert.That(data is Scalar);
            Assert.AreEqual(28, (data as Scalar).Value, 1);
        }
コード例 #7
0
        public void TestRequestMultipleEntryCurrencyConversion()
        {
            global::YahooFinanceIntegration.YahooFinanceIntegration wrapper = new global::YahooFinanceIntegration.YahooFinanceIntegration();
            IMarketData[]   data;
            DateTime[]      dates;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "GOOG";
            query.Date           = new DateTime(2011, 1, 31);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "open";
            query.Market         = "EU";

            Status status = wrapper.GetTimeSeries(query, new DateTime(2011, 2, 1), out dates, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(2, dates.Length);

            Assert.AreEqual(new DateTime(2011, 2, 1), data[1].TimeStamp);
            Assert.AreEqual(new DateTime(2011, 1, 31), data[0].TimeStamp);
            Assert.That(data[0] is Scalar);
            Assert.That(data[1] is Scalar);

            Assert.AreEqual(441, (data[1] as Scalar).Value, 1);
            Assert.AreEqual(446, (data[0] as Scalar).Value, 1);

            query.Field = "close";

            status = wrapper.GetTimeSeries(query, new DateTime(2011, 2, 1), out dates, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(2, dates.Length);

            Assert.AreEqual(new DateTime(2011, 2, 1), data[1].TimeStamp);
            Assert.AreEqual(new DateTime(2011, 1, 31), data[0].TimeStamp);
            Assert.That(data[0] is Scalar);
            Assert.That(data[1] is Scalar);

            Assert.AreEqual(446, (data[1] as Scalar).Value, 1);
            Assert.AreEqual(444, (data[0] as Scalar).Value, 1);
        }
コード例 #8
0
        public void TestRequestMultipleEntry()
        {
            global::EuropeanCentralBankIntegration.EuropeanCentralBankIntegration wrapper = new global::EuropeanCentralBankIntegration.EuropeanCentralBankIntegration();
            IMarketData[]   data;
            DateTime[]      dates;
            MarketDataQuery query = new MarketDataQuery();

            query.Ticker         = "EUCFZAR";
            query.Date           = new DateTime(2011, 1, 31);
            query.MarketDataType = typeof(Scalar).ToString();
            query.Field          = "close";

            Status status = wrapper.GetTimeSeries(query, new DateTime(2011, 2, 1), out dates, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(2, dates.Length);

            Assert.AreEqual(new DateTime(2011, 2, 1), data[0].TimeStamp);
            Assert.AreEqual(new DateTime(2011, 1, 31), data[1].TimeStamp);
            Assert.That(data[0] is Scalar);
            Assert.That(data[1] is Scalar);

            Assert.AreEqual(9.8480, (data[0] as Scalar).Value, 0.0001);
            Assert.AreEqual(9.8458, (data[1] as Scalar).Value, 0.0001);

            query.Ticker = "EURZAR";

            status = wrapper.GetTimeSeries(query, new DateTime(2011, 2, 1), out dates, out data);

            Assert.That(!status.HasErrors, status.ErrorMessage);
            Assert.AreEqual(2, data.Length);
            Assert.AreEqual(2, dates.Length);

            Assert.AreEqual(new DateTime(2011, 2, 1), data[0].TimeStamp);
            Assert.AreEqual(new DateTime(2011, 1, 31), data[1].TimeStamp);
            Assert.That(data[1] is Scalar);
            Assert.That(data[0] is Scalar);

            Assert.AreEqual(9.8480, (data[0] as Scalar).Value, 0.0001);
            Assert.AreEqual(9.8458, (data[1] as Scalar).Value, 0.0001);
        }
コード例 #9
0
        /// <summary>
        /// Gets the market data from a single day.
        /// </summary>
        /// <param name="mdq">
        /// A <see cref="MarketDataQuery"/> with the data request.
        /// </param>
        /// <param name="marketData">
        /// In case of success, the requested market data as <see cref="IMarketData"/>.
        /// </param>
        /// <returns>
        /// A <see cref="RefreshStatus"/> indicating if the query was successful.
        /// </returns>
        public RefreshStatus GetMarketData(MarketDataQuery mdq, out IMarketData marketData)
        {
            if (mdq.MarketDataType == typeof(Fairmat.MarketData.CallPriceMarketData).ToString())
            {
                return(GetCallPriceMarketData(mdq, out marketData));
            }

            // Reuse the Historical time series to get the single quote
            // (Equal start/end date = get the quote of the day).
            DateTime[]    dates;
            IMarketData[] marketDataArray;
            RefreshStatus status = GetTimeSeries(mdq, mdq.Date, out dates, out marketDataArray);

            // If there were errors already just report them back.
            if (status.HasErrors)
            {
                marketData = null;
            }
            else
            {
                // If all was succesful try to prepare data for this type of query.
                status = new RefreshStatus();

                // Do some sanity check on the gathered data.
                if (marketDataArray.Length != 1 && dates.Length != 1 && dates[0] != mdq.Date)
                {
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetMarketData: Requested date " +
                                           "or Market Data not available.";
                    marketData = null;
                }
                else
                {
                    // If they pass just take the first element as result
                    // (which must be also the only one).
                    marketData = marketDataArray[0];
                }
            }

            return(status);
        }
コード例 #10
0
        /// <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>
        /// Gets a series of Historical Market Data from the starting date
        /// to the end date.
        /// </summary>
        /// <param name="mdq">
        /// A <see cref="MarketDataQuery"/> with the data request.
        /// </param>
        /// <param name="end">
        /// A <see cref="DateTime"/> with the ending date of the period to fetch data from.
        /// </param>
        /// <param name="dates">
        /// In case of success, a list of the dates data was fetched from in the requested period.
        /// </param>
        /// <param name="marketData">
        /// In case of success, a list of the fetched market data day
        /// by day corresponding to <see cref="dates"/>.
        /// </param>
        /// <returns>
        /// A <see cref="RefreshStatus"/> indicating if the query was successful.
        /// </returns>
        public RefreshStatus GetTimeSeries(MarketDataQuery mdq, DateTime end, out DateTime[] dates, out IMarketData[] marketData)
        {
            RefreshStatus status = new RefreshStatus();

            string currency;
            bool   invertedRequest = false;

            // Check if it's a close request.
            if (mdq.Field != "close")
            {
                // In case the request is not close return an error.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data not available (only " +
                                       "close values are available, " +
                                       mdq.Field + " was requested).";
                return(status);
            }

            string ticker = RemoveTrailers(mdq.Ticker);

            // Check that the requested value is available.
            if (ticker.StartsWith("EUCF"))
            {
                // Extract the target currency name as that's used to request the data.
                currency = ticker.Remove(0, 4);
            }
            else if (ticker.StartsWith("EUR"))
            {
                // Extract the target currency name as that's used to request the data.
                currency = ticker.Remove(0, 3);
            }
            else if (ticker.EndsWith("EUR"))
            {
                // As EUR is the target currency this request is inverted compared to ECB data.
                invertedRequest = true;
                currency        = ticker.Remove(ticker.LastIndexOf("EUR"), 3);
            }
            else
            {
                // Only EUR TO TARGET CURRENCY is supported using the format EUCF<TARGET CURRENCY>
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data not available (only " +
                                       "conversion rates from EUR to another currency are " +
                                       "available, " + mdq.Ticker + " was requested).";
                return(status);
            }

            // For now only Scalar requests are handled.
            if (mdq.MarketDataType == typeof(Scalar).ToString())
            {
                List <EuropeanCentralBankQuote> quotes = null;

                try
                {
                    // Request the data to the Market Data Provider.
                    quotes = EuropeanCentralBankAPI.GetHistoricalQuotes(currency, mdq.Date, end);
                }
                catch (Exception e)
                {
                    // There can be conversion, server availability
                    // and other exceptions during this request.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available due " +
                                           "to problems with the European Central Bank service: " +
                                           e.Message;
                    return(status);
                }

                // Check if there is at least one result.
                if (quotes.Count >= 1)
                {
                    // Allocate the structures for the output.
                    marketData = new Scalar[quotes.Count];
                    dates      = new DateTime[quotes.Count];

                    // Scan the list of quotes to prepare the data for Fairmat.
                    for (int i = 0; i < quotes.Count; i++)
                    {
                        // Fill the dates array from the date field of each quote.
                        dates[i] = quotes[i].Date;

                        // Prepare the single scalar data.
                        Scalar val = new Scalar();
                        val.TimeStamp = quotes[i].Date;

                        if (invertedRequest)
                        {
                            // Handle the conversion from other currencies to EUR.
                            val.Value = 1 / quotes[i].Value;
                        }
                        else
                        {
                            val.Value = quotes[i].Value;
                        }

                        // Put it in the output structure.
                        marketData[i] = val;
                    }

                    return(status);
                }
                else
                {
                    // If there isn't at least one result return an error.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available: " +
                                           "empty data set for the request.";
                    return(status);
                }
            }
            else
            {
                // If control falls through here it means the request type was not supported.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data request type (" +
                                       mdq.MarketDataType +
                                       ") not supported by the Market Data Provider.";
                return(status);
            }
        }
コード例 #12
0
        /// <summary>
        /// Gets a series of Historical Market Data from the starting date
        /// to the end date.
        /// </summary>
        /// <param name="mdq">
        /// A <see cref="MarketDataQuery"/> with the data request.
        /// </param>
        /// <param name="end">
        /// A <see cref="DateTime"/> with the ending date of the period to fetch data from.
        /// </param>
        /// <param name="dates">
        /// In case of success, a list of the dates data was fetched from in the requested period.
        /// </param>
        /// <param name="marketData">
        /// In case of success, a list of the fetched market data day
        /// by day corresponding to <see cref="dates"/>.
        /// </param>
        /// <returns>
        /// A <see cref="RefreshStatus"/> indicating if the query was successful.
        /// </returns>
        public RefreshStatus GetTimeSeries(MarketDataQuery mdq, DateTime end, out DateTime[] dates, out IMarketData[] marketData)
        {
            RefreshStatus status = new RefreshStatus();

            // Check if close value was requested.
            switch (mdq.Field)
            {
            case "close":
            {
                break;
            }

            default:
            {
                // In case the request is not close return an error.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data not available (only " +
                                       "close values are available, " +
                                       mdq.Field + " was requested).";
                return(status);
            }
            }

            // For now only Scalar requests are handled.
            if (mdq.MarketDataType == typeof(Scalar).ToString())
            {
                List <MEFFHistoricalQuote> quotes = null;

                try
                {
                    // Request the data to the Market Data Provider.
                    quotes = MEFFAPI.GetHistoricalQuotes(TickerUtility.PreparseSymbol(mdq.Ticker), mdq.Date, end);
                }
                catch (Exception e)
                {
                    // There can be conversion, server availability
                    // and other exceptions during this request.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available due " +
                                           "to problems with MEFF service: " + e.Message;
                    return(status);
                }

                // Check if there is at least one result.
                if (quotes.Count >= 1)
                {
                    // Allocate the structures for the output.
                    marketData = new Scalar[quotes.Count];
                    dates      = new DateTime[quotes.Count];

                    // Scan the list of quotes to prepare the data for Fairmat.
                    for (int i = 0; i < quotes.Count; i++)
                    {
                        // Fill the dates array from the date field of each quote.
                        dates[i] = quotes[i].SessionDate;

                        // Prepare the single scalar data.
                        Scalar val = new Scalar();
                        val.TimeStamp = quotes[i].SessionDate;
                        val.Value     = quotes[i].SettlPrice;

                        // Put it in the output structure.
                        marketData[i] = val;
                    }

                    return(status);
                }
                else
                {
                    // If there isn't at least one result return an error.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available: " +
                                           "empty data set for the request.";
                    return(status);
                }
            }
            else
            {
                // If control falls through here it means the request type was not supported.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data request type (" +
                                       mdq.MarketDataType +
                                       ") not supported by the Market Data Provider.";
                return(status);
            }
        }
コード例 #13
0
        /// <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);
        }
コード例 #14
0
        /// <summary>
        /// Gets a series of Historical Market Data from the starting date
        /// to the end date.
        /// </summary>
        /// <param name="mdq">
        /// A <see cref="MarketDataQuery"/> with the data request.
        /// </param>
        /// <param name="end">
        /// A <see cref="DateTime"/> with the ending date of the period to fetch data from.
        /// </param>
        /// <param name="dates">
        /// In case of success, a list of the dates data was fetched from in the requested period.
        /// </param>
        /// <param name="marketData">
        /// In case of success, a list of the fetched market data day
        /// by day corresponding to <see cref="dates"/>.
        /// </param>
        /// <returns>
        /// A <see cref="RefreshStatus"/> indicating if the query was successful.
        /// </returns>
        public RefreshStatus GetTimeSeries(MarketDataQuery mdq, DateTime end, out DateTime[] dates, out IMarketData[] marketData)
        {
            RefreshStatus status = new RefreshStatus();

            // Holds whathever we should take market close or market open values.
            bool closeRequest;

            // Holds the currency conversion target in case a market different than
            // US is choosen. This handles conversion from USD values provided by yahoo.
            string targetMarket = null;

            string ticker = TickerUtility.PreparseSymbol(mdq.Ticker);
            bool   divisionTransformation = false;
            bool   inverseTransformation  = false;

            // Check if open or close value was requested.
            switch (mdq.Field)
            {
            case "open":
            {
                closeRequest = false;
                break;
            }

            case "close":
            {
                closeRequest = true;
                break;
            }

            default:
            {
                // In case the request is neither open or close return an error.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data not available (only " +
                                       "open and close values are available, " +
                                       mdq.Field + " was requested).";
                return(status);
            }
            }

            // Gather in which currency to get the data, this will require
            // an additional data fetching for the target currency for the same time period.
            if (mdq.Market.Length > 0 && mdq.Market != "US")
            {
                // Try to convert the entries in the Fairmat drop down box,
                // for the rest rely on the user.
                switch (mdq.Market)
                {
                case "EU":
                {
                    targetMarket = "EUR";
                    break;
                }

                case "GB":
                {
                    targetMarket = "GBP";
                    break;
                }

                case "JP":
                {
                    targetMarket = "JPY";
                    break;
                }

                case "CH":
                {
                    targetMarket = "CHF";
                    break;
                }

                case "HK":
                {
                    targetMarket = "HKD";
                    break;
                }

                default:
                {
                    // In the fallback scenario just use directly the provided string.
                    targetMarket = mdq.Market;
                    break;
                }
                }
            }

            // Check for currencies and handle them in a special way.
            // Check the single currency only (eg: EUR for USDEUR).
            if (GetCurrencyList().Contains(ticker))
            {
                ticker += "=X";

                // Disable the feature for this for now.
                targetMarket = null;
            }
            else
            {
                // Attempt a more throughout parsing. Check for <currency><currency> formats.
                foreach (string currency in GetCurrencyList())
                {
                    if (ticker.StartsWith(currency))
                    {
                        if (currency == "USD")
                        {
                            // If usd is in the ticker name it's a special case due to the way
                            // Yahoo! Finance keeps currencies.
                            ticker       = ticker.Remove(0, 3) + "=X";
                            targetMarket = null;
                        }
                        else if (ticker.Remove(0, 3) == "USD")
                        {
                            // This is the inverse of the previous case.
                            // From other currency to USD. Similarly to above it's a special case.
                            ticker                = currency + "=X";
                            targetMarket          = null;
                            inverseTransformation = true;
                        }
                        else
                        {
                            // Normal not USD to not USD currency conversions.
                            targetMarket           = currency;
                            ticker                 = ticker.Remove(0, 3) + "=X";
                            divisionTransformation = true;
                        }

                        break;
                    }
                }
            }

            // For now only Scalar requests are handled.
            if (mdq.MarketDataType == typeof(Scalar).ToString())
            {
                List <YahooHistoricalQuote> quotes = null;
                Dictionary <DateTime, YahooHistoricalQuote> currencyQuotes = new Dictionary <DateTime, YahooHistoricalQuote>();

                try
                {
                    // Request the data to the Market Data Provider.
                    quotes = YahooFinanceAPI.GetHistoricalQuotes(ticker, mdq.Date, end);

                    if (targetMarket != null)
                    {
                        // If we need currency quotes in order to handle currency conversions
                        // fetch them now.
                        List <YahooHistoricalQuote> fetchedCurrencyQuotes = YahooFinanceAPI.GetHistoricalQuotes(targetMarket + "=X", mdq.Date, end);

                        // Put all items in a dictionary for easy fetching.
                        fetchedCurrencyQuotes.ForEach(x => currencyQuotes.Add(x.Date, x));
                    }
                }
                catch (Exception e)
                {
                    // There can be conversion, server availability
                    // and other exceptions during this request.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available due " +
                                           "to problems with Yahoo! Finance: " + e.Message;
                    return(status);
                }

                // Check if there is at least one result.
                if (quotes.Count >= 1)
                {
                    // Allocate the structures for the output.
                    List <Scalar>   readyMarketData = new List <Scalar>();
                    List <DateTime> readyDates      = new List <DateTime>();

                    // Scan the list of quotes to prepare the data for Fairmat.
                    for (int i = 0; i < quotes.Count; i++)
                    {
                        // Prepare the single scalar data.
                        Scalar val = new Scalar();
                        val.TimeStamp = quotes[i].Date;
                        val.Value     = (closeRequest == true) ? quotes[i].Close : quotes[i].Open;

                        // Handle currency conversions if needed.
                        if (currencyQuotes != null)
                        {
                            // Check if the entry in a date exists also for the
                            // currency conversion, if not discard the data.
                            if (!currencyQuotes.ContainsKey(quotes[i].Date))
                            {
                                // We skip this entry.
                                continue;
                            }

                            YahooHistoricalQuote currencyQuote = currencyQuotes[quotes[i].Date];

                            //if (divisionTransformation)
                            //{
                            //    val.Value /= (closeRequest == true) ? currencyQuote.Close : currencyQuote.Open;
                            //}
                            //else
                            //{
                            //    val.Value *= (closeRequest == true) ? currencyQuote.Close : currencyQuote.Open;
                            //}
                        }

                        // Apply an inverse transformation, used for currency values when going
                        // from a not USD currency to USD.
                        //if (inverseTransformation)
                        //{
                        //    val.Value = 1 / val.Value;
                        //}

                        // Put it in the output structure.
                        readyMarketData.Add(val);

                        // Fill the dates array from the date field of each quote.
                        readyDates.Add(quotes[i].Date);
                    }

                    // Put in the output data.
                    marketData = readyMarketData.ToArray();
                    dates      = readyDates.ToArray();

                    return(status);
                }
                else
                {
                    // If there isn't at least one result return an error.
                    marketData           = null;
                    dates                = null;
                    status.HasErrors     = true;
                    status.ErrorMessage += "GetTimeSeries: Market data not available: " +
                                           "empty data set for the request.";
                    return(status);
                }
            }
            else
            {
                // If control falls through here it means the request type was not supported.
                marketData           = null;
                dates                = null;
                status.HasErrors     = true;
                status.ErrorMessage += "GetTimeSeries: Market data request type (" +
                                       mdq.MarketDataType +
                                       ") not supported by the Market Data Provider.";
                return(status);
            }
        }
コード例 #15
0
        /// <summary>
        /// Populates CallPriceMarketData data structure from options
        /// quotes and additional calls to market data.
        /// </summary>
        /// <param name="provider">The underlying market data provider.</param>
        /// <param name="mdq">The underlying query.</param>
        /// <param name="quotes">The options quotes list.</param>
        /// <param name="data">The data structure to be populated.</param>
        /// <returns>The status of the operation.</returns>
        public static RefreshStatus GetCallPriceMarketData(IMarketDataProvider provider, MarketDataQuery mdq, List <IOptionQuote> quotes, Fairmat.MarketData.CallPriceMarketData data)
        {
            // Gets call options.
            var calls = quotes.FindAll(x => x.Type == OptionQuoteType.Call);
            var puts  = quotes.FindAll(x => x.Type == OptionQuoteType.Put);

            // Get maturities and strikes.
            var maturtiesDates = quotes.Select(item => item.Maturity).Distinct().OrderBy(x => x).ToList();
            var strikes        = quotes.Select(item => item.Strike).Distinct().OrderBy(x => x).ToList();

            data.Strike = (Vector)strikes.ToArray();
            //var callMaturtiesDates = calls.Select(item => item.MaturityDate).Distinct().ToList();

            Console.WriteLine("Maturities");
            data.Maturity = new Vector(maturtiesDates.Count);
            for (int z = 0; z < maturtiesDates.Count; z++)
            {
                data.Maturity[z] = RightValueDate.DatesDifferenceCalculator(mdq.Date, maturtiesDates[z]);
            }

            data.CallPrice = new Matrix(data.Maturity.Length, data.Strike.Length);
            var putPrices = new Matrix(data.Maturity.Length, data.Strike.Length);

            // Group maturities, calls, puts  with respect to strikes.
            Dictionary <double, Tuple <List <double>, List <double>, List <double> > > atm = new Dictionary <double, Tuple <List <double>, List <double>, List <double> > >();

            for (int si = 0; si < data.Strike.Length; si++)
            {
                for (int mi = 0; mi < maturtiesDates.Count; mi++)
                {
                    IOptionQuote callQuote = calls.Find(x => x.Strike == data.Strike[si] && x.Maturity == maturtiesDates[mi]);
                    if (callQuote != null)
                    {
                        data.CallPrice[mi, si] = callQuote.Price;
                    }

                    IOptionQuote putQuote = puts.Find(x => x.Strike == data.Strike[si] && x.Maturity == maturtiesDates[mi]);
                    if (putQuote != null)
                    {
                        putPrices[mi, si] = putQuote.Price;
                    }

                    if (callQuote != null && putQuote != null)
                    {
                        Tuple <List <double>, List <double>, List <double> > element = null;

                        if (atm.ContainsKey(data.Strike[si]))
                        {
                            element = atm[data.Strike[si]];
                        }
                        else
                        {
                            element = new Tuple <List <double>, List <double>, List <double> >(new List <double>(), new List <double>(), new List <double>());
                            atm.Add(data.Strike[si], element);
                        }

                        element.Item1.Add(data.Maturity[mi]);
                        element.Item2.Add(callQuote.Price);
                        element.Item3.Add(putQuote.Price);
                    }
                }
            }

            Console.WriteLine("CallPrices");
            Console.WriteLine(data.CallPrice);
            Console.WriteLine("Putprices");
            Console.WriteLine(putPrices);

            // Request the spot price .
            var mdq2 = DVPLI.ObjectSerialization.CloneObject(mdq) as MarketDataQuery;

            mdq2.MarketDataType = typeof(Scalar).ToString();
            IMarketData s0;
            var         s0Result = provider.GetMarketData(mdq2, out s0);

            if (s0Result.HasErrors)
            {
                return(s0Result);
            }

            data.S0 = (s0 as Scalar).Value;

            // Load atm info (get the strike with the higher number of elements).
            int    maxElements = -1;
            double argMax      = -1;

            // Keeps the Strike-Spot distance.
            double spotDistance = double.MaxValue;

            // Find the options which minimize the distance from strike price and spot price.
            foreach (double strike in atm.Keys)
            {
                double distance = Math.Abs(strike - data.S0);
                if (distance < spotDistance)
                {
                    spotDistance = distance;
                    maxElements  = atm[strike].Item1.Count;
                    argMax       = strike;
                }
            }

            if (spotDistance != double.MaxValue)
            {
                data.StrikeATM    = argMax;
                data.MaturityATM  = (Vector)atm[argMax].Item1.ToArray();
                data.CallPriceATM = (Vector)atm[argMax].Item2.ToArray();
                data.PutPriceATM  = (Vector)atm[argMax].Item3.ToArray();
            }
            else
            {
                Console.WriteLine("Cannot find atm information");
            }

            data.Ticker = mdq.Ticker;
            data.Market = mdq.Market;
            data.Date   = mdq.Date;

            return(new RefreshStatus());
        }