Alpha Vantage demo code SAFE Research Data Center |
Information about the available content can be found in the documentation page ( https://www.alphavantage.co/documentation/).
A request can be made using an API Key.
import requests
# Replace YOUR_API_KEY with your actual API key
api_key = 'YOUR_API_KEY'
# Specify the stock symbol for which you want to retrieve data
symbol = 'AAPL'
# Specify the type of data you want to retrieve (e.g., 'TIME_SERIES_DAILY_ADJUSTED')
function = 'TIME_SERIES_DAILY_ADJUSTED'
# Make the API request
url = f'https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}'
response = requests.get(url)
# Print the response
print(response.json())
This can also be saved as a CSV.
from alpha_vantage.timeseries import TimeSeries
import csv
# Replace YOUR_API_KEY with your actual API key
api_key = 'YOUR_API_KEY'
# Create a TimeSeries object using your API key
ts = TimeSeries(key
=
api_key, output_format='csv')
# Specify the stock symbol for which you want to retrieve data
symbol = 'AAPL'
# Retrieve daily adjusted time series data for the symbol
data, meta_data = ts.get_daily_adjusted(symbol=symbol)
# Write the data to a CSV file
with open('stock_data.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adjusted Close'])
for row in data:
writer.writerow(row)
or it can be stored to a Pandas data frame.
import pandas as pd
import requests
# Replace YOUR_API_KEY with your actual API key
api_key = 'YOUR_API_KEY'
# Specify the stock symbol for which you want to retrieve data
symbol = 'AAPL'
# Specify the type of data you want to retrieve (e.g., 'TIME_SERIES_DAILY_ADJUSTED')
function = 'TIME_SERIES_DAILY_ADJUSTED'
# Specify the size of the time series.
output_size = 'full'
# Make the API request
url = f'https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}&outputsize={output_size}'
response = requests.get(url)
# Parse the response and extract the data
data = response.json()
stock_data = data['Time Series (Daily)']
# Convert the data to a pandas DataFrame
df=pd.DataFrame(stock_data).T
df.index.name = 'Date'
# Print the DataFrame
df
1. open | 2. high | 3. low | 4. close | 5. adjusted close | 6. volume | 7. dividend amount | 8. split coefficient | |
---|---|---|---|---|---|---|---|---|
Date | ||||||||
2023-01-17 | 134.83 | 137.29 | 134.13 | 135.94 | 135.94 | 63646627 | 0.0000 | 1.0 |
2023-01-13 | 132.03 | 134.92 | 131.66 | 134.76 | 134.76 | 57809719 | 0.0000 | 1.0 |
2023-01-12 | 133.88 | 134.26 | 131.44 | 133.41 | 133.41 | 71379648 | 0.0000 | 1.0 |
2023-01-11 | 131.25 | 133.51 | 130.46 | 133.49 | 133.49 | 69458949 | 0.0000 | 1.0 |
2023-01-10 | 130.26 | 131.2636 | 128.12 | 130.73 | 130.73 | 63896155 | 0.0000 | 1.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... |
1999-11-05 | 84.62 | 88.37 | 84.0 | 88.31 | 0.671185647632425 | 3721500 | 0.0000 | 1.0 |
1999-11-04 | 82.06 | 85.37 | 80.62 | 83.62 | 0.635540073095045 | 3384700 | 0.0000 | 1.0 |
1999-11-03 | 81.62 | 83.25 | 81.0 | 81.5 | 0.619427361363862 | 2932700 | 0.0000 | 1.0 |
1999-11-02 | 78.0 | 81.69 | 77.31 | 80.25 | 0.609926941711042 | 3564600 | 0.0000 | 1.0 |
1999-11-01 | 80.0 | 80.69 | 77.37 | 77.62 | 0.589938058761509 | 2487300 | 0.0000 | 1.0 |
5840 rows × 8 columns