P2SAMAPA commited on
Commit
94d99f9
·
unverified ·
1 Parent(s): b4facee

Update strategy.py

Browse files
Files changed (1) hide show
  1. strategy.py +20 -20
strategy.py CHANGED
@@ -34,7 +34,7 @@ def execute_strategy(preds, y_raw_test, test_dates, target_etfs, fee_bps,
34
  test_dates, [preds, y_raw_test]
35
  )
36
  preds, y_raw_test = filtered_data
37
- else: # transformer
38
  filtered_dates, filtered_data = filter_to_trading_days(
39
  test_dates, [preds, y_raw_test]
40
  )
@@ -45,33 +45,31 @@ def execute_strategy(preds, y_raw_test, test_dates, target_etfs, fee_bps,
45
  strat_rets = []
46
  audit_trail = []
47
 
 
 
48
  today = datetime.now().date()
49
 
50
- # ✅ CRITICAL FIX: T+1 execution means prediction at i executes on day i+1
51
- # So we iterate from 0 to len(preds)-1, and use returns from i+1
52
- for i in range(len(preds) - 1): # Stop one before the end to ensure i+1 exists
53
  if model_type == "ensemble":
54
  best_idx = preds[i]
55
  signal_etf = target_etfs[best_idx].replace('_Ret', '')
56
- # Use NEXT day's return (i+1) because T+1 execution
57
- realized_ret = y_raw_test[i + 1][best_idx]
58
  else: # transformer
59
  best_idx = np.argmax(preds[i])
60
  signal_etf = target_etfs[best_idx].replace('_Ret', '')
61
- # Use NEXT day's return (i+1) because T+1 execution
62
- realized_ret = y_test[i + 1][best_idx]
63
 
64
  net_ret = realized_ret - (fee_bps / 10000)
65
 
66
  strat_rets.append(net_ret)
67
 
68
- # ✅ Show the EXECUTION date (i+1) in audit trail, not prediction date (i)
69
- execution_date = test_dates[i + 1]
70
 
71
- # Only show in audit trail if the execution date is in the past
72
- if execution_date.date() < today:
73
  audit_trail.append({
74
- 'Date': execution_date.strftime('%Y-%m-%d'),
75
  'Signal': signal_etf,
76
  'Realized': realized_ret,
77
  'Net_Return': net_ret
@@ -79,17 +77,19 @@ def execute_strategy(preds, y_raw_test, test_dates, target_etfs, fee_bps,
79
 
80
  strat_rets = np.array(strat_rets)
81
 
82
- # Get next trading day signal (the last prediction is for the next trading day)
83
- if len(test_dates) > 0 and len(preds) > 0:
84
- # The last prediction hasn't been executed yet - it's for the next trading day
85
  last_date = test_dates[-1]
86
  next_trading_date = get_next_trading_day(last_date)
87
 
88
- if model_type == "ensemble":
89
- next_best_idx = preds[-1]
 
 
 
 
90
  else:
91
- next_best_idx = np.argmax(preds[-1])
92
- next_signal = target_etfs[next_best_idx].replace('_Ret', '')
93
  else:
94
  next_trading_date = datetime.now().date()
95
  next_signal = "CASH"
 
34
  test_dates, [preds, y_raw_test]
35
  )
36
  preds, y_raw_test = filtered_data
37
+ else: # transformer - y_test instead of y_raw_test
38
  filtered_dates, filtered_data = filter_to_trading_days(
39
  test_dates, [preds, y_raw_test]
40
  )
 
45
  strat_rets = []
46
  audit_trail = []
47
 
48
+ # ✅ FIX: Only iterate through predictions that have REALIZED returns
49
+ num_realized = len(preds)
50
  today = datetime.now().date()
51
 
52
+ for i in range(num_realized):
 
 
53
  if model_type == "ensemble":
54
  best_idx = preds[i]
55
  signal_etf = target_etfs[best_idx].replace('_Ret', '')
56
+ realized_ret = y_raw_test[i][best_idx]
 
57
  else: # transformer
58
  best_idx = np.argmax(preds[i])
59
  signal_etf = target_etfs[best_idx].replace('_Ret', '')
60
+ realized_ret = y_test[i][best_idx]
 
61
 
62
  net_ret = realized_ret - (fee_bps / 10000)
63
 
64
  strat_rets.append(net_ret)
65
 
66
+ # ✅ Only add to audit trail if this is historical data (not today/future)
67
+ trade_date = test_dates[i]
68
 
69
+ # Only show in audit trail if the date is in the past
70
+ if trade_date.date() < today:
71
  audit_trail.append({
72
+ 'Date': trade_date.strftime('%Y-%m-%d'),
73
  'Signal': signal_etf,
74
  'Realized': realized_ret,
75
  'Net_Return': net_ret
 
77
 
78
  strat_rets = np.array(strat_rets)
79
 
80
+ # Get next trading day signal (for tomorrow)
81
+ if len(test_dates) > 0:
 
82
  last_date = test_dates[-1]
83
  next_trading_date = get_next_trading_day(last_date)
84
 
85
+ if len(preds) > 0:
86
+ if model_type == "ensemble":
87
+ next_best_idx = preds[-1]
88
+ else:
89
+ next_best_idx = np.argmax(preds[-1])
90
+ next_signal = target_etfs[next_best_idx].replace('_Ret', '')
91
  else:
92
+ next_signal = "CASH"
 
93
  else:
94
  next_trading_date = datetime.now().date()
95
  next_signal = "CASH"