Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1508 fixed bugs in line chart visualization for the trading plugin

File size: 5.1 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      values.Add(tList);
81      values.Add(dList);
82      using (var client = new WebClient()) {
83        var s = client.OpenRead("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.xml");
84        if (s != null)
85
86          using (var reader = new XmlTextReader(s)) {
87            reader.MoveToContent();
88            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
89            reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
90            // foreach time
91            do {
92              reader.MoveToAttribute("time");
93              tList.Add(reader.ReadContentAsDateTime());
94              reader.MoveToElement();
95              reader.ReadToDescendant("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
96              // foreach currencys
97              do {
98                // find matching entry
99                if (descriptor.Name.Contains(reader.GetAttribute("currency"))) {
100                  reader.MoveToAttribute("rate");
101                  dList.Add(reader.ReadContentAsDouble());
102
103                  reader.MoveToElement();
104                  // skip remaining siblings
105                  while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref")) ;
106                  break;
107                }
108              } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
109            } while (reader.ReadToNextSibling("Cube", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"));
110          }
111      }
112      // entries in ECB XML are ordered most recent first => reverse lists
113      tList.Reverse();
114      dList.Reverse();
115      // calculate exchange rate deltas
116      var changes = new[] { 0.0 } // first element
117        .Concat(dList.Zip(dList.Skip(1), (prev, cur) => cur - prev)).ToList();
118      values.Add(changes);
119
120      var targetVariable = "d(" + descriptor.Name + ")";
121      var allowedInputVariables = new string[] { targetVariable };
122
123      var ds = new Dataset(new string[] { "Day", descriptor.Name, targetVariable }, values);
124      return new ProblemData(ds, allowedInputVariables, targetVariable);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.