Search in sources :

Example 56 with Decimal

use of org.ta4j.core.Decimal in project ta4j by ta4j.

the class MaximumDrawdownCriterion method calculateMaximumDrawdown.

/**
 * 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(TimeSeries series, CashFlow cashFlow) {
    Decimal maximumDrawdown = Decimal.ZERO;
    Decimal maxPeak = Decimal.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;
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 57 with Decimal

use of org.ta4j.core.Decimal in project ta4j by ta4j.

the class MaximumDrawdownCriterion method calculate.

@Override
public double calculate(TimeSeries series, Trade trade) {
    if (trade != null && trade.getEntry() != null && trade.getExit() != null) {
        CashFlow cashFlow = new CashFlow(series, trade);
        Decimal maximumDrawdown = calculateMaximumDrawdown(series, cashFlow);
        return maximumDrawdown.doubleValue();
    }
    return 0;
}
Also used : CashFlow(org.ta4j.core.analysis.CashFlow) Decimal(org.ta4j.core.Decimal)

Example 58 with Decimal

use of org.ta4j.core.Decimal in project ta4j by ta4j.

the class TotalProfitCriterion method calculateProfit.

/**
 * Calculates the profit of a trade (Buy and sell).
 * @param series a time series
 * @param trade a trade
 * @return the profit of the trade
 */
private double calculateProfit(TimeSeries series, Trade trade) {
    Decimal profit = Decimal.ONE;
    if (trade.isClosed()) {
        // use price of entry/exit order, if NaN use close price of underlying time series
        Decimal exitClosePrice = trade.getExit().getPrice().isNaN() ? series.getBar(trade.getExit().getIndex()).getClosePrice() : trade.getExit().getPrice();
        Decimal entryClosePrice = trade.getEntry().getPrice().isNaN() ? series.getBar(trade.getEntry().getIndex()).getClosePrice() : trade.getEntry().getPrice();
        if (trade.getEntry().isBuy()) {
            profit = exitClosePrice.dividedBy(entryClosePrice);
        } else {
            profit = entryClosePrice.dividedBy(exitClosePrice);
        }
    }
    return profit.doubleValue();
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 59 with Decimal

use of org.ta4j.core.Decimal in project ta4j by ta4j.

the class StochasticOscillatorKIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    HighestValueIndicator highestHigh = new HighestValueIndicator(maxPriceIndicator, timeFrame);
    LowestValueIndicator lowestMin = new LowestValueIndicator(minPriceIndicator, timeFrame);
    Decimal highestHighPrice = highestHigh.getValue(index);
    Decimal lowestLowPrice = lowestMin.getValue(index);
    return indicator.getValue(index).minus(lowestLowPrice).dividedBy(highestHighPrice.minus(lowestLowPrice)).multipliedBy(Decimal.HUNDRED);
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 60 with Decimal

use of org.ta4j.core.Decimal in project ta4j by ta4j.

the class WMAIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    if (index == 0) {
        return indicator.getValue(0);
    }
    Decimal value = Decimal.ZERO;
    if (index - timeFrame < 0) {
        for (int i = index + 1; i > 0; i--) {
            value = value.plus(Decimal.valueOf(i).multipliedBy(indicator.getValue(i - 1)));
        }
        return value.dividedBy(Decimal.valueOf(((index + 1) * (index + 2)) / 2));
    }
    int actualIndex = index;
    for (int i = timeFrame; i > 0; i--) {
        value = value.plus(Decimal.valueOf(i).multipliedBy(indicator.getValue(actualIndex)));
        actualIndex--;
    }
    return value.dividedBy(Decimal.valueOf((timeFrame * (timeFrame + 1)) / 2));
}
Also used : Decimal(org.ta4j.core.Decimal)

Aggregations

Decimal (org.ta4j.core.Decimal)92 Bar (org.ta4j.core.Bar)20 Test (org.junit.Test)8 CorrelationCoefficientIndicator (org.ta4j.core.indicators.statistics.CorrelationCoefficientIndicator)4 Trade (org.ta4j.core.Trade)3 ClosePriceIndicator (org.ta4j.core.indicators.helpers.ClosePriceIndicator)3 Trade (com.github.jnidzwetzki.cryptobot.entity.Trade)2 CashFlow (org.ta4j.core.analysis.CashFlow)2 HighestValueIndicator (org.ta4j.core.indicators.helpers.HighestValueIndicator)2 LowestValueIndicator (org.ta4j.core.indicators.helpers.LowestValueIndicator)2 MockTimeSeries (org.ta4j.core.mocks.MockTimeSeries)2 DifferenceIndicator (org.ta4j.core.indicators.helpers.DifferenceIndicator)1