Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 21:37:33 +0000



Post From: [SC Relay Server] Documentation Needed

[2024-05-03 14:28:16]
MichaelPPTF - Posts: 69
Update: May 3, 2024

As it turns out, this is an important field that I forgot to set in my last response:
Send a LOGON_REQUEST message with the LOGON_REQUEST::Integer_1 field set to 0x2. This causes the second bit of the integer to be true. It is this message with this flag which puts the DTC Protocol Server connection into Relay Server mode.

This is the revised code:


import socket
import json
import threading
import time

def create_logon_request():
logon_request = {
"Type": 1,
"ProtocolVersion": 8,
"HeartbeatIntervalInSeconds": 10,
"Integer_1": 0x2,
}
return json.dumps(logon_request).encode('utf-8') + b'\x00'

def create_heartbeat():
heartbeat = {
"Type": 3,
}
return json.dumps(heartbeat).encode('utf-8') + b'\x00'

def send_heartbeat(sock):
while True:
try:
sock.send(create_heartbeat())
print("Heartbeat sent.")
except socket.error as e:
print("Socket error:", e)
break
time.sleep(10) # Send heartbeat every 10 seconds

def receive_response(sock):
response = b""
try:
while True:
part = sock.recv(1024)
if part:
response += part
# Process each complete message
if response.endswith(b'\x00'):
# Remove the null terminator and decode the message
complete_message = response[:-1].decode('utf-8')
process_message(complete_message)
response = b"" # Reset the buffer after processing
else:
break
except socket.error as e:
print("Socket error:", e)

def process_message(message):
try:
msg_json = json.loads(message)
# Example of handling different types of messages
if msg_json['Type'] == 3:
print("Heartbeat received.")
elif msg_json['Type'] == 101:
print("Market data received:", msg_json)
else:
print("Received message:", msg_json)
except json.JSONDecodeError as e:
print("Error decoding JSON:", e)
except KeyError:
print("Received unknown message type:", message)

def main():
host = '127.0.0.1'
port = 11099
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((host, port))
print("Connected to Sierra Chart Relay Server")

# Send logon request
s.send(create_logon_request())
print("Logon request sent.")

# Start the heartbeat thread
heartbeat_thread = threading.Thread(target=send_heartbeat, args=(s,))
heartbeat_thread.start()

# Handle responses
receive_response(s)

# Wait for the heartbeat thread to finish (if ever)
heartbeat_thread.join()

if __name__ == "__main__":
main()

...and here's what I get back from SC via console output:

Error decoding JSON: Extra data: line 1 column 281 (char 280)
Error decoding JSON: Extra data: line 1 column 146 (char 145)
Error decoding JSON: Extra data: line 1 column 88 (char 87)
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Error decoding JSON: Extra data: line 1 column 88 (char 87)
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 2, 'Price': 1530, 'Volume': 1, 'DateTime': 1714745795}
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 2, 'Price': 1530, 'Volume': 1, 'DateTime': 1714745796}
Error decoding JSON: Extra data: line 1 column 88 (char 87)
Error decoding JSON: Extra data: line 1 column 88 (char 87)
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 1, 'Price': 1528, 'Volume': 1, 'DateTime': 1714745796}
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 1, 'Price': 1528, 'Volume': 1, 'DateTime': 1714745796}
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 1, 'Price': 1528, 'Volume': 1, 'DateTime': 1714745797}
Error decoding JSON: Extra data: line 1 column 87 (char 86)
Received message: {'Type': 112, 'SymbolID': 10, 'AtBidOrAsk': 1, 'Price': 1528, 'Volume': 1, 'DateTime': 1714745797}
...

As you can see, there are some messages that are decoded correctly but a lot of which arent.

I've setup SC to use JSON Encoding:
Encoding:JSON

This setting doesn't appear to affect the outcome:

Automatically use JSON Compact Encoding for Websocket Connections:No

And I don't know why stock data can't be relayed:
2024-05-03 07:25:51.195 | DTC client #31. 127.0.0.1 | Not sending ESM24_FUT_CME security definition to the client. Market data not allowed to be relayed for symbol.