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 HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace 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 | }
|
---|