-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_new_data_kospi.py
246 lines (197 loc) · 8.03 KB
/
load_new_data_kospi.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
import FinanceDataReader as fdr
import pandas as pd
import exchange_calendars as xcals
import os
from datetime import datetime, timedelta
import numpy as np
from tqdm import trange, tqdm
from mpfinance import candlestick2_ochl
import matplotlib.pyplot as plt
from PIL import Image
import yfinance as yf
from csv import writer
import schedule
from pandas_datareader import data
import subprocess
from pykrx import stock
column = ['date','open','high','low','close','volume','ma_5','ma_20','ma_60','ma_120','ma_240']
def df_format(df, last_index):
# Column 조정
df = df.reset_index()
df = df.reset_index()
df = df.drop(['Change'], axis=1)
df = df[['Date', 'Open', 'High', 'Low', 'Close', 'Volume']]
df.columns = column[:6]
df['ma_5'] = np.full(len(df), np.nan)
df['ma_20'] = np.full(len(df), np.nan)
df['ma_60'] = np.full(len(df), np.nan)
df['ma_120'] = np.full(len(df), np.nan)
df['ma_240'] = np.full(len(df), np.nan)
# 날짜 포맷 변경
df['date'] = df['date'].apply(date_format)
# 새로운 데이터 추가
new_data = df.values.tolist()[-1]
new_data.insert(0, last_index)
return new_data
def date_format(date):
return date.strftime("%Y%m%d")
def add_stock(ticker):
today = datetime.today().strftime("%Y-%m-%d")
stock = None
is_open = None
try:
stock_data = pd.read_csv(f'/home/ubuntu/2022_VAIV_Dataset/Stock_Data/Kospi_Data/{ticker}.csv', index_col=0)
last_date_str = str(stock_data.iloc[-1]['date']).split('.')[0]
last_date = datetime.strptime(last_date_str, "%Y%m%d")
last_index = stock_data.index[-1]
curr_date = last_date
while (curr_date < datetime.today()):
try:
curr_date = curr_date + timedelta(1)
df = fdr.DataReader(ticker[1:], curr_date, curr_date)
last_index += 1
new_data = df_format(df, last_index)
with open(f'/home/ubuntu/2022_VAIV_Dataset/Stock_Data/Kospi_Data/{ticker}.csv', 'a', newline='') as f_object:
writer_object = writer(f_object)
writer_object.writerow(new_data)
f_object.close()
except:
continue
except Exception as e:
is_open = False
return stock, is_open
def correction(df, date_format):
df.columns = map(lambda x: str(x)[0].upper() + str(x)[1:], df.columns)
delete_col = set(df.columns) - set(column)
for i in delete_col:
del df[i]
if not df.empty:
# print(df)
df.Date = df.Date.map(lambda x: correct_date(x, date_format))
# df['Date'].map(correct_date)
df = df.replace(0, np.NaN)
df = df.dropna()
df = df.reset_index(drop=True)
df = df[column]
return df
def correct_date(date, date_format):
date = str(date)
date_time = datetime.strptime(date, date_format)
date = date_time.strftime("%Y-%m-%d")
return date
def make_candlestick(ticker, date_list):
df = pd.read_csv(f'/home/ubuntu/2022_VAIV_Dataset/Stock_Data/Kospi_Data/{ticker}.csv', index_col=0)
df.reset_index(inplace=True, drop=True)
#print(df.iloc[4143])
for date in date_list:
plt.style.use('dark_background')
row = None
# date에 해당하는 row의 index 찾기
try:
last_date = date_format(date)
row = df.loc[df['date'] == float(last_date)]
index_list = row.index.tolist()
i = index_list[0] - 20
c = df.iloc[i:i + 20, :]
if (len(c) == 20):
end = date.strftime("%Y-%m-%d")
name = f'/home/ubuntu/2022_VAIV_Dataset/Image/1/224x224/Kospi/{ticker}_{end}_20_224x224.png'
fig = plt.figure(figsize=(224 / 100, 224 / 100))
ax = fig.add_subplot(1, 1, 1)
quote = pd.DataFrame()
quote['Date'] = np.arange(0, 20, 1)
quote = pd.concat([quote, c[['open', 'close', 'high', 'low']]], axis=1)
candlestick2_ochl(ax, c.open, c.close, c.high, c.low, width=0.7, colorup='#77d879',
colordown='#db3f3f', alpha=None)
ax.grid(False)
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.axis('off')
plt.tight_layout(pad=0)
fig.set_constrained_layout_pads(w_pad=0, h_pad=0)
print(name)
fig.savefig(name)
pil_image = Image.open(name)
rgb_image = pil_image.convert('RGB')
rgb_image.save(name)
plt.close(fig)
except Exception as e:
print(ticker)
print(f'Date : {date}')
print(row)
print(e)
continue
def check_image(ticker):
files_path = '/home/ubuntu/2022_VAIV_Dataset/Image/1/224x224/Kospi'
files = os.listdir(files_path)
files = [file for file in files if file[0] == 'A']
count = 0
date_list = []
is_there = True
f_output = open(f'/home/ubuntu/2022_VAIV_Dataset/try/current_image/{ticker}.txt', 'w')
for file in files:
if file.split('_')[0] == ticker:
date_list.append(file.split('_')[1])
f_output.write(f"{file}\n")
f_output.close()
date_list.sort()
if len(date_list) == 0:
is_there = False
return date_list, is_there
def check_date(last_date):
new_date_list = []
is_there_date = False
#last_date_time = datetime.strptime(last_date, "%Y-%m-%d")
#start_date = date_format(last_date_time)
end_date = datetime.today().strftime("%Y-%m-%d")
KOSPI = data.get_data_yahoo("^KS11", last_date, end_date)
new_date_list = KOSPI.index.tolist()
if len(new_date_list) > 1:
new_date_list = new_date_list[1:]
is_there_date = True
return new_date_list, is_there_date
def update_stock():
files_path = '/home/ubuntu/2022_VAIV_Dataset/Stock_Data/Kospi_Data'
files = os.listdir(files_path)
files = [file for file in files if file[0] == 'A']
count = 0
for file in tqdm(files):
ticker = file.split('.')[0]
#print('\nTicker {}/{}\t{}'.format(count, len(files), ticker))
# Update stock data in csv file
try:
stock, is_open = add_stock(ticker)
if is_open:
date_list, is_there = check_image(ticker)
last_date = None
if is_there:
last_date = date_list[-1]
else:
continue
new_date_list, is_there_date = check_date(last_date) # KOSPI 장이 열린 날짜 중에 date_list의 마지막날 이후의 날짜가 담긴 리스트 (1개 이상일 경우 true 리턴 / 0개일 경우 false도 리턴)
if is_there_date:
make_candlestick(ticker, new_date_list) # is_there_date 가 true일 경우만 실행
count += 1
except:
print(f'Ticker : {ticker}')
continue
print(count)
# Update files in try/predict_csv
today = datetime.today().strftime("%Y-%m-%d")
kospi = ['vgg16_4', 'efficient_4']
model_path = '/home/ubuntu/2022_VAIV_Dataset/flask/static/models'
for kospi_model in kospi:
print(kospi_model)
subprocess.call(f'python /home/ubuntu/2022_VAIV_Dataset/try/make_prediction_csv.py -i /home/ubuntu/2022_VAIV_Dataset/Image/1/224x224/Kospi -s {today} -e {today} -d 224 -o /home/ubuntu/2022_VAIV_Dataset/try/predict_csv/KOSPI/{kospi_model}.csv -m {model_path}/{kospi_model}.h5', shell=True)
# Update files in make_graph
subprocess.call('/home/ubuntu/2022_VAIV_SeoHwan/make_graph/make_2022_csv.py', shell=True)
if __name__ == '__main__':
# 실행 주기 설정
# Updating Stock Data
schedule.every().day.at("16:30").do(update_stock) # 매일 오후 3시 30분에 update_stock 함수 실행
#update_stock()
# 실행 시작
while True:
schedule.run_pending()