Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6861


Ignore:
Timestamp:
10/03/11 17:51:02 (13 years ago)
Author:
gkronber
Message:

#1508 bug fixes

Location:
branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Calculators/OnlineSharpeRatioCalculator.cs

    r6123 r6861  
    2929    private int p;
    3030    private double transactionCost;
     31    private int c;
    3132    private OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
    3233    public double SharpeRatio {
     
    5556    public void Reset() {
    5657      p = 0;
     58      c = 0;
    5759      meanAndVarianceCalculator.Reset();
    5860    }
     
    6062    public void Add(double actualReturn, double signal) {
    6163      double iterationReturn = 0.0;
    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;
     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++;
    8392      }
    8493      meanAndVarianceCalculator.Add(iterationReturn);
  • branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/SingleObjective/SymbolicTradingSingleObjectiveSharpeRatioEvaluator.cs

    r6123 r6861  
    6868
    6969    private static IEnumerable<double> GetSignals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, Dataset dataset, IEnumerable<int> rows) {
    70       double signal = 0;
    71       double prevSignal = 0;
    72       foreach (var x in interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows)) {
    73         if (prevSignal == 1) {
    74           signal = x > -1 ? 1 : x > -2 ? 0 : -1; // 0, -1, -2
    75         } else if (prevSignal == 0) {
    76           signal = x > 1 ? 1 : x < -1 ? -1 : 0; // 1, 0 , -1
    77         } else if (prevSignal == -1) {
    78           signal = x < 1 ? -1 : x < 2 ? 0 : 1; // 2, 1, 0
    79         }
    80         yield return signal;
    81         prevSignal = signal;
    82       }
     70      return from x in interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows)
     71             select x > 0.5 ? 1.0 : x < 0.5 ? -1.0 : 0.0;
    8372    }
    8473
  • branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Symbolic/SymbolicTradingModel.cs

    r6126 r6861  
    2020#endregion
    2121
     22using System.Linq;
    2223using System.Collections.Generic;
    2324using HeuristicLab.Common;
     
    4849      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = Interpreter;
    4950      ISymbolicExpressionTree tree = SymbolicExpressionTree;
    50       double signal = 0;
    51       double prevSignal = 0;
    52       foreach (var x in interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)) {
    53         if (prevSignal == 1) {
    54           signal = x > -1 ? 1 : x > -2 ? 0 : -1; // 0, -1, -2
    55         } else if (prevSignal == 0) {
    56           signal = x > 1 ? 1 : x < -1 ? -1 : 0; // 1, 0 , -1
    57         } else if (prevSignal == -1) {
    58           signal = x < 1 ? -1 : x < 2 ? 0 : 1; // 2, 1, 0
    59         }
    60         yield return signal;
    61         prevSignal = signal;
    62       }
     51      return from x in interpreter.GetSymbolicExpressionTreeValues(tree, dataset, rows)
     52             select x > 0.5 ? 1.0 : x < 0.5 ? -1.0 : 0.0;
    6353    }
    6454  }
  • branches/HeuristicLab.Problems.DataAnalysis.Trading/HeuristicLab.Problems.DataAnalysis.Trading/3.4/Views/TradingSolutionLineChartView.cs

    r6138 r6861  
    9494
    9595    private IEnumerable<double> GetProfits(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost) {
    96       double p = 0;
    97       foreach (var signalReturn in returns.Zip(signals, (r, s) => new { Return = r, Signal = s })) {
    98         double iterationReturn = 0.0;
     96      double p = signals.First();
     97      yield return 0.0;
     98      foreach (var signalReturn in returns.Skip(1).Zip(signals.Skip(1), (r, s) => new { Return = r, Signal = s })) {
     99        double iterationReturn = 0;
    99100        double signal = signalReturn.Signal;
    100101        double actualReturn = signalReturn.Return;
    101102        if (p == 0 && signal.IsAlmost(0)) {
    102103        } else if (p == 0 && signal.IsAlmost(1)) {
     104          iterationReturn = 0;
    103105          p = 1;
    104106        } else if (p == 0 && signal.IsAlmost(-1)) {
     107          iterationReturn = 0;
    105108          p = -1;
    106109        } else if (p == 1 && signal.IsAlmost(1)) {
     
    115118          iterationReturn = -actualReturn;
    116119        } else if (p == -1 && signal.IsAlmost(0)) {
    117           iterationReturn = -actualReturn - transactionCost;
     120          iterationReturn = -(actualReturn - transactionCost);
    118121          p = 0;
    119122        } else if (p == -1 && signal.IsAlmost(1)) {
    120           iterationReturn = -actualReturn - transactionCost;
    121           p = -1;
     123          iterationReturn = -(actualReturn - transactionCost);
     124          p = 1;
    122125        }
    123126        yield return iterationReturn;
Note: See TracChangeset for help on using the changeset viewer.