From ed88eca0c77a5c17d3cf3ea90b91303f9525d5be Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Tue, 13 Nov 2018 09:24:40 -0800 Subject: [PATCH] Use correct HA calc --- .../ats/plugin/HeikinAshiCandlestickCalc.java | 87 ++++++++++++++----- .../ats/plugin/TicksCandlestickWindow.java | 2 - 2 files changed, 66 insertions(+), 23 deletions(-) diff --git a/src/main/java/ats/plugin/HeikinAshiCandlestickCalc.java b/src/main/java/ats/plugin/HeikinAshiCandlestickCalc.java index c832c4e..f6897c5 100644 --- a/src/main/java/ats/plugin/HeikinAshiCandlestickCalc.java +++ b/src/main/java/ats/plugin/HeikinAshiCandlestickCalc.java @@ -3,23 +3,30 @@ package ats.plugin; /** * HeikinAshiCandlestickCalc calculates values using the Heikin-Ashi * technique. + * + * @see Heikin-Ashi description */ 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); } } diff --git a/src/main/java/ats/plugin/TicksCandlestickWindow.java b/src/main/java/ats/plugin/TicksCandlestickWindow.java index d2f603c..94801ed 100644 --- a/src/main/java/ats/plugin/TicksCandlestickWindow.java +++ b/src/main/java/ats/plugin/TicksCandlestickWindow.java @@ -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;