Use correct HA calc

This commit is contained in:
2018-11-13 09:24:40 -08:00
parent cc9f25c6b2
commit ed88eca0c7
2 changed files with 66 additions and 23 deletions

View File

@ -3,23 +3,30 @@ package ats.plugin;
/**
* HeikinAshiCandlestickCalc calculates values using the Heikin-Ashi
* technique.
*
* @see <a href="https://stockcharts.com/school/doku.php?id=chart_school:chart_analysis:heikin_ashi">Heikin-Ashi description</a>
*/
class HeikinAshiCandlestickCalc implements CandlestickCalc {
private Double open;
private Double close;
private Double high;
private Double low;
private Double lastOpen;
private Double lastClose;
private Double currentOpen;
private Double currentClose;
private Double currentHigh;
private Double currentLow;
/**
* Reset the calculation for the beginning of an interval.
*/
@Override
public void reset() {
open = null;
close = null;
high = null;
low = null;
lastOpen = currentOpen;
lastClose = currentClose;
currentOpen = null;
currentClose = null;
currentHigh = null;
currentLow = null;
}
/**
@ -28,30 +35,68 @@ class HeikinAshiCandlestickCalc implements CandlestickCalc {
*/
@Override
public void applyValue(double value) {
if (open == null) {
open = value;
if (currentOpen == null) {
currentOpen = value;
}
close = value;
currentClose = value;
if (low == null) {
low = value;
} else if (low.compareTo(value) > 0) {
low = value;
if (currentLow == null) {
currentLow = value;
} else if (currentLow.compareTo(value) > 0) {
currentLow = value;
}
if (high == null) {
high = value;
} else if (high.compareTo(value) < 0) {
high = value;
if (currentHigh == null) {
currentHigh = value;
} else if (currentHigh.compareTo(value) < 0) {
currentHigh = value;
}
}
/**
* HA-Open = (HA-Open(-1) + HA-Close(-1)) / 2
*/
private double calcOpen() {
double sum = 0;
sum += lastOpen != null ? lastOpen : currentOpen;
sum += lastClose != null ? lastClose : currentClose;
return sum / 2d;
}
/**
* HA-Close = (Open(0) + High(0) + Low(0) + Close(0)) / 4
*/
private double calcClose() {
return (currentOpen + currentHigh + currentLow + currentClose) / 4d;
}
/**
* HA-High = Maximum of the High(0), HA-Open(0) or HA-Close(0)
*/
private double calcHigh(double haOpen, double haClose) {
return Math.max(currentHigh, Math.max(haOpen, haClose));
}
/**
* HA-Low = Minimum of the Low(0), HA-Open(0) or HA-Close(0)
*/
private double calcLow(double haOpen, double haClose) {
return Math.min(currentLow, Math.min(haOpen, haClose));
}
/**
* Return our calculated values up to now.
*/
@Override
public OHLCValues getValues() {
return new OHLCValues(open, high, low, close);
double o = calcOpen();
double c = calcClose();
double h = calcHigh(o, c);
double l = calcLow(o, c);
return new OHLCValues(o, h, l, c);
}
}

View File

@ -46,7 +46,6 @@ class TicksCandlestickWindow implements CandlestickWindow {
private long parseTickCount(ExprNode intervalExpression) {
ExprEvaluator evaluator = intervalExpression.getForge().getExprEvaluator();
Object o = evaluator.evaluate(null, true, context);
log.warn("OBJECT " + o.getClass());
return new Long((Integer)o).longValue();
}
@ -67,7 +66,6 @@ class TicksCandlestickWindow implements CandlestickWindow {
windowStartTime = timestamp;
currentTick++;
log.info("tick ", currentTick);
if (currentTick < windowTicks) return;