/
AMD_vs_NVDA_dash.py
64 lines (56 loc) · 1.87 KB
/
AMD_vs_NVDA_dash.py
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
62
63
64
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 21 17:10:19 2023
@author: Tin Hang
"""
# Use this link http://127.0.0.1:8050/
import dash
from dash import dcc, html
import yfinance as yf
# Download data
symbols = ['AMD', 'NVDA']
market = '^GSPC'
start = '2018-01-01'
end = '2023-08-01'
df = yf.download(symbols + [market], start=start, end=end)['Adj Close']
# Calculate daily returns for stocks and the market
df['Daily Return AMD'] = df['AMD'].pct_change()
df['Daily Return NVDA'] = df['NVDA'].pct_change()
df['Daily Return Market'] = df[market].pct_change()
# Normalize closing prices for stocks and market benchmark
normalized_df = (df - df.min()) / (df.max() - df.min())
# Create Dash app
app = dash.Dash(__name__)
# Define app layout
app.layout = html.Div([
html.H1("Stock Data Dashboard"),
dcc.Graph(
id='line-plot',
figure={
'data': [
{'x': df.index, 'y': df['AMD'], 'type': 'line', 'name': 'AMD'},
{'x': df.index, 'y': df['NVDA'], 'type': 'line', 'name': 'NVDA'},
{'x': df.index, 'y': df[market], 'type': 'line', 'name': 'Market'}
],
'layout': {
'title': 'Stock Prices Over Time'
}
}
),
dcc.Graph(
id='normalized-plot',
figure={
'data': [
{'x': normalized_df.index, 'y': normalized_df['AMD'], 'type': 'line', 'name': 'AMD'},
{'x': normalized_df.index, 'y': normalized_df['NVDA'], 'type': 'line', 'name': 'NVDA'},
{'x': normalized_df.index, 'y': normalized_df[market], 'type': 'line', 'name': 'Market'}
],
'layout': {
'title': 'Normalized Stock Prices Over Time'
}
}
)
])
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)