-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGME_TSLA_PLOT.py
More file actions
133 lines (91 loc) · 3.89 KB
/
Copy pathGME_TSLA_PLOT.py
File metadata and controls
133 lines (91 loc) · 3.89 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import warnings
import plotly.io as pio
import requests
from bs4 import BeautifulSoup
pio.renderers.default = 'browser' # Change to 'browser' if 'vscode' doesn't work
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(
rows=2, cols=1, shared_xaxes=True,
subplot_titles=("Historical Share Price", "Historical Revenue"),
vertical_spacing=0.3
)
#
stock_data_specific = stock_data[stock_data['Date'] <= '2021-06-14']
revenue_data_specific = revenue_data[revenue_data['Date'] <= '2021-04-30']
fig.add_trace(
go.Scatter(
x=pd.to_datetime(stock_data_specific['Date']),
y=stock_data_specific['Close'].astype(float),
name="Share Price"
),
row=1, col=1
)
fig.add_trace(
go.Scatter(
x=pd.to_datetime(revenue_data_specific['Date']),
y=revenue_data_specific['Revenue'].astype(float),
name="Revenue"
),
row=2, col=1
)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(
showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True
)
fig.show()
fig.write_html(f"{stock}_revenue_graph.html")
# Fetch Tesla stock data
tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
print(tesla_data.head())
url = " https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/revenue.htm"
response = requests.get(url)
html_data = response.text
soup = BeautifulSoup(html_data , 'html.parser')
dataframe = pd.DataFrame(columns = ["Date" , "Revenue"])
table = soup.find_all("tbody")[1]
tesla_revenue = pd.DataFrame(columns=["Date","Revenue"])
for row in table.find_all("tr"):
cols = row.find_all("td")
date = cols[0].text.strip()
revenue = cols[1].text.strip()
tesla_revenue = pd.concat([tesla_revenue,pd.DataFrame({"Date":[date],"Revenue":[revenue]})], ignore_index = True )
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(r'[\$,]', '', regex=True)
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]
tesla_revenue.tail()
gamestop = yf.Ticker("GME")
gamestop_data = gamestop.history(period="max")
gamestop_data.reset_index(inplace=True)
print(gamestop_data.head())
url2 = 'https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html'
response2 = requests.get(url2)
html_data2 = response.text
soup2 = BeautifulSoup(html_data2 , 'html.parser')
dataframe2 = pd.DataFrame(columns = ["Date" , "Revenue"])
table2 = soup.find_all("tbody")[1]
gamestop_revenue = pd.DataFrame(columns=["Date","Revenue"])
for row in table2.find_all("tr"):
cols = row.find_all("td")
date = cols[0].text.strip()
revenue = cols[1].text.strip()
gamestop_revenue = pd.concat([gamestop_revenue,pd.DataFrame({"Date":[date],"Revenue":[revenue]})], ignore_index = True )
gamestop_revenue["Revenue"] = gamestop_revenue['Revenue'].str.replace(r'[\$,]', '', regex=True)
gamestop_revenue.dropna(inplace=True)
gamestop_revenue = gamestop_revenue[gamestop_revenue['Revenue'] != ""]
gamestop_revenue.tail()
make_graph(tesla_data, tesla_revenue, 'Tesla')
make_graph(gamestop_data, gamestop_revenue, 'Gamestop')