-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNYSE Trending Report.py
194 lines (123 loc) · 4.52 KB
/
NYSE Trending Report.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import yfinance as yf
import re
import pandas as pd
from heapq import nlargest
import numpy as np
from tqdm import tqdm
from datetime import date, datetime, timedelta, time
from pandas_datareader import data as pdr
from pandas.tseries.offsets import BDay
from contextlib import redirect_stdout
import imgkit
from IPython.display import Image
import io
yf.pdr_override() # <== that's all it takes :-)
trap = io.StringIO()
# In[2]:
#market closes at 4pm
two_business_day_ago = (date.today() - BDay(2)).strftime("%Y-%m-%d")
one_business_day_ago = (date.today() - BDay(1)).strftime("%Y-%m-%d")
today = date.today().strftime("%Y-%m-%d")
tomorrow = (date.today() + timedelta(days=1)).strftime("%Y-%m-%d")
if datetime.now().time() > time(16,00):
market_last_close_add_a_day = tomorrow
compare_market_close = one_business_day_ago
elif datetime.today().weekday() < 5 or datetime.today().weekday() == 0:
market_last_close_add_a_day = today
compare_market_close = two_business_day_ago
# In[3]:
snp500 = pd.read_csv('C:\\Users\\Radjammin\\IdeaProjects\\stock_market\\us_company_names.csv')
#snp500=pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
symbols = list(snp500.symbol)
# replacing dots with dashes for Yahoo Finanace
symbols = [re.sub("\.","-",x) for x in symbols]
# In[4]:
market_list = []
error_list = {}
for symbol in tqdm(symbols,position=0, leave=True):
company_trend = None
data_row = {}
with redirect_stdout(trap):
try:
print(f"Downloading Data for {symbol}")
while company_trend is None:
try:
company_trend = pdr.get_data_yahoo(symbol, start=compare_market_close, end=market_last_close_add_a_day, threads= False)
except Exception as e:
error_list[symbol] = e
print(company_trend)
compare_price = company_trend.first('1D')[['Close']].values[0][0]
close_price = company_trend.last('1D')[['Close']].values[-1][-1]
stock_change_percentage = ( close_price / compare_price ) * 100 - 100
data_row['symbol'] = symbol
data_row['stock_change_percentage'] = stock_change_percentage
data_row['close_price'] = close_price
market_list.append(data_row)
print(data_row,compare_price)
except Exception as e:
error_list[symbol] = e
market_list = pd.DataFrame(market_list)
market_list.set_index('symbol',inplace=True)
# In[5]:
market_list
# In[6]:
market_list.info()
# In[7]:
for key, value in error_list.items():
print(key,value)
# In[8]:
top_25 = market_list.nlargest(25, ['stock_change_percentage'])
#top_50 = {key: market_list[key] for key in sorted(market_list, key=market_list.get, reverse=True)[:25]}
# In[9]:
top_25.head()
# In[10]:
top_25.count()
# In[11]:
top_info_list = []
for symbol, row in tqdm(top_25.iterrows(),position=0, leave=True):
company = None
with redirect_stdout(trap):
try:
company = yf.Ticker(symbol).info
top_info_list.append(company)
except Exception as e:
error_list[symbol] = e
# In[12]:
market_panda = pd.DataFrame(top_info_list)
market_panda.set_index('symbol',inplace=True)
market_panda = pd.concat([market_panda, top_25], axis=1)
# In[13]:
pd.set_option('display.max_columns', 999)
market_panda.iloc[[0]]
# In[14]:
if 'MCC' in market_panda.index:
market_panda = market_panda.drop('MCC')
if 'FMO' in market_panda.index:
market_panda = market_panda.drop('FMO')
if 'SSI' in market_panda.index:
market_panda = market_panda.drop('SSI')
# In[15]:
pd.options.display.float_format = "{:,.2f}".format
market_panda[['longName','stock_change_percentage','close_price','fiftyTwoWeekHigh','sector']]
# In[16]:
html_string = '''
<html>
<head><title>HTML Pandas Dataframe with CSS</title></head>
<link rel="stylesheet" type="text/css" href="df_style.css"/>
<body>
{table}
</body>
</html>.
'''
# OUTPUT AN HTML FILE
with open('table_report.html', 'w') as f:
f.write(html_string.format(table=market_panda[['longName','stock_change_percentage','close_price','fiftyTwoWeekHigh','sector']].to_html(classes='mystyle')))
# In[17]:
path_wkthmltoimage = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe'
config = imgkit.config(wkhtmltoimage=path_wkthmltoimage)
imgkit.from_file('table_report.html', 'table_report.jpg',config=config,options={'enable-local-file-access': ''})
# In[18]:
Image(filename='table_report.jpg')