# Struggling with API V2 for get\_barset

**URL:** https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840
**Category:** Alpaca Integration Applications
**Created:** [June 17, 2020, 4:02pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840 "2020-06-17T16:02:43Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [June 17, 2020, 4:02pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/1 "2020-06-17T16:02:43Z")

</div>

I recreated this two years ago which is what turned me on to Alpaca in the first place. It worked fine at the time.  
[https://hackernoon.com/forecasting-market-movements-using-tensorflow-fb73e614cd06](https://hackernoon.com/forecasting-market-movements-using-tensorflow-fb73e614cd06)

Then I got distracted for two years. I’m starting again recreating this, but I have no formal programming education so it’s a slog.

I managed to get through a few issues with changes between 2018 and now, but I’m stuck on this particular section of code:

returned\_data = api.get\_bars(  
symbol,  
barTimeframe,  
start\_dt=trainStartDate,  
end\_dt=evalEndDate).df

```
    # Processes all data into numpy arrays for use by talib
    timeList = np.array(returned_data.index)
    openList = np.array(returned_data.open, dtype=np.float64)
    highList = np.array(returned_data.high, dtype=np.float64)
    lowList = np.array(returned_data.low, dtype=np.float64)
    closeList = np.array(returned_data.close, dtype=np.float64)
    volumeList = np.array(returned_data.volume, dtype=np.float64)

```

get\_bars is now get\_barset - and I’ve managed to make that part work by changing to the below.

api.get\_barset(ticker, barTimeframe, limit=None, start=trainStartDate, end=evalEndDate).df

However, only the timeList line beneath it executes, everything else chokes (doesn’t error, just won’t even execute a print statement beneath that line). Any help greatly appreciated.

---

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [June 25, 2020, 11:57pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/2 "2020-06-25T23:57:42Z")

</div>

This is a ping please help

---

<div class="post-metadata">

### Author: ![hitoshi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/hitoshi/32/69_2.png) [@hitoshi](https://forum.alpaca.markets/u/hitoshi)
#### Post date: [June 30, 2020, 8:01am UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/3 "2020-06-30T08:01:24Z")

</div>

Please keep in mind get\_barset().df returns a multi-level column with (symbol, OHLCV), so you likely need to slice columns by symbol first.

---

<div class="post-metadata">

### Author: ![Riodda](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/riodda/32/1000_2.png) [@Riodda](https://forum.alpaca.markets/u/Riodda)
#### Post date: [July 5, 2020, 3:13pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/4 "2020-07-05T15:13:39Z")

</div>

Hi, i’m happy to share my function, the trick is that the dataframe is 3dimensional with the symbo, not just 2 dimensional.

```
def alpaca_get_15Min_bars(_symbol,_number):
_df=pd.DataFrame()
_temp=pd.DataFrame()
if _number<1000:
    _df = api.get_barset(_symbol, '15Min', limit=_number, start=None, end=None, after=None, until=None).df
else:
    _num_cycles, _residual = divmod(_number, 1000)
    if _residual == 0:
        _df = api.get_barset(_symbol, '15Min', limit=1000, start=None, end=None, after=None, until=None).df
        _num_cycles -=1
    else:
        _df = api.get_barset(_symbol, '15Min', limit=_residual, start=None, end=None, after=None, until=None).df    
    for i in range(1,_num_cycles+1):
        _temp = api.get_barset(_symbol, '15Min', limit=1000, start=None, end=None, after=None, until=_df.first_valid_index().isoformat()).df
        _df= pd.concat([_temp,_df,])
return _df[_symbol]
```

---

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [July 22, 2020, 5:46pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/5 "2020-07-22T17:46:03Z")

</div>

Hitoshi and Riodda,

I found both of these value and took something from of them. Either of you know why certain 15-minute candles are empty for some tickers but not others? Running the below code gives me what you see in the attached image.

def get\_alpaca\_data(api, sh, max\_timestamps):  
df\_all\_tickers = pd.DataFrame()  
df\_this\_ticker = pd.DataFrame()  
tickers = sh.worksheet(‘INPUTS’).col\_values(3)  
df\_all\_tickers = api.get\_barset(tickers, ‘15Min’, limit=1000).df.transpose()  
sh.worksheet(’').clear() set\_with\_dataframe(sh.worksheet('’), df\_all\_tickers, include\_index=True)

 ![asdfasdf](https://us1.discourse-cdn.com/flex020/uploads/alp/original/2X/b/b788efd33fc83bf18f46b640c62f4feb00759a80.png)

---

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [July 22, 2020, 5:47pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/6 "2020-07-22T17:47:13Z")

</div>

> [@dllorens](#):
>
> df\_this\_ticker

Also df\_this\_ticker is since deleted after realizing I didn’t have to loop through the tickers.

---

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [July 28, 2020, 12:03am UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/7 "2020-07-28T00:03:29Z")

</div>

Just pinging this one in case anyone has any thoughts.

---

<div class="post-metadata">

### Author: ![dllorens](https://avatars.discourse-cdn.com/v4/letter/d/f4b2a3/32.png) [@dllorens](https://forum.alpaca.markets/u/dllorens)
#### Post date: [August 6, 2020, 12:38am UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/8 "2020-08-06T00:38:30Z")

</div>

Any luck on this anyone?

---

<div class="post-metadata">

### Author: ![Riodda](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/riodda/32/1000_2.png) [@Riodda](https://forum.alpaca.markets/u/Riodda)
#### Post date: [August 18, 2020, 4:23pm UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/9 "2020-08-18T16:23:23Z")

</div>

Not an expert but i guess it might depend from different market open times ? Do you have the same issue if you look at crypto assets ?

---

<div class="post-metadata">

### Author: ![DizzleRama](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@DizzleRama](https://forum.alpaca.markets/u/DizzleRama)
#### Post date: [August 29, 2020, 3:13am UTC](https://forum.alpaca.markets/t/struggling-with-api-v2-for-get-barset/1840/10 "2020-08-29T03:13:44Z")

</div>

```
def getBarsetLong(timeframe,symbol,limit):
        columns = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume']
        res = []
        barset = api.get_barset(symbol,timeframe,limit)
        symbol_barset = barset[symbol]
        length = len(symbol_barset)
        for x in range(0,length):
            symbol_bars = symbol_barset[x]
            symbol_bars = symbol_bars._raw
            date = symbol_bars['t']
            date=datetime.datetime.fromtimestamp(date).strftime('%Y-%m-%d %H:%M:%S')
            close = symbol_bars['c']
            open = symbol_bars['o']
            volume = symbol_bars['v']
            high = symbol_bars['h']
            low = symbol_bars['l']
            list = [date, open, high, low, close, volume]
            res.append(list)
        df = pd.DataFrame(res, columns=columns)
        df.set_index('Date', inplace=True)
        return df

    start = '2019-04-15'
    end = '2020-04-15'
    start = datetime.datetime.strptime(start,"%Y-%m-%d")
    end = datetime.datetime.strptime(end,"%Y-%m-%d")
    limit=(end-start).days * 24
    if limit > 1000:
        limit = 1000

    sample = getBarsetLong('day', "WMT",limit)
    print(sample.tail(50))

"""
                Open High Low Close Volume
Date                                                              
2020-06-19 04:00:00 118.890 120.3000 117.7500 119.75 10821598
2020-06-22 04:00:00 120.970 122.0900 120.4000 121.68 8124144
2020-06-23 04:00:00 122.750 122.8500 120.9606 121.07 6072862
2020-06-24 04:00:00 120.740 121.1153 119.5800 120.35 6032080
2020-06-25 04:00:00 119.760 120.1600 118.3800 119.63 5735935
2020-06-26 04:00:00 119.600 120.8400 118.0200 118.31 5909472
2020-06-29 04:00:00 118.500 119.6800 118.2150 119.09 4637226
2020-06-30 04:00:00 119.220 120.1300 118.5400 119.66 5555898
2020-07-01 04:00:00 119.450 119.9200 118.6600 119.69 5718034
2020-07-02 04:00:00 120.090 120.8800 118.8600 119.21 5195854
2020-07-06 04:00:00 119.800 119.8700 118.2200 118.88 6617241
2020-07-07 04:00:00 118.450 127.5500 118.2200 126.97 28229924
"""
```
