|
18 | 18 |
|
19 | 19 | from backtesting import Backtest, Strategy
|
20 | 20 | from backtesting._stats import compute_drawdown_duration_peaks
|
| 21 | +from backtesting._util import _Array, _as_str, _Indicator, try_ |
21 | 22 | from backtesting.lib import (
|
22 | 23 | OHLCV_AGG,
|
| 24 | + SignalStrategy, |
| 25 | + TrailingStrategy, |
23 | 26 | barssince,
|
24 | 27 | compute_stats,
|
25 | 28 | cross,
|
26 | 29 | crossover,
|
27 |
| - quantile, |
28 |
| - SignalStrategy, |
29 |
| - TrailingStrategy, |
30 |
| - resample_apply, |
31 | 30 | plot_heatmaps,
|
| 31 | + quantile, |
32 | 32 | random_ohlc_data,
|
| 33 | + resample_apply, |
33 | 34 | )
|
34 |
| -from backtesting.test import GOOG, EURUSD, SMA |
35 |
| -from backtesting._util import _Indicator, _as_str, _Array, try_ |
| 35 | +from backtesting.test import EURUSD, GOOG, SMA |
36 | 36 |
|
37 | 37 | SHORT_DATA = GOOG.iloc[:20] # Short data for fast tests with no indicator lag
|
38 | 38 |
|
@@ -146,7 +146,7 @@ def init(self):
|
146 | 146 |
|
147 | 147 | assert float(self.data.Close) == self.data.Close[-1]
|
148 | 148 |
|
149 |
| - def next(self, FIVE_DAYS=pd.Timedelta('3 days')): |
| 149 | + def next(self, _FEW_DAYS=pd.Timedelta('3 days')): # noqa: N803 |
150 | 150 | assert self.equity >= 0
|
151 | 151 |
|
152 | 152 | assert isinstance(self.sma, _Indicator)
|
@@ -193,7 +193,7 @@ def next(self, FIVE_DAYS=pd.Timedelta('3 days')):
|
193 | 193 | assert self.position.size < 0
|
194 | 194 |
|
195 | 195 | trade = self.trades[0]
|
196 |
| - if self.data.index[-1] - self.data.index[trade.entry_bar] > FIVE_DAYS: |
| 196 | + if self.data.index[-1] - self.data.index[trade.entry_bar] > _FEW_DAYS: |
197 | 197 | assert not trade.is_long
|
198 | 198 | assert trade.is_short
|
199 | 199 | assert trade.size < 0
|
@@ -290,7 +290,7 @@ def almost_equal(a, b):
|
290 | 290 | except TypeError:
|
291 | 291 | return a == b
|
292 | 292 |
|
293 |
| - diff = {key: print(key) or value |
| 293 | + diff = {key: print(key) or value # noqa: T201 |
294 | 294 | for key, value in stats.filter(regex='^[^_]').items()
|
295 | 295 | if not almost_equal(value, expected[key])}
|
296 | 296 | self.assertDictEqual(diff, {})
|
@@ -510,7 +510,7 @@ def coroutine(self):
|
510 | 510 | class TestOptimize(TestCase):
|
511 | 511 | def test_optimize(self):
|
512 | 512 | bt = Backtest(GOOG.iloc[:100], SmaCross)
|
513 |
| - OPT_PARAMS = dict(fast=range(2, 5, 2), slow=[2, 5, 7, 9]) |
| 513 | + OPT_PARAMS = {'fast': range(2, 5, 2), 'slow': [2, 5, 7, 9]} |
514 | 514 |
|
515 | 515 | self.assertRaises(ValueError, bt.optimize)
|
516 | 516 | self.assertRaises(ValueError, bt.optimize, maximize='missing key', **OPT_PARAMS)
|
@@ -556,7 +556,7 @@ def test_method_skopt(self):
|
556 | 556 |
|
557 | 557 | def test_max_tries(self):
|
558 | 558 | bt = Backtest(GOOG.iloc[:100], SmaCross)
|
559 |
| - OPT_PARAMS = dict(fast=range(2, 10, 2), slow=[2, 5, 7, 9]) |
| 559 | + OPT_PARAMS = {'fast': range(2, 10, 2), 'slow': [2, 5, 7, 9]} |
560 | 560 | for method, max_tries, random_state in (('grid', 5, 0),
|
561 | 561 | ('grid', .3, 0),
|
562 | 562 | ('skopt', 7, 0),
|
@@ -589,7 +589,7 @@ def init(self):
|
589 | 589 |
|
590 | 590 | def test_multiprocessing_windows_spawn(self):
|
591 | 591 | df = GOOG.iloc[:100]
|
592 |
| - kw = dict(fast=[10]) |
| 592 | + kw = {'fast': [10]} |
593 | 593 |
|
594 | 594 | stats1 = Backtest(df, SmaCross).optimize(**kw)
|
595 | 595 | with patch('multiprocessing.get_start_method', lambda **_: 'spawn'):
|
@@ -633,7 +633,7 @@ def test_params(self):
|
633 | 633 | bt = Backtest(GOOG.iloc[:100], SmaCross)
|
634 | 634 | bt.run()
|
635 | 635 | with _tempfile() as f:
|
636 |
| - for p in dict(plot_volume=False, |
| 636 | + for p in dict(plot_volume=False, # noqa: C408 |
637 | 637 | plot_equity=False,
|
638 | 638 | plot_return=True,
|
639 | 639 | plot_pl=False,
|
@@ -722,8 +722,8 @@ def next(self):
|
722 | 722 | self.assertEqual(stats['Equity Final [$]'], 0)
|
723 | 723 | self.assertEqual(len(trades), 2)
|
724 | 724 | assert trades[['EntryTime', 'ExitTime']].equals(
|
725 |
| - pd.DataFrame(dict(EntryTime=pd.to_datetime(['2006-11-01', '2008-11-14']), |
726 |
| - ExitTime=pd.to_datetime(['2007-10-31', '2009-09-21'])))) |
| 725 | + pd.DataFrame({'EntryTime': pd.to_datetime(['2006-11-01', '2008-11-14']), |
| 726 | + 'ExitTime': pd.to_datetime(['2007-10-31', '2009-09-21'])})) |
727 | 727 | assert trades['PnL'].round().equals(pd.Series([23469., -34420.]))
|
728 | 728 |
|
729 | 729 | with _tempfile() as f:
|
|
0 commit comments