Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/TradingSolution.cs @ 6125

Last change on this file since 6125 was 6125, checked in by gkronber, 13 years ago

#1508 folder restructuring

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis {
31  /// <summary>
32  /// Abstract base class for trading data analysis solutions
33  /// </summary>
34  [StorableClass]
35  public abstract class TradingSolution : DataAnalysisSolution, ITradingSolution {
36    private const string TrainingSharpeRatioResultName = "Sharpe ratio (training)";
37    private const string TestSharpeRatioResultName = "Sharpe ratio (test)";
38
39    public new ITradingModel Model {
40      get { return (ITradingModel)base.Model; }
41      protected set { base.Model = value; }
42    }
43
44    public new ITradingProblemData ProblemData {
45      get { return (ITradingProblemData)base.ProblemData; }
46      protected set { base.ProblemData = value; }
47    }
48
49    public double TrainingSharpeRatio {
50      get { return ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value; }
51      private set { ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value = value; }
52    }
53
54    public double TestSharpeRatio {
55      get { return ((DoubleValue)this[TestSharpeRatioResultName].Value).Value; }
56      private set { ((DoubleValue)this[TestSharpeRatioResultName].Value).Value = value; }
57    }
58
59    [StorableConstructor]
60    protected TradingSolution(bool deserializing) : base(deserializing) { }
61    protected TradingSolution(TradingSolution original, Cloner cloner)
62      : base(original, cloner) {
63    }
64    public TradingSolution(ITradingModel model, ITradingProblemData problemData)
65      : base(model, problemData) {
66      Add(new Result(TrainingSharpeRatioResultName, "Share ratio of the signals of the model on the training partition", new DoubleValue()));
67      Add(new Result(TestSharpeRatioResultName, "Sharpe ratio of the signals of the model on the test partition", new DoubleValue()));
68
69      RecalculateResults();
70    }
71
72    protected override void OnProblemDataChanged(EventArgs e) {
73      base.OnProblemDataChanged(e);
74      RecalculateResults();
75    }
76    protected override void OnModelChanged(EventArgs e) {
77      base.OnModelChanged(e);
78      RecalculateResults();
79    }
80
81    protected void RecalculateResults() {
82      double[] trainingSignals = TrainingSignals.ToArray(); // cache values
83      IEnumerable<double> trainingReturns = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.PriceVariable, ProblemData.TrainingIndizes);
84      double[] testSignals = TestSignals.ToArray(); // cache values
85      IEnumerable<double> testReturns = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.PriceVariable, ProblemData.TestIndizes);
86   
87      OnlineCalculatorError errorState;
88      double trainingSharpeRatio = OnlineSharpeRatioCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
89      TrainingSharpeRatio = errorState == OnlineCalculatorError.None ? trainingSharpeRatio : double.NaN;
90      double testSharpeRatio = OnlineSharpeRatioCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
91      TestSharpeRatio = errorState == OnlineCalculatorError.None ? testSharpeRatio : double.NaN;
92
93    }
94
95    public virtual IEnumerable<double> Signals {
96      get {
97        return GetSignals(Enumerable.Range(0, ProblemData.Dataset.Rows));
98      }
99    }
100
101    public virtual IEnumerable<double> TrainingSignals {
102      get {
103        return GetSignals(ProblemData.TrainingIndizes);
104      }
105    }
106
107    public virtual IEnumerable<double> TestSignals {
108      get {
109        return GetSignals(ProblemData.TestIndizes);
110      }
111    }
112
113    public virtual IEnumerable<double> GetSignals(IEnumerable<int> rows) {
114      return Model.GetSignals(ProblemData.Dataset, rows);
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.