This question still has my attention. I edited it a bit to maybe attract more problem-solvers ;)
I am currently trying to set up a websocket connection to fetch data from a third party. The connection is set up and runs just well.
However, I am not able to access and use the returned data finally... How can I do this?
My code:
# Websocket Endpoint
socket = 'wss://data.alpaca.markets/stream'
# Websocket authentication dictionary
socket_login_message = {
"action": "authenticate",
"data": {
"key_id": key,
"secret_key": secret_key
}
}
# Open Connection
ws = create_connection("wss://data.alpaca.markets/stream")
# Convert dictionary to str -> bytes object to transfer it via websocket
sck_login_credentials = json.dumps(socket_login_message).encode('utf-8')
# Send the credentials over the socket
ws.send(sck_login_credentials)
# Check if connected
result = ws.recv()
print(result)
# Define Stocks to listen
listen_stocks = {
"action": "listen",
"data": {
"streams": ["Q.AAPl", "Q.CMRE", "Q.PLOW"]
}
}
# Convert dictionary to str -> bytes object to transfer it via websocket
sck_listen_quotes = json.dumps(listen_stocks).encode('utf-8')
# Initialize Listen Process
ws.send(sck_listen_quotes)
# grab response
quotes_response = ws.recv()
# Decode bytes object again
response_str = json.loads(quotes_response)
print(type(quotes_response))
# Prints <class 'str'>
print(type(response_str))
# Prints <class 'dict'>
print(response_str)
# Prints {'stream': 'listening', 'data': {'streams': ['Q.AAPl', 'Q.CMRE', 'Q.PLOW']}}
# Now how to get the data of each stock in streams dict?!
The transferred structure for each stream according to the documentation of the third party is as follows:
{
"ev": "Q",
"T": "APPL",
"x": 17,
"p": 283.35,
"s": 1,
"X": 17,
"P": 283.4,
"S": 1,
"c": [
1
],
"t": 1587407015152775000
}
So how can I assign e.g. T to the variable symbol for each item in streams?