Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Calculators/OnlineSharpeRatioCalculator.cs @ 6861

Last change on this file since 6861 was 6861, checked in by gkronber, 12 years ago

#1508 bug fixes

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