Skip to content

Instantly share code, notes, and snippets.

>>> messy_df.melt(var_list)
Country Name Country Code Indicator Name Indicator Code variable value
0 Aruba ABW Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 1960 NaN
1 Afghanistan AFG Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 1960 0.170627
2 Angola AGO Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 1960 2.061462
3 Albania ALB Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 1960 5.102676
4 Andorra AND Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 1960 NaN
... ... ... ... ... ...
15835 Kosovo XKX Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 2019 NaN
15836 Yemen, Rep. YEM Hospital beds (per 1,000 people) SH.MED.BEDS.ZS 2019 NaN
# ========== LOAD TIINGO TOKEN ==========
with open('../../tokens/tiingo_token.txt', 'r') as f:
tiingo_token = f.read().strip()
# ========== GET STOCK DATA ==========
def get_stock_data(tkn, sym='amzn', start_date='2020-01-01'):
headers = {'Content-Type': 'application/json'}
requestResponse = requests.get("https://api.tiingo.com/tiingo/daily/" + sym + "/prices?startDate=" + start_date + "&token=" + tkn, headers=headers)
>>> amzn_df.head()
date close high low open volume adjClose adjHigh adjLow adjOpen adjVolume divCash splitFactor
0 2020-01-02T00:00:00.000Z 1898.01 1898.01 1864.15 1875.00 4035910 1898.01 1898.01 1864.15 1875.00 4035910 0.0 1.0
1 2020-01-03T00:00:00.000Z 1874.97 1886.20 1864.50 1864.50 3766604 1874.97 1886.20 1864.50 1864.50 3766604 0.0 1.0
2 2020-01-06T00:00:00.000Z 1902.88 1903.69 1860.00 1860.00 4065698 1902.88 1903.69 1860.00 1860.00 4065698 0.0 1.0
3 2020-01-07T00:00:00.000Z 1906.86 1913.89 1892.04 1904.50 4134010 1906.86 1913.89 1892.04 1904.50 4134010 0.0 1.0
4 2020-01-08T00:00:00.000Z 1891.97 1911.00 1886.44 1898.04 3511966 1891.97 1911.00 1886.44 1898.04 3511966 0.0 1.0
amzn_df = amzn_df.assign(bezos_year=(amzn_df.close - amzn_df.iloc[0].close) * 498 * 10**6 * 0.112)
amzn_df = amzn_df.assign(bezos_week=[(row['close'] - amzn_df.iloc[max(0, i-6)].close) * 498 * 10**6 * 0.112 for i, row in amzn_df.iterrows()])
app.layout = html.Div([
html.Div([
dcc.Graph(id='graph_id'),
dcc.Interval(
id='interval_id',
interval=1 * 1000, # in milliseconds
n_intervals=0
)
]),
])
def get_bezos_data():
amzn_data = get_stock_data(tiingo_token).json()
amzn_df = pd.DataFrame(amzn_data)
amzn_df = amzn_df.assign(bezos_year=(amzn_df.close - amzn_df.iloc[0].close) * 498 * 10**6 * 0.112)
return amzn_df
app.layout = dbc.Container([
html.Div([
dcc.Graph(
id='bezos-worth-graph',
card_content_worth = [
dbc.CardHeader(f"Bezos' net worth has increased by: "),
dbc.CardBody([
html.H3([dbc.Badge(f"US${int(bezos_secondly * n)}", color='success')], className="card-title"),
html.P([
f"That is ", dbc.Badge(str(round(bezos_tot/median_us_income, 1)), color='primary'),
" typical U.S. incomes' worth of money."],
className="card-text",
),
])]
# ========== FUNCTION TO GET STOCK DATA ==========
def get_stock_data(tkn, sym='amzn', start_date='2020-01-01'):
headers = {'Content-Type': 'application/json'}
requestResponse = requests.get("https://api.tiingo.com/tiingo/daily/" + sym + "/prices?startDate=" + start_date + "&token=" + tkn, headers=headers)
if requestResponse.status_code == 200:
logger.info(f'Success fetching {sym} data from {start_date} to today')
else:
logger.warning(f'Something looks wrong - status code {requestResponse.status_code}')
fig = px.scatter(total_ticker_df,
x='date', y='close', color='sym', template='plotly_white', title='Sample Stock Price Graphs',
facet_col='sym', facet_col_wrap=3
)
fig.update_traces(mode='lines')
fig.show()
# Build a correlation (and p-value) matrix
r_array = np.zeros([len(symbols_ser), len(symbols_ser)])
p_array = np.zeros([len(symbols_ser), len(symbols_ser)])
for i in range(len(symbols_ser)):
for j in range(len(symbols_ser)):
ser_i = total_ticker_df[total_ticker_df.sym == symbols_ser[i]]['close'].values
ser_j = total_ticker_df[total_ticker_df.sym == symbols_ser[j]]['close'].values
r_ij, p_ij = scipy.stats.pearsonr(ser_i, ser_j)
r_array[i, j] = r_ij
p_array[i, j] = p_ij