Socket Connection

To communicate with JForex platform using the 3rd part application(client), create a Socket server. Server transfers messages from a client to the JForex platform and vice versa. A communication scenario is following:

  • Run a strategy which runs the socket server in a new thread And process command from the client.
  • Send a command from the client to the JForex platform through the socket server.
  • Strategy will execute a requested command in a new thread and print result to the console.

Strategy with socket server description

Strategy runs the socket server on a localhost at a specified port (in our example 7001) When the server receives a message from the client it creates a task tell to the strategy to complete this task and sends back an answer with an information from the JForex platform.

import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.concurrent.*;

@RequiresFullAccess
public class SocketStrategy implements IStrategy {
        private IEngine engine;
        private IConsole console;
        private IContext context;
        private int counter = 0;
        private TransferSocketServer server;

After strategy starts it runs a socket.

  public void onStart(IContext context) throws JFException {
                this.engine = context.getEngine();
                this.console = context.getConsole();
                this.context = context;
                server = new TransferSocketServer();
        }

        public void onAccount(IAccount account) throws JFException {
        }

        public void onMessage(IMessage message) throws JFException {
        }

        public void onStop() throws JFException {
                for (IOrder order : engine.getOrders()) {
                        engine.getOrder(order.getLabel()).close();
                }
                server.stopRun();
        }

        public void onTick(Instrument instrument, ITick tick) throws JFException {
        }

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

        protected String getLabel(Instrument instrument) {
                String label = instrument.name();
                label = label + (counter++);
                label = label.toUpperCase();
                return label;
        }

        public void print(String message) {
                console.getOut().println(message);
        }

TransferSocketServer class run the socket server on the localhost 7001 port and wait message from clients.


        public class TransferSocketServer implements Runnable{
                private ServerSocket server;
                private Socket socket;
                private int port = 7001;
                private boolean serverRun = true;
                private Thread serverThread;

                public TransferSocketServer() {
                        serverThread = new Thread(this, "Server");
                        serverThread.start();
                }

                public void run() {
                        try {
                                server = new ServerSocket(port);
                        } catch (IOException e) {
                                e.printStackTrace(console.getErr());
                        }
                        handleConnection();
                }

                public void stopRun() {
                        serverRun = false;
                        try {
                                server.close();
                        } catch (IOException e) {
                                e.printStackTrace(console.getErr());
                        }
                }

                public void handleConnection() {
                        System.out.println("Sever waiting for client and server message ");
                        while (serverRun) {
                                try {
                                        socket = server.accept();
                                        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                                        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());

                                        processMessageFromClient(inputStream, outputStream);

                                        outputStream.close();
                                        inputStream.close();
                                        socket.close();
                                } catch (SocketException e) {
                                        if (!serverRun) {
                                                System.out.println("Closing server");
                                        } else {
                                                e.printStackTrace(console.getErr());
                                        }
                                } catch (Exception e) {
                                        e.printStackTrace(console.getErr());
                                }
                        }
                }

TransferSocketServer class run the socket server on the localhost 7001 port and wait message from clients.

                private void processMessageFromClient(ObjectInputStream inputStream, ObjectOutputStream outputStream) throws IOException, ClassNotFoundException, InterruptedException, ExecutionException {
                        String message = (String) inputStream.readObject();
                        System.out.println("Message Received: " + message);
                        Future<String> future = context.executeTask(new ClientTask(message));
                        String string = future.get();
                        if (string != null) {
                                outputStream.writeObject("Connected. Command " +  string );
                        } else {
                                outputStream.writeObject("Connected. Incorrect command");
                        }
                }
        }

ClientTask class creates an order in a new thread

   public class ClientTask implements Callable<String> {
                private String message;
                public ClientTask(String message) {
                        this.message = message;
                }

                public String call() throws JFException {
                        if (message.equals("BUY")){
                            engine.submitOrder(getLabel(Instrument.EURUSD), Instrument.EURUSD, OrderCommand.BUY, 0.01);
                                return "Buy order completed";
                        }
                        if (message.equals("SELL")) {
                                engine.submitOrder(getLabel(Instrument.EURUSD), Instrument.EURUSD, OrderCommand.SELL, 0.01);
                                return "Sell order completed";
                        }
            return null;
                }
        }

}

Download

SimpleSocketStrategy.java

Console socket client for the connection to the server

Client connects through socket port 7001 and will sent the buy or the sell command to the JForex platform.

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient {

        public static void main(String[] args) {
                try {
                        InetAddress host = InetAddress.getLocalHost();
                        Socket socket = new Socket(host.getHostName(), 7001);
                        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                        outputStream.writeObject("SELL");
                        ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                        String message = (String) inputStream.readObject();
                        System.out.println("Message: " + message);
                        inputStream.close();
                        outputStream.close();
                } catch (UnknownHostException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                }
        }
}
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.