Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/EcbProblemInstanceProvider.cs @ 9989

Last change on this file since 9989 was 9989, checked in by gkronber, 11 years ago

#1508 worked on problem instance providers for trading problem

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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      }
71      catch (Exception) {
72      }
73      return l;
74    }
75
76    public override IProblemData LoadData(IDataDescriptor descriptor) {
77      var values = new List<IList>();
78      var tList = new List<DateTime>();
79      var dList = new List<double>();
80      double prevValue = 0;
81      values.Add(tList);
82      values.Add(dList);
83      using (var client = new WebClient()) {
84        var s = client.OpenRead("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml");
85        if (s != null)
86
87          using (var reader = new XmlTextReader(s)) {
88            reader.MoveToContent();
89            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
90            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
91            // foreach time
92            do {
93              reader.MoveToAttribute("time");
94              tList.Add(reader.ReadContentAsDateTime());
95              reader.MoveToElement();
96              reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
97              // foreach currencys
98              do {
99                // find matching entry
100                if (descriptor.Name.Contains(reader.GetAttribute("currency"))) {
101                  reader.MoveToAttribute("rate");
102                  var curValue = reader.ReadContentAsDouble();
103                  if (!dList.Any())
104                    dList.Add(0.0);
105                  else {
106                    dList.Add(curValue - prevValue);
107                  }
108                  prevValue = curValue;
109
110                  reader.MoveToElement();
111                  // skip remaining siblings
112                  while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")) ;
113                  break;
114                }
115              } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
116            } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
117          }
118      }
119      var allowedInputVariables = new string[] { descriptor.Name };
120      var targetVariable = descriptor.Name;
121      var ds = new Dataset(new string[] { "Day", descriptor.Name }, values);
122      return new ProblemData(ds, allowedInputVariables, targetVariable);
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.