Open
Description
Question form pre-submit checklist.
- I have searched the existing issues to ensure there isn't already an issue about this question.
- My question has to do with the Python SDK and isn't a general question about the API. (If it is please open your issue here)
- My question isn't about how to do a specific algorithm or asking for trade advice (answers to these are outside the scope of this repo).
Question
Wondering how often news become available on NewsDataStream
.
584F
I'm asking because I tried to consume them with the two examples below. Each of them is running for at least 15 min without a result. Is that because I'm not consuming this stream correctly or is the news stream sparse with only one news per hour for example?
Example 1
import asyncio
from alpaca.data.live.news import NewsDataStream
from alpaca.data.models import News
import os
API_KEY = os.getenv("ALPACA_API_KEY")
SECRET_KEY = os.getenv("ALPACA_SECRET_KEY")
async def on_news(news: dict):
print("Received news:", news)
async def main():
stream = NewsDataStream(api_key=API_KEY, secret_key=SECRET_KEY)
stream.subscribe_news(on_news, "*")
await stream.run()
if __name__ == "__main__":
asyncio.run(main())
Example 2
import asyncio
import threading
from alpaca.data.live.news import NewsDataStream
from alpaca.data.models import News
import os
import signal
import time
API_KEY = os.getenv("ALPACA_API_KEY")
SECRET_KEY = os.getenv("ALPACA_SECRET_KEY")
async def on_news(news: dict):
print("Received news:", news)
print("type of news:", type(news))
def run_stream(stream):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(stream._run_forever())
def main():
# Instantiate the data stream
stream = NewsDataStream(api_key=API_KEY, secret_key=SECRET_KEY)
stream.subscribe_news(on_news, "*")
# Start the WebSocket connection in a separate thread
ws_thread = threading.Thread(target=run_stream, args=(stream,))
ws_thread.daemon = True # Allow the thread to be terminated when main program exits
ws_thread.start()
# Keep the main thread running
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nShutting down...")
stream.stop()
ws_thread.join(timeout=5) # Wait for the thread to finish
if __name__ == "__main__":
main()
Metadata
Metadata
Assignees
Labels
No labels