Skip to content

Fix ftr and sym_wma4 crashing on data shorter than the window - #207

Open
wavebyrd wants to merge 1 commit into
jealous:masterfrom
wavebyrd:fix/short-frame-ftr-sym-wma4
Open

wavebyrd wants to merge 1 commit into
jealous:masterfrom
wavebyrd:fix/short-frame-ftr-sym-wma4

Conversation

@wavebyrd

Copy link
Copy Markdown

Since #200 (numpy rewrite), ftr and the RVGI columns raise on data with fewer rows than their window:

df = StockDataFrame.retype(ohlcv.head(3))  # any OHLCV frame with 3 rows
df["ftr"]   # ValueError: window shape cannot be larger than input array shape
df["rvgi"]  # ValueError: could not broadcast input array from shape (2,) into shape (0,)
StockDataFrame.sym_wma4(pd.Series([4, 2, 2]))  # same broadcast error
  • _ftr passes the prices to np.lib.stride_tricks.sliding_window_view without checking that there are at least window of them.
  • _sym_wma4 assigns np.convolve(..., mode="valid") to out[3:]. With fewer than 4 values, numpy swaps the arguments and returns 5 - n values, which don't fit.

Before #200 both returned zeros in this case. The other windowed helpers (_mad, linear_wma, linear_reg, _inertia) already return early on short input, so this adds a guard to these two. Results for data longer than the window are unchanged.

Tests: added test_ftr_shorter_than_window, test_rvgi_shorter_than_window and test_sym_wma4_shorter_than_window to test.py. They fail with ValueError on master and pass with the fix. The full suite passes (135), and flake8 stockstats.py test.py is clean.

Since the numpy rewrite, _ftr calls sliding_window_view and _sym_wma4 assigns a 'valid' convolution without checking the length, so ftr, rvgi/rvgis and sym_wma4 raise ValueError when there are fewer rows than the window. Return zeros in that case, as before the rewrite; the other windowed helpers already guard short input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant