Skip to content

Commit d39034b

Browse files
committed
testing test_lookahead_bias
1 parent 7ad354e commit d39034b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

tests/test_factor.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ def test_factors(self):
4343
df_aapl_open = df.loc[(slice(None), 'AAPL'), 'open']
4444
df_msft_open = df.loc[(slice(None), 'MSFT'), 'open']
4545

46-
def test_expected(factor, _expected_aapl, _expected_msft, _len=8, decimal=7, delay=True):
46+
def test_expected(factor, _expected_aapl, _expected_msft, _len=8, decimal=7,
47+
delay=True, check_bias=True):
4748
engine.remove_all_factors()
4849
engine.add(factor, 'test')
4950
_result = engine.run('2019-01-01', '2019-01-15', delay)
51+
if check_bias:
52+
engine.test_lookahead_bias('2019-01-01', '2019-01-15')
5053
result_aapl = _result.loc[(slice(None), 'AAPL'), 'test'].values
5154
result_msft = _result.loc[(slice(None), 'MSFT'), 'test'].values
5255
assert_almost_equal(result_aapl[-_len:], _expected_aapl[-_len:], decimal=decimal)
@@ -142,9 +145,12 @@ def test_expected(factor, _expected_aapl, _expected_msft, _len=8, decimal=7, del
142145

143146
expected_aapl = [149, 149, 151.55, 156.03, 161, 153.69, 157, 156.94, np.nan]
144147
expected_msft = [104.39, 103.2, 105.22, 106, 103.2, 103.39, 108.85, np.nan]
145-
# bcuz down is shift(-2) then shift(1)
148+
# bcuz delay=True, so it is shift(-2) and then shift(1)
149+
self.assertRaisesRegex(RuntimeError, '.*look-ahead bias.*', test_expected,
150+
spectre.factors.OHLCV.close.shift(-2), expected_aapl, expected_msft,
151+
total_rows)
146152
test_expected(spectre.factors.OHLCV.close.shift(-2),
147-
expected_aapl, expected_msft, total_rows)
153+
expected_aapl, expected_msft, total_rows, check_bias=False)
148154

149155
# test zscore and shift, mask bug
150156
expected_aapl = [1.] * 10

0 commit comments

Comments
 (0)