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 connect to two accounts with API
 Post subject: How to connect to two accounts with API Post rating: 0   New post Posted: Sat 25 Jan, 2014, 13:36 
User avatar

User rating: 0
Joined: Mon 16 Sep, 2013, 17:54
Posts: 4
Location: PolandPoland
Hi,
How to connect to two accounts(I have something like that):

package singlejartest;

import com.dukascopy.api.system.ISystemListener;
import com.dukascopy.api.system.IClient;
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.IEngine;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.Instrument;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

/**
 * This small program demonstrates how to initialize Dukascopy client and start a strategy
 */
public class MainMain {
    private static final Logger LOGGER = LoggerFactory.getLogger(MainMain.class);
    private static IEngine engine;
   
    //url of the DEMO jnlp
    private static String jnlpUrl = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    //user name
    private static String userName = "DEMO";
    //password
    private static String password = "pass";

    //url of the DEMO jnlp second account
    private static String jnlpUrl2 = "https://www.dukascopy.com/client/demo/jclient/jforex.jnlp";
    //user name
    private static String userName2 = "DEMO1";
    //password
    private static String password2 = "pass1";
   
   
   
   
    public static void main(String[] args) throws Exception {
        //get the instance of the IClient interface
        final IClient client = ClientFactory.getDefaultInstance();
        final IClient client2 = ClientFactory.getDefaultInstance();
        //set the listener that will receive system events
        client.setSystemListener(new ISystemListener() {
            private int lightReconnects = 3;

           @Override
           public void onStart(long processId) {
                LOGGER.info("Strategy started: " + processId);
           }

         @Override
         public void onStop(long processId) {
                LOGGER.info("Strategy stopped: " + processId);
                if (client.getStartedStrategies().size() == 0) {
                    System.exit(0);
                }
         }

         @Override
         public void onConnect() {
                LOGGER.info("Connected");
                lightReconnects = 3;
         }

         @Override
         public void onDisconnect() {
                LOGGER.warn("Disconnected");
                if (lightReconnects > 0) {
                    client.reconnect();
                    --lightReconnects;
                } else {
                    try {
                        //sleep for 10 seconds before attempting to reconnect
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                    try {
                        client.connect(jnlpUrl, userName, password);
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
         }
      });

        //set the listener that will receive system events client2
        client2.setSystemListener(new ISystemListener() {
            private int lightReconnects = 3;

           @Override
           public void onStart(long processId) {
                LOGGER.info("Strategy2 started: " + processId);
           }

         @Override
         public void onStop(long processId) {
                LOGGER.info("Strategy2 stopped: " + processId);
                if (client2.getStartedStrategies().size() == 0) {
                    System.exit(0);
                }
         }

         @Override
         public void onConnect() {
                LOGGER.info("Connected2");
                lightReconnects = 3;
         }

         @Override
         public void onDisconnect() {
                LOGGER.warn("Disconnected2");
                if (lightReconnects > 0) {
                    client2.reconnect();
                    --lightReconnects;
                } else {
                    try {
                        //sleep for 10 seconds before attempting to reconnect
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        //ignore
                    }
                    try {
                        client2.connect(jnlpUrl, userName, password);
                    } catch (Exception e) {
                        LOGGER.error(e.getMessage(), e);
                    }
                }
         }
      });

// client loger info       
        LOGGER.info("Connecting client...");
        //connect to the server using jnlp, user name and password
        client.connect(jnlpUrl, userName, password);
// client2 loger info
        LOGGER.info("Connecting client2...");
        //connect to the server using jnlp, user name and password
        client.connect(jnlpUrl2, userName2, password2);
       
        //wait for it to connect client1
        int i = 10; //wait max ten seconds
        while (i > 0 && !client.isConnected()) {
            Thread.sleep(1000);
            i--;
        }
       
        //wait for it to connect client2
        int ii = 10; //wait max ten seconds
        while (ii > 0 && !client2.isConnected()) {
            Thread.sleep(1000);
            ii--;
        }
       
        // client
        if (!client.isConnected()) {
            LOGGER.error("Failed to connect client Dukascopy servers");
            System.exit(1);
        }
       
        // client2
        if (!client2.isConnected()) {
            LOGGER.error("Failed to connect client2 to Dukascopy servers");
            System.exit(1);
        }
       
        //subscribe to the instruments client
        Set<Instrument> instruments = new HashSet<Instrument>();
        instruments.add(Instrument.EURUSD);
        LOGGER.info("Subscribing instruments client...");
        client.setSubscribedInstruments(instruments);

        //subscribe to the instruments client2
        Set<Instrument> instruments2 = new HashSet<Instrument>();
        instruments2.add(Instrument.GBPUSD);
        LOGGER.info("Subscribing instruments client2...");
        client2.setSubscribedInstruments(instruments2);
       
        //workaround for LoadNumberOfCandlesAction for JForex-API versions > 2.6.64
        Thread.sleep(5000);
       
        //start the strategy client
        LOGGER.info("Starting client");
        //client.startStrategy(new MA_Play());
     
        [color=#FF0040]/// get open order  [/color]
   
        //now it's running
       
        //start the strategy client2
        LOGGER.info("Starting  client2");
        //client2.startStrategy(new MA_Play());

       
       [color=#FF0040]// Open new order[/color]

        //now it's running
    }
}



I need to get an open position from one account and open it on another account
how to do something like that?
or
Is it possible to connect from the strategy to another account and get opened orders?

Regards
:mrgreen:


 
 Post subject: Re: How to connect to two accounts with API Post rating: 0   New post Posted: Mon 27 Jan, 2014, 08:24 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
There is only account per process allowed, this is per API design. See:
viewtopic.php?f=65&t=35436


 
 Post subject: Re: How to connect to two accounts with API Post rating: 0   New post Posted: Tue 28 Jan, 2014, 13:07 
User avatar

User rating: 164
Joined: Mon 08 Oct, 2012, 10:35
Posts: 676
Location: NetherlandsNetherlands
Quote:
I need to get an open position from one account and open it on another account
how to do something like that?


Without knowing more about this, this would be a possible way:
As Support pointed out, you have to run two platform instances for the 2 accounts. This can be running on the same PC, no restrictions about that.
A strategy is running on platform A, which strategy monitors the opened orders frequently (every 5 second, for example). This strategy updates (write to) an output file (xml, cvs, whatever you want) with the details of the orders (instrument, amount, SL/TP, etc).
There is another strategy running on platform B, which strategy reads the mentioned output file, and if a new order is entered, it will submit an order.


A similar concept has been developed already. It is called the MT4/JForex client bridge, which connects a MetaTrader platform with JForex, and copies every order that is placed in MetaTrader to JForex. You can take a look on the source code to get starting.


 

Jump to:  

cron
  © 1998-2025 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