Dukascopy Support Board
http://www.dukascopy.com/swiss/english/forex/jforex/forum/

How to make this code also sell ? Please help.
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=57757
Page 1 of 1

Author:  NightOwl [ Sat 25 Dec, 2021, 07:03 ]
Post subject:  How to make this code also sell ? Please help.

Hello,

I have this code. That I would like to also do sell orders. The output should look like
"BUY, SELL, BUY, SELL, BUY, SELL,,,"
So an never ending loop of BUY and after that SELL

Please help, thanks! :)

package jforex;
 
import java.util.*;
 
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
 
public class Strategy implements IStrategy {
    private IEngine engine;
    private IConsole console;
    private IHistory history;
    private IContext context;
    private IIndicators indicators;
    private IUserInterface userInterface;
     
    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
    @Configurable("Amount")
    public double amount = 0.10;
    @Configurable("Stop loss")
    public int stopLossPips = 10;
    @Configurable("Take profit")
    public int takeProfitPips = 100;
     
    private OrderCommand orderCmd = null;
     
    private int counter = 0;
     
    public void onStart(IContext context) throws JFException {
        this.engine = context.getEngine();
        this.console = context.getConsole();
        this.history = context.getHistory();
        this.context = context;
        this.indicators = context.getIndicators();
        this.userInterface = context.getUserInterface();
         
    }
 
    public void onAccount(IAccount account) throws JFException {
    }
 
    public void onMessage(IMessage message) throws JFException {
    }
 
    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 {
         
        // for example strategy choose BUY order and store to global variable orderCmd
        // in this point strategy make a signal to create an order
        // https://www.dukascopy.com/wiki/en/development/strategy-api/strategy-examples
        this.orderCmd = OrderCommand.BUY;
         
         
        // then sends the order
        submitOrder(orderCmd);
         
        // resubmit
        resubmitOrder();
         
    }
     
    public void resubmitOrder() throws JFException {
        // check if theres no open order in globara array
        // engine.getOrders() for selected instrument
        // if the array is not empty quit from the function
        // but if the array engine.getOrders is empty then send order again
        if (engine.getOrders(this.instrument).size() != 0) return;
        submitOrder(orderCmd);
    }
         
    // https://www.dukascopy.com/wiki/en/development/strategy-api/strategy-examples/sma-simple
    public void submitOrder(OrderCommand orderCmd) throws JFException {
         
        // limit to one order
        if (engine.getOrders(this.instrument).size() > 0) return;
     
        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
        IOrder order =  engine.submitOrder(getLabel(instrument),
            this.instrument, orderCmd, this.amount, 0, 20, stopLossPrice, takeProfitPrice);
        order.waitForUpdate(2000);
    }
 
    protected String getLabel(Instrument instrument) {
        String label = instrument.name();
        label = label + (counter++);
        label = label.toUpperCase();
        return label;
    }
 
    private double getPipPrice(int pips) {
        return pips * this.instrument.getPipValue();
    }
     
}

  Page 1 of 1