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 buy and sell at same time, please help.
 Post subject: How to buy and sell at same time, please help. Post rating: 0   New post Posted: Thu 22 Jul, 2021, 13:44 

User rating: 4
Joined: Wed 23 May, 2018, 20:08
Posts: 37
Location: FinlandFinland
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?


 
 Post subject: Re: How to buy and sell at same time, please help. Post rating: 1   New post Posted: Wed 28 Jul, 2021, 01:38 

User rating: 18
Joined: Thu 20 Apr, 2017, 22:42
Posts: 160
Location: Russian Federation,
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.


 

Jump to:  

  © 1998-2011 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