Search in sources :

Example 31 with Decimal

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

the class DXIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    Decimal pdiValue = plusDIIndicator.getValue(index);
    Decimal mdiValue = minusDIIndicator.getValue(index);
    if (pdiValue.plus(mdiValue).equals(Decimal.ZERO)) {
        return Decimal.ZERO;
    }
    return pdiValue.minus(mdiValue).abs().dividedBy(pdiValue.plus(mdiValue)).multipliedBy(Decimal.HUNDRED);
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 32 with Decimal

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

the class SimpleLinearRegressionIndicator method calculateRegressionLine.

/**
 * Calculates the regression line.
 * @param startIndex the start index (inclusive) in the time series
 * @param endIndex the end index (inclusive) in the time series
 */
private void calculateRegressionLine(int startIndex, int endIndex) {
    // First pass: compute xBar and yBar
    Decimal sumX = Decimal.ZERO;
    Decimal sumY = Decimal.ZERO;
    for (int i = startIndex; i <= endIndex; i++) {
        sumX = sumX.plus(Decimal.valueOf(i));
        sumY = sumY.plus(indicator.getValue(i));
    }
    Decimal nbObservations = Decimal.valueOf(endIndex - startIndex + 1);
    Decimal xBar = sumX.dividedBy(nbObservations);
    Decimal yBar = sumY.dividedBy(nbObservations);
    // Second pass: compute slope and intercept
    Decimal xxBar = Decimal.ZERO;
    Decimal xyBar = Decimal.ZERO;
    for (int i = startIndex; i <= endIndex; i++) {
        Decimal dX = Decimal.valueOf(i).minus(xBar);
        Decimal dY = indicator.getValue(i).minus(yBar);
        xxBar = xxBar.plus(dX.multipliedBy(dX));
        xyBar = xyBar.plus(dX.multipliedBy(dY));
    }
    slope = xyBar.dividedBy(xxBar);
    intercept = yBar.minus(slope.multipliedBy(xBar));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 33 with Decimal

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

the class VarianceIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    final int startIndex = Math.max(0, index - timeFrame + 1);
    final int numberOfObservations = index - startIndex + 1;
    Decimal variance = Decimal.ZERO;
    Decimal average = sma.getValue(index);
    for (int i = startIndex; i <= index; i++) {
        Decimal pow = indicator.getValue(i).minus(average).pow(2);
        variance = variance.plus(pow);
    }
    variance = variance.dividedBy(Decimal.valueOf(numberOfObservations));
    return variance;
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 34 with Decimal

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

the class NVIIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    if (index == 0) {
        return Decimal.THOUSAND;
    }
    Bar currentBar = series.getBar(index);
    Bar previousBar = series.getBar(index - 1);
    Decimal previousValue = getValue(index - 1);
    if (currentBar.getVolume().isLessThan(previousBar.getVolume())) {
        Decimal currentPrice = currentBar.getClosePrice();
        Decimal previousPrice = previousBar.getClosePrice();
        Decimal priceChangeRatio = currentPrice.minus(previousPrice).dividedBy(previousPrice);
        return previousValue.plus(priceChangeRatio.multipliedBy(previousValue));
    }
    return previousValue;
}
Also used : Bar(org.ta4j.core.Bar) Decimal(org.ta4j.core.Decimal)

Example 35 with Decimal

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

the class OnBalanceVolumeIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    if (index == 0) {
        return Decimal.ZERO;
    }
    Decimal yesterdayClose = series.getBar(index - 1).getClosePrice();
    Decimal todayClose = series.getBar(index).getClosePrice();
    if (yesterdayClose.isGreaterThan(todayClose)) {
        return getValue(index - 1).minus(series.getBar(index).getVolume());
    } else if (yesterdayClose.isLessThan(todayClose)) {
        return getValue(index - 1).plus(series.getBar(index).getVolume());
    }
    return getValue(index - 1);
}
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