source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Solution.cs @ 9928

Last change on this file since 9928 was 9928, checked in by mkommend, 7 years ago

#1508: Minor code cleanup in trading plugin.

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