@@ -61,12 +61,12 @@ def test_save_and_open(self):
6161 (0.5 , datetime (2020 , 6 , 30 )),
6262 (0.2 , datetime (2020 , 3 , 31 ))
6363 ],
64- growth = 0 .0 ,
65- growth_percentage = 0 .0 ,
66- total_net_gain = 0 .0 ,
67- total_net_gain_percentage = 0 .0 ,
68- final_value = 0 .0 ,
69- cagr = 0 .0 ,
64+ growth = 1 .0 ,
65+ growth_percentage = 1 .0 ,
66+ total_net_gain = 1 .0 ,
67+ total_net_gain_percentage = 1 .0 ,
68+ final_value = 1 .0 ,
69+ cagr = 1 .0 ,
7070 sharpe_ratio = 0.0 ,
7171 rolling_sharpe_ratio = [
7272 (0.0 , datetime (2020 , 1 , 1 )),
@@ -280,24 +280,79 @@ def test_save_and_open(self):
280280 backtest = Backtest (
281281 backtest_runs = [backtest_run ],
282282 backtest_permutation_tests = [permutation_test_metrics ],
283- backtest_summary = backtest_metrics
283+ backtest_summary = backtest_metrics ,
284+ metadata = {"strategy" : "test_strategy" },
285+ risk_free_rate = 0.02
284286 )
285287
286288 # Save the backtest
287289 backtest .save (self .dir_path )
288290 self .assertTrue ((self .dir_path / "runs" ).exists ())
289291
290- # Check that there is atleast one results file
292+ # Check that there is at least one results file
291293 results_dir = (self .dir_path / "runs" )
292294 self .assertEqual (1 , (len (os .listdir (results_dir ))))
293295
296+ # Get the first run
297+ first_run_dir = results_dir / os .listdir (results_dir )[0 ]
298+ self .assertTrue ((first_run_dir / "run.json" ).exists ())
299+ self .assertTrue ((first_run_dir / "metrics.json" ).exists ())
300+
294301 # Check that there is a permutation tests directory
295302 self .assertTrue ((self .dir_path / "permutation_tests" ).exists ())
296303
297304 # Check if there are 2 permutated_metrics folders
298305 permutated_metrics_dir = (self .dir_path / "permutation_tests" )
299306
300307 self .assertEqual (1 , (len (os .listdir (permutated_metrics_dir ))))
308+ self .assertTrue ((self .dir_path / "metadata.json" ).exists ())
309+ self .assertTrue ((self .dir_path / "summary.json" ).exists ())
310+
311+ loaded_backtest = Backtest .open (self .dir_path )
312+ self .assertEqual (
313+ len (loaded_backtest .get_all_backtest_runs ()), 1
314+ )
315+ self .assertEqual (
316+ len (loaded_backtest .get_all_backtest_permutation_tests ()),
317+ 1
318+ )
319+
320+ first_backtest_run = loaded_backtest .get_all_backtest_runs ()[0 ]
321+ self .assertEqual (
322+ first_backtest_run .trading_symbol , "EUR"
323+ )
324+ self .assertEqual (
325+ first_backtest_run .backtest_start_date ,
326+ datetime (2020 , 1 , 1 )
327+ )
328+ self .assertEqual (
329+ first_backtest_run .backtest_end_date ,
330+ datetime (2020 , 12 , 31 )
331+ )
332+ self .assertEqual (
333+ first_backtest_run .initial_unallocated , 1000.0
334+ )
335+ self .assertEqual (
336+ len (first_backtest_run .portfolio_snapshots ), 2
337+ )
338+ self .assertEqual (
339+ len (first_backtest_run .trades ), 1
340+ )
341+ self .assertEqual (
342+ len (first_backtest_run .orders ), 1
343+ )
344+ self .assertEqual (
345+ len (first_backtest_run .positions ), 1
346+ )
347+
348+ backtest_metrics = first_backtest_run .backtest_metrics
349+ self .assertIsInstance (backtest_metrics , BacktestMetrics )
350+ self .assertEqual (backtest_metrics .cagr , 1.0 )
351+ self .assertEqual (backtest_metrics .sharpe_ratio , 0.0 )
352+ self .assertEqual (backtest_metrics .sortino_ratio , 0.0 )
353+ self .assertEqual (backtest_metrics .calmar_ratio , 0.0 )
354+ self .assertEqual (backtest_metrics .profit_factor , 0.0 )
355+ self .assertEqual (backtest_metrics .annual_volatility , 0.0 )
301356
302357 def test_get_backtest_run (self ):
303358 pass
0 commit comments