This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
stockstats is a Python library that provides a StockDataFrame wrapper around pandas.DataFrame with inline stock statistics/indicators support. Users access indicators by column name (e.g., df['macd'], df['rsi_14']), and values are calculated on-demand.
# Install dependencies
pip install -r requirements.txt
pip install -r test-requirements.txt
# Run all tests with coverage
pytest --cov=stockstats test.py
# Run a single test
pytest test.py::StockDataFrameTest::test_get_rsi -v
# Run linting
flake8 stockstats.py test.py
# Run tox (for multi-version testing)
toxThe library is a single-file module with these key components:
StockDataFrame class (extends pd.DataFrame):
- Overrides
__getitem__to intercept column access and auto-calculate indicators - Uses
__init_column/__init_not_exist_columnto trigger calculation when a column doesn't exist - Handler methods named
_get_<indicator>compute each indicator (e.g.,_get_rsi,_get_macd)
_Meta class: Parses column names like close_20_sma into components (column, window, indicator name) and provides access to default windows.
Column naming patterns:
<indicator>- uses default column and window (e.g.,rsi,macd)<indicator>_<window>- uses default column with custom window (e.g.,rsi_6)<column>_<window>_<indicator>- full specification (e.g.,close_20_sma)<column>_delta- difference from previous row (e.g.,close_delta)<left>_x_<right>/<left>_xu_<right>/<left>_xd_<right>- cross/cross-up/cross-down between two columns<left>_le_<right>,_ge_,_lt_,_gt_,_eq_,_ne_- comparison operators between columns
Column resolution order (in __init_not_exist_column):
- Check the
handlerproperty map (multi-column indicators like macd/macds/macdh) - Check for
_deltasuffix - Check for cross patterns (
_x_,_xu_,_xd_) - Check for comparison patterns (
_le_,_ge_, etc.) - Parse as
(column, window, name)and dispatch to_get_<name>handler
Configuration:
_dft_windowsdict: default window sizes for each indicatorset_dft_window(name, value): modify defaults at runtime- Class-level constants like
BOLL_STD_TIMES,KDJ_PARAMfor indicator tuning
- Add default window to
_dft_windowsdict if needed - Add default column to
_dft_columndict if needed - Create
_get_<name>(self, meta: _Meta)method - For multi-column indicators, add entry to
handlerproperty mapping tuple of names to handler
Uses PyHamcrest matchers. Test data files are in test_data/. The YFinanceCompatibilityTest class downloads live data from Yahoo Finance.
- Run
flake8 stockstats.py test.pybefore each commit to ensure code style compliance - Use
git commit --amendfor subsequent changes within a single PR to keep history clean