I'm using Alpaca's API with websocket to get stock price data. I want to use the variable position
outside the function on_message
but message
is not previously defined so I get an error when I run the code name message is not defined
def on_message(ws, message):
global position
json.loads(message)
position = (message[0]['bp'])
import websocket, json
def on_open(ws):
print("opened")
auth_data = {
"action": "auth", "key": <API_KEY>, "secret": <API_SECRET>
}
ws.send(json.dumps(auth_data))
listen_message = {"action":"subscribe","quotes":["AAPL"]}
ws.send(json.dumps(listen_message))
def on_message(ws, message):
global position
json.loads(message)
position = (message[0]['bp'])
socket = "wss://stream.data.alpaca.markets/v2/iex"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
ws.run_forever()
sorry if this is a badly worded question it's about 1 am and im tired.