This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # See sample charts of correlated stocks | |
| sim_charts = np.argsort(r_array[0])[-6:] | |
| sim_syms = [filt_df.sym.unique()[i] for i in sim_charts] | |
| sim_df = filt_df[filt_df.sym.isin(sim_syms)] | |
| fig = px.scatter(sim_df, | |
| x='date', y='close', color='sym', template='plotly_white', title='Sample Stock Price Graphs', | |
| facet_col='sym', facet_col_wrap=3 | |
| ) | |
| fig.update_yaxes(matches=None, showticklabels=False) # If we want to simply view curve 'shapes' | |
| fig.update_traces(mode='lines') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Cluster data | |
| model = sklearn.cluster.SpectralBiclustering(n_clusters=3, method='log', random_state=0) | |
| model.fit(r_array) | |
| r_array_srt = r_array[np.argsort(model.row_labels_)] | |
| r_array_srt = r_array_srt[:, np.argsort(model.column_labels_)] | |
| syms_srt = filt_df.sym.unique()[np.argsort(model.row_labels_)] | |
| fig = px.imshow(r_array_srt, x=syms_srt, y=syms_srt, | |
| color_continuous_scale=px.colors.sequential.Bluyl, color_continuous_midpoint=0, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fig = px.imshow(r_array, x=symbols_ser, y=symbols_ser, | |
| color_continuous_scale=px.colors.sequential.Bluyl, color_continuous_midpoint=0, | |
| ) | |
| fig.update_xaxes(side="top") | |
| fig.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> print(r_array) | |
| [[ 1. 0.94783247 0.75459535 0.89186946 0.93008958 -0.68727618] | |
| [ 0.94783247 1. 0.79397608 0.91511258 0.92275757 -0.71430629] | |
| [ 0.75459535 0.79397608 1. 0.74710565 0.87785573 -0.3759863 ] | |
| [ 0.89186946 0.91511258 0.74710565 1. 0.86919002 -0.61902468] | |
| [ 0.93008958 0.92275757 0.87785573 0.86919002 1. -0.5900107 ] | |
| [-0.68727618 -0.71430629 -0.3759863 -0.61902468 -0.5900107 1. ]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ========== 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}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | |
| ), | |
| ])] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.layout = html.Div([ | |
| html.Div([ | |
| dcc.Graph(id='graph_id'), | |
| dcc.Interval( | |
| id='interval_id', | |
| interval=1 * 1000, # in milliseconds | |
| n_intervals=0 | |
| ) | |
| ]), | |
| ]) |