Search in sources :

Example 26 with Decimal

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

the class StandardReversalIndicator method calculateR3.

private Decimal calculateR3(List<Integer> barsOfPreviousPeriod, int index) {
    Bar bar = getTimeSeries().getBar(barsOfPreviousPeriod.get(0));
    Decimal low = bar.getMinPrice();
    Decimal high = bar.getMaxPrice();
    for (int i : barsOfPreviousPeriod) {
        low = (getTimeSeries().getBar(i).getMinPrice()).min(low);
        high = (getTimeSeries().getBar(i).getMaxPrice()).max(high);
    }
    return high.plus(Decimal.TWO.multipliedBy((pivotPointIndicator.getValue(index).minus(low))));
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 27 with Decimal

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

the class CovarianceIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    final int startIndex = Math.max(0, index - timeFrame + 1);
    final int numberOfObservations = index - startIndex + 1;
    Decimal covariance = Decimal.ZERO;
    Decimal average1 = sma1.getValue(index);
    Decimal average2 = sma2.getValue(index);
    for (int i = startIndex; i <= index; i++) {
        Decimal mul = indicator1.getValue(i).minus(average1).multipliedBy(indicator2.getValue(i).minus(average2));
        covariance = covariance.plus(mul);
    }
    covariance = covariance.dividedBy(Decimal.valueOf(numberOfObservations));
    return covariance;
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 28 with Decimal

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

the class PeriodicalGrowthRateIndicator method getTotalReturn.

/**
 * Gets the TotalReturn from the calculated results of the method 'calculate'.
 * For a timeFrame = number of trading days within a year (e. g. 251 days in the US)
 * and "end of day"-bars you will get the 'Annualized Total Return'.
 * Only complete timeFrames are taken into the calculation.
 * @return the total return from the calculated results of the method 'calculate'
 */
public double getTotalReturn() {
    Decimal totalProduct = Decimal.ONE;
    int completeTimeframes = (getTimeSeries().getBarCount() / timeFrame);
    for (int i = 1; i <= completeTimeframes; i++) {
        int index = i * timeFrame;
        Decimal currentReturn = getValue(index);
        // Skip NaN at the end of a series
        if (currentReturn != Decimal.NaN) {
            currentReturn = currentReturn.plus(Decimal.ONE);
            totalProduct = totalProduct.multipliedBy(currentReturn);
        }
    }
    return (Math.pow(totalProduct.doubleValue(), (1.0 / completeTimeframes)));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 29 with Decimal

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

the class ConvergenceDivergenceIndicator method calculateNegativeConvergence.

/**
 * @param index the actual index
 * @return true, if negative convergent
 */
private Boolean calculateNegativeConvergence(int index) {
    CorrelationCoefficientIndicator cc = new CorrelationCoefficientIndicator(ref, other, timeFrame);
    boolean isConvergent = cc.getValue(index).isGreaterThanOrEqual(minStrenght);
    Decimal slope = calculateSlopeRel(index);
    boolean isNegative = slope.isLessThanOrEqual(minSlope.abs().multipliedBy(Decimal.valueOf(-1)));
    return isConvergent && isNegative;
}
Also used : Decimal(org.ta4j.core.Decimal) CorrelationCoefficientIndicator(org.ta4j.core.indicators.statistics.CorrelationCoefficientIndicator)

Example 30 with Decimal

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

the class ConvergenceDivergenceIndicator method calculateNegativeDivergence.

/**
 * @param index the actual index
 * @return true, if negative divergent
 */
private Boolean calculateNegativeDivergence(int index) {
    CorrelationCoefficientIndicator cc = new CorrelationCoefficientIndicator(ref, other, timeFrame);
    boolean isDivergent = cc.getValue(index).isLessThanOrEqual(minStrenght.multipliedBy(Decimal.valueOf(-1)));
    if (isDivergent) {
        // If "isDivergent" and "ref" is positive, then "other" must be negative.
        Decimal slope = calculateSlopeRel(index);
        return slope.isLessThanOrEqual(minSlope.abs().multipliedBy(Decimal.valueOf(-1)));
    }
    return false;
}
Also used : Decimal(org.ta4j.core.Decimal) CorrelationCoefficientIndicator(org.ta4j.core.indicators.statistics.CorrelationCoefficientIndicator)

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