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