Showing posts with label python Alpaca Polygon. Show all posts
Showing posts with label python Alpaca Polygon. Show all posts

Thursday, September 3, 2020

Plogon Python for Alpaca

 # Be sure to pip install websocket-client

# Details: https://pypi.org/project/websocket-client/

import websocket
import json
def on_message(ws, message):
    with open('data.txt', 'a') as outfile:
        json.dump(message, outfile)
    print(message)
    

def on_error(ws, error):
    print(error)

def on_close(ws):
    #print("### closed ###")
    f.close

def on_open(ws):
    ws.send('{"action":"auth","params":"XXXXX"}')
    ws.send('{"action":"subscribe","params":"A.AAPL"}')

f = open("demofile.txt", "a")
if __name__ == "__main__":
    # websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://socket.polygon.io/stocks",
                              on_message = on_message,
                              on_error = on_error,
                              on_close = on_close)
    ws.on_open = on_open
    

    ws.run_forever()