Chart Operations

Apart from chart object operations, JForex-API also contains methods for doing chart-related actions, namely:

  • open and close chart,
  • retrieve chart's feed properties,
  • retrieve chart drawable coordinates,
  • save chart as an image,
  • add mouse listener.

Open/Close chart

private IChart chart;

@Override
public void onStart(IContext context) throws JFException {  
    this.context = context;      
    IFeedDescriptor feedDescriptor = 
        new TimePeriodAggregationFeedDescriptor(Instrument.EURUSD, Period.TEN_SECS, OfferSide.BID);
    chart = context.openChart(feedDescriptor); 
}

@Override
public void onStop() throws JFException {
    context.closeChart(chart);
}

ChartOperations.java

Retrieve feed properties

private IChart chart;
private IConsole console;
@Override
public void onStart(IContext context) throws JFException {  
    this.console = context.getConsole();
    chart = context.getChart(Instrument.EURUSD);

    console.getOut().println("Chart's feed: " + chart.getFeedDescriptor()); //will print all feed's parameters
    console.getOut().println("Chart's instrument: " + 
        chart.getFeedDescriptor().getInstrument()); //retrieve a specific feed parameter    
}

ChartOperations.java

Retrieve drawable coordinates

private IChart chart;
private IConsole console;
@Override
public void onStart(IContext context) throws JFException {
    this.console = context.getConsole();
    chart = context.getChart(Instrument.EURUSD);
    print("Chart drawable coordinates: min(%s,%.5f); max(%s,%.5f)",
            DateUtils.format(chart.getMinTime()), chart.getMinPrice(),
            DateUtils.format(chart.getMaxTime()), chart.getMaxPrice()
    );
}
private void print(String message, Object... args){
    print(String.format(message,args));
}

ChartOperations.java

Save as image

private IChart chart;
private IConsole console;
@Override
public void onStart(IContext context) throws JFException {
    this.console = context.getConsole();
    chart = context.getChart(Instrument.EURUSD);
    try {
        File file = new File(context.getFilesDir().getPath() + File.separator + "ChartImage.png");
        ImageIO.write(chart.getImage(), "png", file);
        print("chart image saved in: " + file.getAbsolutePath());
    } catch (Exception e) {
        e.printStackTrace(console.getErr());
    }
}

ChartOperations.java

Add mouse listener

One can add a mouse listener to a IChartPanel by calling the IChartPanel.addMouseListener method. Object IChartPanelMouseEvent contains price-time coordinates and other event properties from the event's source. Events source can be accessed by IChartPanelMouseEvent.getSourceEvent() method.

Note: Available with JForex-API 2.7.8.

private IConsole console;
private IChart chart;
private IChartPanelMouseListener listener;

@Override
public void onStart(IContext context) throws JFException {
    console = context.getConsole();
    chart = context.getChart(Instrument.EURUSD);
    if(chart == null){
        console.getErr().println("No chart opened for EURUSD!");
        context.stop();
    }
    chart.addMouseListener(false, listener = new IChartPanelMouseListener(){
        public void mouseClicked(IChartPanelMouseEvent e) { print(e, "mouse Clicked");}
        public void mousePressed(IChartPanelMouseEvent e) { print(e, "mouse Pressed");}
        public void mouseReleased(IChartPanelMouseEvent e) { print(e, "mouse Released");}
        public void mouseEntered(IChartPanelMouseEvent e) { print(e, "mouse Entered");}
        public void mouseExited(IChartPanelMouseEvent e) { print(e, "mouse Exited");}
        public void mouseDragged(IChartPanelMouseEvent e) { print(e, "mouse Dragged");}
        public void mouseMoved(IChartPanelMouseEvent e) { print(e, "mouse Moved");}
    });

}

private void print(IChartPanelMouseEvent e, String comment){
    // call e.getPrice and e.getTime instead of e.toString
    // if you wish to customize the logging formatting or make use of the values
    console.getOut().println(String.format("%s %s x=%s y=%s",
        comment, e.toString(), e.getSourceEvent().getXOnScreen(), e.getSourceEvent().getYOnScreen()));
}

@Override
public void onStop() throws JFException {
    chart.removeMouseListener(listener);
}

MouseListenerStrat.java

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