public static IEnumerable <double> GetProfits(IEnumerable <double> returns, IEnumerable <double> signals, double transactionCost) { var calc = new OnlineProfitCalculator(transactionCost); IEnumerator <double> returnsEnumerator = returns.GetEnumerator(); IEnumerator <double> signalsEnumerator = signals.GetEnumerator(); // always move forward both enumerators (do not use short-circuit evaluation!) while (returnsEnumerator.MoveNext() & signalsEnumerator.MoveNext()) { double signal = signalsEnumerator.Current; double @return = returnsEnumerator.Current; double prevTotalProfit = calc.Profit; calc.Add(@return, signal); double curTotalProfit = calc.Profit; yield return(curTotalProfit - prevTotalProfit); } // check if both enumerators are at the end to make sure both enumerations have the same length if (returnsEnumerator.MoveNext() || signalsEnumerator.MoveNext()) { throw new ArgumentException("Number of elements in first and second enumeration doesn't match."); } }
public void Add(double actualReturn, double signal) { double prevTotalProfit = profitCalculator.Profit; profitCalculator.Add(actualReturn, signal); double curTotalProfit = profitCalculator.Profit; meanAndVarianceCalculator.Add(curTotalProfit - prevTotalProfit); }
public static IEnumerable<double> GetProfits(IEnumerable<double> returns, IEnumerable<double> signals, double transactionCost) { var calc = new OnlineProfitCalculator(transactionCost); IEnumerator<double> returnsEnumerator = returns.GetEnumerator(); IEnumerator<double> signalsEnumerator = signals.GetEnumerator(); // always move forward both enumerators (do not use short-circuit evaluation!) while (returnsEnumerator.MoveNext() & signalsEnumerator.MoveNext()) { double signal = signalsEnumerator.Current; double @return = returnsEnumerator.Current; double prevTotalProfit = calc.Profit; calc.Add(@return, signal); double curTotalProfit = calc.Profit; yield return curTotalProfit - prevTotalProfit; } // check if both enumerators are at the end to make sure both enumerations have the same length if (returnsEnumerator.MoveNext() || signalsEnumerator.MoveNext()) { throw new ArgumentException("Number of elements in first and second enumeration doesn't match."); } }