Tuesday, September 15, 2020

Python Websocket Alpaca (code works if you put in the key)

 # Be sure to pip install websocket-client

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


import websocket

import json

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


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":"XXXX"}')

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()

No comments:

Post a Comment