File size: 1,573 Bytes
813fa51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)