My Question: How can I pluck data every 30’s from a stream in python. The alpaca api provides hundreds of messages every few seconds but I only want one data point every 30 seconds (to make analysis faster).
My approach has been to put my data handler to sleep for 30 seconds and then when it wakes up a for range statement prevents it from collecting more than one data point before it goes back to sleep again.
Actual vs Expected Results:
The message handler works well at recording only one message but instead of recording a message from the second in which it is awake, it is recording what appears to be the next message in the queue, even if I received that message 30 seconds ago and there have been 1000’s of messages since then.
Possabilities: Maybe there is a way to access only the last item in a stream?
#Create data handler for stream def on__message(message): #for range ensures only one message is recoreded when function is awake for i in range(1): with open("C:/Users/micha/github/trade_bot/intraday_data.csv", "a") as outfile: #pull variables from message timestamp = q.timestamp bid_price = q.bid_price ask_price = q.ask_price #create writer object & point at outfile writer = csv.writer(outfile) #write/append variables to outfile writer.writerow([timestamp,bid_price, ask_price]) #put recorder to sleep for 30s time.sleep(30) #Create stream object stream = Stream(APCA_API_KEY_ID, APCA_API_SECRET_KEY, APCA_API_DATA_URL, data_feed='iex') #Subscribe to stream and point stream to my handler - 'on_message' stream.subscribe_quotes(on_message, 'AAPL') stream.run()