-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_finviz.py
141 lines (99 loc) · 3.81 KB
/
copy_finviz.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
import pandas as pd
from pandas_datareader import data as web
import datetime
from time import sleep
def number_of_stocks(url):
'''Returns the total number of stocks for given screen's URL.'''
df_temp = pd.read_html(url)
total = int(df_temp[9][0].str.split()[0][1]) # Reads the number behind "Total: "
#print('Number of stocks to check: {}'.format(total))
return total
def symbols_from_url(url):
'''Returns a list of stocks from a single Finviz page.'''
df = pd.read_html(url)
stocks = list(df[10][1])[1:]
return stocks
def get_column_names(url):
'''Returns a list with column names from a single Finviz page.'''
df = pd.read_html(url)[10] # Extracts the table #10 from a list of dataframes
vals = df.values
zoznam = list(vals[0])
return zoznam
def change_name(ret:str):
'''Clears a string from specific characters'''
restricted_chars = ['.', ' ', '/']
new_char = []
for i in ret:
if i not in restricted_chars:
new_char.append(i.lower())
return ('').join(new_char)
def get_all_symbols(url):
'''Returns a list of all stock tickers from given screener.'''
total = number_of_stocks(url)
counter = 1
stocks = []
while counter <= total:
appendix = '&r=' + str(counter) if counter > 1 else ''
path = url + appendix
#print(path)
stocks += symbols_from_url(path)
counter += 20
return stocks
def n_years_into_days(n):
'''Turns a number of years into number of records in trading statistics.'''
today = datetime.date.today()
year_today = today.year
start_date = datetime.date(today.year - n, today.month, today.day)
sp500_df = web.DataReader('^GSPC', data_source='yahoo', start=start_date, end=today)
return len(sp500_df)
DNES = datetime.date.today()
PRED_ROKOM = DNES - datetime.timedelta(days=n_years_into_days(1))
SP500 = web.DataReader('^GSPC', 'yahoo', PRED_ROKOM, DNES)
def rs_index(ticker):
'''Generates a Relative Strength Index'''
sleep(1)
try:
df = web.DataReader(ticker, 'yahoo', PRED_ROKOM, DNES)
rsl = df['Adj Close'] / SP500['Adj Close']
total_range = rsl.max() - rsl.min()
rs_percentage = (rsl[-1] - rsl.min()) / total_range * 100
rs_percentage = round(rs_percentage, 2)
print('{:10}{}'.format(ticker, rs_percentage))
return rs_percentage
except:
print('{:10}{}'.format(ticker, 'N/A'))
return 'N/A'
def generate_dataframe(url):
'''Generates a dataframe object from Finviz table data.'''
column_names_set = False # Test existencie pola s nazvami rubrik
total = number_of_stocks(url) # Celkovy pocet akcii v screene
counter = 1
records = [] # Ulozisko individualnych riadkov tabulky
while counter <= total:
appendix = '&r=' + str(counter) if counter > 1 else ''
path = url + appendix
# Read all data from a table
df_temp = pd.read_html(path)
values_temp = df_temp[10].values.tolist()
if not column_names_set:
column_names = values_temp[0]
column_names_set = True
records.extend(values_temp[1:])
counter += 20
columns = [change_name(item) for item in column_names]
df = pd.DataFrame(records, columns=columns)
del df['no']
return df
'''
if __name__ == '__main__':
print('Zaciname:', datetime.datetime.now())
url = 'http://finviz.com/screener.ashx?v=111&f=ind_stocksonly,sh_avgvol_o50,sh_price_o5,ta_sma200_sb50&ft=3'
pole = get_all_symbols(url)
counter = 1
for item in pole:
if '-' in item:
item = item.replace('-', '')
print('#{}:\t{}\tData from WSJ.com: {}'.format(counter, item, get_stock_price_from_wsj(item)))
counter += 1
print('Koniec:', datetime.datetime.now())
'''