Support Board
Date/Time: Tue, 26 Nov 2024 03:51:58 +0000
[User Discussion] - Python DTC Logon Sequence for Beginners
View Count: 408
[2024-01-04 23:17:08] |
User862761 - Posts: 19 |
Not sure how many others are trying to use python with sierra charts and DTC, but thought I would drop some information for those looking for any specific examples, as I think they would have helped me quite a lot. All this information can be found on SC website, but if it can help anyone by being in the same post, I thought it worth the couple minutes to post. 1. Despite the documentation, you can no longer stream live market data with the DTC protocol. It used to be possible, which is why you will find messages showing support for live data streaming. Due to regulatory changes, it is no longer possible. 2. I used the following links to figure out how to use the DTC protocol a. General information on logon sequence message types: Authentication and Connection Monitoring Messages b. Instructions on how to use each encoding: https://www.sierrachart.com/index.php?page=doc/DTCProtocol.php#EncodingRequest b1. There is an important bit of information on link b, that talks about how utf is not supported. If you do everything right, but choose the wrong encoding, then you will receive a heartbeat response, not a logon response. c. This link has all of the message particulars for the protocol: https://www.sierrachart.com/DTC_Files/DTCProtocol.h 3. Below is my code. This code is taylored to JSON encoding import socket import json # This is the local host. connection must be established on the same machine that is running SC host = '127.0.0.1' # This port is specified on the SC interface, as well as I believe on one of the links it explains this. In any case, to find this port, you go Global Settings>Sierra Chart Server Settings>[*DTC Protocol Server] port = 11099 # This is pretty standard, just specifying that you are using IPv4 and TCP client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # This will connect you to the socket client_socket.connect((host, port)) # You can find these types on the .h file, but type 1 indicates that it is a log on request # I don't believe you actually need the protocol version, but 8 means that it is using the latest version # Depending on your settings, you may or may not need the username and password. If you set the allowed incoming IPs to any IP, you will need to have this # Heartbeat is also required. dtc_logon_request = {'Type': 1, 'ProtocolVersion': 8, 'Username': '<username>', 'Password': '<password>', 'HeartbeatIntervalInSeconds': 30} # This will take your dictionary, and serialize it into binary with the correct formatting request_data = json.dumps(dtc_logon_request, ensure_ascii=True).encode('latin-1') # This is a neccesary null terminator that is discussed in link a I believe. I'll be honest though, this part is from ChatGPT, but I know it is necessary to get the logon response request_data += b'\x00' # Send the JSON-encoded data over the socket client_socket.sendall(request_data) 4. This is the logon sequence that worked for me. In order to receive the response, you will have to receive the message. The following was written by chatGPT, and it does work but it also adds on multiple heartbeat messages. Again, use at your own discretion. I can't say exactly what is happening on this part because I didn't write it, but you may find it useful. I also can't comment on it for the same reason. # Receive the response data response_data = b'' while True: chunk = client_socket.recv(1024) if not chunk: break response_data += chunk # Print the received response (assuming it's in UTF-8 encoding) print(f"Received data: {response_data.decode('utf-8')}") # Close the connection client_socket.close() 5. If I have made any egregious errors, feel free to correct me SC engineers, but this is something that would have helped a noob like me out a bunch, and will hopefully save some grinders a bit of time and money. Date Time Of Last Edit: 2024-01-04 23:20:21
|
[2024-01-06 00:49:33] |
user_xyz - Posts: 416 |
Thanks for outlining this. It's been on my to-do for sometime now I might tackle this weekend. I've bookmarked and will add anything here I find relevant.
|
To post a message in this thread, you need to log in with your Sierra Chart account: