How are you folks ensuring your algos are correct? Just running backtests and manually inspecting trades to build confidence?
I have started writing unit tests and it is saving me time. Running the unit tests are much faster than waiting for even a short backtest.
There's an example below. This is a helper function which takes a ln prices and returns whether there were any 15% gaps in the past 90 days.
It produces this one line of output:
TestResults(failed=0, attempted=5)
But if I mess the function up, say I shift the window the wrong way, it gives detailed output like this:
**********************************************************************
File "__main__", line 5, in __main__.jump_screen
Failed example:
jump_screen(pd.DataFrame([[2],[2.31],[2.33]]).applymap(np.log), window=1)
Expected:
0
0 False
1 False
2 True
Got:
0
0 False
1 True
2 False
**********************************************************************
...
Here's the code. The tests are Python code in the docstring:
def jump_screen(ln_series, window=90):
'''
Tests whether ln_series has no +15%, -13% jumps in the past window days.
>>> jump_screen(pd.DataFrame([[2],[2.31],[2.33]]).applymap(np.log), window=1)
0
0 False
1 False
2 True
>>> jump_screen(pd.DataFrame([[2, 1,], [2.6, 1.1]]).applymap(np.log), window=1)
0 1
0 False False
1 False True
>>> jump_screen(pd.DataFrame([[1], [1.1], [1.2], [1.3]]).applymap(np.log), window=1)
0
0 False
1 True
2 True
3 True
>>> jump_screen(pd.DataFrame([[1, 1],
... [1.15, 1],
... [1.15, 1],
... [1.15, 0.86],
... [1.15, 0.86],
... [1.15, 0.86]]).applymap(np.log), window=2)
0 1
0 False False
1 False False
2 False True
3 True False
4 True False
5 True True
'''
ln_delta = ln_series - ln_series.shift()
no_jump = ln_delta.abs() < np.log(1.15)
return no_jump.rolling(window).min().fillna(0.0).astype(bool)
doctest.testmod()
Only downside is that it is pretty verbose.
I'm interested in what others are using.