Spaces:
Running
Running
| import json | |
| import requests | |
| import datetime | |
| import pandas as pd | |
| import streamlit as st | |
| import os | |
| import re | |
| import yfinance as yf | |
| import plotly.graph_objects as go | |
| from datetime import timedelta | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| def get_finnhub_data(example: str) -> json: | |
| """ | |
| Pass in the "example" string from the API documentation. It changes for every endpoint. | |
| :param1 example: '/company-news?symbol=AAPL&from=2023-08-15&to=2023-08-20' | |
| """ | |
| base_url = 'https://finnhub.io/api/v1//' | |
| token = f"&token={os.environ['finnhub_token']}" | |
| request = requests.get(f"{base_url}{example}{token}") | |
| return request.json() | |
| def get_alpha_vantage_data(example: str) -> json: | |
| """ | |
| Pass in the "function" string from the API documentation. It changes for every endpoint. | |
| :param1 example: '' | |
| """ | |
| base_url = 'https://www.alphavantage.co/query?' | |
| token = f"&apikey={os.environ['alpha_api_key']}" | |
| request = requests.get(f"{base_url}{example}{token}") | |
| return request.json() | |
| sp = yf.Ticker("^GSPC") | |
| sp_hist = sp.history(period="1y")['Close'] | |
| sp500 = sp_hist.reset_index().rename(columns={'Close':'sp500_Close'}) | |
| sp500['sp500_variance'] = sp500['sp500_Close'].var() | |
| beta = sp500.cov().loc['sp500_Close']['Close'] / sp500['sp500_variance'].max() | |
| max_date = sp500['Date'].max() | |
| # plotting sp500 price over time | |
| price_chart = go.Scatter( | |
| x=sp500.Date, | |
| y=sp500.sp500_Close, | |
| name = '1y price history' | |
| ) | |
| fig_candle = go.Figure(price_chart) | |
| st.plotly_chart(fig_candle, use_container_width=True) |