From cb2432ef7cadf7c42180a1c6148f9f9be2f5fdf5 Mon Sep 17 00:00:00 2001 From: Cedric Zhuang Date: Sat, 24 Jan 2026 12:12:04 -0800 Subject: [PATCH] Fix _tp() to always return pandas Series When amount column is present, _tp() was returning a numpy array instead of a pandas Series, causing inconsistent behavior in downstream indicators like MFI. Now both code paths consistently return a Series via to_series(). Co-Authored-By: Claude Opus 4.5 --- README.md | 2 +- stockstats.py | 10 ++++++---- test.py | 4 ++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 69869418..4386c393 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![codecov](https://codecov.io/gh/jealous/stockstats/branch/master/graph/badge.svg?token=IFMD1pVJ7T)](https://codecov.io/gh/jealous/stockstats) [![pypi](https://img.shields.io/pypi/v/stockstats.svg)](https://pypi.python.org/pypi/stockstats) -VERSION: 0.6.6 +VERSION: 0.6.7 ## Introduction diff --git a/stockstats.py b/stockstats.py index 278f9d35..df9a8fa7 100644 --- a/stockstats.py +++ b/stockstats.py @@ -969,11 +969,13 @@ def _shifted_cr_sma(self, cr, window): cr_sma = self.sma(cr, window) return self.s_shift(cr_sma, -int(window / 2.5 + 1)) - def _tp(self): + def _tp(self) -> pd.Series: if "amount" in self: - return self.amount.values / self.volume.values - total = self.close.values + self.high.values + self.low.values - return self.to_series(total / 3.0) + tp = self.amount.values / self.volume.values + else: + total = self.close.values + self.high.values + self.low.values + tp = total / 3.0 + return self.to_series(tp) def _get_tp(self, meta: _Meta): self[meta.name] = self._tp() diff --git a/test.py b/test.py index 96080f83..02de2f83 100644 --- a/test.py +++ b/test.py @@ -728,6 +728,10 @@ def test_mfi(self): assert_that(mfi_15.loc[first], near_to(0.5)) assert_that(mfi_15.loc[last], near_to(0.6733)) + def test_mfi_with_amount(self): + mfi = self._supor['mfi'] + assert_that(mfi.loc[20160817], near_to(0.48265)) + def test_ker(self): stock = self.get_stock_90days() k = stock["ker"]