Use Map

Map is a java.util interface for mapping keys to values. One may make use of it to bind a characteristic JForex object to some parameter, for instance:

  • Instrument to some counter;
  • Order to some time value, chart object or to another order;
  • Order to a set of orders.

The example strategy maps each instrument to its tick count since the start of the strategy.

package jforex.test;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import com.dukascopy.api.*;

 * The strategy shows how one can us Map functionality to
 * conveniently bind JForex objects to values, in the particular case
 * we map each instrument to its tick count since the start of the strategy.
public class InstrumentCounters implements IStrategy {

    private IConsole console;
    private IContext context;

    Set<Instrument> instruments = new HashSet<Instrument>();
    Map<Instrument, Integer> counters = new HashMap<Instrument, Integer>();

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") 
        {{ setTimeZone(TimeZone.getTimeZone("GMT")); }};

    public void onStart(IContext context) throws JFException {

        console = context.getConsole();
        this.context = context;


        for(Instrument instrument : instruments){
            counters.put(instrument, 0);


    public void onTick(Instrument instrument, ITick tick) throws JFException {
        //work only with our set of instruments
        //increase counter by 1
        counters.put(instrument, counters.get(instrument) + 1);

        console.getOut().println(instrument + " tick arrived");
        for(Map.Entry<Instrument, Integer> entry : counters.entrySet()){
            console.getOut().println(String.format("%s %s tick count: %s", sdf.format(tick.getTime()), entry.getKey(), entry.getValue()));

    private void subscribeInstruments(Set<Instrument> instruments){

        // wait max 1 second for the instruments to get subscribed
        int i = 10;
        while (!context.getSubscribedInstruments().containsAll(instruments)) {
            try {
                console.getOut().println("Instruments not subscribed yet " + i);
            } catch (InterruptedException e) {

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
    public void onMessage(IMessage message) throws JFException {}
    public void onAccount(IAccount account) throws JFException {}
    public void onStop() throws JFException {}


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