Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How to make this code also sell ? Please help.
 Post subject: How to make this code also sell ? Please help. Post rating: 0   New post Posted: Sat 25 Dec, 2021, 07:03 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 44
Location: FinlandFinland
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();
    }
     
}


 

Jump to:  

cron
  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com