@sarahvignale You figured it out!
The client objects (eg the OptionHistoricalDataClient or the StockHistoricalDataClient) store state information about the TCP connection and a few other things. The client object can only store one state at a time, which works fine in a sequential synchronous process flow. However, if that object is used asynchronously (ie trying to manage two or more concurrent connections) it gets confused and data gets dropped etc.
The fix? If you have asynchronous functions, instantiate any clients locally inside those functions. Do not use a global client which could potentially be used simultaneously by another function.
Don’t do this
data_client = StockHistoricalDataClient(api_key=ALPACA_API_KEY_ID, secret_key=ALPACA_API_SECRET_KEY)
async def run_asynch_1:
data_client.get_stock_bars(bars_request_params)
async def run_asynch_2:
data_client.get_stock_bars(bars_request_params)
Do this instead
async def run_asynch_1:
data_client = StockHistoricalDataClient(api_key=ALPACA_API_KEY_ID, secret_key=ALPACA_API_SECRET_KEY)
data_client.get_stock_bars(bars_request_params)
async def run_asynch_2:
data_client = StockHistoricalDataClient(api_key=ALPACA_API_KEY_ID, secret_key=ALPACA_API_SECRET_KEY)
data_client.get_stock_bars(bars_request_params)
That way each client maintains state for each separate process.