@@ -513,6 +513,7 @@ def test_optimize(self):
513513 self .assertRaises (TypeError , bt .optimize , maximize = 15 , ** OPT_PARAMS )
514514 self .assertRaises (TypeError , bt .optimize , constraint = 15 , ** OPT_PARAMS )
515515 self .assertRaises (ValueError , bt .optimize , constraint = lambda d : False , ** OPT_PARAMS )
516+ self .assertRaises (ValueError , bt .optimize , return_optimization = True , ** OPT_PARAMS )
516517
517518 res = bt .optimize (** OPT_PARAMS )
518519 self .assertIsInstance (res , pd .Series )
@@ -531,6 +532,40 @@ def test_optimize(self):
531532 with _tempfile () as f :
532533 bt .plot (filename = f , open_browser = False )
533534
535+ def test_method_skopt (self ):
536+ bt = Backtest (GOOG .iloc [:100 ], SmaCross )
537+ res , heatmap , skopt_results = bt .optimize (
538+ fast = range (2 , 20 ), slow = np .arange (2 , 20 , dtype = object ),
539+ constraint = lambda p : p .fast < p .slow ,
540+ max_tries = 30 ,
541+ method = 'skopt' ,
542+ return_optimization = True ,
543+ return_heatmap = True ,
544+ random_state = 2 )
545+ self .assertIsInstance (res , pd .Series )
546+ self .assertIsInstance (heatmap , pd .Series )
547+ self .assertGreater (heatmap .max (), 1.1 )
548+ self .assertGreater (heatmap .min (), - 2 )
549+ self .assertEqual (- skopt_results .fun , heatmap .max ())
550+ self .assertEqual (heatmap .index .tolist (), heatmap .dropna ().index .unique ().tolist ())
551+
552+ def test_max_tries (self ):
553+ bt = Backtest (GOOG .iloc [:100 ], SmaCross )
554+ OPT_PARAMS = dict (fast = range (2 , 10 , 2 ), slow = [2 , 5 , 7 , 9 ])
555+ for method , max_tries , random_state in (('grid' , 5 , 2 ),
556+ ('grid' , .3 , 2 ),
557+ ('skopt' , 7 , 0 ),
558+ ('skopt' , .45 , 0 )):
559+ with self .subTest (method = method ,
560+ max_tries = max_tries ,
561+ random_state = random_state ):
562+ _ , heatmap = bt .optimize (max_tries = max_tries ,
563+ method = method ,
564+ random_state = random_state ,
565+ return_heatmap = True ,
566+ ** OPT_PARAMS )
567+ self .assertEqual (len (heatmap ), 6 )
568+
534569 def test_nowrite_df (self ):
535570 # Test we don't write into passed data df by default.
536571 # Important for copy-on-write in Backtest.optimize()
0 commit comments