You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.8 KiB

Start by importing the api key
#open file with your API key
with open('.api_key') as file:
API_key = file.read()
instally the alpha_vantage package
#install Alpha Vantage Python package
%pip install alpha_vantage
Importing packages from alpha_vantage and elsewhere
#import libraries
from alpha_vantage.timeseries import TimeSeries
import requests
from bs4 import BeautifulSoup
import pandas as pd
import io
The TimeSeries package onv will allow us to gather stock data, requests will help with making the requests, bs will help with formatting response, pandas will help
with creating data frames and io will also help with formarring our data!
Create a time series variable called ts1 and associate our api key with it.
#build TimeSeries variable from Alpha Vantage API
ts1 = TimeSeries(key = API_key)
Run the get_monthly method against the time series and the argument is the stock symbol - you can get that bt googling the company name and stock symbol - eg Apple stock symbol
For Apple - it is AAPL
#get monthly stock data of Apple
ts1.get_monthly("AAPL")
Generates a lot of data saved in the file apple_monthly_stock_data.txt. Looks like this is split by calendar month
Data for most recent month which is a part month - April 2024 in my case and most recent update was 5 April - today is Sunday 7th so that's most recent date when the stock
exchange was open for each of the other momths, the date shown is also the last date on which the stock exchange was open for that month
Opening and closing values are the values at the start and end of each month or in the case of this month, the closing is just the most recent value - high and low are
the highest and lowest values for a given month - stock is the volume of stock traded in a given month for this company