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,12 +3,16 @@ package ats.plugin;
/** /**
* HeikinAshiCandlestickCalc calculates values using the Heikin-Ashi * HeikinAshiCandlestickCalc calculates values using the Heikin-Ashi
* technique. * 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 { class HeikinAshiCandlestickCalc implements CandlestickCalc {
private Double open; private Double lastOpen;
private Double close; private Double lastClose;
private Double high; private Double currentOpen;
private Double low; private Double currentClose;
private Double currentHigh;
private Double currentLow;
/** /**
@ -16,10 +20,13 @@ class HeikinAshiCandlestickCalc implements CandlestickCalc {
*/ */
@Override @Override
public void reset() { public void reset() {
open = null; lastOpen = currentOpen;
close = null; lastClose = currentClose;
high = null;
low = null; currentOpen = null;
currentClose = null;
currentHigh = null;
currentLow = null;
} }
/** /**
@ -28,30 +35,68 @@ class HeikinAshiCandlestickCalc implements CandlestickCalc {
*/ */
@Override @Override
public void applyValue(double value) { public void applyValue(double value) {
if (open == null) { if (currentOpen == null) {
open = value; currentOpen = value;
} }
close = value; currentClose = value;
if (low == null) { if (currentLow == null) {
low = value; currentLow = value;
} else if (low.compareTo(value) > 0) { } else if (currentLow.compareTo(value) > 0) {
low = value; currentLow = value;
} }
if (high == null) { if (currentHigh == null) {
high = value; currentHigh = value;
} else if (high.compareTo(value) < 0) { } else if (currentHigh.compareTo(value) < 0) {
high = value; 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. * Return our calculated values up to now.
*/ */
@Override @Override
public OHLCValues getValues() { 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) { private long parseTickCount(ExprNode intervalExpression) {
ExprEvaluator evaluator = intervalExpression.getForge().getExprEvaluator(); ExprEvaluator evaluator = intervalExpression.getForge().getExprEvaluator();
Object o = evaluator.evaluate(null, true, context); Object o = evaluator.evaluate(null, true, context);
log.warn("OBJECT " + o.getClass());
return new Long((Integer)o).longValue(); return new Long((Integer)o).longValue();
} }
@ -67,7 +66,6 @@ class TicksCandlestickWindow implements CandlestickWindow {
windowStartTime = timestamp; windowStartTime = timestamp;
currentTick++; currentTick++;
log.info("tick ", currentTick);
if (currentTick < windowTicks) return; if (currentTick < windowTicks) return;