コード例 #1
0
        public void ParseXMLCorrectlyParsesData()
        {
            var targetBars = new List <OHLCBar>
            {
                new OHLCBar {
                    DT = new DateTime(2013, 12, 12), Open = 97.55m, High = 98.18m, Low = 97.31m, Close = 97.50m, Volume = null, OpenInterest = 153787
                },
                new OHLCBar {
                    DT = new DateTime(2013, 12, 11), Open = 98.65m, High = 98.75m, Low = 97.2m, Close = 97.44m, Volume = null, OpenInterest = 189430
                },
                new OHLCBar {
                    DT = new DateTime(2013, 12, 10), Open = 97.25m, High = 98.74m, Low = 97.24m, Close = 98.51m, Volume = null, OpenInterest = 218380
                },
            };

            string         xml  = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <dataset> <errors> </errors> <id type=\"integer\">2298870</id> <source-code>OFDP</source-code> <code>FUTURE_CL1</code> <name>NYMEX Crude Oil Futures, Continuous Contract #1 (CL1) (Front Month)</name> <urlize-name>NYMEX-Crude-Oil-Futures-Continuous-Contract-1-CL1-Front-Month</urlize-name> <description> &lt;p&gt;Historical Futures Prices: Crude Oil Futures, Continuous Contract #1. Non-adjusted price based on spot-month continuous contract calculations. Raw futures data from New York Mercantile Exchange (NYMEX). &lt;/p&gt; </description> <updated-at>2013-12-13T01:59:28Z</updated-at> <frequency>daily</frequency> <from-date>1983-03-30</from-date> <to-date>2013-12-12</to-date> <column-names type=\"array\"> <column-name>Date</column-name> <column-name>Open</column-name> <column-name>High</column-name> <column-name>Low</column-name> <column-name>Settle</column-name> <column-name>Volume</column-name> <column-name>Open Interest</column-name> </column-names> <private type=\"boolean\">false</private> <type nil=\"true\"/> <display-url>http://www.ofdp.org/continuous_contracts/data?exchange=NYM&amp;symbol=CL&amp;depth=1</display-url> <data type=\"array\"> <datum type=\"array\"> <datum>2013-12-12</datum> <datum type=\"float\">97.55</datum> <datum type=\"float\">98.18</datum> <datum type=\"float\">97.31</datum> <datum type=\"float\">97.5</datum> <datum nil=\"true\"/> <datum type=\"float\">153787.0</datum> </datum> <datum type=\"array\"> <datum>2013-12-11</datum> <datum type=\"float\">98.65</datum> <datum type=\"float\">98.75</datum> <datum type=\"float\">97.2</datum> <datum type=\"float\">97.44</datum> <datum nil=\"true\"/> <datum type=\"float\">189430.0</datum> </datum> <datum type=\"array\"> <datum>2013-12-10</datum> <datum type=\"float\">97.25</datum> <datum type=\"float\">98.74</datum> <datum type=\"float\">97.24</datum> <datum type=\"float\">98.51</datum> <datum nil=\"true\"/> <datum type=\"float\">218380.0</datum> </datum> </data> </dataset> ";
            List <OHLCBar> bars = QuandlUtils.ParseXML(xml);

            Assert.AreEqual(targetBars.Count, bars.Count);

            for (int i = 0; i < bars.Count; i++)
            {
                Assert.AreEqual(targetBars[i].LongDate, bars[i].LongDate);
                Assert.AreEqual(targetBars[i].Open, bars[i].Open);
                Assert.AreEqual(targetBars[i].High, bars[i].High);
                Assert.AreEqual(targetBars[i].Low, bars[i].Low);
                Assert.AreEqual(targetBars[i].Close, bars[i].Close);
                Assert.AreEqual(targetBars[i].Volume, bars[i].Volume);
                Assert.AreEqual(targetBars[i].OpenInterest, bars[i].OpenInterest);
            }
        }
コード例 #2
0
        public void RequestHistoricalData(HistoricalDataRequest request)
        {
            if (request.Frequency < BarSize.OneDay)
            {
                throw new Exception("Quandl unsupported bar size. Minimum is one day.");
            }

            string freqString = "daily"; //collapse=none|daily|weekly|monthly|quarterly|annual

            switch (request.Frequency)
            {
            case BarSize.OneWeek:
                freqString = "weekly";
                break;

            case BarSize.OneMonth:
                freqString = "monthly";
                break;

            case BarSize.OneQuarter:
                freqString = "quarterly";
                break;

            case BarSize.OneYear:
                freqString = "annual";
                break;
            }

            string requestURL = string.Format(
                "http://www.quandl.com/api/v1/datasets/{0}.xml?trim_start={1}&trim_end={2}&sort_order=asc&collapse={3}",
                request.Instrument.DatasourceSymbol,
                request.StartingDate.ToString("yyyy-MM-dd"),
                request.EndingDate.ToString("yyyy-MM-dd"),
                freqString);

            //if the user has provided an authentication code, we slap it on at the end
            if (!string.IsNullOrEmpty(authToken))
            {
                requestURL += string.Format("&auth_token={0}", authToken);
            }

            //download the data
            string data;

            using (WebClient webClient = new WebClient())
            {
                try
                {
                    data = webClient.DownloadString(requestURL);
                }
                catch (Exception ex)
                {
                    string errMsg = "Quandl: error downloading data. " + ex.Message;
                    Log(LogLevel.Error, errMsg);
                    RaiseEvent(Error, this, new ErrorArgs(-1, errMsg));
                    return;
                }
            }

            //then parse it
            List <OHLCBar> bars;

            try
            {
                bars = QuandlUtils.ParseXML(data);
            }
            catch (Exception ex)
            {
                Log(LogLevel.Error, ex.Message);
                RaiseEvent(Error, this, new ErrorArgs(-1, ex.Message));
                return;
            }

            //send back the data using the HistoricalDataArrived event
            RaiseEvent(HistoricalDataArrived, this, new HistoricalDataEventArgs(request, bars));
        }