-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
351 lines (266 loc) · 12.1 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import datetime as dt
import glob
import json
import os
import sys
import time
import pickle
# ------------------- global ----------------------
### 증권거래소 종목들 가져오기
#sp500_list = fdr.StockListing('S&P500')
#nyse_list = fdr.StockListing('NYSE')
#nasdaq_list = fdr.StockListing('NASDAQ')
### 지수 데이터 가져오기
# nasdaq = fdr.DataReader('IXIC', '2020-01-01', '2023-02-25')
# sp500 = fdr.DataReader('US500', '2020-01-01', '2023-02-25')
# dowjones = fdr.DataReader('DJI', '2020-01-01', '2023-02-25')
from jdStockDataManager import JdStockDataManager
from jdChart import JdChart
from jdGlobal import get_yes_no_input
from jdGlobal import data_folder
from jdGlobal import metadata_folder
from qtWindow import JdWindowClass
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import pandas as pd
import jdStockFilteringManager
sd = JdStockDataManager()
sf = jdStockFilteringManager.JdStockFilteringManager(sd)
def DrawStockDatas(stock_datas_dic, selected_tickers, inStockManager : JdStockDataManager, maxCnt = -1):
if __name__ == "__main__" :
#QApplication : 프로그램을 실행시켜주는 클래스
app = QApplication(sys.argv)
#WindowClass의 인스턴스 생성
myWindow = JdWindowClass()
chart = JdChart(inStockManager)
chart.init_plots_for_stock(stock_datas_dic, selected_tickers)
myWindow.set_chart_class(chart)
#프로그램 화면을 보여주는 코드
myWindow.show()
#프로그램을 이벤트루프로 진입시키는(프로그램을 작동시키는) 코드
app.exec_()
def DrawMomentumIndex(updown_nyse, updown_nasdaq, updown_sp500, bOnlyForScreenShot = False):
# show first element
chart = JdChart(sd)
chart.bOnlyForScreenShot = bOnlyForScreenShot
chart.init_plots_for_up_down(updown_nyse, updown_nasdaq, updown_sp500)
chart.draw_updown_chart()
def draw_atr_expansion(atr_changes_df : pd.DataFrame , bOnlyForScreenShot = False):
# show first element
chart = JdChart(sd)
chart.bOnlyForScreenShot = bOnlyForScreenShot
chart.init_plots_for_atr_up_down(atr_changes_df)
chart.draw_updown_ATR_chart()
def draw_count_data_Index(mtt_cnt_df, name : str, chart_type : str, bOnlyForScreenShot = False):
"""
name : {name} Count chart, {name} Count Moving Average ..
chart_type : line or bar
"""
chart = JdChart(sd)
chart.bOnlyForScreenShot = bOnlyForScreenShot
chart.init_plots_for_count_data(mtt_cnt_df, chart_type)
chart.draw_count_data_chart(name, chart_type)
def remove_outdated_tickers():
with open("DataReader_exception.json", "r") as outfile:
data = json.load(outfile)
keys = data.keys()
for key in keys:
file_path = os.path.join(data_folder, key + '.csv')
if os.path.exists(file_path):
os.remove(file_path)
print(file_path, 'is removed!')
def remove_local_caches():
local_dir = os.path.join(os.getcwd())
for filename in os.listdir(local_dir):
if filename.startswith('cache_'):
os.remove(os.path.join(local_dir, filename))
sd.reset_caches()
def screen_stocks_and_show_chart(filter_function, bUseLocalLoadedStockDataForScreening, bSortByRS):
bUseLocalCache = get_yes_no_input('Do you want to see last chart data? \n It will just show your last chart data without screening. \n (y/n)')
if bUseLocalCache:
try:
with open('cache_tickers', "rb") as f:
tickers = pickle.load(f)
with open('cache_stock_datas_dic', 'rb') as f:
stock_data = pickle.load(f)
except FileNotFoundError:
print('Can not find your last stock chart data in local.\n The chart data will be re-generated. ')
bUseLocalCache = False
stock_data, tickers = sf.screening_stocks_by_func(filter_function, bUseLocalLoadedStockDataForScreening, bSortByRS)
else:
stock_data, tickers = sf.screening_stocks_by_func(filter_function, bUseLocalLoadedStockDataForScreening, bSortByRS)
# 데이터를 파일에 저장
if not bUseLocalCache:
with open('cache_tickers', "wb") as f:
pickle.dump(tickers, f)
with open('cache_stock_datas_dic', "wb") as f:
pickle.dump(stock_data, f)
print(tickers)
print("filtered stock count: " ,len(tickers))
if len(tickers) > 0:
DrawStockDatas(stock_data, tickers, sd)
else:
print("there's no tickers to draw!")
print("Select the chart type. \n \
1: Stock Data Chart \n \
2: Momentum Index Chart \n \
3: Sync local .csv datas from web and gernerate other meta datas.(up_down, RS, industry, mtt count, etc ..) \n \
4: cook up-down datas using local csv files. \n \
5: cook local stock data. \n \
6: Download stock data from web and overwrite local files. (It will takes so long...) \n \
7: cook ATRS Ranking \n \
8: cook industry Ranking \n \
9: cook screening result as xlsx file. \n \
10: MTT Index chart \n \
11: FA50 Index chart \n \
12: Generate All indicators and screening result \n \
13: Power gap histroy screen \n ")
index = int(input())
if index == 1:
sf.MTT_ADR_minimum = 2.5
sf.LastDayMinimumVolume = 1000000
#screen_stocks_and_show_chart(sf.filter_stocks_high_ADR_swing, True, True)
#screen_stocks_and_show_chart(sf.filter_stocks_MTT, True, True)
screen_stocks_and_show_chart(sf.filter_stocks_young, True, True)
#screen_stocks_and_show_chart(sf.filter_stocks_Bull_Snort, True, True)
#screen_stocks_and_show_chart(sf.filter_stocks_rs_8_10, True, True)
sf.MTT_ADR_minimum = 2
#screen_stocks_and_show_chart(sf.filter_stock_hope_from_bottom, True, True)
#screen_stocks_and_show_chart(sf.filter_stock_ALL, True, False)
#screen_stocks_and_show_chart(sf.filter_stock_Good_RS, True, True)
#screen_stocks_and_show_chart(sf.filter_stocks_high_ADR_swing, True, True)
#screen_stocks_and_show_chart(filter_stock_power_gap, True, True)
elif index == 2:
updown_nyse, updown_nasdaq, updown_sp500 = sd.getUpDownDataFromCsv(365*2)
DrawMomentumIndex(updown_nyse, updown_nasdaq, updown_sp500)
elif index == 3:
remove_local_caches()
sd.syncCsvFromWeb(5)
sd.cookUpDownDatas()
sd.cook_ATR_Expansion_Counts()
sd.cook_Nday_ATRS150_exp(365*2)
sd.cook_ATRS150_exp_Ranks(365*2)
sd.cook_short_term_industry_rank_scores()
sd.cook_long_term_industry_rank_scores()
sd.cook_filter_count_data(sf.filter_stocks_MTT, "MTT_Counts", 10, True)
sd.cook_filter_count_data(sf.filter_stock_FA50, "FA50_Counts", 10, True)
sd.cook_top10_in_industries()
elif index == 4:
sd.cookUpDownDatas()
sd.cook_ATR_Expansion_Counts()
elif index == 5:
remove_local_caches()
sd.cookLocalStockData()
elif index == 6:
remove_local_caches()
sd.downloadStockDatasFromWeb(365*6, False) # you have 6 year data....
remove_outdated_tickers()
sd.remove_acquisition_tickers()
sd.cook_Stock_GICS_df()
sd.cook_Nday_ATRS150_exp(365*2)
sd.cook_ATRS150_exp_Ranks(365*2)
sd.cook_top10_in_industries()
sd.cook_filter_count_data(sf.filter_stocks_MTT, "MTT_Counts", 365*3, False)
sd.cook_filter_count_data(sf.filter_stock_FA50, "FA50_Counts", 365*3, False)
elif index == 7:
sd.cook_Nday_ATRS150_exp(365*2)
sd.cook_ATRS150_exp_Ranks(365*2)
elif index == 8:
sd.cook_short_term_industry_rank_scores()
sd.cook_long_term_industry_rank_scores()
sd.cook_top10_in_industries()
elif index == 9:
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stock_Custom, True, True)
first_stock_data : pd.DataFrame = stock_data[tickers[0]]
lastday = str(first_stock_data.index[-1].date())
sd.cook_stock_info_from_tickers(tickers, f'MTT_Leaders_{lastday}')
elif index == 10:
df = sd.get_count_data_from_csv("MTT")
draw_count_data_Index(df, "MTT", "line")
elif index == 11:
df = sd.get_count_data_from_csv("FA50", 365*3)
draw_count_data_Index(df, "FA50", "bar")
# ATR Expansion
#sd.cookUpDownATR_Expansion()
#df = sd.get_count_data_from_csv("ATR_Expansion", 365*1)
#draw_atr_expansion(df)
elif index == 12:
# auto generate indicator screenshot and MTT xlsx file. #
## MI index chart
updown_nyse, updown_nasdaq, updown_sp500 = sd.getUpDownDataFromCsv(365*3)
DrawMomentumIndex(updown_nyse, updown_nasdaq, updown_sp500, True)
### MTT count chart
df = sd.get_count_data_from_csv("MTT")
draw_count_data_Index(df, "MTT", "line", True)
### FA50 count chart
df = sd.get_count_data_from_csv("FA50")
draw_count_data_Index(df, "FA50", "bar", True)
### ATR_Expansion chart
df = sd.get_count_data_from_csv("ATR_Expansion", 365*1)
draw_atr_expansion(df, True)
## cook MTT stock list as xlsx.
# get mtt screen list
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stocks_MTT, True, True)
# Hack: get last date string from first stock data and use it for filename.
first_stock_data : pd.DataFrame = stock_data[tickers[0]]
lastday = str(first_stock_data.index[-1].date())
### COOK MTT
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_MTT_{lastday}')
### COOK High ADR Swing
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stocks_high_ADR_swing, True, True)
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_HighAdrSwing_{lastday}')
### COOK RS 8/10
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stocks_rs_8_10, True, True)
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_RS_8_10_{lastday}')
### COOK Hope from bottom
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stock_hope_from_bottom, True, True)
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_hope_from_bottom_{lastday}')
### COOK High ADR Swing
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stocks_high_ADR_swing, True, True)
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_high_ADR_swing_{lastday}')
### COOK Young
stock_data, tickers = sf.screening_stocks_by_func(sf.filter_stocks_young, True, True)
if len(tickers) > 0:
sd.cook_stock_info_from_tickers(tickers, f'US_Young_{lastday}')
### PRINT power gap tickers
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stock_power_gap, True, True, -1)
s = str.format(f"[{lastday}] power gap tickers: ") + str(tickers)
print(s)
### PRINT bull snort tickers
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stocks_Bull_Snort, True, True, -1)
s = str.format(f"[{lastday}] bull snort tickers: ") + str(tickers)
print(s)
### PRINT RS 8/10 tickers
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stocks_rs_8_10, True, True, -1)
s = str.format(f"[{lastday}] RS 8/10 tickers: ") + str(tickers)
print(s)
### PRINT Hope from bottom
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stock_hope_from_bottom, True, True, -1)
s = str.format(f"[{lastday}] Hope from bottom tickers: ") + str(tickers)
print(s)
### PRINT High ADR Swing
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stocks_high_ADR_swing, True, True, -1)
s = str.format(f"[{lastday}] High ADR Swing tickers: ") + str(tickers)
print(s)
### PRINT Young
stock_data_dic, tickers = sf.screening_stocks_by_func(sf.filter_stocks_young, True, True, -1)
s = str.format(f"[{lastday}] Young Stock tickers: ") + str(tickers)
print(s)
elif index == 13:
sf.cook_power_gap_profiles(20*12*5, 20, 20)
sf.cook_open_gap_profiles(20*12*5, 20, 20)
sf.get_filter_gap_stocks_in_range(20, 0, sf.filter_stock_power_gap)
elif index == 14:
sd.cook_ATR_Expansion_Counts()
df = sd.get_count_data_from_csv("ATR_Expansion", 365*1)
draw_atr_expansion(df, True)
# --------------------------------------------------------------------