Make candlestick calc configurable

This commit is contained in:
2018-11-02 11:56:10 -07:00
parent 36985bbfb0
commit d1225c27d1
3 changed files with 86 additions and 1 deletions

View File

@ -5,6 +5,7 @@ package ats.plugin;
* values. For example, OHLC or Heikin-Ashi. * values. For example, OHLC or Heikin-Ashi.
*/ */
interface CandlestickCalc { interface CandlestickCalc {
public enum Type { OHLC, HeikinAshi };
/** /**
* Reset the calculation for the beginning of an interval. * Reset the calculation for the beginning of an interval.

View File

@ -0,0 +1,57 @@
package ats.plugin;
/**
* HeikinAshiCandlestickCalc calculates values using the Heikin-Ashi
* technique.
*/
class HeikinAshiCandlestickCalc implements CandlestickCalc {
private Double open;
private Double close;
private Double high;
private Double low;
/**
* Reset the calculation for the beginning of an interval.
*/
@Override
public void reset() {
open = null;
close = null;
high = null;
low = null;
}
/**
* Accept a current tick value to apply to the current
* calculations.
*/
@Override
public void applyValue(double value) {
if (open == null) {
open = value;
}
close = value;
if (low == null) {
low = value;
} else if (low.compareTo(value) > 0) {
low = value;
}
if (high == null) {
high = value;
} else if (high.compareTo(value) < 0) {
high = value;
}
}
/**
* Return our calculated values up to now.
*/
@Override
public OHLCValues getValues() {
return new OHLCValues(open, high, low, close);
}
}

View File

@ -27,6 +27,9 @@ public class OHLCPlugInView extends ViewSupport {
private EventBean[] lastData; private EventBean[] lastData;
/**
* Create a new plugin.
*/
public OHLCPlugInView(AgentInstanceViewFactoryChainContext context, public OHLCPlugInView(AgentInstanceViewFactoryChainContext context,
ExprNode calcTypeExpression, ExprNode calcTypeExpression,
ExprNode timeTypeExpression, ExprNode timeTypeExpression,
@ -39,8 +42,32 @@ public class OHLCPlugInView extends ViewSupport {
this.valueExpression = valueExpression; this.valueExpression = valueExpression;
service = context.getStatementContext().getEventAdapterService(); service = context.getStatementContext().getEventAdapterService();
CandlestickCalc calc = new OHLCCandlestickCalc();
window = new DurationWindow(this, calc, context, intervalExpression); window = new DurationWindow(this, calc, context, intervalExpression);
CandlestickCalc calc = chooseCalc(calcTypeExpression);
}
/**
* Return the proper CandlestickCalc according to the value of the
* calc type parameter.
*/
private CandlestickCalc chooseCalc(ExprNode calcTypeExpression) {
String calcType = exprNodeValue(calcTypeExpression);
if (CandlestickCalc.Type.valueOf(calcType) == CandlestickCalc.Type.HeikinAshi) {
log.info("Using Heikin-Ashi calc type");
return new HeikinAshiCandlestickCalc();
}
log.info("Using OHLC calc type");
return new OHLCCandlestickCalc();
}
/**
* Return the string value of a node.
*/
private String exprNodeValue(ExprNode node) {
ExprEvaluator evaluator = node.getForge().getExprEvaluator();
return (String)evaluator.evaluate(null, true, context);
} }
/** /**