Пример #1
0
        public void CanGetSeriesObservations()
        {
            const string response     = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<observations realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" observation_start=""1776-07-04"" observation_end=""9999-12-31"" units=""lin"" output_type=""1"" file_type=""xml"" order_by=""observation_date"" sort_order=""asc"" count=""495"" offset=""0"" limit=""100000"">
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-01-01"" value=""358.02""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-02-01"" value=""357.5450""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-03-01"" value=""357.5187""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-04-01"" value=""357.5032""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-05-01"" value=""357.4130""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-06-01"" value=""357.4118""/>
</observations>";
            var          downloader   = new MockDownloader(response);
            var          fred         = new Fred("key", downloader);
            var          observations = fred.GetSeriesObservations("EXJPUS");
            var          expectedUrl  = "http://api.stlouisfed.org/fred/series/observations?api_key=key&series_id=EXJPUS" + RealtimeNow + "&limit=100000&offset=0&sort_order=asc&observation_start=1776-07-04&observation_end=9999-12-31&units=lin&frequency=&aggregation_method=avg&output_type=1&file_type=xml&vintage_dates=";

            Assert.AreEqual(expectedUrl, downloader.Url);
            Assert.AreEqual(6, observations.Count());
        }
Пример #2
0
        static void Main(string[] args)
        {
            var fred = new Fred("661c0a90e914477da5a7518293de5f8e");

            int startYear;
            int endYear;
            int startMonth;
            int endMonth;

            int deflateYear = 0;

            var data = new Dictionary <string, IList <Observation> >
            {
            };

            //get year of rGDP dollars
            //if (data.ContainsKey("rGDP"))
            {
                //var series = data["rGDP"];

                var series = fred.GetSeries("GDPC1");
                var units  = series.Units;
                //Console.WriteLine("Units: ");
                //Console.WriteLine(units);

                //string resultString = Regex.Match(units, @"\d+").Value;
                deflateYear = extractNumber(series.Units);

                Console.WriteLine("deflate year: {0}", deflateYear.ToString());
            }

            string entry;

            WriteLine("Pushing ENTER accepts default values");
            Write("Enter start year [2005]: ");
            entry = ReadLine();
            if (entry == "")
            {
                entry = "2005";
            }
            startYear = Convert.ToInt32(entry);

            WriteLine("Enter end year [2015] ");
            Write("years must cover rGDP real year to deinflate: ");
            entry = ReadLine();
            if (entry == "")
            {
                entry = "2015";
            }
            endYear = Convert.ToInt32(entry);

            Write("Enter start month, ex. Jan = 1 or Oct = [10]: ");
            entry = ReadLine();
            if (entry == "")
            {
                entry = "10";
            }
            startMonth = Convert.ToInt32(entry);

            Write("Enter end month, ex. Jan = 1 or Oct = [10]: ");
            entry = ReadLine();
            if (entry == "")
            {
                entry = "10";
            }
            endMonth = Convert.ToInt32(entry);

            DateTime startDate = new DateTime(startYear, startMonth, 1);
            DateTime endDate   = new DateTime(endYear, endMonth, DateTime.DaysInMonth(endYear, endMonth));

            //can't use # in GetSeriesObservations.  Ignores the dates.

            //my names
            string[] dataNames = new string[] { "rGDP", "pSaveRate", "fedFundRate", "empPopRatio", "consConfIndex", "consPriceIndex", "housingSeries" };

            //online series names
            string[] obsNames = new string[] { "GDPC1", "PSAVERT", "DFF", "EMRATIO", "UMCSENT", "CP0000USM086NEST", "SPCS20RSA" };

            int dataCounter = 0;

            //create lists as FredAPI objects
            foreach (var instance in dataNames)
            {
                if (dataNames[dataCounter] == "rGDP")
                {
                    data.Add(dataNames[dataCounter], fred.GetSeriesObservations(obsNames[dataCounter], startDate, endDate).ToList());
                }
                else
                {
                    data.Add(dataNames[dataCounter], fred.GetSeriesObservations(obsNames[dataCounter], startDate, endDate, frequency: Frequency.Monthly).ToList());
                }

                dataCounter++;
            }
            ;

            //MinMaxDates(ref data);

            //convert data to sortedList (the DateTime becomes key of the SortedList), the string is the Key of the Dictionary.
            var sortedDataList = new Dictionary <string, SortedList <DateTime, double?> > {
            };

            dataCounter = 0;

            foreach (var obData in data)
            {
                var list = obData.Value;
                sortedDataList.Add(dataNames[dataCounter], new SortedList <DateTime, double?>(list.ToDictionary(x => x.Date, x => x.Value)));
                dataCounter++;
            }

            //print data before changes
            //PrintData(sortedDataList); - will crash now because of gaps in rGDP

            //insert records (into rGDP)
            //ideally might want to have this second.
            FillInGaps(ref sortedDataList);

            //identify minmax dates, and trim list, if ran before FillInGaps, rGDP doesn't populate up till HighestDate2
            MinMaxDates(ref sortedDataList);

            PrintData(sortedDataList);
            PrintDataToFile(sortedDataList, "preConversion");

            //deInflate

            deInflate(ref sortedDataList, deflateYear);

            PrintData(sortedDataList);

            PrintDataToFile(sortedDataList, "postConversion");

            parseData(sortedDataList, args);
        }
Пример #3
0
        public void CanGetSeriesObservations()
        {
            const string response = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<observations realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" observation_start=""1776-07-04"" observation_end=""9999-12-31"" units=""lin"" output_type=""1"" file_type=""xml"" order_by=""observation_date"" sort_order=""asc"" count=""495"" offset=""0"" limit=""100000"">
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-01-01"" value=""358.02""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-02-01"" value=""357.5450""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-03-01"" value=""357.5187""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-04-01"" value=""357.5032""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-05-01"" value=""357.4130""/>
  <observation realtime_start=""2012-04-26"" realtime_end=""2012-04-26"" date=""1971-06-01"" value=""357.4118""/>
</observations>";
            var downloader = new MockDownloader(response);
            var fred = new Fred("key", downloader);
            var observations = fred.GetSeriesObservations("EXJPUS");
            var expectedUrl = "http://api.stlouisfed.org/fred/series/observations?api_key=key&series_id=EXJPUS" + RealtimeNow + "&limit=100000&offset=0&sort_order=asc&observation_start=1776-07-04&observation_end=9999-12-31&units=lin&frequency=&aggregation_method=avg&output_type=1&file_type=xml&vintage_dates=";
            Assert.AreEqual(expectedUrl, downloader.Url);
            Assert.AreEqual(6, observations.Count());
        }