Python Streaming data websocket asyncio

269 Views Asked by At

Can i get some help please i have this code below everything works fine but after a while i receive this error i am using Anaconda Spyder IDE i am not sure what the issue is the script will run but then error out after a few minutes enter image description here enter image description here

from polygon import RESTClient

import asyncio
import nest_asyncio
nest_asyncio.apply()



#client = RESTClient(api_key="")


from polygon import WebSocketClient
from polygon.websocket.models import WebSocketMessage
from typing import List
import json

global QuoteList
global TradeList
QuoteList = []
TradeList = []

ws = WebSocketClient(api_key="", feed='socket.polygon.io', market='options',subscriptions=[
    "T.O:SPY230210C00408000", "Q.O:SPY230210C00408000", 
    "T.O:SPY230210P00407000", "Q.O:SPY230210P00407000"])


def handle_msg(msgs: List[WebSocketMessage]):
    out_file1 = open("PolygonQuote1.json", "w")
    out_file2 = open("PolygonTrade1.json", "w")
    #for m in msgs:
    m = msgs[len(msgs)-1]
    print(m)
    if m.event_type == 'Q':
        QuoteList.append({'TimeStamp': m.timestamp, 
                          'BidPrice' : m.bid_price, 
                          'BidSize' :  m.bid_size, 
                          'AskPrice' : m.ask_price, 
                          'AskSize' :  m.ask_size, 
                          'Symbol' :  m.symbol})
        
    elif m.event_type == 'T':
        TradeList.append({'TimeStamp': m.timestamp,
                          'Price' : m.price, 
                          'Size' : m.size, 
                          'Symbol' :  m.symbol})
            
    json.dump(QuoteList, out_file1, indent=4)
    json.dump(TradeList, out_file2, indent=4)
            
    




asyncio.run(ws.run(handle_msg))
1

There are 1 best solutions below

0
willcode.co On

I believe you might be encountering a bug with Spyder IDE based on the similarity of your error message and the one found here: https://github.com/spyder-ide/spyder/issues/16863

Have you tried using newer versions of the IDE ?