Skip to content

Commit eb9d9f0

Browse files
Merge pull request #43 from joshyattridge/increase_speed_of_previous_high_low
Increase speed of previous high low function
2 parents 26dd012 + f4f2c0d commit eb9d9f0

9 files changed

+51477
-68
lines changed

smartmoneyconcepts/smc.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -710,32 +710,32 @@ def previous_high_low(cls, ohlc: DataFrame, time_frame: str = "1D") -> Series:
710710
broken_high = np.zeros(len(ohlc), dtype=np.int32)
711711
broken_low = np.zeros(len(ohlc), dtype=np.int32)
712712

713+
resampled_ohlc = ohlc.resample(time_frame).agg(
714+
{
715+
"open": "first",
716+
"high": "max",
717+
"low": "min",
718+
"close": "last",
719+
"volume": "sum",
720+
}
721+
).dropna()
722+
713723
currently_broken_high = False
714724
currently_broken_low = False
715725
last_broken_time = None
716726
for i in range(len(ohlc)):
717-
resampled_ohlc = (
718-
ohlc[:i]
719-
.resample(time_frame)
720-
.agg(
721-
{
722-
"open": "first",
723-
"high": "max",
724-
"low": "min",
725-
"close": "last",
726-
"volume": "sum",
727-
}
728-
)
729-
)
730-
if len(resampled_ohlc) >= 1:
731-
if last_broken_time != resampled_ohlc.index[-1]:
732-
currently_broken_high = False
733-
currently_broken_low = False
734-
last_broken_time = resampled_ohlc.index[-1]
735727
# remove rows with nan values (ignoring weekends)
736-
resampled_ohlc = resampled_ohlc.dropna()
737-
previous_high[i] = resampled_ohlc["high"].iloc[-2] if len(resampled_ohlc) > 1 else np.nan
738-
previous_low[i] = resampled_ohlc["low"].iloc[-2] if len(resampled_ohlc) > 1 else np.nan
728+
resampled_previous_index = np.where(
729+
resampled_ohlc.index < ohlc.index[i]
730+
)[0]
731+
if len(resampled_previous_index) <= 1:
732+
previous_high[i] = np.nan
733+
previous_low[i] = np.nan
734+
continue
735+
resampled_previous_index = resampled_previous_index[-1]
736+
737+
previous_high[i] = resampled_ohlc["high"].iloc[resampled_previous_index]
738+
previous_low[i] = resampled_ohlc["low"].iloc[resampled_previous_index]
739739
currently_broken_high = ohlc["high"].iloc[i] > previous_high[i] or currently_broken_high
740740
currently_broken_low = ohlc["low"].iloc[i] < previous_low[i] or currently_broken_low
741741
broken_high[i] = 1 if currently_broken_high else 0

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def previous_high_low_result_data():
5959
test_instrument = "EURUSD"
6060
return pd.read_csv(
6161
os.path.join(
62-
"tests/test_data", test_instrument, "previous_high_low_result_data.csv"
62+
"tests/test_data", test_instrument, "previous_high_low_result_data_4h.csv"
6363
)
6464
)
6565

0 commit comments

Comments
 (0)