analyser_hj3415 4.3.2__py3-none-any.whl → 4.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- analyser_hj3415/analyser/tsa/myprophet.py +52 -14
- {analyser_hj3415-4.3.2.dist-info → analyser_hj3415-4.4.0.dist-info}/METADATA +1 -1
- {analyser_hj3415-4.3.2.dist-info → analyser_hj3415-4.4.0.dist-info}/RECORD +5 -5
- {analyser_hj3415-4.3.2.dist-info → analyser_hj3415-4.4.0.dist-info}/WHEEL +0 -0
- {analyser_hj3415-4.3.2.dist-info → analyser_hj3415-4.4.0.dist-info}/entry_points.txt +0 -0
@@ -1,6 +1,7 @@
|
|
1
1
|
from collections import OrderedDict
|
2
|
-
|
2
|
+
import datetime
|
3
3
|
from typing import Tuple, List, Dict, Union
|
4
|
+
import time
|
4
5
|
|
5
6
|
import yfinance as yf
|
6
7
|
import pandas as pd
|
@@ -22,7 +23,7 @@ mylogger = setup_logger(__name__,'WARNING')
|
|
22
23
|
class ProphetLatestData:
|
23
24
|
ticker: str
|
24
25
|
|
25
|
-
date: date = field(metadata={"marshmallow_field": fields.Date()})
|
26
|
+
date: datetime.date = field(metadata={"marshmallow_field": fields.Date()})
|
26
27
|
price: float
|
27
28
|
yhat: float
|
28
29
|
yhat_upper: float
|
@@ -96,7 +97,7 @@ class MyProphet:
|
|
96
97
|
- 데이터를 Prophet 형식에 맞게 전처리합니다.
|
97
98
|
- Prophet 모델을 사용하여 예측 데이터를 생성합니다.
|
98
99
|
"""
|
99
|
-
def get_raw_data() -> pd.DataFrame:
|
100
|
+
def get_raw_data(max_retries: int = 3, delay_sec: int = 2) -> pd.DataFrame:
|
100
101
|
"""
|
101
102
|
Yahoo Finance에서 4년간의 주가 데이터를 가져옵니다.
|
102
103
|
|
@@ -104,16 +105,34 @@ class MyProphet:
|
|
104
105
|
pd.DataFrame: 가져온 주가 데이터프레임.
|
105
106
|
"""
|
106
107
|
# 오늘 날짜 가져오기
|
107
|
-
today = datetime.today()
|
108
|
+
today = datetime.datetime.today()
|
108
109
|
|
109
110
|
# 4년 전 날짜 계산 (4년 = 365일 * 4)
|
110
|
-
four_years_ago = today - timedelta(days=365 * 4)
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
111
|
+
four_years_ago = today - datetime.timedelta(days=365 * 4)
|
112
|
+
|
113
|
+
# 시도 횟수만큼 반복
|
114
|
+
for attempt in range(1, max_retries + 1):
|
115
|
+
try:
|
116
|
+
data = yf.download(
|
117
|
+
tickers=self.ticker,
|
118
|
+
start=four_years_ago.strftime('%Y-%m-%d'),
|
119
|
+
end=today.strftime('%Y-%m-%d')
|
120
|
+
)
|
121
|
+
# 데이터가 비어있지 않다면 성공으로 판단
|
122
|
+
if not data.empty:
|
123
|
+
return data
|
124
|
+
else:
|
125
|
+
# 다운로드 자체는 성공했지만, 빈 데이터가 반환될 경우
|
126
|
+
print(f"[{attempt}/{max_retries}] 다운로드 결과가 비어 있습니다. 재시도합니다...")
|
127
|
+
except Exception as e:
|
128
|
+
# 예외 발생 시, 재시도
|
129
|
+
print(f"[{attempt}/{max_retries}] 다운로드 실패: {e}. 재시도합니다...")
|
130
|
+
|
131
|
+
# 재시도 전 대기
|
132
|
+
time.sleep(delay_sec)
|
133
|
+
|
134
|
+
mylogger.error(f"주가 데이터를 다운로드하지 못했습니다 (최대 {max_retries}회 시도 실패).")
|
135
|
+
return pd.DataFrame()
|
117
136
|
|
118
137
|
def preprocessing_for_prophet() -> pd.DataFrame:
|
119
138
|
"""
|
@@ -246,10 +265,10 @@ class MyProphet:
|
|
246
265
|
)
|
247
266
|
|
248
267
|
data.trading_action, data.score = scoring(data.price, data.yhat_lower, data.yhat_upper)
|
249
|
-
except Exception:
|
268
|
+
except Exception as e:
|
250
269
|
data = ProphetLatestData(
|
251
270
|
ticker=self.ticker,
|
252
|
-
date=datetime.now().date(),
|
271
|
+
date=datetime.datetime.now().date(),
|
253
272
|
price=float('nan'),
|
254
273
|
yhat=float('nan'),
|
255
274
|
yhat_lower=float('nan'),
|
@@ -341,7 +360,7 @@ class MyProphet:
|
|
341
360
|
"""
|
342
361
|
try:
|
343
362
|
# %Y-%m-%d 형식으로 문자열을 datetime 객체로 변환 시도
|
344
|
-
datetime.strptime(date_string, '%Y-%m-%d')
|
363
|
+
datetime.datetime.strptime(date_string, '%Y-%m-%d')
|
345
364
|
return True
|
346
365
|
except ValueError:
|
347
366
|
# 변환이 실패하면 ValueError가 발생, 형식이 맞지 않음
|
@@ -457,3 +476,22 @@ class MIProphet(MyProphet):
|
|
457
476
|
self._mi_type = mi_type
|
458
477
|
self.ticker = getattr(MIs, mi_type)
|
459
478
|
|
479
|
+
@staticmethod
|
480
|
+
def ticker_to_mitype(ticker: str):
|
481
|
+
dict_fields = MIs._asdict()
|
482
|
+
reverse_map = {value: key for key, value in dict_fields.items()}
|
483
|
+
return reverse_map.get(ticker)
|
484
|
+
|
485
|
+
@staticmethod
|
486
|
+
def mitype_to_ticker(mi_type: str):
|
487
|
+
return getattr(MIs, mi_type)
|
488
|
+
|
489
|
+
@staticmethod
|
490
|
+
def bulk_get_latest_data(mi_types: List[str], refresh: bool) -> Dict[str, ProphetLatestData]:
|
491
|
+
ticker_data = MyProphet.bulk_get_latest_data([MIProphet.mitype_to_ticker(mi_type) for mi_type in mi_types],
|
492
|
+
refresh=refresh)
|
493
|
+
mi_data = {}
|
494
|
+
for ticker, data in ticker_data.items():
|
495
|
+
mi_data[MIProphet.ticker_to_mitype(ticker)] = data
|
496
|
+
return mi_data
|
497
|
+
|
@@ -11,8 +11,8 @@ analyser_hj3415/analyser/eval/red.py,sha256=Abf5HPsNWKnDF4cbZ8xhlNsMF6ljN_sShCAY
|
|
11
11
|
analyser_hj3415/analyser/tsa/__init__.py,sha256=7j-WshikzsDGGo_wuFoMPNmYfY-bLSEMd6o1MKSQKLI,150
|
12
12
|
analyser_hj3415/analyser/tsa/common.py,sha256=iRwk88zBtEIqzSmTQWmQIWiiqZ9gN7GLtDUa5rx8IGM,1918
|
13
13
|
analyser_hj3415/analyser/tsa/lstm.py,sha256=howngiVaEFKccYif6vrREm6skZiDzxazq44SdY_82yQ,28906
|
14
|
-
analyser_hj3415/analyser/tsa/myprophet.py,sha256=
|
15
|
-
analyser_hj3415-4.
|
16
|
-
analyser_hj3415-4.
|
17
|
-
analyser_hj3415-4.
|
18
|
-
analyser_hj3415-4.
|
14
|
+
analyser_hj3415/analyser/tsa/myprophet.py,sha256=QHeIHinCZzMhuLpAqrMdy4E56MFbCGd89i9dR8cGXrw,18976
|
15
|
+
analyser_hj3415-4.4.0.dist-info/entry_points.txt,sha256=ZfjPnJuH8SzvhE9vftIPMBIofsc65IAWYOhqOC_L5ck,65
|
16
|
+
analyser_hj3415-4.4.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
|
17
|
+
analyser_hj3415-4.4.0.dist-info/METADATA,sha256=4m0sEx7lcK10xFNx7-C4gDVvWwWXlcJesac3n7rwPgA,6811
|
18
|
+
analyser_hj3415-4.4.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|