Skip to content

Instantly share code, notes, and snippets.

@pssolanki111
Last active July 26, 2022 17:09
Show Gist options
  • Save pssolanki111/9275f1af16934637f7021e3236b74c6d to your computer and use it in GitHub Desktop.
Save pssolanki111/9275f1af16934637f7021e3236b74c6d to your computer and use it in GitHub Desktop.
TDA streaming template to work around the symbols limits
import asyncio
import tda
class TheStreamer:
def __init__(self):
self.queue = asyncio.Queue()
self.stream_client = StreamClient(client, account_id=CONFIG.account_number)
await self.login()
async def run(self):
self.stream_client.add_level_one_option_handler(level_1_message_handler)
asyncio.create_task(self.consume_queue())
asyncio.create_task(self.manage_sub_unsub())
while 1:
await self.stream_client.handle_message()
async def manage_sub_unsub(self): # ya'd wanna choose a different name
while 1:
option_symbols = db_get_symbols()
if len(option_symbols) < 1:
await asyncio.sleep(5) # adjust the sleep
continue
splits = groupe(2000, option_symbols)
for split in splits:
await self.stream_client.level_one_option_subs(split)
await asyncio.sleep(60)
await self.stream_client.level_one_option_unsubs(split)
await asyncio.sleep(2)
async def consume_queue(self):
while 1:
msg = await self.queue.get()
# process it
async def level_1_message_handler(self, msg): # no need to check if queue is full, since you don't have an upper limit for queue
await self.queue.put(msg)
async def login(self):
await self.stream_client.login()
await self.stream_client.quality_of_service(StreamClient.QOSLevel.EXPRESS)
async def main():
streamer = TheStreamer()
await streamer.run()
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment