[9964] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9964] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections;
|
---|
| 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Net;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.Problems.Instances;
|
---|
| 28 | using System.Linq;
|
---|
| 29 |
|
---|
| 30 | namespace 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");
|
---|
[9991] | 101 | dList.Add(reader.ReadContentAsDouble());
|
---|
[9964] | 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 | }
|
---|
[10004] | 112 | // keep only the rows with data for this exchange rate
|
---|
| 113 | if (tList.Count > dList.Count)
|
---|
| 114 | tList.RemoveRange(dList.Count, tList.Count - dList.Count);
|
---|
| 115 | else if (dList.Count > tList.Count)
|
---|
| 116 | dList.RemoveRange(tList.Count, dList.Count - tList.Count);
|
---|
| 117 |
|
---|
[9991] | 118 | // entries in ECB XML are ordered most recent first => reverse lists
|
---|
| 119 | tList.Reverse();
|
---|
| 120 | dList.Reverse();
|
---|
[10004] | 121 |
|
---|
[9991] | 122 | // calculate exchange rate deltas
|
---|
| 123 | var changes = new[] { 0.0 } // first element
|
---|
| 124 | .Concat(dList.Zip(dList.Skip(1), (prev, cur) => cur - prev)).ToList();
|
---|
| 125 | values.Add(changes);
|
---|
| 126 |
|
---|
| 127 | var targetVariable = "d(" + descriptor.Name + ")";
|
---|
| 128 | var allowedInputVariables = new string[] { targetVariable };
|
---|
| 129 |
|
---|
| 130 | var ds = new Dataset(new string[] { "Day", descriptor.Name, targetVariable }, values);
|
---|
[9964] | 131 | return new ProblemData(ds, allowedInputVariables, targetVariable);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|