@@ -88,18 +88,23 @@ class BacktestRun:
8888
8989 def to_dict (self ) -> dict :
9090 """
91- Convert the Backtest instance to a dictionary.
92-
93- Returns:
94- dict: A dictionary representation of the Backtest instance.
91+ Convert the Backtest instance to a dictionary with all
92+ date/datetime fields as ISO strings (always UTC).
9593 """
94+ def ensure_iso (value ):
95+ if hasattr (value , "isoformat" ):
96+ if value .tzinfo is None :
97+ value = value .replace (tzinfo = timezone .utc )
98+ return value .isoformat ()
99+ return value
100+
96101 backtest_metrics = self .backtest_metrics .to_dict () \
97102 if self .backtest_metrics else None
98103 return {
99104 "backtest_metrics" : backtest_metrics ,
100- "backtest_start_date" : self .backtest_start_date ,
105+ "backtest_start_date" : ensure_iso ( self .backtest_start_date ) ,
101106 "backtest_date_range_name" : self .backtest_date_range_name ,
102- "backtest_end_date" : self .backtest_end_date ,
107+ "backtest_end_date" : ensure_iso ( self .backtest_end_date ) ,
103108 "trading_symbol" : self .trading_symbol ,
104109 "initial_unallocated" : self .initial_unallocated ,
105110 "number_of_runs" : self .number_of_runs ,
@@ -109,7 +114,7 @@ def to_dict(self) -> dict:
109114 "trades" : [trade .to_dict () for trade in self .trades ],
110115 "orders" : [order .to_dict () for order in self .orders ],
111116 "positions" : [position .to_dict () for position in self .positions ],
112- "created_at" : self .created_at ,
117+ "created_at" : ensure_iso ( self .created_at ) ,
113118 "symbols" : self .symbols ,
114119 "number_of_days" : self .number_of_days ,
115120 "number_of_trades" : self .number_of_trades ,
0 commit comments