Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Calculators/OnlineSharpeRatioCalculator.cs @ 12448

Last change on this file since 12448 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.4 KB
RevLine 
[6123]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
24
[9743]25namespace HeuristicLab.Problems.DataAnalysis.Trading {
[6123]26  public class OnlineSharpeRatioCalculator : IOnlineCalculator {
27
28    private OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
[9744]29    private OnlineProfitCalculator profitCalculator;
30
[6123]31    public double SharpeRatio {
32      get {
33        if (meanAndVarianceCalculator.Variance > 0)
34          return meanAndVarianceCalculator.Mean / Math.Sqrt(meanAndVarianceCalculator.Variance);
35        else return 0.0;
36      }
37    }
38
39    public OnlineSharpeRatioCalculator(double transactionCost) {
40      this.meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
[9744]41      this.profitCalculator = new OnlineProfitCalculator(transactionCost);
[6123]42      Reset();
43    }
44
45    #region IOnlineCalculator Members
46    public OnlineCalculatorError ErrorState {
47      get {
[9744]48        return meanAndVarianceCalculator.MeanErrorState | meanAndVarianceCalculator.VarianceErrorState | profitCalculator.ErrorState;
[6123]49      }
50    }
51    public double Value {
52      get { return SharpeRatio; }
53    }
54    public void Reset() {
[9744]55      profitCalculator.Reset();
[6123]56      meanAndVarianceCalculator.Reset();
57    }
58
59    public void Add(double actualReturn, double signal) {
[9744]60      double prevTotalProfit = profitCalculator.Profit;
61      profitCalculator.Add(actualReturn, signal);
62      double curTotalProfit = profitCalculator.Profit;
63
64      meanAndVarianceCalculator.Add(curTotalProfit - prevTotalProfit);
[6123]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.