@@ -43,10 +43,13 @@ def test_factors(self):
43
43
df_aapl_open = df .loc [(slice (None ), 'AAPL' ), 'open' ]
44
44
df_msft_open = df .loc [(slice (None ), 'MSFT' ), 'open' ]
45
45
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 ):
47
48
engine .remove_all_factors ()
48
49
engine .add (factor , 'test' )
49
50
_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' )
50
53
result_aapl = _result .loc [(slice (None ), 'AAPL' ), 'test' ].values
51
54
result_msft = _result .loc [(slice (None ), 'MSFT' ), 'test' ].values
52
55
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
142
145
143
146
expected_aapl = [149 , 149 , 151.55 , 156.03 , 161 , 153.69 , 157 , 156.94 , np .nan ]
144
147
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 )
146
152
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 )
148
154
149
155
# test zscore and shift, mask bug
150
156
expected_aapl = [1. ] * 10
0 commit comments