I would like to ask please about how to iterate in a link and save the data.
I am using Alpha Vantage and they only allow 1 month per request for intraday minute trading. In order to get the second month you have to change the slice argument in the link.
I want to create a for loop that goes through my list and pulls the data for each month and save it in a data frame. The following are the steps to get the data and what I did so far.
This gives me only the year2month2 but not the whole periods. Can someone please guide me here what I am doing wrong
# replace the "demo" apikey below with your own key from https://www.alphavantage.co/support/#api-key
symbol = 'pltr'
interval = '1min'
periods=['year1month1','year1month2','year1month3','year1month4','year1month5',
'year1month6','year1month7','year1month8','year1month9','year1month10',
'year1month11','year1month12','year2month1','year2month2','year2month3',
'year2month4','year2month5','year2month6','year2month7','year2month8',
'year2month9','year2month10','year2month11','year2month12']
df=[]
for period in periods:
df = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+ticker+'&interval=15min&slice='+period+'&apikey='+apiKey+'&datatype=csv&outputsize=full')
df.append(period)
#Show output
print(df)
I was able to get intraday data as follow and thought of sharing my answer.
ticker = 'IBM'
periods=['year1month1','year1month2','year1month3','year1month4','year1month5','year1month6','year1month7','year1month8',
'year1month9','year1month10','year1month11','year1month12','year2month1','year2month2','year2month3','year2month4'
,'year2month5','year2month6','year2month7','year2month8','year2month9','year2month10','year2month11','year2month12']
interval = '1min'
price_data = []
for i in periods:
prices = pd.read_csv('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol='+ticker+'&interval='+interval+'&slice='+i+'&apikey='+apiKey+'&datatype=csv&outputsize=full')
price_data.append(prices)
data = pd.concat(price_data)
columns = ['Date_Time', 'open', 'high', 'low', 'close', 'volume']
data.columns = columns
data = data[1:]
data.set_index('Date_Time', inplace=True)
data.index = pd.to_datetime(data.index)
data = data.iloc[::-1]
data = data.astype(float)
data