[6123] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6123] | 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 |
|
---|
[9743] | 22 | using System.Collections.Generic;
|
---|
[6861] | 23 | using System.Linq;
|
---|
[6123] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[9822] | 28 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
[6123] | 29 |
|
---|
[9822] | 30 | namespace HeuristicLab.Problems.DataAnalysis.Trading.Symbolic {
|
---|
[6123] | 31 | /// <summary>
|
---|
| 32 | /// Represents a symbolic trading model
|
---|
| 33 | /// </summary>
|
---|
[14711] | 34 | [StorableType("7B314E74-BB15-48D9-B007-B9064E082CB3")]
|
---|
[9745] | 35 | [Item(Name = "Model (symbolic trading)", Description = "Represents a symbolic trading model.")]
|
---|
| 36 | public class Model : SymbolicDataAnalysisModel, IModel {
|
---|
[6123] | 37 |
|
---|
| 38 | [StorableConstructor]
|
---|
[9745] | 39 | protected Model(bool deserializing) : base(deserializing) { }
|
---|
| 40 | protected Model(Model original, Cloner cloner)
|
---|
[6123] | 41 | : base(original, cloner) { }
|
---|
[9745] | 42 | public Model(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter)
|
---|
[9743] | 43 | : base(tree, interpreter, -10, 10) { }
|
---|
[6123] | 44 |
|
---|
| 45 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[9745] | 46 | return new Model(this, cloner);
|
---|
[6123] | 47 | }
|
---|
| 48 |
|
---|
[12509] | 49 | public IEnumerable<double> GetSignals(IDataset dataset, IEnumerable<int> rows) {
|
---|
[6123] | 50 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = Interpreter;
|
---|
| 51 | ISymbolicExpressionTree tree = SymbolicExpressionTree;
|
---|
[9744] | 52 | return GetSignals(interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows));
|
---|
[6123] | 53 | }
|
---|
[9744] | 54 |
|
---|
| 55 | // Transforms an enumerable of real values to an enumerable of trading signals (buy(1) / hold(0) / sell(-1))
|
---|
| 56 | public static IEnumerable<double> GetSignals(IEnumerable<double> xs) {
|
---|
| 57 | // two iterations over xs
|
---|
| 58 | // 1) determine min / max to calculate the mid-range value
|
---|
| 59 | // 2) range is split into three thirds
|
---|
| 60 | double max = double.NegativeInfinity;
|
---|
| 61 | double min = double.PositiveInfinity;
|
---|
| 62 | foreach (var x in xs) {
|
---|
| 63 | if (x > max) max = x;
|
---|
| 64 | if (x < min) min = x;
|
---|
| 65 | }
|
---|
| 66 | if (double.IsInfinity(max) || double.IsNaN(max) || double.IsInfinity(min) || double.IsNaN(min))
|
---|
| 67 | return xs.Select(x => 0.0);
|
---|
| 68 |
|
---|
| 69 | double range = (max - min);
|
---|
| 70 | double midRange = range / 2.0 + min;
|
---|
| 71 | double offset = range / 6.0;
|
---|
| 72 | return from x in xs
|
---|
| 73 | select x > midRange + offset ? 1.0 : x < midRange - offset ? -1.0 : 0.0;
|
---|
| 74 | }
|
---|
[6123] | 75 | }
|
---|
| 76 | }
|
---|