Lab 05 Solution
Solution for Section 2:
import requests
res = requests.get("https://catfact.ninja/fact")
j = res.json()
print(j['fact'])
Solution for Section 3:
import requests
key = "<insert your key>"
params = {
"function": "TIME_SERIES_DAILY_ADJUSTED",
"symbol": "AAPL",
"apikey": key
}
response = requests.get("https://www.alphavantage.co/query", params = params)
data = response.json()
print(data['Time Series (Daily)']['2020-02-18']['1. open'])
Solution for Section 4:
import plotly.graph_objects as go
import requests
from random import randint
def plot_data(data):
fig = go.Figure()
for company in data:
random_rgb = (randint(0, 255), randint(0, 255), randint(0, 255))
company_data = data[company]
fig.add_trace(go.Scatter(
x = [point[0] for point in company_data],
y = [point[1] for point in company_data],
name = company,
line_color = f"rgb({random_rgb[0]}, {random_rgb[1]}, {random_rgb[2]})",
opacity = 0.8)
)
fig.update_layout(
title="Timeseries of high price for day",
xaxis_title="timeseries (daily)",
yaxis_title="stock price (USD)"
)
fig.show()
key = "<insert your key>"
params = {
"function": "TIME_SERIES_DAILY_ADJUSTED",
"apikey": key
}
data_responses = {}
companies = ["AAPL", "MSFT", "GOOGL", "FB", "AMZN"]
for company_name in companies:
params["symbol"] = company_name
response_json = requests.get("https://www.alphavantage.co/query", params = params).json()
daily = response_json["Time Series (Daily)"]
data_responses[company_name] = [(date, point['2. high']) for date, point in list(daily.items())[:30]]
plot_data(data_responses)
Section 5:
Code will vary by student For Alpha Vantage information, check here