Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/24/13 12:51:02 (11 years ago)
Author:
gkronber
Message:

#1508 refactoring: removed unused classes, unified calculation of profits and signals, implemented profit-evaluator.

File:
1 edited

Legend:

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

    r9743 r9744  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526
     
    2829
    2930    private int p;
    30     private double transactionCost;
     31    private readonly double transactionCost;
    3132    private int c;
    3233    private double sum;
     
    6364        } else if (p == 0 && signal.IsAlmost(1)) {
    6465          p = 1;
     66          iterationReturn = -transactionCost;
    6567        } else if (p == 0 && signal.IsAlmost(-1)) {
    6668          p = -1;
     69          iterationReturn = -transactionCost;
    6770        } else if (p == 1 && signal.IsAlmost(1)) {
    6871          iterationReturn = actualReturn;
     
    8992
    9093    public static double Calculate(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost, out OnlineCalculatorError errorState) {
     94      errorState = OnlineCalculatorError.None;
     95      return GetProfits(returns, signals, transactionCost).Sum();
     96    }
     97
     98    public static IEnumerable<double> GetProfits(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost) {
     99      var calc = new OnlineProfitCalculator(transactionCost);
     100
    91101      IEnumerator<double> returnsEnumerator = returns.GetEnumerator();
    92102      IEnumerator<double> signalsEnumerator = signals.GetEnumerator();
    93       OnlineProfitCalculator calculator = new OnlineProfitCalculator(transactionCost);
    94103
    95104      // always move forward both enumerators (do not use short-circuit evaluation!)
     
    97106        double signal = signalsEnumerator.Current;
    98107        double @return = returnsEnumerator.Current;
    99         calculator.Add(@return, signal);
     108
     109        double prevTotalProfit = calc.Profit;
     110        calc.Add(@return, signal);
     111        double curTotalProfit = calc.Profit;
     112
     113        yield return curTotalProfit - prevTotalProfit;
    100114      }
    101115
     
    103117      if (returnsEnumerator.MoveNext() || signalsEnumerator.MoveNext()) {
    104118        throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
    105       } else {
    106         errorState = calculator.ErrorState;
    107         return calculator.Profit;
    108119      }
    109120    }
Note: See TracChangeset for help on using the changeset viewer.