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
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.
This is a ping please help
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.
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]
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)
dllorens:
df_this_ticker
Also df_this_ticker is since deleted after realizing I didn’t have to loop through the tickers.
Just pinging this one in case anyone has any thoughts.
Riodda
August 18, 2020, 4:23pm
9
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 ?
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
"""
1 Like