Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.Problems.DataAnalysis.Trading/3.4/InstanceProviders/EcbProblemInstanceProvider.cs @ 15344

Last change on this file since 15344 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Net;
26using System.Xml;
27using HeuristicLab.Problems.Instances;
28using System.Linq;
29
30namespace HeuristicLab.Problems.DataAnalysis.Trading {
31  public class EcbProblemInstanceProvider : ProblemInstanceProvider<IProblemData> {
32    private class EcbDataDescriptor : IDataDescriptor {
33      public string Name { get; set; }
34      public string Description { get; set; }
35    }
36
37    public override string Name {
38      get { return "European Central Bank FX Data Provider"; }
39    }
40
41    public override string Description {
42      get { return "Downloads exchange rate data from the ECB"; }
43    }
44
45    public override Uri WebLink {
46      get { return new Uri("http://www.ecb.europa.eu/stats/eurofxref/"); }
47    }
48
49    public override string ReferencePublication {
50      get { return string.Empty; }
51    }
52
53    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
54      var l = new List<IDataDescriptor>();
55      try {
56        using (var client = new WebClient()) {
57          var s = client.OpenRead("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml");
58          if (s == null) return l;
59
60          using (var reader = new XmlTextReader(s)) {
61            reader.MoveToContent();
62            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
63            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
64            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
65            do {
66              l.Add(new EcbDataDescriptor() { Name = "EUR / " + reader.GetAttribute("currency"), Description = string.Empty });
67            } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
68          }
69        }
70      } catch (Exception) {
71      }
72      return l;
73    }
74
75    public override IProblemData LoadData(IDataDescriptor descriptor) {
76      var values = new List<IList>();
77      var tList = new List<DateTime>();
78      var dList = new List<double>();
79      values.Add(tList);
80      values.Add(dList);
81      using (var client = new WebClient()) {
82        var s = client.OpenRead("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml");
83        if (s != null)
84
85          using (var reader = new XmlTextReader(s)) {
86            reader.MoveToContent();
87            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
88            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
89            // foreach time
90            do {
91              reader.MoveToAttribute("time");
92              tList.Add(reader.ReadContentAsDateTime());
93              reader.MoveToElement();
94              reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
95              // foreach currencys
96              do {
97                // find matching entry
98                if (descriptor.Name.Contains(reader.GetAttribute("currency"))) {
99                  reader.MoveToAttribute("rate");
100                  dList.Add(reader.ReadContentAsDouble());
101
102                  reader.MoveToElement();
103                  // skip remaining siblings
104                  while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")) ;
105                  break;
106                }
107              } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
108            } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
109          }
110      }
111      // keep only the rows with data for this exchange rate
112      if (tList.Count > dList.Count)
113        tList.RemoveRange(dList.Count, tList.Count - dList.Count);
114      else if (dList.Count > tList.Count)
115        dList.RemoveRange(tList.Count, dList.Count - tList.Count);
116
117      // entries in ECB XML are ordered most recent first => reverse lists
118      tList.Reverse();
119      dList.Reverse();
120
121      // calculate exchange rate deltas
122      var changes = new[] { 0.0 } // first element
123        .Concat(dList.Zip(dList.Skip(1), (prev, cur) => cur - prev)).ToList();
124      values.Add(changes);
125
126      var targetVariable = "d(" + descriptor.Name + ")";
127      var allowedInputVariables = new string[] { targetVariable };
128
129      var ds = new Dataset(new string[] { "Day", descriptor.Name, targetVariable }, values);
130      return new ProblemData(ds, allowedInputVariables, targetVariable);
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.