Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Calculators/OnlineProfitCalculator.cs @ 9176

Last change on this file since 9176 was 9176, checked in by gkronber, 11 years ago

#1508 updated trading plugin to work with current trunk version

File size: 3.9 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 HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineProfitCalculator : IOnlineCalculator {
28
29    private int p;
30    private double transactionCost;
31    private int c;
32    private double sum;
33    public double Profit {
34      get { return sum; }
35    }
36
37    public OnlineProfitCalculator(double transactionCost) {
38      this.transactionCost = transactionCost;
39      Reset();
40    }
41
42    #region IOnlineCalculator Members
43    public OnlineCalculatorError ErrorState {
44      get { return OnlineCalculatorError.None; }
45    }
46    public double Value {
47      get { return Profit; }
48    }
49    public void Reset() {
50      p = 0;
51      c = 0;
52      sum = 0.0;
53    }
54
55    public void Add(double actualReturn, double signal) {
56      double iterationReturn = 0.0;
57      if (c == 0) {
58        p = (int)signal;
59        iterationReturn = 0;
60        c++;
61      } else {
62        if (p == 0 && signal.IsAlmost(0)) {
63        } else if (p == 0 && signal.IsAlmost(1)) {
64          p = 1;
65        } else if (p == 0 && signal.IsAlmost(-1)) {
66          p = -1;
67        } else if (p == 1 && signal.IsAlmost(1)) {
68          iterationReturn = actualReturn;
69        } else if (p == 1 && signal.IsAlmost(0)) {
70          iterationReturn = actualReturn - transactionCost;
71          p = 0;
72        } else if (p == 1 && signal.IsAlmost(-1)) {
73          iterationReturn = actualReturn - transactionCost;
74          p = -1;
75        } else if (p == -1 && signal.IsAlmost(-1)) {
76          iterationReturn = -actualReturn;
77        } else if (p == -1 && signal.IsAlmost(0)) {
78          iterationReturn = -actualReturn - transactionCost;
79          p = 0;
80        } else if (p == -1 && signal.IsAlmost(1)) {
81          iterationReturn = -actualReturn - transactionCost;
82          p = 1;
83        }
84        c++;
85      }
86      sum += iterationReturn;
87    }
88    #endregion
89
90    public static double Calculate(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost, out OnlineCalculatorError errorState) {
91      IEnumerator<double> returnsEnumerator = returns.GetEnumerator();
92      IEnumerator<double> signalsEnumerator = signals.GetEnumerator();
93      OnlineProfitCalculator calculator = new OnlineProfitCalculator(transactionCost);
94
95      // always move forward both enumerators (do not use short-circuit evaluation!)
96      while (returnsEnumerator.MoveNext() & signalsEnumerator.MoveNext()) {
97        double signal = signalsEnumerator.Current;
98        double @return = returnsEnumerator.Current;
99        calculator.Add(@return, signal);
100      }
101
102      // check if both enumerators are at the end to make sure both enumerations have the same length
103      if (returnsEnumerator.MoveNext() || signalsEnumerator.MoveNext()) {
104        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
105      } else {
106        errorState = calculator.ErrorState;
107        return calculator.Profit;
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.