Indicator chart panels

Whenever an indicator gets plotted on a chart, a new indicator panel gets created. From a strategy one can do the the following actions with the panel:

  • Add additional indicators to the same panel.
  • Add chart objects to the panel (and do other object actions).
  • Remove the panel by removing all of its indicators.

Regarding chart objects, it is important to note that each IChartPanel (including the main chart) handles its own chart objects, meaning that:

  • The key uniqueness is chart panel bound.
  • One can't add the same IChartObject to different chart panels.

Note: The functionality is available with JForex-API 2.7.4.

Add indicator and chart objects

One can add another indicator to the same IChartPanel by using the addIndicator method and add chart objects by using the IChartPanel.add method. Note that only indicators with IIndicator.getIndicatorInfo().isOverChart()=false can be added to an indicator panel, those with isOverChart()=true can be added to the main chart only. Consider adding two RSI indicators with different time periods to the same indicator panel:

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

    if(chart == null){            
        console.getErr().println("No chart opened for " + instrument);
        context.stop(); //stop the strategy right away
    }

    //add RSI with timePeriod = 15
    IChartPanel rsiPanel = chart.add(indicators.getIndicator("RSI"), new Object[]{15});

    //and another RSI to the same panel
    rsiPanel.add(indicators.getIndicator("RSI"), new Object[]{30}, 
        new Color[]{Color.MAGENTA},new DrawingStyle[]{DrawingStyle.LINE}, new int[]{1});

    //add an extra level line
    IHorizontalLineChartObject hLine = chart.getChartObjectFactory().createHorizontalLine("subHLine", 50);
    hLine.setColor(Color.RED);
    hLine.setLineStyle(LineStyle.DASH_DOT_DOT);
    rsiPanel.add(hLine);

    //we can't add to chart panels with isOverChart()=true
    IndicatorInfo emaInfo = indicators.getIndicator("EMA").getIndicatorInfo();
    if(emaInfo.isOverChart()){
        console.getOut().println("can't add " + emaInfo.getName() +
            " to the indicator panel since it can be plotted only on the main chart!");
    }
}

IndicatorPanelAddStochAndHLines.java

Remove indicators and indicator panels

One removes an indicator by calling the IChartPanel.removeIndicator method. By removing the last indicator panel's indicator, the panel itself gets removed. It can be detected if the panel is still under the main chart by checking IChartPanel.isActive(). All active indicator panels can get retrieved by calling the IChart.getSubPanels() method. Consider removing all indicator panels on strategy stop:

@Override
public void onStop() throws JFException {

    //remove all indicator panels
    while(chart.getIndicatorPanels().size() > 0){
        IChartPanel panel = chart.getIndicatorPanels().get(0);
        for (IIndicator indicator : panel.getIndicators()) {
            panel.removeIndicator(indicator);
        }
    }
}

IndicatorPanelAddStochAndHLines.java

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