-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
58 lines (46 loc) · 2.27 KB
/
app.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
from stock_screener import Stock, StockScreener
import requests
from bs4 import BeautifulSoup
# Get sp500 ticker and sector
url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find('table', {'class': 'wikitable sortable'})
rows = table.find_all('tr')[1:] # skip the header row
sp500 = []
for row in rows:
cells = row.find_all('td')
ticker = cells[0].text.strip()
company = cells[1].text.strip()
sector = cells[3].text.strip()
sp500.append({'ticker': ticker, 'company': company, 'sector': sector})
def get_sp500_stocks():
sp500_stocks = [Stock(stock['ticker'], stock['sector']) for stock in sp500]
# print(sp500_stocks)
return sp500_stocks
# Run example with 2 stocks
# filters = [lambda stock: StockScreener.filter_sector(stock, 'Interactive Media & Services'),
# lambda stock: StockScreener.filter_price(stock, 60, 200),
# lambda stock: StockScreener.filter_metric(stock, 'shares_outstanding', '>', 3*1e9)]
# sp500_stocks = [Stock('NKLA', 'Interactive Media & Services'), Stock('GOOGL', 'Interactive Media & Services')]
# screener = StockScreener(sp500_stocks, filters)
# screener.add_data()
# filtered_stocks = screener.apply_filters()
# Run screener for all sp500 tickers
filters = [lambda stock: StockScreener.filter_sector(stock, 'Interactive Media & Services'),
lambda stock: StockScreener.filter_price(stock, 40, 300),
# lambda stock: StockScreener.filter_metric(stock, 'profit_margin', '>', 5),
lambda stock: StockScreener.filter_technical_indicators(stock, 'UpperBand', '>', 'price'),
lambda stock: StockScreener.filter_technical_indicators(stock, 'LowerBand', '<', 'price')
]
sp500_stocks = [Stock('GOOG', 'Interactive Media & Services'), Stock('GOOGL', 'Interactive Media & Services'), Stock('MTCH', 'Interactive Media & Services'), Stock('META', 'Interactive Media & Services' )]
#sp500_stocks = get_sp500_stocks()
screener = StockScreener(sp500_stocks, filters)
# Add Data
screener.add_data()
# Apply Filters
filtered_stocks = screener.apply_filters()
# Train Model
screener.train_models()
# Make Predictions
predicted_stocks = screener.predict_stocks(filtered_stocks)