/** * Calculates the Maximum drawdown from a cash flow over a series. * @param series the time series * @param cashFlow the cash flow * @return the Maximum drawdown from a cash flow over a series */ private decimal CalculateMaximumDrawdown(ITimeSeries series, CashFlow cashFlow) { decimal MaximumDrawdown = Decimals.Zero; decimal MaxPeak = Decimals.Zero; if (!series.IsEmpty()) { // The series is not empty for (int i = series.GetBeginIndex(); i <= series.GetEndIndex(); i++) { decimal value = cashFlow.GetValue(i); if (value.IsGreaterThan(MaxPeak)) { MaxPeak = value; } decimal drawdown = MaxPeak.Minus(value).DividedBy(MaxPeak); if (drawdown.IsGreaterThan(MaximumDrawdown)) { MaximumDrawdown = drawdown; } } } return(MaximumDrawdown); }
public void getEndGetBeginGetBarCountIsEmpty() { // series Assert.AreEqual(0, defaultSeries.GetBeginIndex()); Assert.AreEqual(bars.Count - 1, defaultSeries.GetEndIndex()); Assert.AreEqual(bars.Count, defaultSeries.GetBarCount()); Assert.IsFalse(defaultSeries.IsEmpty()); // Constrained series Assert.AreEqual(2, constrainedSeries.GetBeginIndex()); Assert.AreEqual(4, constrainedSeries.GetEndIndex()); Assert.AreEqual(3, constrainedSeries.GetBarCount()); Assert.IsFalse(constrainedSeries.IsEmpty()); // Empty series Assert.AreEqual(-1, emptySeries.GetBeginIndex()); Assert.AreEqual(-1, emptySeries.GetEndIndex()); Assert.AreEqual(0, emptySeries.GetBarCount()); Assert.IsTrue(emptySeries.IsEmpty()); }