# Get\_Stock\_Bars for multiple assets

**URL:** <https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618>\
**Category:** Alpaca Market Data\
**Created:** [March 31, 2025, 4:35pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618 "2025-03-31T16:35:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DamnFine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/damnfine/32/7085_2.png) [@DamnFine](https://forum.alpaca.markets/u/DamnFine)\
**Post date:** [March 31, 2025, 4:35pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618/1 "2025-03-31T16:35:08Z")

</div>

Wrote this function which inherits from a class where multiple symbols are passed. However, only one symbol’s bars are returned, and it is always the first symbol alphabetically. I am passing [“GOOG”, “TSLA”, “AAPL”] and getting results only for “AAPL”

```
 def GetLastXBars(self, NumOfBars:int) -> BarSet: # dict:
    """
    Basic simulation to simulate the success of trades, once the trade entry point was set.
    """
    Request = StockBarsRequest(symbol_or_symbols=self.StockSymbols, limit=NumOfBars, timeframe=self.alpaca_time_frame)
    response = self.stock_client.get_stock_bars(request_params=Request)
    return response

```

---

<div class="post-metadata">

**Author:** ![PythonUserX](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PythonUserX](https://forum.alpaca.markets/u/PythonUserX)\
**Post date:** [March 31, 2025, 5:17pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618/2 "2025-03-31T17:17:47Z")

</div>

The issue arises because response from Alpaca’s get\_stock\_bars method returns a dictionary keyed by symbol, not a merged dataset. Your current code just returns this dictionary, and when you try to access bars, you’re likely only using the first symbol alphabetically by default, causing the confusion.

Why the issue occurs:

Alpaca’s API returns something like:

{  
“AAPL”: […], # Bars for AAPL  
“GOOG”: […], # Bars for GOOG  
“TSLA”: […], # Bars for TSLA  
}

When you call methods expecting a flat structure or iterate incorrectly, only the first symbol (alphabetically) will appear.

You must explicitly access each symbol’s bars within the returned dictionary. Here’s the correct way to handle it:

def GetLastXBars(self, NumOfBars:int) → dict:  
“”"  
Returns a dictionary of Bar objects keyed by each symbol.  
“”"  
request = StockBarsRequest(  
symbol\_or\_symbols=self.StockSymbols,  
limit=NumOfBars,  
timeframe=self.alpaca\_time\_frame  
)  
response = self.stock\_client.get\_stock\_bars(request\_params=request)

```
# Access the `.data` attribute explicitly if you're using the newer Alpaca SDK.
bars_dict = response.data

return bars_dict

```

Example of how to correctly access results after calling the function:

bars = instance.GetLastXBars(5)

for symbol in bars:  
print(f"Bars for {symbol}:")  
for bar in bars[symbol]:  
print(bar)

```
You might have done something like this inadvertently:

```

bars = instance.GetLastXBars(5)  
for bar in bars:  
print(bar) # This just iterates the dictionary keys, i.e., symbols, or only one symbol’s bars.

Correction: Always iterate explicitly:

bars = instance.GetLastXBars(5)  
for symbol, bar\_list in bars.items():  
print(f"Symbol: {symbol}")  
for bar in bar\_list:  
print(bar)

---

<div class="post-metadata">

**Author:** ![DamnFine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/damnfine/32/7085_2.png) [@DamnFine](https://forum.alpaca.markets/u/DamnFine)\
**Post date:** [April 1, 2025, 12:44pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618/3 "2025-04-01T12:44:19Z")

</div>

Thank you for your support and very detailed answer, but I still get only the results for AAPL.  
I am calling my function like so:  
xbars = BaseTradeUtil([‘TSLA’, ‘GOOG’, ‘AAPL’])  
xdict = xbars.GetLastXBars(2)  
for symbol, bar\_list in xdict.items():  
print(f"Symbol: {symbol}")  
for bar in bar\_list:  
print(bar)

and the result is below:  
Symbol: AAPL  
symbol=‘AAPL’ timestamp=datetime.datetime(2025, 4, 1, 7, 51, tzinfo=TzInfo(UTC)) open=221.42 high=221.46 low=221.41 close=221.46 volume=2308.0 trade\_count=81.0 vwap=221.417122  
symbol=‘AAPL’ timestamp=datetime.datetime(2025, 4, 1, 8, 2, tzinfo=TzInfo(UTC)) open=221.2 high=221.2 low=220.74 close=220.74 volume=9125.0 trade\_count=344.0 vwap=221.039978

Any thoughts?

---

<div class="post-metadata">

**Author:** ![DamnFine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/damnfine/32/7085_2.png) [@DamnFine](https://forum.alpaca.markets/u/DamnFine)\
**Post date:** [April 1, 2025, 1:19pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618/4 "2025-04-01T13:19:32Z")

</div>

ok. I finally figured it out. I used limit in the request passing limit = 3 expecting that it would retrieve 3 candles for each symbol.  
Turns out using limit, limits the total number of candles retrieved, not related to the number of symbols. so if I limit the timeframe to 3 days, and specify limit=7, it would retrieve 3 candles from AAPL, 3 from GOOG and only 1 from TSLA. Totaling 7 records.  
So now I need to specify the correct timeframe and this will solve my problem.  
Thank you !

---

<div class="post-metadata">

**Author:** ![Dan\_Whitnable\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/dan_whitnable_alpaca/32/1658_2.png) [@Dan\_Whitnable\_Alpaca](https://forum.alpaca.markets/u/Dan_Whitnable_Alpaca)\
**Post date:** [April 3, 2025, 1:00pm UTC](https://forum.alpaca.markets/t/get-stock-bars-for-multiple-assets/16618/5 "2025-04-03T13:00:47Z")

</div>

@DamnFine Glad you figured out the `limit` parameter. It is a bit confusing. A better name for the parameter would probably have been `max_total_bars_per_page`. As you noted, it doesn’t limit the bars per symbol, but rather the total bars. If one were fetching a single symbol that would be the same but it’s not when fetching multiple symbols.

A subtle point is there may be times when fewer than the `limit` is returned, but then more bars are returned in a subsequent ‘page’. This is the result of a behind the scenes implementation detail attempting to optimize response speed. If the API is ever waiting on the Alpaca database to return more bars, it will return what it has. It then continues with any subsequent bars as another ‘page’.
