Include libraries

In order to use an external library one has to add a @Library annotation to the strategy. There are two options how to refer to the libaries:

  1. By name, for instance:

    public class Strategy implements IStrategy {

    in this case the library has to be placed in the strategy files' directory, i.e., IContext.getFilesDir().

  2. By full path, for instance:

    public class Strategy implements IStrategy {

in this case one also has to add the @RequiresFullAccess annotation.

Consider an example strategy which uses an external library. For library download link and launch instructions see strategy comments.

package jforex.lib;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.math.NumberUtils;

import com.dukascopy.api.*;
import com.dukascopy.api.IIndicators.AppliedPrice;

 * The strategy demonstrates how to use an external library in a strategy.
 * Namely it uses the commons-lang3-3.0.1.jar library, which can be downloaded here:
 * The strategy itself prints the last 10 values and reversed values of sma and as 
 * well finds min/max values.
 * Before running the strategy place the commons-lang3-3.0.1.jar in {@link IContext#getFilesDir()} directory
public class SmaFlipMinMax implements IStrategy {

    private IConsole console;

    public void onStart(IContext context) throws JFException {
        console = context.getConsole();
        long time = context.getHistory().getBar(Instrument.EURUSD, Period.TEN_MINS, OfferSide.BID, 1).getTime();
        double[] sma = context.getIndicators().sma(
            Instrument.EURUSD, Period.TEN_MINS, OfferSide.BID, AppliedPrice.CLOSE, 
            12, Filter.NO_FILTER, 10, time, 0);
        double[] smaReversed = ArrayUtils.clone(sma);
        double min = NumberUtils.min(sma);
        double max = NumberUtils.max(sma);
            "sma for last 10 candles \nasc: %s \ndesc:%s \nmax=%.5f min=%.5f", 
            toString(sma), toString(smaReversed), max, min).println();

    private static String toString(double[] arr) {
        StringBuilder sb = new StringBuilder();
        for (int r = 0; r < arr.length; r++) {
            sb.append(String.format("[%s] %.5f; ", r, arr[r]));
        return sb.toString();

    public void onStop() throws JFException {    }
    public void onTick(Instrument instrument, ITick tick) throws JFException {}
    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 {}


Package and use classes from a jar file

Here is an example strategy StrategyWithJar that uses a MyClass from myJar.jar.


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