AlanRex commited on
Commit
7a06693
·
verified ·
1 Parent(s): 76830f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -33
app.py CHANGED
@@ -146,41 +146,41 @@ class TradingBacktester:
146
  predictions_history = {}
147
 
148
  # 為每個交易日生成預測
149
- for i in range(60, len(data)): # 從第60天開始,確保有足夠歷史資料
150
- current_date = data.index[i]
151
- historical_data = data.iloc[:i+1].copy() # 到當前日期的歷史資料
152
-
153
- # 確保歷史資料有必要的欄位結構 (yfinance格式)
154
- if 'Close' not in historical_data.columns and 'close' in historical_data.columns:
155
- historical_data['Close'] = historical_data['close']
156
- if 'Volume' not in historical_data.columns and 'volume' in historical_data.columns:
157
- historical_data['Volume'] = historical_data['volume']
158
- if 'High' not in historical_data.columns and 'high' in historical_data.columns:
159
- historical_data['High'] = historical_data['high']
160
- if 'Low' not in historical_data.columns and 'low' in historical_data.columns:
161
- historical_data['Low'] = historical_data['low']
162
- if 'Open' not in historical_data.columns and 'open' in historical_data.columns:
163
- historical_data['Open'] = historical_data['open']
164
-
165
- try:
166
- # 呼叫預測函數 - get_prediction會處理特徵計算
167
- predictions = {}
168
- for days in [1, 5, 10, 20]:
169
- pred_result = predictor_func(historical_data, days)
170
- if pred_result:
171
- predictions[f'{days}d'] = pred_result.get('change_pct', 0)
172
- else:
173
- predictions[f'{days}d'] = 0
174
-
175
- predictions_history[current_date] = predictions
176
 
177
- except Exception as e:
178
- # print(f"預測失敗 {current_date}: {e}")
179
- predictions_history[current_date] = {
180
- '1d': 0, '5d': 0, '10d': 0, '20d': 0
181
- }
 
 
 
 
 
 
182
 
183
- return predictions_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  def run_backtest(self, stock_data, predictor_func, start_date=None, end_date=None):
186
  """
 
146
  predictions_history = {}
147
 
148
  # 為每個交易日生成預測
149
+ for i in range(60, len(data)): # 從第60天開始,確保有足夠歷史資料
150
+ current_date = data.index[i]
151
+ historical_data = data.iloc[:i+1].copy() # 到當前日期的歷史資料
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
+ # 確保歷史資料有必要的欄位結構 (yfinance格式)
154
+ if 'Close' not in historical_data.columns and 'close' in historical_data.columns:
155
+ historical_data['Close'] = historical_data['close']
156
+ if 'Volume' not in historical_data.columns and 'volume' in historical_data.columns:
157
+ historical_data['Volume'] = historical_data['volume']
158
+ if 'High' not in historical_data.columns and 'high' in historical_data.columns:
159
+ historical_data['High'] = historical_data['high']
160
+ if 'Low' not in historical_data.columns and 'low' in historical_data.columns:
161
+ historical_data['Low'] = historical_data['low']
162
+ if 'Open' not in historical_data.columns and 'open' in historical_data.columns:
163
+ historical_data['Open'] = historical_data['open']
164
 
165
+ try:
166
+ # 呼叫預測函數 - get_prediction會處理特徵計算
167
+ predictions = {}
168
+ for days in [1, 5, 10, 20]:
169
+ pred_result = predictor_func(historical_data, days)
170
+ if pred_result:
171
+ predictions[f'{days}d'] = pred_result.get('change_pct', 0)
172
+ else:
173
+ predictions[f'{days}d'] = 0
174
+
175
+ predictions_history[current_date] = predictions
176
+
177
+ except Exception as e:
178
+ # print(f"預測失敗 {current_date}: {e}")
179
+ predictions_history[current_date] = {
180
+ '1d': 0, '5d': 0, '10d': 0, '20d': 0
181
+ }
182
+
183
+ return predictions_history
184
 
185
  def run_backtest(self, stock_data, predictor_func, start_date=None, end_date=None):
186
  """