use of org.ta4j.core.indicators.helpers.HighestValueIndicator in project ta4j by ta4j.
the class IsHighestRule method isSatisfied.
@Override
public boolean isSatisfied(int index, TradingRecord tradingRecord) {
HighestValueIndicator highest = new HighestValueIndicator(ref, timeFrame);
Decimal highestVal = highest.getValue(index);
Decimal refVal = ref.getValue(index);
final boolean satisfied = !refVal.isNaN() && !highestVal.isNaN() && refVal.equals(highestVal);
traceIsSatisfied(index, satisfied);
return satisfied;
}
use of org.ta4j.core.indicators.helpers.HighestValueIndicator in project ta4j by ta4j.
the class ParabolicSarIndicator method calculate.
@Override
protected Decimal calculate(int index) {
Decimal sar = Decimal.NaN;
if (index == series.getBeginIndex()) {
// no trend detection possible for the first value
return sar;
} else if (index == series.getBeginIndex() + 1) {
// start trend detection
currentTrend = series.getBar(series.getBeginIndex()).getClosePrice().isLessThan(series.getBar(index).getClosePrice());
if (!currentTrend) {
// down trend
// put sar on max price of candlestick
sar = maxPriceIndicator.getValue(index);
currentExtremePoint = sar;
minMaxExtremePoint = currentExtremePoint;
} else {
// up trend
// put sar on min price of candlestick
sar = minPriceIndicator.getValue(index);
currentExtremePoint = sar;
minMaxExtremePoint = currentExtremePoint;
}
return sar;
}
Decimal priorSar = getValue(index - 1);
if (currentTrend) {
// if up trend
sar = priorSar.plus(accelerationFactor.multipliedBy((currentExtremePoint.minus(priorSar))));
currentTrend = minPriceIndicator.getValue(index).isGreaterThan(sar);
if (!currentTrend) {
// check if sar touches the min price
// sar starts at the highest extreme point of previous up trend
sar = minMaxExtremePoint;
// switch to down trend and reset values
currentTrend = false;
startTrendIndex = index;
accelerationFactor = accelarationStart;
// put point on max
currentExtremePoint = series.getBar(index).getMinPrice();
minMaxExtremePoint = currentExtremePoint;
} else {
// up trend is going on
currentExtremePoint = new HighestValueIndicator(maxPriceIndicator, index - startTrendIndex).getValue(index);
if (currentExtremePoint.isGreaterThan(minMaxExtremePoint)) {
incrementAcceleration();
minMaxExtremePoint = currentExtremePoint;
}
}
} else {
// downtrend
sar = priorSar.minus(accelerationFactor.multipliedBy(((priorSar.minus(currentExtremePoint)))));
currentTrend = maxPriceIndicator.getValue(index).isGreaterThanOrEqual(sar);
if (currentTrend) {
// check if switch to up trend
// sar starts at the lowest extreme point of previous down trend
sar = minMaxExtremePoint;
accelerationFactor = accelarationStart;
startTrendIndex = index;
currentExtremePoint = series.getBar(index).getMaxPrice();
minMaxExtremePoint = currentExtremePoint;
} else {
// down trend io going on
currentExtremePoint = new LowestValueIndicator(minPriceIndicator, index - startTrendIndex).getValue(index);
if (currentExtremePoint.isLessThan(minMaxExtremePoint)) {
incrementAcceleration();
minMaxExtremePoint = currentExtremePoint;
}
}
}
return sar;
}
Aggregations