From aa745e8876b7aaf38ed04739a0bd2559328923fe Mon Sep 17 00:00:00 2001 From: Joshua Attridge Date: Thu, 9 May 2024 21:04:39 +0100 Subject: [PATCH] fixed previous_high_low function --- smartmoneyconcepts/smc.py | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/smartmoneyconcepts/smc.py b/smartmoneyconcepts/smc.py index 8f7bf66..2f37638 100644 --- a/smartmoneyconcepts/smc.py +++ b/smartmoneyconcepts/smc.py @@ -678,33 +678,21 @@ def previous_high_low(cls, ohlc: DataFrame, time_frame: str = "1D") -> Series: ohlc.index = pd.to_datetime(ohlc.index) - resampled_ohlc = ohlc.resample(time_frame).agg( - { - "open": "first", - "high": "max", - "low": "min", - "close": "last", - "volume": "sum", - } - ) - - # for every candle in ohlc add a new column with the previous high and low - # Fix: Import the datetime module previous_high = np.zeros(len(ohlc), dtype=np.float32) previous_low = np.zeros(len(ohlc), dtype=np.float32) for i in range(len(ohlc)): - current_time = ohlc.index[i] - # get the 1st high where the current time is greater than the time from the resampled ohlc - previous_high_index = resampled_ohlc["high"].where( - resampled_ohlc.index < current_time - ).last_valid_index() - previous_high[i] = resampled_ohlc["high"][previous_high_index] if previous_high_index is not None else np.nan - # get the 1st low where the current time is greater than the time from the resampled ohlc - previous_low_index = resampled_ohlc["low"].where( - resampled_ohlc.index < current_time - ).last_valid_index() - previous_low[i] = resampled_ohlc["low"][previous_low_index] if previous_low_index is not None else np.nan + resampled_ohlc = ohlc[:i].resample(time_frame).agg( + { + "open": "first", + "high": "max", + "low": "min", + "close": "last", + "volume": "sum", + } + ) + previous_high[i] = resampled_ohlc["high"][-2] if len(resampled_ohlc) > 1 else np.nan + previous_low[i] = resampled_ohlc["low"][-2] if len(resampled_ohlc) > 1 else np.nan previous_high = pd.Series(previous_high, name="PreviousHigh") previous_low = pd.Series(previous_low, name="PreviousLow")