Skip to content

Commit

Permalink
improved highs_lows function by replacing the thresh with a percentag…
Browse files Browse the repository at this point in the history
…e, and fixxed liquidity function
  • Loading branch information
joshua attridge authored and joshua attridge committed Oct 5, 2023
1 parent cef13d7 commit 09e25ba
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
15 changes: 13 additions & 2 deletions build/lib/smartmoneyconcepts/smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def fvg(cls, ohlc: DataFrame) -> Series:

mitigated_index = np.zeros(len(ohlc), dtype=np.int32)
for i in np.where(fvg != 0)[0]:
mask = np.zeros(len(ohlc), dtype=np.bool)
if fvg[i] == 1:
mask = ohlc["low"][i + 2 :] <= top[i]
elif fvg[i] == -1:
Expand Down Expand Up @@ -150,12 +151,19 @@ def ob(cls, ohlc: DataFrame) -> Series:
fvg = cls.fvg(ohlc)

ob = np.where((fvg["FVG"].shift(-1) != 0) & (fvg["FVG"] == 0), fvg["FVG"].shift(-1), 0)
top = np.where((fvg["FVG"].shift(-1) == -1) & (ohlc["high"] < ohlc["high"].shift(-1)), ohlc["high"].shift(-1), ohlc["high"])
bottom = np.where((fvg["FVG"].shift(-1) == 1) & (ohlc["low"] > ohlc["low"].shift(-1)), ohlc["low"].shift(-1), ohlc["low"])
# top is equal to the current candles high unless the ob is -1 and the next candles high is higher than the current candles high then top is equal to the next candles high
top = np.where(
(ob == -1) & (ohlc["high"].shift(-1) > ohlc["high"]), ohlc["high"].shift(-1), ohlc["high"]
)
# bottom is equal to the current candles low unless the ob is 1 and the next candles low is lower than the current candles low then bottom is equal to the next candles low
bottom = np.where(
(ob == 1) & (ohlc["low"].shift(-1) < ohlc["low"]), ohlc["low"].shift(-1), ohlc["low"]
)

# set mitigated to np.nan
mitigated_index = np.zeros(len(ohlc), dtype=np.int32)
for i in np.where(ob != 0)[0]:
mask = np.zeros(len(ohlc), dtype=np.bool)
if ob[i] == 1:
mask = ohlc["low"][i + 2 :] <= top[i]
elif ob[i] == -1:
Expand Down Expand Up @@ -261,3 +269,6 @@ def liquidity(cls, ohlc: DataFrame, range_percent=0.01, up_thresh=0.05, down_thr
return pd.concat(
[liquidity, buy_sell_side, level, liquidity_end, liquidity_swept], axis=1
)


# TODO: correct mask error for ob and fvg
Binary file removed dist/smartmoneyconcepts-0.0.8-py3-none-any.whl
Binary file not shown.
Binary file removed dist/smartmoneyconcepts-0.0.8.tar.gz
Binary file not shown.
Binary file added dist/smartmoneyconcepts-0.0.9-py3-none-any.whl
Binary file not shown.
Binary file added dist/smartmoneyconcepts-0.0.9.tar.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion smartmoneyconcepts.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: smartmoneyconcepts
Version: 0.0.8
Version: 0.0.9
Summary: Getting indicators based on smart money concepts or ICT
Home-page: https://github.com/joshyattridge/smartmoneyconcepts
Author: Joshua Attridge
Expand Down
26 changes: 11 additions & 15 deletions smartmoneyconcepts/SMC.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ def fvg(cls, ohlc: DataFrame) -> Series:
)

@classmethod
def highs_lows(cls, ohlc: DataFrame, up_thresh=0.05, down_thresh=-0.05) -> Series:
highs_lows = peak_valley_pivots(ohlc["close"], up_thresh, down_thresh)
def highs_lows(cls, ohlc: DataFrame, percentage_thresh=0.05) -> Series:
# subtract the highest high from the lowest low
pip_range = max(ohlc["high"]) - min(ohlc["low"])
pip_range = pip_range * percentage_thresh
highs_lows = peak_valley_pivots(ohlc["close"], pip_range, -pip_range)
still_adjusting = True
while still_adjusting:
still_adjusting = False
Expand Down Expand Up @@ -183,7 +186,7 @@ def ob(cls, ohlc: DataFrame) -> Series:
)

@classmethod
def liquidity(cls, ohlc: DataFrame, range_percent=0.01, up_thresh=0.05, down_thresh=-0.05) -> Series:
def liquidity(cls, ohlc: DataFrame, range_percent=0.01) -> Series:
"""
Liquidity
Liquidity is when there are multiply highs within a small range of each other.
Expand All @@ -194,14 +197,13 @@ def liquidity(cls, ohlc: DataFrame, range_percent=0.01, up_thresh=0.05, down_thr
pip_range = (max(ohlc["high"]) - min(ohlc["low"])) * range_percent

# get the highs and lows
highs_lows = cls.highs_lows(ohlc, up_thresh, down_thresh)
highs_lows = cls.highs_lows(ohlc)
levels = highs_lows["Levels"]
highs_lows = highs_lows["HighsLows"]

# go through all of the high levels and if there are more than 1 within the pip range, then it is liquidity
liquidity = np.zeros(len(ohlc), dtype=np.int32)
buy_sell_side = np.zeros(len(ohlc), dtype=np.int32)
liquidity_level = np.zeros(len(ohlc), dtype=np.int32)
liquidity_level = np.zeros(len(ohlc), dtype=np.float32)
liquidity_end = np.zeros(len(ohlc), dtype=np.int32)
liquidity_swept = np.zeros(len(ohlc), dtype=np.int32)

Expand All @@ -227,7 +229,6 @@ def liquidity(cls, ohlc: DataFrame, range_percent=0.01, up_thresh=0.05, down_thr
temp_liquidity_levels
)
liquidity[i] = 1
buy_sell_side[i] = 2 # 2 is buy
liquidity_level[i] = average_high
liquidity_end[i] = end
liquidity_swept[i] = swept
Expand All @@ -254,21 +255,16 @@ def liquidity(cls, ohlc: DataFrame, range_percent=0.01, up_thresh=0.05, down_thr
average_low = sum(temp_liquidity_levels) / len(
temp_liquidity_levels
)
liquidity[i] = 1
buy_sell_side[i] = 1
liquidity[i] = -1
liquidity_level[i] = average_low
liquidity_end[i] = end
liquidity_swept[i] = swept

liquidity = pd.Series(liquidity, name="Liquidity")
buy_sell_side = pd.Series(buy_sell_side, name="BuySellSide")
level = pd.Series(liquidity_level, name="Level")
liquidity_end = pd.Series(liquidity_end, name="End")
liquidity_swept = pd.Series(liquidity_swept, name="Swept")

return pd.concat(
[liquidity, buy_sell_side, level, liquidity_end, liquidity_swept], axis=1
)


# TODO: correct mask error for ob and fvg
[liquidity, level, liquidity_end, liquidity_swept], axis=1
)
Binary file modified smartmoneyconcepts/__pycache__/SMC.cpython-39.pyc
Binary file not shown.
12 changes: 6 additions & 6 deletions tests/SMC.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from smartmoneyconcepts.smc import smc

df = pd.read_csv("EURUSD_15M.csv")
df = df.iloc[-200:]
df = df.iloc[-5000:]
df = df.reset_index(drop=True)
fig = go.Figure(
data=[
Expand Down Expand Up @@ -48,7 +48,7 @@ def add_FVG(fig):
return fig

def add_highs_lows(fig):
highs_lows_data = smc.highs_lows(df, up_thresh=0.0005, down_thresh=-0.0005)
highs_lows_data = smc.highs_lows(df)

# remove from highs_lows_data
indexs = []
Expand Down Expand Up @@ -104,7 +104,7 @@ def add_liquidity(fig):

# draw a line horizontally for each liquidity level
for i in range(len(liquidity_data["Liquidity"])):
if liquidity_data["Liquidity"][i] == 1:
if liquidity_data["Liquidity"][i] != 0:
fig.add_trace(
go.Scatter(
x=[df["date"][i], df["date"][liquidity_data["End"][i]]],
Expand All @@ -127,7 +127,7 @@ def add_liquidity(fig):
liquidity_data["Level"][i],
(
df["high"][liquidity_data["Swept"][i]]
if liquidity_data["BuySellSide"][i] == 2
if liquidity_data["Liquidity"][i] == 1
else df["low"][liquidity_data["Swept"][i]]
),
],
Expand All @@ -140,8 +140,8 @@ def add_liquidity(fig):
return fig


fig = add_FVG(fig)
# fig = add_FVG(fig)
fig = add_highs_lows(fig)
fig = add_OB(fig)
# fig = add_OB(fig)
fig = add_liquidity(fig)
fig.show()

0 comments on commit 09e25ba

Please sign in to comment.