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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 |
|
---|
30 | namespace 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 | private const string TrainingProfitResultName = "Profit (training)";
|
---|
39 | private const string TestProfitResultName = "Profit (test)";
|
---|
40 |
|
---|
41 | public new ITradingModel Model {
|
---|
42 | get { return (ITradingModel)base.Model; }
|
---|
43 | protected set { base.Model = value; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public new ITradingProblemData ProblemData {
|
---|
47 | get { return (ITradingProblemData)base.ProblemData; }
|
---|
48 | protected set { base.ProblemData = value; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public double TrainingSharpeRatio {
|
---|
52 | get { return ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value; }
|
---|
53 | private set { ((DoubleValue)this[TrainingSharpeRatioResultName].Value).Value = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public double TestSharpeRatio {
|
---|
57 | get { return ((DoubleValue)this[TestSharpeRatioResultName].Value).Value; }
|
---|
58 | private set { ((DoubleValue)this[TestSharpeRatioResultName].Value).Value = value; }
|
---|
59 | }
|
---|
60 | public double TrainingProfit {
|
---|
61 | get { return ((DoubleValue)this[TrainingProfitResultName].Value).Value; }
|
---|
62 | private set { ((DoubleValue)this[TrainingProfitResultName].Value).Value = value; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public double TestProfit {
|
---|
66 | get { return ((DoubleValue)this[TestProfitResultName].Value).Value; }
|
---|
67 | private set { ((DoubleValue)this[TestProfitResultName].Value).Value = value; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | protected TradingSolution(bool deserializing) : base(deserializing) { }
|
---|
72 | protected TradingSolution(TradingSolution original, Cloner cloner)
|
---|
73 | : base(original, cloner) {
|
---|
74 | }
|
---|
75 | public TradingSolution(ITradingModel model, ITradingProblemData problemData)
|
---|
76 | : base(model, problemData) {
|
---|
77 | Add(new Result(TrainingSharpeRatioResultName, "Share ratio of the signals of the model on the training partition", new DoubleValue()));
|
---|
78 | Add(new Result(TestSharpeRatioResultName, "Sharpe ratio of the signals of the model on the test partition", new DoubleValue()));
|
---|
79 | Add(new Result(TrainingProfitResultName, "Profit of the model on the training partition", new DoubleValue()));
|
---|
80 | Add(new Result(TestProfitResultName, "Profit of the model on the test partition", new DoubleValue()));
|
---|
81 |
|
---|
82 | RecalculateResults();
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | protected override void OnModelChanged() {
|
---|
87 | base.OnModelChanged();
|
---|
88 | RecalculateResults();
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void OnProblemDataChanged() {
|
---|
92 | base.OnProblemDataChanged();
|
---|
93 | RecalculateResults();
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected override void RecalculateResults() {
|
---|
97 | double[] trainingSignals = TrainingSignals.ToArray(); // cache values
|
---|
98 | IEnumerable<double> trainingReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TrainingIndices);
|
---|
99 | double[] testSignals = TestSignals.ToArray(); // cache values
|
---|
100 | IEnumerable<double> testReturns = ProblemData.Dataset.GetDoubleValues(ProblemData.PriceVariable, ProblemData.TestIndices);
|
---|
101 |
|
---|
102 | OnlineCalculatorError errorState;
|
---|
103 | double trainingSharpeRatio = OnlineSharpeRatioCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
|
---|
104 | TrainingSharpeRatio = errorState == OnlineCalculatorError.None ? trainingSharpeRatio : double.NaN;
|
---|
105 | double testSharpeRatio = OnlineSharpeRatioCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
|
---|
106 | TestSharpeRatio = errorState == OnlineCalculatorError.None ? testSharpeRatio : double.NaN;
|
---|
107 |
|
---|
108 | double trainingProfit = OnlineProfitCalculator.Calculate(trainingReturns, trainingSignals, ProblemData.TransactionCosts, out errorState);
|
---|
109 | TrainingProfit = errorState == OnlineCalculatorError.None ? trainingProfit : double.NaN;
|
---|
110 | double testProfit = OnlineProfitCalculator.Calculate(testReturns, testSignals, ProblemData.TransactionCosts, out errorState);
|
---|
111 | TestProfit = errorState == OnlineCalculatorError.None ? testProfit : double.NaN;
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | public virtual IEnumerable<double> Signals {
|
---|
116 | get {
|
---|
117 | return GetSignals(Enumerable.Range(0, ProblemData.Dataset.Rows));
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public virtual IEnumerable<double> TrainingSignals {
|
---|
122 | get {
|
---|
123 | return GetSignals(ProblemData.TrainingIndices);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | public virtual IEnumerable<double> TestSignals {
|
---|
128 | get {
|
---|
129 | return GetSignals(ProblemData.TestIndices);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public virtual IEnumerable<double> GetSignals(IEnumerable<int> rows) {
|
---|
134 | return Model.GetSignals(ProblemData.Dataset, rows);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|