Search in sources :

Example 61 with Decimal

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

the class FisherIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    if (index <= 0) {
        return Decimal.ZERO;
    }
    Decimal value = intermediateValue.getValue(index);
    if (value.isGreaterThan(VALUE_MAX)) {
        value = VALUE_MAX;
    } else if (value.isLessThan(VALUE_MIN)) {
        value = VALUE_MIN;
    }
    // Fisher = gamma * Log((1 + Value) / (1 - Value)) + delta * priorFisher
    Decimal term1 = Decimal.valueOf((Math.log(Decimal.ONE.plus(value).dividedBy(Decimal.ONE.minus(value)).doubleValue())));
    Decimal term2 = getValue(index - 1);
    return gamma.multipliedBy(term1).plus(delta.multipliedBy(term2));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 62 with Decimal

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

the class CCIIndicator method calculate.

@Override
protected Decimal calculate(int index) {
    final Decimal typicalPrice = typicalPriceInd.getValue(index);
    final Decimal typicalPriceAvg = smaInd.getValue(index);
    final Decimal meanDeviation = meanDeviationInd.getValue(index);
    if (meanDeviation.isZero()) {
        return Decimal.ZERO;
    }
    return (typicalPrice.minus(typicalPriceAvg)).dividedBy(meanDeviation.multipliedBy(FACTOR));
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 63 with Decimal

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

the class CashFlow method calculate.

/**
 * Calculates the cash flow for a single trade.
 * @param trade a single trade
 */
private void calculate(Trade trade) {
    final int entryIndex = trade.getEntry().getIndex();
    int begin = entryIndex + 1;
    if (begin > values.size()) {
        Decimal lastValue = values.get(values.size() - 1);
        values.addAll(Collections.nCopies(begin - values.size(), lastValue));
    }
    int end = trade.getExit().getIndex();
    for (int i = Math.max(begin, 1); i <= end; i++) {
        Decimal ratio;
        if (trade.getEntry().isBuy()) {
            ratio = timeSeries.getBar(i).getClosePrice().dividedBy(timeSeries.getBar(entryIndex).getClosePrice());
        } else {
            ratio = timeSeries.getBar(entryIndex).getClosePrice().dividedBy(timeSeries.getBar(i).getClosePrice());
        }
        values.add(values.get(entryIndex).multipliedBy(ratio));
    }
}
Also used : Decimal(org.ta4j.core.Decimal)

Example 64 with Decimal

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

the class WMAIndicatorTest method calculate.

@Test
public void calculate() {
    MockTimeSeries series = new MockTimeSeries(1d, 2d, 3d, 4d, 5d, 6d);
    Indicator<Decimal> close = new ClosePriceIndicator(series);
    Indicator<Decimal> wmaIndicator = new WMAIndicator(close, 3);
    assertDecimalEquals(wmaIndicator.getValue(0), 1);
    assertDecimalEquals(wmaIndicator.getValue(1), 1.6667);
    assertDecimalEquals(wmaIndicator.getValue(2), 2.3333);
    assertDecimalEquals(wmaIndicator.getValue(3), 3.3333);
    assertDecimalEquals(wmaIndicator.getValue(4), 4.3333);
    assertDecimalEquals(wmaIndicator.getValue(5), 5.3333);
}
Also used : Decimal(org.ta4j.core.Decimal) MockTimeSeries(org.ta4j.core.mocks.MockTimeSeries) ClosePriceIndicator(org.ta4j.core.indicators.helpers.ClosePriceIndicator) Test(org.junit.Test)

Example 65 with Decimal

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

the class StopLossRule method isSatisfied.

@Override
public boolean isSatisfied(int index, TradingRecord tradingRecord) {
    boolean satisfied = false;
    // No trading history or no trade opened, no loss
    if (tradingRecord != null) {
        Trade currentTrade = tradingRecord.getCurrentTrade();
        if (currentTrade.isOpened()) {
            Decimal entryPrice = currentTrade.getEntry().getPrice();
            Decimal currentPrice = closePrice.getValue(index);
            Decimal threshold = entryPrice.multipliedBy(lossRatioThreshold);
            if (currentTrade.getEntry().isBuy()) {
                satisfied = currentPrice.isLessThanOrEqual(threshold);
            } else {
                satisfied = currentPrice.isGreaterThanOrEqual(threshold);
            }
        }
    }
    traceIsSatisfied(index, satisfied);
    return satisfied;
}
Also used : Trade(org.ta4j.core.Trade) 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