-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreener.py
215 lines (163 loc) · 5.6 KB
/
screener.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import operator
from typing import Dict, List
import numpy as np
import pandas as pd
import yfinance as yf
from nasdaq_100_ticker_history import tickers_as_of
from tools import atr, roc, sma
# def
MAX_STOCKS = 10
def get_monthly_index():
sp_500 = yf.download("^GSPC")
sp_500["sma"] = sma(sp_500.Close, 300)
sp_500["Date"] = sp_500.index
sp_500["month"] = sp_500["Date"].dt.strftime("%y-%m")
sp_500 = (
sp_500.groupby("month")
.agg(
Date=("Date", "last"),
Close=("Close", "last"),
sma=("sma", "last"),
)
.reset_index()
.set_index("Date")
.sort_index()
)
return sp_500
def get_nasdaq_symbols_monthly(year: int, month: int) -> List:
return list(tickers_as_of(year, month, 1))
def get_nasdaq_symbols() -> List:
nasdaq_tickers = dict()
for year in range(2016, 2025, 1):
for month in range(1, 13, 1):
nasdaq_tickers[f"{year - 2000}-{month:02}"] = list(
tickers_as_of(year, month, 1)
)
all = []
for value in nasdaq_tickers.values():
all = all + value
nasdaq_tickers["all"] = list(set(all))
return nasdaq_tickers["all"]
def get_stocks(symbols: List[str]) -> Dict[str, pd.DataFrame]:
"""_summary_
Args:
symbols (List[str]): _description_
Returns:
Dict[str, pd.DataFrame]: _description_
"""
dfs = {}
stock_data = yf.download(
symbols,
rounding=2,
progress=False,
group_by="ticker",
)
# perform some pre preparation
for symbol in stock_data.columns.get_level_values(0).unique():
# drop unclear items
df = stock_data[symbol]
df = df[~(df.High == df.Low)]
df = df.dropna()
df.index = pd.to_datetime(df.index).tz_convert(None)
if len(df):
dfs[symbol.lower()] = df
return dfs
def resample_stocks_to_month(df: pd.DataFrame) -> pd.DataFrame:
df["Date"] = df.index
df["month"] = df["Date"].dt.strftime("%y-%m")
df = df.groupby("month").agg(
Date=("Date", "last"),
Open=("Open", "first"),
Close=("Close", "last"),
score=("score", "last"),
)
return df.reset_index().set_index("Date").sort_index()
def get_score(data: pd.DataFrame) -> pd.Series:
roc_intervall = [intervall for intervall in range(20, 260, 20)]
for intervall in roc_intervall:
data[f"roc_{intervall}"] = (roc(data.Close, 20)).shift(intervall)
data["score"] = np.where(
(data.Close > sma(data.Close, 100)) & (atr(data, 100) > atr(data, 20)),
data[[f"roc_{intervall}" for intervall in roc_intervall]].mean(axis=1),
np.nan,
)
return data["score"]
def ndx100_list():
table = pd.read_html("https://en.wikipedia.org/wiki/Nasdaq-100#Components")[4]
return list(table.Ticker)
def prepare_stocks(index: pd.DataFrame) -> pd.DataFrame:
tracker = index.copy()
# stocks = get_stocks(ndx100_list())
stocks = get_stocks(get_nasdaq_symbols() + ["^ndx"])
for symbol, df in stocks.items():
df["score"] = get_score(df)
df = resample_stocks_to_month(df)
df[symbol] = df["score"]
tracker = pd.merge(
tracker, df[[symbol]], left_index=True, right_index=True, how="left"
)
return tracker
def get_top_stocks(stocks: dict) -> dict:
year = 2000 + int(stocks["month"][:2])
month = int(stocks["month"][-2:])
if month == 12:
year = year + 1
month = 1
else:
month = month + 1
try:
nasdaq_symbols = get_nasdaq_symbols_monthly(year, month)
except Exception:
print("No known tickers")
year = 2000 + int(stocks["month"][:2])
month = int(stocks["month"][-2:])
print(f"fallback to {year}-{month}")
nasdaq_symbols = get_nasdaq_symbols_monthly(year, month)
nasdaq_symbols = [symbol.lower() for symbol in nasdaq_symbols]
stocks.pop("month")
stocks.pop("Close")
stocks.pop("sma")
try:
ndx_min = stocks.pop("^ndx")
except KeyError:
print(f"ndx missing at {year}-{month:0>2}")
ndx_min = 0
try:
stocks.pop("googl")
except KeyError:
pass
stocks = {
k: v
for k, v in stocks.items()
if (v > 0 and v > ndx_min) and k in nasdaq_symbols
}
return sorted(stocks.items(), key=operator.itemgetter(1))[-MAX_STOCKS:]
if __name__ == "__main__":
sp_500 = get_monthly_index()
ndx_stocks = prepare_stocks(index=sp_500)
if sp_500.iloc[-1].Close > sp_500.iloc[-1].sma:
top_stocks = get_top_stocks(ndx_stocks.iloc[-2].dropna().to_dict())
current_month = [ticker for (ticker, _) in top_stocks]
top_stocks = get_top_stocks(ndx_stocks.iloc[-1].to_dict())
next_month = [ticker for (ticker, _) in top_stocks]
unchanged_stocks = set(current_month).intersection(next_month)
removed_stocks = set(current_month) - set(next_month)
added_stocks = set(next_month) - set(current_month)
changes_txt = "# Planned transactions for next month\n"
changes_txt = (
changes_txt
+ "\n## New\n"
+ "\n".join([f"+ {stocks}" for stocks in added_stocks])
)
changes_txt = (
changes_txt
+ "\n## Unchanged\n"
+ "\n".join([f"* {stocks}" for stocks in unchanged_stocks])
)
changes_txt = (
changes_txt
+ "\n## Leave\n"
+ "\n".join([f"- {stocks}" for stocks in removed_stocks])
)
with open("CHANGES.md", "w") as text_file:
text_file.write(changes_txt)