Candlestick pattern indicators

This section contains examples which show how to work with Candlestick pattern indicators - how to:

  • Find a pattern by shift and by candlestick bar interval.
  • Find the latest occurrence of the pattern.
  • Plot patterns on chart - both single and multiple.
  • Use designated set of candlestick patterns in the same strategy.
  • Use all 61 candlestick patterns in the same strategy.

Use hammer pattern

CandlePatternsHammer strategy shows how to use hammer pattern within a strategy:

  • On its start (i.e. in the onStart method) it finds hammer pattern occurrences over the last 100 candlesticks and prints to console the latest occurrence by shift.
  • On every candlestick (i.e. in the onBar method) it checks if it is of hammer pattern.

Also the strategy plots the indicator on chart:

candlePatternsHammer

Consider the onStart method (find full source code below the snippet):

private String indName = "CDLHAMMER";

@Override
public void onStart(IContext context) throws JFException {
    indicators = context.getIndicators();
    console = context.getConsole();
    history = context.getHistory();
    chart = context.getChart(feedDescriptor.getInstrument());
    this.context = context;

    int candlesBefore = candleCount, candlesAfter = 0;
    long currBarTime = history.getFeedData(feedDescriptor, 0).getTime();

    IIndicator indicator = context.getIndicators().getIndicator(indName);

    Object[] patternUni = indicators.calculateIndicator(feedDescriptor, new OfferSide[] { feedDescriptor.getOfferSide() }, indName,
            new AppliedPrice[] { IIndicators.AppliedPrice.CLOSE }, new Object[] { }, candlesBefore, currBarTime, candlesAfter);

    //all candle patterns have just one output - we're good with 1-dimensional array
    int[] values = (int[]) patternUni[0];
    Set<Integer> occurrences = new LinkedHashSet<Integer>();

    for(int i=0; i < values.length; i++){
        int shift = values.length - 1 - i;
        if(values[i] != 0){
            occurrences.add(shift);
        }
    }
    int lastOccurrence = occurrences.isEmpty() 
        ? -1 
        : Collections.min(occurrences);

    console.getOut().format("%s pattern occurances over last %s bars=%s; last occurrence shift=%s; all occurences: %s",
            indicator.getIndicatorInfo().getTitle(), candleCount,occurrences.size(), lastOccurrence, occurrences.toString()
            ).println();            

    if (plotOnChart && chart != null) {
        if(chart == null){
            console.getWarn().println("can't add to chart - chart is null!");
            return;
        }
        chart.add(indicator);
    }

}

Note: the example strategy is applicable to all other candlestick pattern indicators, just by changing the indicator name. Please mind checking for optional input count (see the CandlePatternsMultiple strategy).

CandlePatternsHammer.java

Use multiple patterns

CandlePatternsMultiple strategy calculates multiple candlestick pattern indicators. The strategy is an extended version of the CandlePatternsHammer strategy, with the following changes:

  • It calculates the following 6 candlestick patterns: hammer, morning star, doji, spinning top, shooting star, hanging man.
  • It plots values by using signal arrow chart objects, instead of using standard plotting routine.

The reason of the latter change is to prevent the patterns from overlapping on the chart - such that one can see that multiple patterns apply to the same candlestick.

candlePatternsMulti2

Switching between the candlestick pattern indicators does not take much effort, since almost all of them have the same metadata - one input and one output. The sole difference between the indicators is the presence or absence of penetration optional input, which gets handled in the following way:

//most of the candle patterns have no optional inputs, those that have - it's penetration parameter
Object[] optInputArray = indicator.getIndicatorInfo().getNumberOfOptionalInputs() == 0 
    ? new Object[] { } 
    : new Object[] { penetration }; 
Object[] patternUni = indicators.calculateIndicator(instrument, period, new OfferSide[] { side }, "CDLHAMMER",
    new AppliedPrice[] { IIndicators.AppliedPrice.CLOSE }, optInputArray, Filter.NO_FILTER, candlesBefore, currBarTime, candlesAfter);

CandlePatternsMultiple.java

Use all candlestick patterns

CandlePatternsAll strategy calculates all candlestick pattern indicators that are available in the platform. Essentially, the strategy differs from the CandlePatternsMultiple strategy only by calculated indicator count and by the way the list of indicators gets compiled.

candlePatternsAll

The strategy retrieves all candlestick pattern indicators in the following way:

Collection<String> indNames = indicators.getAllNames();
for(String name : indNames){
    IndicatorInfo info = indicators.getIndicator(name).getIndicatorInfo();
    if(info.getGroupName().equalsIgnoreCase("Pattern Recognition") && name.contains("CDL")){
        patternNames.add(name);
    }
}

CandlePatternsAll.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.