Search in sources :

Example 71 with Decimal

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

the class MeanDeviationIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    Decimal absoluteDeviations = Decimal.ZERO;
    final Decimal average = sma.getValue(index);
    final int startIndex = Math.max(0, index - timeFrame + 1);
    final int nbValues = index - startIndex + 1;
    for (int i = startIndex; i <= index; i++) {
        // For each period...
        absoluteDeviations = absoluteDeviations.plus(indicator.getValue(i).minus(average).abs());
    }
    return absoluteDeviations.dividedBy(Decimal.valueOf(nbValues));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 72 with Decimal

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

the class PearsonCorrelationIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    Decimal n = Decimal.valueOf(timeFrame);
    Decimal Sx = Decimal.ZERO;
    Decimal Sy = Decimal.ZERO;
    Decimal Sxx = Decimal.ZERO;
    Decimal Syy = Decimal.ZERO;
    Decimal Sxy = Decimal.ZERO;
    for (int i = Math.max(getTimeSeries().getBeginIndex(), index - timeFrame + 1); i <= index; i++) {
        Decimal x = indicator1.getValue(i);
        Decimal y = indicator2.getValue(i);
        Sx = Sx.plus(x);
        Sy = Sy.plus(y);
        Sxy = Sxy.plus(x.multipliedBy(y));
        Sxx = Sxx.plus(x.multipliedBy(x));
        Syy = Syy.plus(y.multipliedBy(y));
    }
    // (n * Sxx - Sx * Sx) * (n * Syy - Sy * Sy)
    Decimal toSqrt = (n.multipliedBy(Sxx).minus(Sx.multipliedBy(Sx))).multipliedBy(n.multipliedBy(Syy).minus(Sy.multipliedBy(Sy)));
    if (toSqrt.isGreaterThan(Decimal.ZERO)) {
        // pearson = (n * Sxy - Sx * Sy) / sqrt((n * Sxx - Sx * Sx) * (n * Syy - Sy * Sy))
        return (n.multipliedBy(Sxy).minus(Sx.multipliedBy(Sy))).dividedBy(Decimal.valueOf(Math.sqrt(toSqrt.doubleValue())));
    }
    return Decimal.NaN;
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 73 with Decimal

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

the class PeriodicalGrowthRateIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    Decimal currentValue = indicator.getValue(index);
    int helpPartialTimeframe = index % timeFrame;
    double helpFullTimeframes = Math.floor((double) indicator.getTimeSeries().getBarCount() / (double) timeFrame);
    double helpIndexTimeframes = (double) index / (double) timeFrame;
    double helpPartialTimeframeHeld = (double) helpPartialTimeframe / (double) timeFrame;
    double partialTimeframeHeld = (helpPartialTimeframeHeld == 0) ? 1.0 : helpPartialTimeframeHeld;
    // Avoid calculations of returns:
    // a.) if index number is below timeframe
    // e.g. timeframe = 365, index = 5 => no calculation
    // b.) if at the end of a series incomplete timeframes would remain
    Decimal timeframedReturn = Decimal.NaN;
    if ((index >= timeFrame) && /*(a)*/
    (helpIndexTimeframes < helpFullTimeframes)) /*(b)*/
    {
        Decimal movingValue = indicator.getValue(index - timeFrame);
        Decimal movingSimpleReturn = (currentValue.minus(movingValue)).dividedBy(movingValue);
        double timeframedReturn_double = Math.pow((1 + movingSimpleReturn.doubleValue()), (1 / partialTimeframeHeld)) - 1;
        timeframedReturn = Decimal.valueOf(timeframedReturn_double);
    }
    return timeframedReturn;
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 74 with Decimal

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

the class ThreeBlackCrowsIndicator method hasVeryShortLowerShadow.

/**
 * @param index the bar/candle index
 * @return true if the bar/candle has a very short lower shadow, false otherwise
 */
private boolean hasVeryShortLowerShadow(int index) {
    Decimal currentLowerShadow = lowerShadowInd.getValue(index);
    // We use the white candle index to remove to bias of the previous crows
    Decimal averageLowerShadow = averageLowerShadowInd.getValue(whiteCandleIndex);
    return currentLowerShadow.isLessThan(averageLowerShadow.multipliedBy(factor));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 75 with Decimal

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

the class ThreeWhiteSoldiersIndicator method isGrowing.

/**
 * @param index the current bar/candle index
 * @return true if the current bar/candle is growing, false otherwise
 */
private boolean isGrowing(int index) {
    Bar prevBar = series.getBar(index - 1);
    Bar currBar = series.getBar(index);
    final Decimal prevOpenPrice = prevBar.getOpenPrice();
    final Decimal prevClosePrice = prevBar.getClosePrice();
    final Decimal currOpenPrice = currBar.getOpenPrice();
    final Decimal currClosePrice = currBar.getClosePrice();
    // Opens within the body of the previous candle
    return currOpenPrice.isGreaterThan(prevOpenPrice) && currOpenPrice.isLessThan(prevClosePrice) && // Closes above the previous close price
    currClosePrice.isGreaterThan(prevClosePrice);
}
Also used : Bar(org.ta4j.core.Bar) 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