Skip to content

Commit

Permalink
Merge pull request #27 from joshyattridge:test_and_fix_previous_highs…
Browse files Browse the repository at this point in the history
…_lows

fixed previous_high_low function
  • Loading branch information
joshyattridge authored May 9, 2024
2 parents f24cfbc + aa745e8 commit 6123881
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions smartmoneyconcepts/smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6123881

Please sign in to comment.