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.

Socket server send data onTick/onBar
 Post subject: Socket server send data onTick/onBar Post rating: 0   New post Posted: Wed 15 Jul, 2020, 12:10 

User rating: 0
Joined: Mon 04 Nov, 2019, 21:12
Posts: 10
Location: Switzerland,
Hi everyone

I'd like to send certain data every time the "onTick" or "onBar" method is called. To test I used price data.

I used Dukas Serverstrategy with TCP Socket as a base. The only change I made was to exclude the send and received method from the establish connection method. This allows me to send a message everywhere in the code. But it doesn't work when i call the method from "onTick" or "onBar".
Is this supposed not to work?

        public void establishConnection() {
            System.out.println("Server waiting for client and server message ");
            print("Server waiting for client and server message ");
            while (serverRun) {
                try {
                    socket = server.accept();
                    console.getOut().println( " THE CLIENT"+" "+ socket.getInetAddress() +":"+socket.getPort()+" IS CONNECTED ");
                    String fromClient = receiveMessage();
                    //print("Message from Client: " + fromClient);
                    //sendMessage(fromClient);
                    //socket.close();
                } catch (SocketException e) {
                    if (!serverRun) {
                        System.out.println("Closing server");
                        console.getOut().println("Closing server");
                    } else {
                        e.printStackTrace(console.getErr());
                    }
                } catch (Exception e) {
                    e.printStackTrace(console.getErr());
                }
            }
        }
       
        public String receiveMessage() throws IOException {
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String fromClient = in.readLine();
            //console.getOut().println("fromclient: " + fromClient);
            return fromClient;
        }
       
        public void sendMessage(String fromServer) throws IOException, ClassNotFoundException, InterruptedException, ExecutionException {
            PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
            out.println(fromServer);
            console.getOut().println("Reply from Server: " + fromServer);
            out.flush();
        }


I call the method like this:
    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException{
        if (instrument.equals(Instrument.EURUSD)) {                    //filter EUR/USD data
            if (EURUSD == '0') {
                EURUSD = askBar.getClose();
                value = Double.toString(EURUSD);
            }
            //printData();
            server.sendMessage(value);
        }
    }

It throws an exception that IOException is not handled for "server.sendMessage()". If I add this in the code like this:

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException, ClassNotFoundException, InterruptedException, ExecutionException{
        if (instrument.equals(Instrument.EURUSD)) {                    //filter EUR/USD data
            if (EURUSD == '0') {
                EURUSD = askBar.getClose();
                value = Double.toString(EURUSD);
            }
            //printData();
            server.sendMessage(value);
        }
    }

It throws an error that these exceptions are not compatible with "onBar".

Are there any workarounds or methods how to make this work?

Thanks a lot!
Cheers, Luke


 
 Post subject: Re: Socket server send data onTick/onBar Post rating: 0   New post Posted: Thu 23 Jul, 2020, 10:51 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Greetings.

You can wrap IOException to JFException:

    public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
        if (instrument.equals(Instrument.EURUSD)) {                    //filter EUR/USD data
            if (EURUSD == '0') {
                EURUSD = askBar.getClose();
                value = Double.toString(EURUSD);
            }
            //printData();
            try {
                server.sendMessage(value);
            } catch (IOException | ClassNotFoundException | InterruptedException | ExecutionException ex) {
                throw new JFException("Error while sending message", ex);
            }
        }
    }


Regards.


 
 Post subject: Re: Socket server send data onTick/onBar Post rating: 0   New post Posted: Wed 29 Jul, 2020, 21:16 

User rating: 0
Joined: Mon 04 Nov, 2019, 21:12
Posts: 10
Location: Switzerland,
Good evening.

Thank you very much for the hint. Code is working now!

Cheers, Luke


 

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