tbdavid2019 commited on
Commit
ead29e0
·
1 Parent(s): fc08f05
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -12,22 +12,33 @@ def get_stock_data(ticker, period):
12
  # Function to prepare the data for Chronos-Bolt
13
 
14
  def prepare_data_chronos(data):
15
- # Convert to the correct format
16
  df = data.reset_index()
17
- df = df.rename(columns={
18
- 'Date': 'timestamp',
19
- 'Close': 'target',
20
- })
21
 
22
- # Ensure correct data types
23
- df['timestamp'] = pd.to_datetime(df['timestamp'])
24
- df['target'] = df['target'].astype('float32')
25
- df['item_id'] = 'stock'
 
 
26
 
27
- # Create TimeSeriesDataFrame directly
28
- ts_df = TimeSeriesDataFrame(df)
 
 
29
 
30
- return ts_df
 
 
 
 
 
 
 
 
 
 
 
31
 
32
 
33
  # Function to fetch stock indices (you already defined these)
@@ -84,16 +95,21 @@ def get_top_10_potential_stocks(period, selected_indices):
84
 
85
  for ticker in stock_list:
86
  try:
87
- # Get stock data
88
  data = get_stock_data(ticker, period)
89
  if data.empty:
90
  continue
91
 
92
- # Prepare data
93
  ts_data = prepare_data_chronos(data)
94
 
95
- # Create predictor and fit model
96
- predictor = TimeSeriesPredictor(prediction_length=prediction_length)
 
 
 
 
 
97
  predictor.fit(
98
  ts_data,
99
  hyperparameters={
@@ -101,10 +117,10 @@ def get_top_10_potential_stocks(period, selected_indices):
101
  }
102
  )
103
 
104
- # Make predictions
105
  predictions = predictor.predict(ts_data)
106
 
107
- # Calculate potential
108
  potential = (predictions.iloc[-1] - data['Close'].iloc[-1]) / data['Close'].iloc[-1]
109
  stock_predictions.append((ticker, potential, data['Close'].iloc[-1], predictions.iloc[-1]))
110
 
 
12
  # Function to prepare the data for Chronos-Bolt
13
 
14
  def prepare_data_chronos(data):
15
+ # 重設索引並準備數據
16
  df = data.reset_index()
 
 
 
 
17
 
18
+ # 創建符合官方格式的數據框
19
+ formatted_df = pd.DataFrame({
20
+ 'item_id': ['stock'] * len(df),
21
+ 'timestamp': pd.to_datetime(df['Date']),
22
+ 'value': df['Close'].astype('float32') # 使用 'value' 而不是 'target'
23
+ })
24
 
25
+ print("Data types:", formatted_df.dtypes)
26
+ print("Sample data:", formatted_df.head())
27
+ # 按照時間戳排序
28
+ formatted_df = formatted_df.sort_values('timestamp')
29
 
30
+ try:
31
+ # 使用 from_data_frame 方法創建 TimeSeriesDataFrame
32
+ ts_df = TimeSeriesDataFrame.from_data_frame(
33
+ formatted_df,
34
+ id_column='item_id',
35
+ timestamp_column='timestamp',
36
+ target_column='value'
37
+ )
38
+ return ts_df
39
+ except Exception as e:
40
+ print(f"Error creating TimeSeriesDataFrame: {str(e)}")
41
+ raise
42
 
43
 
44
  # Function to fetch stock indices (you already defined these)
 
95
 
96
  for ticker in stock_list:
97
  try:
98
+ # 獲取股票數據
99
  data = get_stock_data(ticker, period)
100
  if data.empty:
101
  continue
102
 
103
+ # 準備數據
104
  ts_data = prepare_data_chronos(data)
105
 
106
+ # 創建預測器
107
+ predictor = TimeSeriesPredictor(
108
+ prediction_length=prediction_length,
109
+ target='value' # 指定目標列名
110
+ )
111
+
112
+ # 訓練模型
113
  predictor.fit(
114
  ts_data,
115
  hyperparameters={
 
117
  }
118
  )
119
 
120
+ # 進行預測
121
  predictions = predictor.predict(ts_data)
122
 
123
+ # 計算潛力
124
  potential = (predictions.iloc[-1] - data['Close'].iloc[-1]) / data['Close'].iloc[-1]
125
  stock_predictions.append((ticker, potential, data['Close'].iloc[-1], predictions.iloc[-1]))
126