jmfernandes / robin_stocks

This is a library to use with Robinhood Financial App. It currently supports trading crypto-currencies, options, and stocks. In addition, it can be used to get real time ticker information, assess the performance of your portfolio, and can also get tax documents, total dividends paid, and more. More info at
http://www.robin-stocks.com
MIT License
1.69k stars 459 forks source link

Any plan to make the API request async #306

Open yidea opened 3 years ago

yidea commented 3 years ago

Currently all API here r seq, in some case e.g. scanning a bunch of stock price will take longer time, any plan we have the async support down the road?

dgabriele commented 3 years ago

Why don't you just use ThreadPoolExecutor? Async really isn't much of a solve for what you could already do with existing concurrency features in Python. Just do something like this:

import concurrent.futures
from concurrent.futures import ThreadPoolExecutor

def get_price(symbol):
  pass  # TODO: implement API call to fetch stock data

with ThreadPoolExecutor(max_workers=4) as executor:
  futures = [executor.submit(get_price, symbol) for symbol in symbols]
  done, not_done = concurrent.futures.wait(futures)
  data = [future.result() for future in done]

The data var is a list of the return values generated by get_price. I don't recommend using more than just a few max_workers because Robinhood could easily flag your account for abuse of their API if you send too many requests too quickly.