SMA Simple

The SMA Simple strategy is based on SMA (Simple moving average) indicator with time period 50. The strategy makes an order whenever the SMA line crosses a candle. The order is made the same direction as the crossed candle goes i.e.:

  • If the SMA line crosses an ascending candle, a buy order is made,
  • If the SMA line crosses an descending candle, a sell order is made.
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.IIndicators.AppliedPrice;

public class SMASimpleStrategy implements IStrategy {
        private IEngine engine;
        private IHistory history;
        private IIndicators indicators;
        private int counter = 0;

Initialize configurable parameters.

        public Instrument instrument = Instrument.EURUSD;
        public Period selectedPeriod = Period.FIFTEEN_MINS;
        @Configurable("SMA filter")
        public Filter indicatorFilter = Filter.NO_FILTER;
        public double amount = 0.02;
        @Configurable("Stop loss")
        public int stopLossPips = 10;
        @Configurable("Take profit")
        public int takeProfitPips = 90;

        public void onStart(IContext context) throws JFException {
                this.engine = context.getEngine();
                this.history = context.getHistory();
                this.indicators = context.getIndicators();

        public void onAccount(IAccount account) throws JFException {

        public void onMessage(IMessage message) throws JFException {

Close all orders when the strategy stops.

        public void onStop() throws JFException {
                //close all orders
                for (IOrder order : engine.getOrders()) {

        public void onTick(Instrument instrument, ITick tick) throws JFException {

In every fifteen mintues onBar checks if the SMA line has crossed the previous candle, if that is the case then a new order gets created.

        public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {

                if (!instrument.equals(Instrument.EURUSD) || !period.equals(Period.FIFTEEN_MINS))

                IBar prevBar = history.getBar(instrument, selectedPeriod, OfferSide.BID, 1);

                int smaTimePeriod = 50;
                int candlesBefore = 2;
                int candlesAfter = 0;

                double sma = indicators.sma(instrument, selectedPeriod, OfferSide.BID, AppliedPrice.CLOSE, 
                    smaTimePeriod, indicatorFilter, 
                    candlesBefore, prevBar.getTime(), candlesAfter)[0];

                // SMA crossed previous green candle
                if (prevBar.getOpen() < sma && prevBar.getClose() > sma) {
                // SMA crossed previous red candle
                if (prevBar.getOpen() > sma && prevBar.getClose() < sma) {

Method submitOrder calculates Take Profit and Stop Loss prices and creates the order.

   private IOrder submitOrder(OrderCommand orderCmd) throws JFException {

        double stopLossPrice, takeProfitPrice;

        // Calculating stop loss and take profit prices
        if (orderCmd == OrderCommand.BUY) {
            stopLossPrice = history.getLastTick(this.instrument).getBid();
            stopLossPrice -= getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getBid();
            takeProfitPrice += getPipPrice(this.takeProfitPips);
        } else {
            stopLossPrice = history.getLastTick(this.instrument).getAsk();
            stopLossPrice += getPipPrice(this.stopLossPips);
            takeProfitPrice = history.getLastTick(this.instrument).getAsk();
            takeProfitPrice -= getPipPrice(this.takeProfitPips);

        // Submitting an order for the specified instrument at the current market price
        return engine.submitOrder(getLabel(instrument),
            this.instrument, orderCmd, this.amount, 0, 20, stopLossPrice, takeProfitPrice);

    protected String getLabel(Instrument instrument) {
        String label =;
        label = label + (counter++);
        label = label.toUpperCase();
        return label;

    private double getPipPrice(int pips) {
        return pips * this.instrument.getPipValue();

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