MCPcopy Index your code
hub / github.com/kernc/backtesting.py / test_optimize

Method test_optimize

backtesting/test/_test.py:577–604  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

575
576class TestOptimize(TestCase):
577 def test_optimize(self):
578 bt = Backtest(GOOG.iloc[:100], SmaCross)
579 OPT_PARAMS = {'fast': range(2, 5, 2), 'slow': [2, 5, 7, 9]}
580
581 self.assertRaises(ValueError, bt.optimize)
582 self.assertRaises(ValueError, bt.optimize, maximize='missing key', **OPT_PARAMS)
583 self.assertRaises(ValueError, bt.optimize, maximize='missing key', **OPT_PARAMS)
584 self.assertRaises(TypeError, bt.optimize, maximize=15, **OPT_PARAMS)
585 self.assertRaises(TypeError, bt.optimize, constraint=15, **OPT_PARAMS)
586 self.assertRaises(ValueError, bt.optimize, constraint=lambda d: False, **OPT_PARAMS)
587 self.assertRaises(ValueError, bt.optimize, return_optimization=True, **OPT_PARAMS)
588
589 res = bt.optimize(**OPT_PARAMS)
590 self.assertIsInstance(res, pd.Series)
591
592 default_maximize = inspect.signature(Backtest.optimize).parameters['maximize'].default
593 res2 = bt.optimize(**OPT_PARAMS, maximize=lambda s: s[default_maximize])
594 self.assertDictEqual(res.filter(regex='^[^_]').fillna(-1).to_dict(),
595 res2.filter(regex='^[^_]').fillna(-1).to_dict())
596
597 res3, heatmap = bt.optimize(**OPT_PARAMS, return_heatmap=True,
598 constraint=lambda d: d.slow > 2 * d.fast)
599 self.assertIsInstance(heatmap, pd.Series)
600 self.assertEqual(len(heatmap), 4)
601 self.assertEqual(heatmap.name, default_maximize)
602
603 with _tempfile() as f:
604 bt.plot(filename=f, open_browser=False)
605
606 def test_method_sambo(self):
607 bt = Backtest(GOOG.iloc[:100], SmaCross, finalize_trades=True)

Callers

nothing calls this directly

Calls 4

optimizeMethod · 0.95
plotMethod · 0.95
BacktestClass · 0.90
_tempfileFunction · 0.85

Tested by

no test coverage detected