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