Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Calculators/OnlineSharpeRatioCalculator.cs @ 14303

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

#2649: merged r14226 and r14292 from trunk to stable

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
24
25namespace HeuristicLab.Problems.DataAnalysis.Trading {
26  public class OnlineSharpeRatioCalculator : IOnlineCalculator {
27
28    private OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
29    private OnlineProfitCalculator profitCalculator;
30
31    public double SharpeRatio {
32      get {
33        if (meanAndVarianceCalculator.PopulationVariance > 0)
34          return meanAndVarianceCalculator.Mean / Math.Sqrt(meanAndVarianceCalculator.PopulationVariance);
35        else return 0.0;
36      }
37    }
38
39    public OnlineSharpeRatioCalculator(double transactionCost) {
40      this.meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
41      this.profitCalculator = new OnlineProfitCalculator(transactionCost);
42      Reset();
43    }
44
45    #region IOnlineCalculator Members
46    public OnlineCalculatorError ErrorState {
47      get {
48        return meanAndVarianceCalculator.MeanErrorState | meanAndVarianceCalculator.PopulationVarianceErrorState | profitCalculator.ErrorState;
49      }
50    }
51    public double Value {
52      get { return SharpeRatio; }
53    }
54    public void Reset() {
55      profitCalculator.Reset();
56      meanAndVarianceCalculator.Reset();
57    }
58
59    public void Add(double actualReturn, double signal) {
60      double prevTotalProfit = profitCalculator.Profit;
61      profitCalculator.Add(actualReturn, signal);
62      double curTotalProfit = profitCalculator.Profit;
63
64      meanAndVarianceCalculator.Add(curTotalProfit - prevTotalProfit);
65    }
66    #endregion
67
68    public static double Calculate(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost, out OnlineCalculatorError errorState) {
69      IEnumerator<double> returnsEnumerator = returns.GetEnumerator();
70      IEnumerator<double> signalsEnumerator = signals.GetEnumerator();
71      OnlineSharpeRatioCalculator calculator = new OnlineSharpeRatioCalculator(transactionCost);
72
73      // always move forward both enumerators (do not use short-circuit evaluation!)
74      while (returnsEnumerator.MoveNext() & signalsEnumerator.MoveNext()) {
75        double signal = signalsEnumerator.Current;
76        double @return = returnsEnumerator.Current;
77        calculator.Add(@return, signal);
78      }
79
80      // check if both enumerators are at the end to make sure both enumerations have the same length
81      if (returnsEnumerator.MoveNext() || signalsEnumerator.MoveNext()) {
82        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
83      } else {
84        errorState = calculator.ErrorState;
85        return calculator.SharpeRatio;
86      }
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.