Login Page - Create Account

Support Board


Date/Time: Tue, 23 Apr 2024 11:56:31 +0000



[Programming Help] - Trying to run JAVA demo program

View Count: 3922

[2017-10-12 20:02:18]
User178540 - Posts: 15
Hello,

I have written a simple JAVA code using protoc, in order to connect via DTC and get market data.
But no reponse arrives to the program. BTW, is there any example test program I can use?

----------------------

public static void main(String argv[]) throws Exception {
    System.out.println("Starting...");
    
    String modifiedSentence;
    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
    Socket clientSocket = new Socket("127.0.0.1", 11099);

    if (clientSocket.isConnected()) {
      System.out.println("Connected. ");
    }
    
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

    // User DTC API
    MarketDataRequest request = MarketDataRequest.newBuilder()
                    .setRequestAction(RequestActionEnum.SUBSCRIBE)
                    .setSymbolID(1)
                    .setSymbol("GOOG")
                    .build();
    
    System.out.println(request.toString());
                    
    
    
    String sentence = request.toString();
    outToServer.writeBytes(sentence + '\n');

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    sentence = inFromUser.readLine();
    modifiedSentence = inFromServer.readLine();
    System.out.println("FROM SERVER: " + modifiedSentence);
    
    Thread.sleep(300000);
    
    clientSocket.close();
  }
}
[2017-10-12 21:49:27]
Sierra Chart Engineering - Posts: 104368
Follow step numbers 1 through 10 here:
Data and Trading Communications (DTC) Protocol Server: Server Usage

We updated the documentation. Make sure you set the Encoding to Google Protocol Buffers and send the Login message.

Are you at least able to establish a connection?

Also do not use this symbol: ("GOOG")

Set Global Settings >> Data/Trade Service Settings >> Service to SC Data - All Services.

And use the symbol EURUSD for testing.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-10-12 22:02:50]
User178540 - Posts: 15
Thanks for the reponse, basic connection is done, yet no other message such as LOGON seems to arrive to the server:
Logs:
-----------------
DTC Protocol server | Incoming connection from 127.0.0.1. | 2017-10-12 22:01:09
DTCServer (74) | Creating socket. | 2017-10-12 22:01:09
DTC client #19. 127.0.0.1 | No activity on network socket. Closing network socket. | 2017-10-12 22:01:29
DTCServer (74) | Initiating close of socket by core. | 2017-10-12 22:01:29
DTCServer (74) | Shutdown started. Waiting for graceful close. | 2017-10-12 22:01:29
DTCServer (74) | Socket gracefully closed by remote side. | 2017-10-12 22:01:29
DTCServer (74) | Closed. | 2017-10-12 22:01:29
--------------------

Code:
------------------
  Socket clientSocket = new Socket("127.0.0.1", 11099);

    if (clientSocket.isConnected()) {
      System.out.println("Connected. ");
    }

    LogonRequest request = LogonRequest.newBuilder()
        .setHeartbeatIntervalInSeconds(5)
        .setClientName("AnyName")
        .build();
    
    request.writeTo(CodedOutputStream.newInstance(clientSocket.getOutputStream()));
    
    System.out.println("After sendin message");
    
    LogonResponse response = LogonResponse.parseFrom(CodedInputStream.newInstance(clientSocket.getInputStream()));
    
    System.out.println(response);
    
    clientSocket.close();
------------------------
[2017-10-12 22:23:39]
User178540 - Posts: 15
Update, also setting the SET TCP NO DELAY didn't work. same result.
------------
Socket clientSocket = new Socket();
clientSocket.setTcpNoDelay(true);
clientSocket.connect(new InetSocketAddress("127.0.0.1", 11099));
---------
[2017-10-12 23:00:49]
Sierra Chart Engineering - Posts: 104368
Here is the description for Google protocol buffer encoding:
DTC Protocol: Google Protocol Buffers (GPB)

Make sure you are sending the header ahead of each message indicating both the size and type of the message being sent.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-10-12 23:01:23
[2017-10-12 23:04:09]
Sierra Chart Engineering - Posts: 104368
You may also want to start using JSON encoding in order to make this easy in the beginning and then you can switch to Protocol Buffers. That is a very simplified way to get started and easy to understand.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-10-12 23:04:25
[2017-10-13 13:15:20]
User178540 - Posts: 15
I want to try JSON.
Should I set type field for each message? for example, what is the type of LOGON message?
where all the types specification are located?
do you have example JSONs?
[2017-10-13 13:44:26]
User178540 - Posts: 15
For example,
After connection is done sent JSON:
{"Type":1,"HeartbeatIntervalInSeconds":5"}

but no response is send back:
-------------
DTCServer (410) | Creating socket. | 2017-10-13 13:40:12
DTC client #32. 127.0.0.1 | No activity on network socket. Closing network socket. | 2017-10-13 13:40:32
DTCServer (410) | Initiating close of socket by core. | 2017-10-13 13:40:32
DTCServer (410) | Shutdown started. Waiting for graceful close. | 2017-10-13 13:40:32
DTCServer (410) | Socket gracefully closed by remote side. | 2017-10-13 13:40:32
DTCServer (410) | Closed. | 2017-10-13 13:40:32
Deleting individual DTC server: DTC client #32. 127.0.0.1 | 2017-10-13 13:41:01
----------------------
imageconfiguration.gif / V - Attached On 2017-10-13 13:44:10 UTC - Size: 116.11 KB - 581 views
[2017-10-13 17:36:14]
Sierra Chart Engineering - Posts: 104368
Should I set type field for each message? for example, what is the type of LOGON message?

Yes, you must do this. For the actual message type numbers, you can find them in this header file:
http://dtcprotocol.org/DTC_Files/DTCProtocol.h

In this configuration window:
https://www.sierrachart.com/Download.php?Folder=SupportBoard&download=13707

Set the Encoding to JSON.

We are also going to add a log message indicating how many bytes are in the receive buffer before we log this:

DTC client #32. 127.0.0.1 | No activity on network socket. Closing network socket. | 2017-10-13 13:40:32

This will tell us, if you sent at least some data.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-10-13 17:37:11
[2017-10-13 19:33:10]
User178540 - Posts: 15
I chnaged to JSON and still the same. no response from sever.

What is missing then?
This is the body:
"{\"Type\":1,\"HeartbeatIntervalInSeconds\":5\"}"

-------------
DTC Protocol server | Incoming connection from 127.0.0.1. | 2017-10-13 19:30:45
DTCServer (535) | Creating socket. | 2017-10-13 19:30:45
DTC client #36. 127.0.0.1 | No activity on network socket. Closing network socket. | 2017-10-13 19:31:05
DTCServer (535) | Initiating close of socket by core. | 2017-10-13 19:31:05
DTCServer (535) | Shutdown started. Waiting for graceful close. | 2017-10-13 19:31:05
DTCServer (535) | Socket gracefully closed by remote side. | 2017-10-13 19:31:05
DTCServer (535) | Closed. | 2017-10-13 19:31:05
--------------------------
[2017-10-13 21:31:10]
Sierra Chart Engineering - Posts: 104368
Make sure you send a null terminator byte after that JSON message.

Otherwise, Sierra Chart is not going to process the received message and this is what we believe the problem is due to.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2017-10-16 17:52:31]
User178540 - Posts: 15
Thanks, indeed it was the issue.
[2017-12-02 17:41:59]
User905938 - Posts: 10
I think I have made it to the null Terminator, but it takes 30 seconds - 2 min to receive a response from the SC DTC server; when I send a baddly formed message after the login request it returns instantaneously. Can you please help me understand what I am missing. I am using Json response and I have the SC DTC server operating I think correctly, log attached at bottom.

Here is my sample socket code (package name removed), and I brought the messages into a String for simplicity.

=====================================================================================================
Java Class
=====================================================================================================

import java.io.*;
import java.net.*;
import java.util.Date;


public class SierraChartSocketClient {

public SierraChartSocketClient() {
String testServerName = "127.0.0.1";
int port = 11099;

try{
Socket socket = openSocket(testServerName, port);
socket.setTcpNoDelay(true);

String result = loginToSocketAndRequestData(socket);

System.out.println(result);

socket.close();
}catch(Exception e){
e.printStackTrace();
}

}

private String loginToSocketAndRequestData(Socket socket) throws Exception{
byte nullByte = 0x00;
String loginRequestString = "{\"Type\":1,\"Integer_1\":1,\"HeartbeatIntervalInSeconds\":60\", \"ClientName\": \"Sample Application\"}";

String marketDataRequestCL = "{\"Type\":101,\"RequestAction\":\"SUBSCRIBE\",\"SymbolID\":1,\"Symbol\":\"CLF8\", \"Exchange\":\"NYMEX\"}";
String heartBeat = "{\"Type\":3,\"CurrentDateTime \":\"" + (int) (System.currentTimeMillis() / 1000L) + "\" ";

try{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));


BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
System.out.println("Sending Over Login Request: " + new Date(System.currentTimeMillis()).toString());
bufferedOutputStream.write(loginRequestString.getBytes());
bufferedOutputStream.write(nullByte);
bufferedOutputStream.flush();


StringBuilder stringBuilder = new StringBuilder();
String str = bufferedReader.readLine();
System.out.println(str);
System.out.println("Received Login Response: " + new Date(System.currentTimeMillis()).toString());

bufferedOutputStream.write(heartBeat.getBytes());
bufferedOutputStream.flush();
System.out.println("Sending Heartbeat: " + new Date(System.currentTimeMillis()).toString());

bufferedOutputStream.write(marketDataRequestCL.getBytes("ISO-8859-1"));
bufferedOutputStream.write(nullByte);

while((str = bufferedReader.readLine()) != null){
stringBuilder.append(str + "\n");
System.out.println(str);
}


return null;
}catch (IOException e){
e.printStackTrace();
throw e;
}
}

private Socket openSocket(String server, int port) throws Exception{
Socket socket;

try{
InetAddress inteAddress = InetAddress.getByName(server);
SocketAddress socketAddress = new InetSocketAddress(inteAddress, port);

socket = new Socket();

int timeoutInMs = 45*1000; // 45 seconds
socket.connect(socketAddress, timeoutInMs);

return socket;

}catch (SocketTimeoutException e){
System.err.println("Timed out waiting for the socket.");
e.printStackTrace();
throw e;

}
}

public static void main(String[] args){
new SierraChartSocketClient();
}

}


=====================================================================================================
Java Console Output
=====================================================================================================
Sending Over Login Request: Sat Dec 02 10:38:07 MST 2017
{"Type":2,"ProtocolVersion":8,"Result":1,"Integer_1":0,"MarketDepthUpdatesBestBidAndAsk":0,"TradingIsSupported":0,"OCOOrdersSupported":0,"OrderCancelReplaceSupported":0,"SecurityDefinitionsSupported":1,"HistoricalPriceDataSupported":0,"ResubscribeWhenMarketDataFeedAvailable":0,"MarketDepthIsSupported":1,"OneHistoricalPriceDataRequestPerConnection":0,"BracketOrdersSupported":0,"UseIntegerPriceOrderMessages":0,"UsesMultiplePositionsPerSymbolAndTradeAccount":0,"MarketDataSupported":1,"ResultText":"Connected to SC DTC Protocol server. Service=rim","ReconnectAddress":"","ServerName":"SC DTC Server","SymbolExchangeDelimiter":""} {"Type":3,"NumDroppedMessages":0,"CurrentDateTime":0} {"Type":3,"NumDroppedMessages":0,"CurrentDateTime":0} {"Type":3,"NumDroppedMessages":0,"CurrentDateTime":0}
Received Login Response: Sat Dec 02 10:40:47 MST 2017
Sending Heartbeat: Sat Dec 02 10:40:47 MST 2017
null

Process finished with exit code 0

=====================================================================================================
Sierra Chart Log
=====================================================================================================
Software version: 1656 | 2017-12-02 10:38:03
Enabled for: Advanced Features. | 2017-12-02 10:38:03
Enabled for: Sierra Chart Historical Data Service. | 2017-12-02 10:38:03
Allow Support for Sierra Chart Data Feeds is enabled. | 2017-12-02 10:38:03
Current selected Data/Trading service: CQG FIX Trading | 2017-12-02 10:38:03
Chart Update Interval: 500 | 2017-12-02 10:38:03
Data/Trade Service Settings automatically set. | 2017-12-02 10:38:03
Time Zone: -07:00:00 (MST-07MDT+01,M3.2.0/02:00,M11.1.0/02:00) | 2017-12-02 10:38:03
2017-12-02 10:38:03 Local computer time | 2017-12-02 10:38:03
2017-12-02 17:38:03 Local computer time in UTC | 2017-12-02 10:38:03
2017-12-02 10:38:03 Local computer time in SC Time Zone | 2017-12-02 10:38:03
2017-12-02 17:30:04 Server time in UTC | 2017-12-02 10:38:03
Local computer UTC time and Server UTC time difference: 6 seconds. | 2017-12-02 10:38:03
Program path: C:\SierraChart\ | 2017-12-02 10:38:03
Data Files path: C:\SierraChart\Data\ | 2017-12-02 10:38:03
OS Version Number: 6.1 | 2017-12-02 10:38:03
Locale Setting: C | 2017-12-02 10:38:03

DTC Protocol server | Incoming connection from 127.0.0.1. | 2017-12-02 10:38:07
DTCServer (929) | Creating socket. | 2017-12-02 10:38:07
DTC client #59. 127.0.0.1 | Requested heartbeat interval: 40 | 2017-12-02 10:38:07
DTC client #59. Sample Application. 127.0.0.1 | Received logon request. | 2017-12-02 10:38:07
DTC client #59. Sample Application. 127.0.0.1 | Sent successful Logon response message to client. Username: . Result text: Connected to SC DTC Protocol server. Service=rim | 2017-12-02 10:38:07
DTC client #59. Sample Application. 127.0.0.1 | No heartbeat received after 1m 20s. Closing network socket. | 2017-12-02 10:40:48
DTCServer (929) | Initiating close of socket by core. | 2017-12-02 10:40:48
DTCServer (929) | Shutdown started. Waiting for graceful close. | 2017-12-02 10:40:48
DTCServer (929) | Socket gracefully closed by remote side. | 2017-12-02 10:40:48
DTCServer (929) | Closed. | 2017-12-02 10:40:48
[2017-12-05 16:53:01]
User905938 - Posts: 10
Bumping this back up the list
[2017-12-06 00:27:40]
Sierra Chart Engineering - Posts: 104368
We cannot provide programming help.

But what we can say is definitively there is a problem on your side where messages are not being sent and therefore not received by Sierra Chart:
DTC client #59. Sample Application. 127.0.0.1 | No heartbeat received after 1m 20s. Closing network socket. | 2017-12-02 10:40:48

Make sure there is a null terminator after each one. If there is not a null terminator, then that can lead to a problem where the messages just never get processed.

Also we see this:

String marketDataRequestCL = "{\"Type\":101,\"RequestAction\":\"SUBSCRIBE\",\"SymbolID\":1,\"Symbol\":\"CLF8\", \"Exchange\":\"NYMEX\"}";



In regards to the Exchange field, that is not needed. Refer to the Notes section here about this:
Data and Trading Communications (DTC) Protocol Server: Special Notes
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-12-06 00:30:17
[2017-12-06 08:31:10]
User905938 - Posts: 10
My Question is what is the "null terminator" from a Java perspective that I need to be using? Java Strings are not null terminated by default.

I am converting the string to byte[] by using String.getBytes() prior to writing it to the socket connection.
Date Time Of Last Edit: 2017-12-06 08:32:28
[2017-12-06 08:33:51]
Sierra Chart Engineering - Posts: 104368
Maybe someone else can help. We are not familiar with Java.

Would be best to seek help on another Java specific forum.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2017-12-06 08:34:02
[2018-07-25 12:47:38]
traderacademie - Posts: 38
Hi,

I happened to be trying this as well.

Null terminator isn't giving the problem, you already solved that.

The problem is that you're using BufferedReader. This won't work, you need to use BufferedInputStream.

Rgds

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account