use of name.abuchen.portfolio.math.Risk.Drawdown in project portfolio by buchen.
the class RiskTest method testDrawdown.
@Test
public void testDrawdown() {
int size = 10;
LocalDate[] dates = getDates(size);
double[] values = new double[size];
for (int i = 0; i < size; i++) values[i] = i;
Drawdown drawdown = new Drawdown(values, dates, 0);
// Every new value is a new peak, so there never is a drawdown and
// therefore the magnitude is 0
assertThat(drawdown.getMaxDrawdown(), is(0d));
// Drawdown duration is the longest duration between peaks. Every value
// is a peak, so 1 day is every time the duration. The fact that there
// is never a drawdown does not negate the duration
assertThat(drawdown.getMaxDrawdownDuration().getDays(), is(1l));
drawdown = new Drawdown(new double[] { 1, 1, -0.5, 1, 1, 2, 3, 4, 5, 6 }, dates, 0);
// the drawdown is from 2 to 0.5 which is 1.5 or 75% of 2
assertThat(drawdown.getMaxDrawdown(), is(0.75d));
// the first peak is the first 2. The second 2 is not a peak, the next
// peak is the 3, which is 5 days later
assertThat(drawdown.getMaxDrawdownDuration().getDays(), is(5l));
drawdown = new Drawdown(new double[] { 0, 0.1, 0.2, -1.4 }, getDates(4), 0);
// The drawdown is from 1.2 to -0.4 or 1.6, which is 4/3 from 1.2
assertThat(drawdown.getMaxDrawdown(), closeTo(4d / 3, 0.1e-10));
}
use of name.abuchen.portfolio.math.Risk.Drawdown in project portfolio by buchen.
the class MaxDrawdownDurationWidget method update.
@Override
public void update() {
super.update();
PerformanceIndex index = getDashboardData().getDataSeriesCache().lookup(get(DataSeriesConfig.class).getDataSeries(), get(ReportingPeriodConfig.class).getReportingPeriod());
Drawdown drawdown = index.getDrawdown();
Interval maxDDDuration = drawdown.getMaxDrawdownDuration();
indicator.setText(MessageFormat.format(Messages.LabelXDays, maxDDDuration.getDays()));
boolean isUntilEndOfPeriod = maxDDDuration.getEnd().equals(index.getReportInterval().getEndDate());
String maxDDSupplement = isUntilEndOfPeriod ? Messages.TooltipMaxDrawdownDurationEndOfPeriod : Messages.TooltipMaxDrawdownDurationFromXtoY;
// recovery time
Interval recoveryTime = drawdown.getLongestRecoveryTime();
isUntilEndOfPeriod = recoveryTime.getEnd().equals(index.getReportInterval().getEndDate());
String recoveryTimeSupplement = isUntilEndOfPeriod ? Messages.TooltipMaxDrawdownDurationEndOfPeriod : Messages.TooltipMaxDrawdownDurationFromXtoY;
InfoToolTip.attach(indicator, // $NON-NLS-1$
Messages.TooltipMaxDrawdownDuration + "\n\n" + MessageFormat.format(maxDDSupplement, formatter.format(maxDDDuration.getStart()), formatter.format(maxDDDuration.getEnd())) + // $NON-NLS-1$
"\n\n" + MessageFormat.format(Messages.TooltipMaxDurationLowToHigh, recoveryTime.getDays()) + MessageFormat.format(recoveryTimeSupplement, formatter.format(recoveryTime.getStart()), formatter.format(recoveryTime.getEnd())));
}
use of name.abuchen.portfolio.math.Risk.Drawdown in project portfolio by buchen.
the class PerformanceIndex method getDrawdown.
public Drawdown getDrawdown() {
if (drawdown == null) {
int startAt = 0;
for (; startAt < totals.length; startAt++) if (totals[startAt] != 0)
break;
if (startAt == totals.length)
startAt = totals.length - 1;
drawdown = new Drawdown(accumulated, dates, startAt);
}
return drawdown;
}
Aggregations