Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Adding Indicators

Simon W edited this page Dec 22, 2017 · 7 revisions

For further information take a look at the program structure and how to start the application

How to create fast a chart indicator?

Instantiate a TaChartIndicatorBox and add the wanted Indicator
For example add fast an VolumeIndicator to your chart and initialize all other indicators that are in the xml file:

// At first you have to create an instance of the TaChartIndicatorBox (you also need a TimeSeries object)
ChartIndicatorBox chartIndicatorBox = new ChartIndicatorBox(); 

// than you can add your indicator to that box
chartIndicatorBox.addChartIndicator("id1",new VolumeIndicator(series), "myVolume", true, TaTypes.categories.DEFAULT);

How to create detailed a chart indicator (e.g. that consists of several ta4j-indicators, with custom names and style)?

For example bollinger bands are indicators that need several indicators in ta4j, like upper bollinger band, lower bollinger band and middle bollinger band. If you want a menu entry to plot all three lines at once, add indicators, names, renderer object, flag (if sobplot) and category to the indicatorBox:

// you can load parameters from a properties file and initialize with it the ta4j-indicators
String timeFrame= parameter.getParameter("bollingerBands_1","Time Frame");
String sd = parameter.getParamter("bollingerBands1", "Standard Deviation");

StandardDeviationIndicator standardDeviation = new StandardDeviationIndicator(closePriceIndicator, Integer.parseInt(sd));
BollingerBandsMiddleIndicator bbm = new BollingerBandsMiddleIndicator(closePriceIndicator);
BollingerBandsUpperIndicator bbu = new BollingerBandsUpperIndicator(bbm, standardDeviation,Decimal.valueOf(timeFrame));
BollingerBandsLowerIndicator bbl = new BollingerBandsLowerIndicator(bbm, standardDeviation,Decimal.valueOf(timeFrame));

// Initialize a list for all indicators 
List<Indicator> indicatorList = new ArrayList<>();
indicatorList.add(bbm);
indicatorList.add(bbu);
indicatorList.add(bbl);

// Give them names
List<String> namesList = new ArrayList<>();
namesList.add("Middle Band");
namesList.add("Upper Band");
namesList.add("Lower Band");

// With XYLineAndShapeRenderer you can define the color, type, size and so on for each ta4j-indicator
XYLineAndShapeRenderer bbRenderer = new XYLineAndShapeRenderer();
bbRenderer.setSeriesPaint(0, Color.YELLOW);
bbRenderer.setSeriesStroke(0, TaTypes.DOT_DOT_LINE);
bbRenderer.setSeriesShape(0, TaTypes.shape_none );
bbRenderer.setSeriesPaint(1, Color.BLUE);
bbRenderer.setSeriesStroke(1,TaTypes.DOT_DOT_LINE);
bbRenderer.setSeriesShape(1, TaTypes.shape_none );
bbRenderer.setSeriesPaint(2, Color.BLUE);
bbRenderer.setSeriesStroke(2, TaTypes.DOT_DOT_LINE);
bbRenderer.setSeriesShape(2, TaTypes.shape_none );

// add the indicator to the box by giving the indicatorList, tha namesList, the name for menu entry, the renderer, flag if
// it should be a sub plot and the category for the menu
indicatorBox.addChartIndicator("BollingerBands1",indicatorList,namesList,String.format("Bollinger Bands (%s, %s)", sd, timeFrame), bbRenderer, false, IndicatorParameters.TaCategory.BOLLINGER);