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

How to buy and sell at same time, please help.
http://www.dukascopy.com/swiss/english/forex/jforex/forum/viewtopic.php?f=65&t=57691
Page 1 of 1

Author:  NightOwl [ Thu 22 Jul, 2021, 13:44 ]
Post subject:  How to buy and sell at same time, please help.

Hello, I would like to have a code that buys and sells at same time, but this way it is clearly not working. Here is the code.

import com.dukascopy.api.*;

public class flipper implements IStrategy {
   
   

    @Configurable("Instrument")
    public Instrument instrument = Instrument.EURUSD;
     
    @Configurable("Amount")
    public double betsize = 0.001;

    @Configurable("Stop Loss")
    public double stoploss = 300;
   
    @Configurable("Take Profit")
    public double takeprofit = 5;
   
    @Configurable("Spread")
    public double spreadlimit = 1;
   


    private IEngine engine;
    private IConsole console;
    private IOrder order;
   
    @Override
    public void onStart(IContext context) throws JFException {
       

       
         this.engine = context.getEngine();
         this.console = context.getConsole();
        engine = context.getEngine();
        console = context.getConsole();
        context.setSubscribedInstruments(java.util.Collections.singleton(Instrument.EURUSD), true);
       
    }
   
    public void onMessage(IMessage message) throws JFException {}
    public void onTick(Instrument instrument, ITick tick) throws JFException {
       
        order = engine.submitOrder("EURUSD" + System.currentTimeMillis(), Instrument.EURUSD, IEngine.OrderCommand.BUY, betsize ,0, spreadlimit, tick.getAsk() - stoploss  * Instrument.EURUSD.getPipValue(),
                tick.getAsk() + takeprofit * Instrument.EURUSD.getPipValue(), 0, "A: "  + " B: ");
     
        order = engine.submitOrder("EURUSD" + System.currentTimeMillis(), Instrument.EURUSD, IEngine.OrderCommand.SELL, betsize, 0, spreadlimit, tick.getBid() + stoploss * Instrument.EURUSD.getPipValue(),
                tick.getBid() - takeprofit * Instrument.EURUSD.getPipValue(), 0, "A: " + " B: ");
       
       
    }


       

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


And here is the result Image

https://imgur.com/a/ceNVpZO

as you can see it does not buy and sell every time, even that the code says it should?

Author:  mtnfx [ Wed 28 Jul, 2021, 01:38 ]
Post subject:  Re: How to buy and sell at same time, please help.

JForex API does not allow multiple orders to have same label. In your case SELL will have in 99.99% of cases same label as BUY.

  Page 1 of 1