EXPO
unknown
python
24 days ago
5.0 kB
1
Indexable
Never
import dash from dash import html from dash import dcc from dash.dependencies import Input, Output from dash import dash_table import warnings import yfinance as yf import pandas as pd import plotly.express as px warnings.filterwarnings('ignore') # Hide warnings app = dash.Dash(__name__) # Setting up the start date and end date start = '2024-01-01' end = '2024-07-01' # Collecting data about Bitcoin from Yahoo Finance btc = yf.download('BTC-USD', start, end) btc.reset_index(inplace=True) crypto = btc[['Date', 'Adj Close']] crypto = crypto.rename(columns={'Adj Close': 'BTC'}) # Calculating the 7-day moving average for Bitcoin crypto['BTC_7DAY_MA'] = crypto.BTC.rolling(7).mean() # Collecting data about Ethereum from Yahoo Finance eth = yf.download('ETH-USD', start, end) eth.reset_index(inplace=True) crypto["ETH"] = eth["Adj Close"] # Calculating the 7-day moving average for Ethereum crypto['ETH_7DAY_MA'] = crypto.ETH.rolling(7).mean() # Collecting data about Dogecoin from Yahoo Finance doge = yf.download('DOGE-USD', start, end) doge.reset_index(inplace=True) crypto["DOGE"] = doge["Adj Close"] # Calculating the 7-day moving average for Dogecoin crypto['DOGE_7DAY_MA'] = crypto.DOGE.rolling(7).mean() # Calculating the correlation between different coins crypto.set_index("Date", inplace=True) correlation = crypto[['BTC', 'ETH', 'DOGE']].corr() app.layout = html.Div([ html.Div([ html.H1(children='Analysing Crypto Charts', style={'textAlign': 'center', 'color': '#212529'}) ], className='row'), dcc.Dropdown( id='dropdown', options=[ {'label': 'Bitcoin', 'value': 'BTC'}, {'label': 'Bitcoin MA', 'value': 'BTC_7DAY_MA'}, {'label': 'Ethereum', 'value': 'ETH'}, {'label': 'Ethereum MA', 'value': 'ETH_7DAY_MA'}, {'label': 'Dogecoin', 'value': 'DOGE'}, {'label': 'Dogecoin MA', 'value': 'DOGE_7DAY_MA'}, {'label': 'ALL coins', 'value': 'ALL'}, {'label': 'Correlation', 'value': 'CORREL'} ], value='BTC'), dcc.Graph(id='bar_plot'), html.Div([ html.H1(children='Correlation', style={'textAlign': 'center', 'color': '#212529'}) ], className='row'), html.Div([ html.P( children="Correlation shows the strength of a relationship between two variables and is expressed numerically by the correlation coefficient. The correlation coefficient's values range between -1.0 and 1.0. A perfect positive correlation means that the correlation coefficient is exactly 1. This implies that as one security moves, either up or down, the other security moves in lockstep, in the same direction. A perfect negative correlation means that two assets move in opposite directions, while a zero correlation implies no linear relationship at all.", style={'textAlign': 'center', 'color': '#212529'}) ], className='row'), html.Div([ dash_table.DataTable(id='table', columns=[{"name": i, "id": i} for i in correlation.columns], data=correlation.to_dict('records')) ], className='row') ]) @app.callback(Output(component_id='bar_plot', component_property='figure'), [Input(component_id='dropdown', component_property='value')]) def graph_update(dropdown_value): print(dropdown_value) if dropdown_value == "BTC": fig = px.line(crypto, y=['BTC']) elif dropdown_value == "BTC_7DAY_MA": fig = px.line(crypto, y=['BTC_7DAY_MA']) elif dropdown_value == "ETH": fig = px.line(crypto, y=['ETH']) elif dropdown_value == "ETH_7DAY_MA": fig = px.line(crypto, y=['ETH_7DAY_MA']) elif dropdown_value == "DOGE": fig = px.line(crypto, y=['DOGE']) elif dropdown_value == "DOGE_7DAY_MA": fig = px.line(crypto, y=['DOGE_7DAY_MA']) elif dropdown_value == "ALL": fig = px.line(crypto, y=['BTC', 'ETH', 'DOGE']) else: fig = px.imshow(correlation, text_auto=True, # Adds text annotations with the correlation values color_continuous_scale='RdBu_r', # Use a color scale that contrasts well aspect="auto") # Adjust the aspect ratio for better readability fig.update_layout( title='Correlation Heatmap', xaxis_title='Cryptocurrencies', yaxis_title='Cryptocurrencies', coloraxis_showscale=True, # Show color scale font=dict(size=12), margin=dict(l=40, r=40, t=40, b=40) # Adjust margins for better layout ) fig.update_layout(title='Crypto Prices Over Time' if dropdown_value != "CORREL" else 'Correlation Heatmap', xaxis_title='Dates' if dropdown_value != "CORREL" else '', yaxis_title='Prices' if dropdown_value != "CORREL" else '') return fig app.run_server(debug=True, host='0.0.0.0')
Leave a Comment