Search in sources :

Example 1 with Drawdown

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));
}
Also used : LocalDate(java.time.LocalDate) Drawdown(name.abuchen.portfolio.math.Risk.Drawdown) Test(org.junit.Test)

Example 2 with Drawdown

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())));
}
Also used : PerformanceIndex(name.abuchen.portfolio.snapshot.PerformanceIndex) Drawdown(name.abuchen.portfolio.math.Risk.Drawdown) Interval(name.abuchen.portfolio.util.Interval)

Example 3 with Drawdown

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;
}
Also used : Drawdown(name.abuchen.portfolio.math.Risk.Drawdown)

Aggregations

Drawdown (name.abuchen.portfolio.math.Risk.Drawdown)3 LocalDate (java.time.LocalDate)1 PerformanceIndex (name.abuchen.portfolio.snapshot.PerformanceIndex)1 Interval (name.abuchen.portfolio.util.Interval)1 Test (org.junit.Test)1