analyser_hj3415 2.9.4__tar.gz → 2.9.5__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: analyser_hj3415
3
- Version: 2.9.4
3
+ Version: 2.9.5
4
4
  Summary: Stock analyser and database processing programs
5
5
  Requires-Python: >=3.6
6
6
  Description-Content-Type: text/markdown
@@ -12,6 +12,7 @@ Requires-Dist: yfinance>=0.2.44
12
12
  Requires-Dist: prophet>=1.1.6
13
13
  Requires-Dist: kaleido>=0.2.1
14
14
  Requires-Dist: matplotlib>=3.9.2
15
+ Requires-Dist: tensorflow>=2.18.0
15
16
 
16
17
  ### analyser-hj3415
17
18
 
@@ -1,6 +1,8 @@
1
1
  """
2
2
  Time Series Analysis
3
3
  """
4
+ from pprint import pprint
5
+
4
6
  import numpy as np
5
7
  import yfinance as yf
6
8
  from datetime import datetime, timedelta
@@ -98,6 +100,8 @@ class MyProphet:
98
100
 
99
101
  # 추가 변수를 정규화
100
102
  df['volume_scaled'] = self.scaler.fit_transform(df[['volume']])
103
+ tsa_logger.debug('_preprocessing_for_prophet')
104
+ tsa_logger.debug(df)
101
105
  return df
102
106
 
103
107
  def _make_forecast(self) -> pd.DataFrame:
@@ -108,12 +112,16 @@ class MyProphet:
108
112
 
109
113
  # 향후 180일 동안의 주가 예측
110
114
  future = self.model.make_future_dataframe(periods=180)
115
+ tsa_logger.debug('_make_forecast_future')
116
+ tsa_logger.debug(future)
111
117
 
112
118
  # 미래 데이터에 거래량 추가 (평균 거래량을 사용해 정규화)
113
119
  future_volume = pd.DataFrame({'volume': [self.raw_data['Volume'].mean()] * len(future)})
114
120
  future['volume_scaled'] = self.scaler.transform(future_volume[['volume']])
115
121
 
116
122
  forecast = self.model.predict(future)
123
+ tsa_logger.debug('_make_forecast')
124
+ tsa_logger.debug(forecast)
117
125
  return forecast
118
126
 
119
127
  def get_yhat(self) -> dict:
@@ -292,11 +300,14 @@ class MyLSTM:
292
300
  four_years_ago = today - timedelta(days=365 * 4)
293
301
  tsa_logger.info(f"start: {four_years_ago.strftime('%Y-%m-%d')}, end: {today.strftime('%Y-%m-%d')}")
294
302
 
295
- return yf.download(
303
+ df = yf.download(
296
304
  self.code + '.KS',
297
305
  start=four_years_ago.strftime('%Y-%m-%d'),
298
306
  end=today.strftime('%Y-%m-%d')
299
307
  )
308
+ df.index = df.index.tz_localize(None)
309
+ tsa_logger.debug(df)
310
+ return df
300
311
 
301
312
  def _preprocessing_for_lstm(self) -> LSTMData:
302
313
  """
@@ -538,17 +549,61 @@ class MyLSTM:
538
549
  future_dates, final_future_predictions = self.get_final_predictions(refresh=refresh)
539
550
  final_future_predictions = final_future_predictions.reshape(-1) # 차원을 하나 줄인다.
540
551
 
541
- # Plotly를 사용한 시각화
552
+ # 데이터 준비
553
+ self.raw_data = self.raw_data.reset_index()
554
+ data = self.raw_data[['Date', 'Close']][-120:].reset_index(drop=True)
555
+
556
+ # 'Date'와 'Close' 열 추출
557
+ actual_dates = pd.to_datetime(data['Date'])
558
+ actual_close = data['Close']
559
+
560
+ # 'actual_close'가 Series인지 확인
561
+ if isinstance(actual_close, pd.DataFrame):
562
+ actual_close = actual_close.squeeze()
563
+
564
+ # 'Close' 열의 데이터 타입 확인
565
+ actual_close = actual_close.astype(float)
566
+
567
+ # 예측 데이터 준비
568
+ predicted_dates = pd.to_datetime(future_dates)
569
+ predicted_close = pd.Series(final_future_predictions, index=range(len(final_future_predictions))).astype(float)
570
+
571
+ # 그래프 생성
542
572
  fig = go.Figure()
543
573
 
544
- # 실제 데이터
545
- fig.add_trace(go.Scatter(x=self.raw_data.index[-120:], y=self.raw_data['Close'][-120:], mode='markers', name='실제주가'))
546
- tsa_logger.debug(f"self.raw_data.index[-120:] - {self.raw_data.index[-120:]}")
547
- tsa_logger.debug(f"self.raw_data['Close'][-120:] - {self.raw_data['Close'][-120:]}")
548
- # 예측 데이터
549
- fig.add_trace(go.Scatter(x=future_dates, y=final_future_predictions, mode='lines+markers', name='예측치(30일)'))
550
- tsa_logger.debug(f"future_dates - {future_dates}")
551
- tsa_logger.debug(f"final_future_predictions - {final_future_predictions}")
574
+ # 실제 데이터 추가
575
+ fig.add_trace(go.Scatter(
576
+ x=actual_dates,
577
+ y=actual_close,
578
+ mode='markers',
579
+ name='실제주가'
580
+ ))
581
+
582
+ # 예측 데이터 추가
583
+ fig.add_trace(go.Scatter(
584
+ x=predicted_dates,
585
+ y=predicted_close,
586
+ mode='lines+markers',
587
+ name='예측치(30일)'
588
+ ))
589
+
590
+ # 레이아웃 업데이트
591
+ fig.update_layout(
592
+ xaxis_title='일자',
593
+ yaxis_title='주가(원)',
594
+ xaxis=dict(
595
+ tickformat='%Y/%m',
596
+ ),
597
+ yaxis=dict(
598
+ tickformat=".0f",
599
+ ),
600
+ showlegend=True,
601
+ )
602
+
603
+ tsa_logger.debug(f"actual_dates({len(actual_dates)}) - {actual_dates}")
604
+ tsa_logger.debug(f"actual_close({len(actual_close)} - {actual_close}")
605
+ tsa_logger.debug(f"predicted_dates({len(future_dates)}) - {future_dates}")
606
+ tsa_logger.debug(f"predicted_close({len(predicted_close)}) - {predicted_close}")
552
607
 
553
608
  fig.update_layout(
554
609
  # title=f'{self.code} {self.name} 주가 예측 그래프(prophet)',
@@ -5,7 +5,7 @@ build-backend = "flit_core.buildapi"
5
5
 
6
6
  [project]
7
7
  name = "analyser_hj3415"
8
- version = "2.9.4"
8
+ version = "2.9.5"
9
9
  description = "Stock analyser and database processing programs"
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.6"
@@ -28,6 +28,7 @@ dependencies = [
28
28
  "prophet>=1.1.6",
29
29
  "kaleido>=0.2.1", #plotly로 이미지출력위해
30
30
  "matplotlib>=3.9.2",
31
+ "tensorflow>=2.18.0",
31
32
  ]
32
33
 
33
34
  [project.scripts]