Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/Model.cs @ 17021

Last change on this file since 17021 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 3.4 KB
RevLine 
[6123]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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
[16243]22using System;
[9743]23using System.Collections.Generic;
[6861]24using System.Linq;
[6123]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
[16565]28using HEAL.Attic;
[9822]29using HeuristicLab.Problems.DataAnalysis.Symbolic;
[6123]30
[9822]31namespace HeuristicLab.Problems.DataAnalysis.Trading.Symbolic {
[6123]32  /// <summary>
33  /// Represents a symbolic trading model
34  /// </summary>
[16565]35  [StorableType("EDBE6BAD-B331-4301-AA8C-234196942DF4")]
[9745]36  [Item(Name = "Model (symbolic trading)", Description = "Represents a symbolic trading model.")]
37  public class Model : SymbolicDataAnalysisModel, IModel {
[6123]38
39    [StorableConstructor]
[16565]40    protected Model(StorableConstructorFlag _) : base(_) { }
[9745]41    protected Model(Model original, Cloner cloner)
[6123]42      : base(original, cloner) { }
[9745]43    public Model(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter)
[9743]44      : base(tree, interpreter, -10, 10) { }
[6123]45
46    public override IDeepCloneable Clone(Cloner cloner) {
[9745]47      return new Model(this, cloner);
[6123]48    }
49
[12509]50    public IEnumerable<double> GetSignals(IDataset dataset, IEnumerable<int> rows) {
[6123]51      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = Interpreter;
52      ISymbolicExpressionTree tree = SymbolicExpressionTree;
[9744]53      return GetSignals(interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows));
[6123]54    }
[9744]55
[16243]56    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
57      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
58      return IsDatasetCompatible(problemData.Dataset, out errorMessage);
59    }
60
61
[9744]62    // Transforms an enumerable of real values to an enumerable of trading signals (buy(1) / hold(0) / sell(-1))
63    public static IEnumerable<double> GetSignals(IEnumerable<double> xs) {
64      // two iterations over xs
65      // 1) determine min / max to calculate the mid-range value
66      // 2) range is split into three thirds
67      double max = double.NegativeInfinity;
68      double min = double.PositiveInfinity;
69      foreach (var x in xs) {
70        if (x > max) max = x;
71        if (x < min) min = x;
72      }
73      if (double.IsInfinity(max) || double.IsNaN(max) || double.IsInfinity(min) || double.IsNaN(min))
74        return xs.Select(x => 0.0);
75
76      double range = (max - min);
77      double midRange = range / 2.0 + min;
78      double offset = range / 6.0;
79      return from x in xs
80             select x > midRange + offset ? 1.0 : x < midRange - offset ? -1.0 : 0.0;
81    }
[6123]82  }
83}
Note: See TracBrowser for help on using the repository browser.