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));
}
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));
}
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));
}
}
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);
}
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;
}
Aggregations