-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrppo-mul-opt.py
More file actions
1558 lines (1339 loc) · 66.4 KB
/
rppo-mul-opt.py
File metadata and controls
1558 lines (1339 loc) · 66.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import gym_trading_env
import gymnasium as gym
import pandas as pd
from sb3_contrib import RecurrentPPO
from stable_baselines3.common.callbacks import EvalCallback, StopTrainingOnNoModelImprovement, BaseCallback, CallbackList
from stable_baselines3.common.monitor import Monitor
import numpy as np
import random
import os
from datetime import datetime
import optuna
from optuna.pruners import MedianPruner
from optuna.samplers import TPESampler
import logging
import shutil
import traceback
import sys
import math
import hashlib
import glob
from pathlib import Path
import argparse
import traceback
from gym_trading_env.environments import MultiDatasetTradingEnv
class predictableMultiDatasetTradingEnv(MultiDatasetTradingEnv):
def __init__(self, dataset_paths: list, *args, **kwargs):
# This __init__ method bypasses the MultiDatasetTradingEnv constructor
# and calls the TradingEnv constructor directly. This allows us to use
# a fixed list of dataset paths instead of a glob pattern.
# 1. Set up properties managed by MultiDatasetTradingEnv
self.dataset_pathes = dataset_paths
if len(self.dataset_pathes) == 0:
raise FileNotFoundError(
"The provided 'dataset_paths' list is empty.")
self.dataset_nb_uses = np.zeros(shape=(len(self.dataset_pathes),))
# Extract kwargs that belong to MultiDatasetTradingEnv, not TradingEnv
self.preprocess = kwargs.pop('preprocess', lambda df: df)
self.episodes_between_dataset_switch = kwargs.pop(
'episodes_between_dataset_switch', 1)
self._episodes_on_this_dataset = 0
# 2. Load the first dataset using the same logic as MultiDatasetTradingEnv
first_dataset_df = self.next_dataset()
# 3. Call the grandparent (TradingEnv) constructor directly
# This ensures all other parameters (reward_function, windows, etc.) are passed correctly.
super(MultiDatasetTradingEnv, self).__init__(
df=first_dataset_df, *args, **kwargs)
def reset(self, seed=None, options=None, **kwargs):
# The reset logic from MultiDatasetTradingEnv is preserved.
# It handles switching datasets when an episode ends.
if seed is not None:
# Ensure dataset selection is deterministic for evaluation
np.random.seed(seed)
self.dataset_nb_uses.fill(0)
self._episodes_on_this_dataset = 0
# Explicitly set the next dataset to ensure predictability
self._set_df(self.next_dataset())
return super(MultiDatasetTradingEnv, self).reset(seed=seed, options=options, **kwargs)
return super().reset(seed=seed, options=options, **kwargs)
# Import base reward function
# from reward import reward_function_5
def reward_function_5(
history,
window: int = 30, # look-back for risk metrics
r_free: float = 0.0, # risk-free rate per step
w_return: float = 1.00, # weights for each component
w_risk: float = 0.30,
w_drawdown: float = 0.20,
w_cost: float = 0.001,
w_alpha: float = 0.50,
clip_value: float = 1.0,
eps: float = 1e-8
):
"""
A robust reward that stays numerically stable and combines:
• immediate log-return
• risk-adjusted return (Sharpe-like)
• draw-down penalty
• turnover / transaction cost penalty
• market out-performance bonus (alpha)
All terms are internally normalised and the final reward is
clipped to [-clip_value, clip_value] to avoid gradient explosions.
"""
# -------------- Safety checks --------------
if len(history) < 2:
return 0.0
# ---------- 1. Immediate (current) return ----------
curr_val = history["portfolio_valuation", -1]
prev_val = history["portfolio_valuation", -2]
r_t = np.log(curr_val / prev_val) # robust to scale
# ---------- 2. Risk-adjusted return (Sharpe) ----------
# Take the last `window` log-returns
values = np.asarray(history["portfolio_valuation"], dtype=np.float64)
returns = np.diff(np.log(values[-(window + 1):]))
if returns.size > 1:
sharpe = (returns.mean() - r_free) / (returns.std() + eps)
else:
sharpe = 0.0
# ---------- 3. Draw-down ----------
peak = values.max()
drawdown = (peak - curr_val) / (peak + eps) # ∈ [0,1]
# ---------- 4. Transaction-cost penalty ----------
pos_now = history["position", -1]
pos_prev = history["position", -2] if len(history) > 2 else pos_now
# 0 (no trade) … 2 (full flip)
turnover = abs(pos_now - pos_prev)
# ---------- 5. Market out-performance (alpha) ----------
if "data_close" in history.columns:
m_ret = np.log(history["data_close", -1] / history["data_close", -2])
alpha = r_t - m_ret
else:
alpha = 0.0
# ---------- Final weighted reward ----------
reward = (
w_return * r_t +
w_risk * sharpe -
w_drawdown * drawdown -
w_cost * turnover +
w_alpha * alpha
)
# ---------- Numerical housekeeping ----------
if np.isnan(reward) or np.isinf(reward):
reward = 0.0
reward = float(np.clip(reward, -clip_value, clip_value))
return reward
# Helper functions for metrics
def calculate_max_drawdown(history):
portfolio_valuations = np.asarray(
history['portfolio_valuation'], dtype=np.float64)
if len(portfolio_valuations) < 2:
return 0.0
peaks = np.maximum.accumulate(portfolio_valuations)
drawdowns = (peaks - portfolio_valuations) / (peaks + 1e-8)
return np.max(drawdowns)
def calculate_annualized_return(history):
portfolio_valuations = np.asarray(
history['portfolio_valuation'], dtype=np.float64)
if len(portfolio_valuations) < 2:
return 0.0
total_return = (
portfolio_valuations[-1] - portfolio_valuations[0]) / portfolio_valuations[0]
start_date = pd.to_datetime(history['date', 0])
end_date = pd.to_datetime(history['date', -1])
duration_in_days = (end_date - start_date).days
# Handle edge cases for duration
if duration_in_days <= 0:
return 0.0
duration_in_years = duration_in_days / 365.25
# Prevent issues with very short durations or extreme returns
if duration_in_years < 1/365.25: # Less than 1 day
return total_return # Return simple return for very short periods
# Handle extreme cases that could cause numerical issues
if total_return <= -1.0: # Complete loss
return -1.0
if total_return > 100: # Extremely high returns (>10000%)
return 100.0 # Cap at 10000% annualized return
try:
# Calculate annualized return with safety checks
annualized_return = (1 + total_return) ** (1 / duration_in_years) - 1
# Check for invalid results
if np.isnan(annualized_return) or np.isinf(annualized_return):
return total_return / duration_in_years # Fallback to linear approximation
# Cap extreme annualized returns
if annualized_return > 100:
return 100.0
if annualized_return < -1.0:
return -1.0
return annualized_return
except (OverflowError, ZeroDivisionError, ValueError):
# Fallback to linear approximation if power calculation fails
return total_return / duration_in_years
def calculate_sharpe_ratio(history, risk_free_rate=0.0404):
portfolio_valuations = np.asarray(
history['portfolio_valuation'], dtype=np.float64)
if len(portfolio_valuations) < 2:
return 0.0
returns = np.diff(portfolio_valuations) / portfolio_valuations[:-1]
start_date = pd.to_datetime(history['date', 0])
end_date = pd.to_datetime(history['date', -1])
duration_in_days = (end_date - start_date).days
if duration_in_days <= 0:
return 0.0
# Assuming daily data for simplicity in calculating daily risk-free rate
daily_risk_free_rate = (1 + risk_free_rate)**(1/365.25) - 1
excess_returns = returns - daily_risk_free_rate
mean_excess_return = np.mean(excess_returns)
std_dev_returns = np.std(returns)
if std_dev_returns < 1e-8:
return 0.0
# Annualize Sharpe Ratio
sharpe_ratio = mean_excess_return / std_dev_returns
annualized_sharpe = sharpe_ratio * \
np.sqrt(365.25) # Assuming daily returns
return annualized_sharpe
# Generate unique timestamp-based ID for this run, or use one from args
parser = argparse.ArgumentParser(
description="Train a trading agent with Optuna.")
parser.add_argument('--run-id', type=str,
help='Specify a run ID to continue a previous optimization.')
parser.add_argument('--dataset-dir', type=str,
default='dataset/1d-2005',
help='Base dataset directory. Must contain subfolders train, val, test each containing .pkl files.')
# Use parse_known_args to avoid issues with other args if any, especially in notebooks
args, _ = parser.parse_known_args()
if args.run_id:
RUN_ID = args.run_id
print(f"Continuing optimization with provided RUN_ID: {RUN_ID}")
else:
RUN_ID = datetime.now().strftime("%Y%m%d_%H%M%S")
print(
f"Starting new optimization. Please record this ID for tracking: {RUN_ID}")
# Configure logging with more detailed format
os.makedirs('optuna_logs', exist_ok=True)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
logging.FileHandler(f'optuna_logs/debug_{RUN_ID}.log')
]
)
logger = logging.getLogger(__name__)
# Set seeds for reproducibility
SEED = 42
def set_seeds(seed):
"""Set all random seeds for reproducibility"""
random.seed(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
try:
import torch
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
except ImportError:
pass
set_seeds(SEED)
# Validate and set dataset directory
def validate_dataset_dir(dataset_dir: str) -> None:
"""Ensure provided dataset_dir exists and has train/val/test subdirs each containing at least one .pkl file."""
p = Path(dataset_dir)
if not p.exists():
raise FileNotFoundError(f"Specified dataset_dir does not exist: {dataset_dir}")
missing = []
no_pkl = []
for sub in ('train', 'val', 'test'):
subdir = p / sub
if not subdir.exists() or not subdir.is_dir():
missing.append(sub)
else:
pkls = list(subdir.glob('*.pkl'))
if len(pkls) == 0:
no_pkl.append(sub)
if missing:
raise FileNotFoundError(f"dataset_dir '{dataset_dir}' missing subfolders: {missing}")
if no_pkl:
raise FileNotFoundError(f"dataset_dir '{dataset_dir}' subfolders missing .pkl files: {no_pkl}")
DATASET_DIR = args.dataset_dir
try:
validate_dataset_dir(DATASET_DIR)
logger.info(f"Using dataset dir: {DATASET_DIR}")
except Exception as e:
logger.error(f"Invalid dataset directory provided: {e}")
raise
# Custom preprocessing function
def create_preprocess_function(feature_config):
"""Create a preprocessing function based on feature configuration"""
def preprocess(df: pd.DataFrame):
# Create your features based on the configuration
try:
# Log detailed DataFrame information for debugging
logger.debug(f"Input DataFrame shape: {df.shape}")
logger.debug(f"Available columns: {df.columns.tolist()}")
logger.debug(
f"DataFrame index range: {df.index.min()} to {df.index.max()}")
if logger.isEnabledFor(logging.DEBUG):
logger.debug(f"DataFrame info:\n{df.info()}")
# Check for empty DataFrame
if df.empty:
logger.error("Input DataFrame is empty!")
raise ValueError("Input DataFrame is empty")
# Check for NaN values
used_columns = ['open', 'close', 'high', 'low', 'volume', 'macd', 'rsi', 'close_10_sma',
'close_10_ema', 'adx', 'boll_ub', 'boll_lb', 'boll', 'kdjk', 'kdjd', 'kdjj', 'atr']
used_columns = [f'norm_{col}' for col in used_columns]
nan_counts = df[used_columns].isnull().sum()
if nan_counts.any():
logger.warning(
f"NaN values found in columns: {nan_counts[nan_counts > 0].to_dict()}")
# Basic price features (always included for trading)
if 'norm_close' in df.columns:
if df['norm_close'].isna().all():
logger.error(
"Critical: 'norm_close' column is entirely NaN!")
raise ValueError("'norm_close' column is entirely NaN")
df["feature_close"] = df["norm_close"]
else:
logger.error(
"Critical: 'norm_close' column not found in dataset!")
raise ValueError(
"'norm_close' column is required but not found in dataset")
# Optional features based on trial suggestions with validation
if feature_config.get('use_volume', True):
if 'norm_volume' in df.columns:
if df['norm_volume'].isna().all():
logger.warning(
"'norm_volume' column is entirely NaN, skipping")
else:
df["feature_volume"] = df["norm_volume"]
else:
logger.debug("'norm_volume' column not found in dataset")
if feature_config.get('use_high', True):
if 'norm_high' in df.columns:
if df['norm_high'].isna().all():
logger.warning(
"'norm_high' column is entirely NaN, skipping")
else:
df["feature_high"] = df["norm_high"]
else:
logger.debug("'norm_high' column not found in dataset")
if feature_config.get('use_low', True):
if 'norm_low' in df.columns:
if df['norm_low'].isna().all():
logger.warning(
"'norm_low' column is entirely NaN, skipping")
else:
df["feature_low"] = df["norm_low"]
else:
logger.debug("'norm_low' column not found in dataset")
if feature_config.get('use_open', True):
if 'norm_open' in df.columns:
if df['norm_open'].isna().all():
logger.warning(
"'norm_open' column is entirely NaN, skipping")
else:
df["feature_open"] = df["norm_open"]
else:
logger.debug("'norm_open' column not found in dataset")
# Technical indicators with validation
if feature_config.get('use_macd', True):
if 'norm_macd' in df.columns:
if df['norm_macd'].isna().all():
logger.warning(
"'norm_macd' column is entirely NaN, skipping")
else:
df["feature_macd"] = df["norm_macd"]
else:
logger.debug("'norm_macd' column not found in dataset")
if feature_config.get('use_rsi', False):
if 'norm_rsi' in df.columns:
if df['norm_rsi'].isna().all():
logger.warning(
"'norm_rsi' column is entirely NaN, skipping")
else:
df["feature_rsi"] = df["norm_rsi"]
else:
logger.debug("'norm_rsi' column not found in dataset")
if feature_config.get('use_sma', False):
if 'norm_close_10_sma' in df.columns:
if df['norm_close_10_sma'].isna().all():
logger.warning(
"'norm_close_10_sma' column is entirely NaN, skipping")
else:
df["feature_sma"] = df["norm_close_10_sma"]
else:
logger.debug(
"'norm_close_10_sma' column not found in dataset")
if feature_config.get('use_ema', False):
if 'norm_close_10_ema' in df.columns:
if df['norm_close_10_ema'].isna().all():
logger.warning(
"'norm_close_10_ema' column is entirely NaN, skipping")
else:
df["feature_ema"] = df["norm_close_10_ema"]
else:
logger.debug(
"'norm_close_10_ema' column not found in dataset")
if feature_config.get('use_adx', False):
if 'norm_adx' in df.columns:
if df['norm_adx'].isna().all():
logger.warning(
"'norm_adx' column is entirely NaN, skipping")
else:
df["feature_adx"] = df["norm_adx"]
else:
logger.debug("'norm_adx' column not found in dataset")
if feature_config.get('use_bb_upper', False):
if 'norm_boll_ub' in df.columns:
if df['norm_boll_ub'].isna().all():
logger.warning(
"'norm_boll_ub' column is entirely NaN, skipping")
else:
df["feature_bb_upper"] = df["norm_boll_ub"]
else:
logger.debug("'norm_boll_ub' column not found in dataset")
if feature_config.get('use_bb_lower', False):
if 'norm_boll_lb' in df.columns:
if df['norm_boll_lb'].isna().all():
logger.warning(
"'norm_boll_lb' column is entirely NaN, skipping")
else:
df["feature_bb_lower"] = df["norm_boll_lb"]
else:
logger.debug("'norm_boll_lb' column not found in dataset")
if feature_config.get('use_bb_middle', False):
if 'norm_boll' in df.columns:
if df['norm_boll'].isna().all():
logger.warning(
"'norm_boll' column is entirely NaN, skipping")
else:
df["feature_bb_middle"] = df["norm_boll"]
else:
logger.debug("'norm_boll' column not found in dataset")
if feature_config.get('use_stoch_k', False):
if 'norm_kdjk' in df.columns:
if df['norm_kdjk'].isna().all():
logger.warning(
"'norm_kdjk' column is entirely NaN, skipping")
else:
df["feature_stoch_k"] = df["norm_kdjk"]
else:
logger.debug("'norm_kdjk' column not found in dataset")
if feature_config.get('use_stoch_d', False):
if 'norm_kdjd' in df.columns:
if df['norm_kdjd'].isna().all():
logger.warning(
"'norm_kdjd' column is entirely NaN, skipping")
else:
df["feature_stoch_d"] = df["norm_kdjd"]
else:
logger.debug("'norm_kdjd' column not found in dataset")
if feature_config.get('use_stoch_j', False):
if 'norm_kdjj' in df.columns:
if df['norm_kdjj'].isna().all():
logger.warning(
"'norm_kdjj' column is entirely NaN, skipping")
else:
df["feature_stoch_j"] = df["norm_kdjj"]
else:
logger.debug("'norm_kdjj' column not found in dataset")
if feature_config.get('use_atr', False):
if 'norm_atr' in df.columns:
if df['norm_atr'].isna().all():
logger.warning(
"'norm_atr' column is entirely NaN, skipping")
else:
df["feature_atr"] = df["norm_atr"]
else:
logger.debug("'norm_atr' column not found in dataset")
# Validate that we have at least some features
feature_columns = [
col for col in df.columns if col.startswith('feature_')]
if len(feature_columns) == 0:
logger.error(
"No feature columns created! Check dataset and feature configuration.")
raise ValueError(
"No valid features could be created from the dataset")
logger.debug(
f"Created {len(feature_columns)} features: {feature_columns}")
logger.debug(f"Output DataFrame shape: {df.shape}")
logger.debug(
f"Feature columns data types: {df[feature_columns].dtypes.to_dict()}")
except Exception as e:
logger.error(f"Error during preprocessing: {e}")
logger.error(f"Full traceback:\n{traceback.format_exc()}")
logger.error(f"Available columns: {df.columns.tolist()}")
logger.error(f"DataFrame shape: {df.shape}")
logger.error(f"Feature config: {feature_config}")
raise
return df
return preprocess
def create_tunable_reward_function(trial):
"""Create a reward function with tunable weights based on trial suggestions"""
# Suggest weight parameters for different reward components
w_return = trial.suggest_float('w_return', 0.5, 2.0)
w_risk = trial.suggest_float('w_risk', 0.0, 1.0)
w_drawdown = trial.suggest_float('w_drawdown', 0.0, 1.0)
w_cost = trial.suggest_float('w_cost', 0.0001, 0.01)
w_alpha = trial.suggest_float('w_alpha', 0.0, 1.0)
# Other reward function parameters
window = trial.suggest_int('reward_window', 10, 50)
clip_value = trial.suggest_float('clip_value', 0.5, 2.0)
def tunable_reward_function(history):
try:
# Add validation before calling reward function
if history is None:
logger.error("History is None in reward function")
raise ValueError("History cannot be None")
# Log history information for debugging
if hasattr(history, 'shape'):
logger.debug(f"History shape: {history.shape}")
elif hasattr(history, '__len__'):
logger.debug(f"History length: {len(history)}")
else:
logger.debug(f"History type: {type(history)}")
# Check if history is a pandas DataFrame or Series
if hasattr(history, 'index'):
logger.debug(f"History index length: {len(history.index)}")
if hasattr(history, 'columns'):
logger.debug(
f"History columns: {history.columns.tolist()}")
return reward_function_5(
history=history,
window=window,
w_return=w_return,
w_risk=w_risk,
w_drawdown=w_drawdown,
w_cost=w_cost,
w_alpha=w_alpha,
clip_value=clip_value
)
except Exception as e:
logger.error(f"Error in tunable_reward_function: {e}")
logger.error(f"Full traceback:\n{traceback.format_exc()}")
logger.error(f"History type: {type(history)}")
if hasattr(history, 'shape'):
logger.error(f"History shape: {history.shape}")
elif hasattr(history, '__len__'):
logger.error(f"History length: {len(history)}")
raise
return tunable_reward_function
def evaluate_sharpe_ratio(model, eval_env, n_episodes=10, base_seed=42, annual_risk_free_rate=0.0404,
custom_reward_function=None, preprocess_func=None, windows=None,
trading_fees=None, borrow_interest_rate=None):
"""
Evaluate the trained model using Sharpe ratio as a consistent metric.
This function evaluates the actual trading performance independent of the reward function.
IMPORTANT: All trials use the SAME sequence of episodes for fair comparison.
Each trial evaluates on identical datasets/starting points to ensure that
performance differences are due to hyperparameters, not random evaluation conditions.
NOTICE: Current implementation has some issue on statistical properties of Sharpe ratio. But with the fixed ranges of validation dataset,
the Sharpe ratio should be calculated in a mostly consistent manner across trials.
Args:
model: Trained RL model
eval_env: Evaluation environment (not used, kept for compatibility)
n_episodes: Number of episodes to evaluate
base_seed: Base seed for consistent evaluation across all trials
annual_risk_free_rate (float): The annualized risk-free rate (e.g., 0.0404 for 4.04%, taken from annual performance of US 1-year Treasury).
custom_reward_function: Reward function to use for the environment
preprocess_func: Preprocessing function to use for the environment
windows: Windows parameter for the environment
trading_fees: Trading fees parameter for the environment
borrow_interest_rate: Borrow interest rate parameter for the environment
Returns:
float: Sharpe ratio of the portfolio returns
"""
# Create predictable evaluation environment specifically for Sharpe ratio evaluation
val_dataset_paths = sorted(glob.glob(os.path.join(DATASET_DIR, 'val', '*.pkl')))
if not val_dataset_paths:
raise FileNotFoundError("No validation datasets found.")
predictable_eval_env = predictableMultiDatasetTradingEnv(
dataset_paths=val_dataset_paths,
reward_function=custom_reward_function,
preprocess=preprocess_func,
windows=windows,
positions=[-1, 0, 1],
trading_fees=trading_fees,
borrow_interest_rate=borrow_interest_rate,
)
predictable_eval_env.add_metric(
'Sym', lambda history: history['data_symbol', -1] if 'data_symbol' in history.columns else 'Unknown')
predictable_eval_env.add_metric('PosChg', lambda history: np.sum(
np.diff(history['position']) != 0))
predictable_eval_env.add_metric(
'Len', lambda history: len(history['position']))
predictable_eval_env.add_metric(
'MDD', lambda history: f"{calculate_max_drawdown(history) * 100:.2f}%")
predictable_eval_env.add_metric(
'AnnRet', lambda history: f"{calculate_annualized_return(history) * 100:.2f}%")
predictable_eval_env.add_metric(
'Sharpe', lambda history: f"{calculate_sharpe_ratio(history):.2f}")
predictable_eval_env.add_metric(
'Reward',
lambda history: f"{np.sum(history['reward']):.4f}" if 'reward' in history.columns else "N/A"
)
portfolio_values = []
episode_returns = []
episode_excess_returns = []
for episode in range(n_episodes):
# Use SAME seed sequence for ALL trials - ensures fair comparison
# All trials evaluate on identical episodes:
# - Trial A, Episode 0: base_seed + 0
# - Trial A, Episode 1: base_seed + 1
# - Trial B, Episode 0: base_seed + 0 (SAME as Trial A)
# - Trial B, Episode 1: base_seed + 1 (SAME as Trial A)
# This guarantees fair comparison across trials
episode_seed = base_seed + episode
logger.info(f"Starting episode {episode} with seed {episode_seed}")
obs, _ = predictable_eval_env.reset(seed=episode_seed)
done = False
initial_value = None
step_count = 0
logger.info(
f"Episode {episode}: Sharpe eval env reset successful. Obs shape: {obs.shape if hasattr(obs, 'shape') else type(obs)}")
# Clear portfolio values for the new episode
portfolio_values.clear()
# Get initial episode information after reset
try:
if hasattr(predictable_eval_env, 'unwrapped') and hasattr(predictable_eval_env.unwrapped, 'historical_info'):
history = predictable_eval_env.unwrapped.historical_info
# Get symbol information
symbol = "Unknown"
if 'data_symbol' in history.columns:
symbol = history['data_symbol', -
1] if len(history) > 0 else "Unknown"
# Get start date
start_date = "Unknown"
if 'date' in history.columns and len(history) > 0:
start_date = pd.to_datetime(
history['date', -1]).strftime('%Y-%m-%d')
logger.info(
f"Episode {episode}: Symbol={symbol}, Start_date={start_date}")
except Exception as e:
logger.warning(
f"Episode {episode}: Could not get initial episode info: {e}")
while not done:
action, _ = model.predict(obs, deterministic=True)
obs, reward, terminated, truncated, info = predictable_eval_env.step(
action)
done = terminated or truncated
step_count += 1
# Track portfolio valuation
if hasattr(predictable_eval_env, 'unwrapped') and hasattr(predictable_eval_env.unwrapped, 'historical_info'):
current_value = predictable_eval_env.unwrapped.historical_info[
"portfolio_valuation", -1]
logger.debug(
f"Episode {episode}, Step {step_count}: Portfolio value: {current_value:.2f}")
if initial_value is None:
initial_value = current_value
logger.info(
f"Episode {episode}: Initial portfolio value: {initial_value:.2f}")
portfolio_values.append(current_value)
# Get final episode information
try:
if hasattr(predictable_eval_env, 'unwrapped') and hasattr(predictable_eval_env.unwrapped, 'historical_info'):
history = predictable_eval_env.unwrapped.historical_info
# Get symbol information (should be same as start)
symbol = "Unknown"
if 'data_symbol' in history.columns:
symbol = history['data_symbol', -
1] if len(history) > 0 else "Unknown"
# Get end date
end_date = "Unknown"
if 'date' in history.columns and len(history) > 0:
end_date = pd.to_datetime(
history['date', -1]).strftime('%Y-%m-%d')
# Get episode length from history
episode_length = len(history) if hasattr(
history, '__len__') else step_count
logger.info(f"Episode {episode}: Symbol={symbol}, End_date={end_date}, "
f"Episode_length={episode_length}, Steps_taken={step_count}")
except Exception as e:
logger.warning(
f"Episode {episode}: Could not get final episode info: {e}")
# Calculate episode return and risk-adjusted return
if initial_value is not None and len(portfolio_values) > 1:
final_value = portfolio_values[-1]
episode_return = (final_value - initial_value) / initial_value
episode_returns.append(episode_return)
logger.info(f"Episode {episode}: Initial_value={initial_value:.2f}, "
f"Final_value={final_value:.2f}, Episode_return={episode_return:.4f}")
# Calculate episode duration in years to adjust the risk-free rate
try:
history = predictable_eval_env.unwrapped.historical_info
start_date_obj = pd.to_datetime(history['date', 0])
end_date_obj = pd.to_datetime(history['date', -1])
duration_in_days = (end_date_obj - start_date_obj).days
logger.debug(f"Episode {episode}: Duration={duration_in_days} days "
f"({start_date_obj.strftime('%Y-%m-%d')} to {end_date_obj.strftime('%Y-%m-%d')})")
# Avoid division by zero if episode is less than a day
if duration_in_days > 0:
duration_in_years = duration_in_days / 365.25
# De-annualize the risk-free rate for the episode's duration
episode_risk_free_return = (
1 + annual_risk_free_rate)**duration_in_years - 1
else:
episode_risk_free_return = 0.0
# Calculate excess return over the risk-free rate
excess_return = episode_return - episode_risk_free_return
episode_excess_returns.append(excess_return)
logger.info(f"Episode {episode}: Risk_free_return={episode_risk_free_return:.4f}, "
f"Excess_return={excess_return:.4f}")
except Exception as e:
logger.warning(
f"Episode {episode}: Could not calculate duration/excess return: {e}")
# Fallback: assume zero risk-free return
excess_return = episode_return
episode_excess_returns.append(excess_return)
else:
logger.warning(f"Episode {episode}: Insufficient data for return calculation "
f"(initial_value={initial_value}, portfolio_values_len={len(portfolio_values)})")
# Close the predictable evaluation environment
predictable_eval_env.close()
# Calculate Sharpe ratio from episode returns
if len(episode_returns) > 1:
# Use mean of excess returns
mean_excess_return = np.mean(episode_excess_returns)
# Use std of portfolio returns (standard definition of Sharpe Ratio)
std_return = np.std(episode_returns)
# Avoid division by zero
if std_return > 1e-8:
sharpe_ratio = mean_excess_return / std_return
else:
# If no volatility, return mean excess return
sharpe_ratio = mean_excess_return
else:
# Not enough episodes to calculate meaningful Sharpe ratio
sharpe_ratio = 0.0
logger.info(
f"Final evaluation results across {len(episode_returns)} episodes:")
logger.info(f" Mean excess return: {np.mean(episode_excess_returns):.4f}")
logger.info(f" Std return: {np.std(episode_returns):.4f}")
logger.info(f" Sharpe ratio: {sharpe_ratio:.4f}")
logger.info(f" Episode returns: {[f'{r:.4f}' for r in episode_returns]}")
return sharpe_ratio
class OptunaPruningCallback(BaseCallback):
"""Custom callback for Optuna pruning during training"""
def __init__(self, trial: optuna.Trial, eval_env, model, base_seed=42,
custom_reward_function=None, preprocess_func=None, windows=None,
trading_fees=None, borrow_interest_rate=None, verbose: int = 0):
super().__init__(verbose)
self.trial = trial
self.eval_env = eval_env
self.model = model
self.base_seed = base_seed
self.custom_reward_function = custom_reward_function
self.preprocess_func = preprocess_func
self.windows = windows
self.trading_fees = trading_fees
self.borrow_interest_rate = borrow_interest_rate
def _on_step(self) -> bool:
# This callback is called after each evaluation by EvalCallback
# We'll evaluate using Sharpe ratio for pruning decisions
if hasattr(self.parent, 'n_calls') and self.parent.n_calls > 0:
# The step is the total number of timesteps, which aligns with the pruner's parameters
step = self.parent.n_calls
logger.info(
f"Pruning: Evaluating trial {self.trial.number} at step {step}")
try:
# Quick Sharpe ratio evaluation for pruning (fewer episodes)
# Use consistent base seed for fair comparison across all trials
sharpe_ratio = evaluate_sharpe_ratio(
model=self.model,
eval_env=self.eval_env,
n_episodes=math.ceil(65 * 0.25), # 25% of 65 episodes
base_seed=self.base_seed, # Same episodes for all trials
custom_reward_function=self.custom_reward_function,
preprocess_func=self.preprocess_func,
windows=self.windows,
trading_fees=self.trading_fees,
borrow_interest_rate=self.borrow_interest_rate
)
self.trial.report(sharpe_ratio, step)
if self.trial.should_prune():
logger.info(
f"Trial {self.trial.number} pruned at step {step} with Sharpe ratio {sharpe_ratio:.4f}")
raise optuna.TrialPruned()
except optuna.TrialPruned:
# Re-raise TrialPruned exceptions - they should propagate
raise
except Exception as e:
# If evaluation fails, don't prune (continue training)
logger.warning(f"Pruning evaluation failed: {str(e)}")
logger.warning(f"Full traceback:\n{traceback.format_exc()}")
# Don't raise the exception, just continue training
return True
def objective(trial):
"""
Optuna objective function for hyperparameter optimization.
IMPORTANT: This function evaluates trials using Sharpe ratio instead of mean reward
to avoid the circular dependency problem where the reward function weights are being
optimized while simultaneously being used as the evaluation metric.
The Sharpe ratio provides a consistent, meaningful metric across all trials that
measures risk-adjusted returns independent of the reward function formulation.
"""
train_env = None
eval_env = None
trial_dir = None
model = None
try:
logger.info(f"Starting trial {trial.number}")
# Suggest hyperparameters
learning_rate = trial.suggest_float(
'learning_rate', 1e-5, 1e-2, log=True)
n_steps = trial.suggest_categorical('n_steps', [512, 1024, 2048, 4096])
batch_size = trial.suggest_categorical(
'batch_size', [32, 64, 128, 256])
n_epochs = trial.suggest_int('n_epochs', 3, 30)
gamma = trial.suggest_float('gamma', 0.9, 0.9999)
gae_lambda = trial.suggest_float('gae_lambda', 0.8, 0.99)
clip_range = trial.suggest_float('clip_range', 0.1, 0.4)
ent_coef = trial.suggest_float('ent_coef', 1e-4, 1e-2, log=True)
vf_coef = trial.suggest_float('vf_coef', 0.1, 1.0)
# PPO-specific hyperparameters
# Increased minimum window size
windows = trial.suggest_int('windows', 10, 60)
trading_fees = trial.suggest_float('trading_fees', 0.0005, 0.002)
borrow_interest_rate = trial.suggest_float(
'borrow_interest_rate', 0.0001, 0.0005)
logger.info(f"Trial {trial.number}: Suggested hyperparameters: "
f"learning_rate={learning_rate}, n_steps={n_steps}, batch_size={batch_size}, "
f"n_epochs={n_epochs}, gamma={gamma}, gae_lambda={gae_lambda}, "
f"clip_range={clip_range}, ent_coef={ent_coef}, vf_coef={vf_coef}, "
f"windows={windows}, trading_fees={trading_fees}, borrow_interest_rate={borrow_interest_rate}")
# Feature selection hyperparameters
# Note: 'close' price is always included as it's essential for trading
# Other features are optional and will be optimized by Optuna
feature_config = {
# Basic OHLCV features (volume, high, low, open are optional)
'use_volume': trial.suggest_categorical('use_volume', [True, False]),
'use_high': trial.suggest_categorical('use_high', [True, False]),
'use_low': trial.suggest_categorical('use_low', [True, False]),
'use_open': trial.suggest_categorical('use_open', [True, False]),
# Momentum Indicators
# Relative Strength Index
'use_rsi': trial.suggest_categorical('use_rsi', [True, False]),
# Moving Average Convergence Divergence
'use_macd': trial.suggest_categorical('use_macd', [True, False]),
# Stochastic Oscillator %K
'use_stoch_k': trial.suggest_categorical('use_stoch_k', [True, False]),
# Stochastic Oscillator %D
'use_stoch_d': trial.suggest_categorical('use_stoch_d', [True, False]),
# Stochastic Oscillator %J
'use_stoch_j': trial.suggest_categorical('use_stoch_j', [True, False]),
# Trend Indicators
# Simple Moving Average
'use_sma': trial.suggest_categorical('use_sma', [True, False]),
# Exponential Moving Average
'use_ema': trial.suggest_categorical('use_ema', [True, False]),
# Average Directional Index
'use_adx': trial.suggest_categorical('use_adx', [True, False]),
# Volatility Indicators
# Bollinger Bands Upper
'use_bb_upper': trial.suggest_categorical('use_bb_upper', [True, False]),
# Bollinger Bands Lower
'use_bb_lower': trial.suggest_categorical('use_bb_lower', [True, False]),
# Bollinger Bands Middle
'use_bb_middle': trial.suggest_categorical('use_bb_middle', [True, False]),
# Average True Range
'use_atr': trial.suggest_categorical('use_atr', [True, False]),
}
# Create preprocessing function with selected features