Skip to content

Commit eeb7128

Browse files
Merge pull request #15 from joshyattridge/previous_candles_high_low
Previous candles high low
2 parents a1f5951 + a643d06 commit eeb7128

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The Smart Money Concepts Python Indicator is a sophisticated financial tool developed for traders and investors to gain insights into market sentiment, trends, and potential reversals. This indicator is inspired by Inner Circle Trader (ICT) concepts like Order blocks, Liquidity, Fair Value Gap, Swing Highs and Lows, Break of Structure, Change of Character, and more. Please Take a look and contribute to the project.
44

5-
![alt text](https://github.com/joshyattridge/smart-money-concepts/blob/21656dd807c4077f345b6cbf29b1bc37672628e9/tests/test_binance.png)
5+
![alt text](https://github.com/joshyattridge/smart-money-concepts/blob/53dd0b0a5e598d04b0cd3d10af71a0c252ad850d/tests/test_binance.png)
66

77
## Installation
88

@@ -109,6 +109,21 @@ Level = the level of the liquidity<br>
109109
End = the index of the last liquidity level<br>
110110
Swept = the index of the candle that swept the liquidity<br>
111111

112+
### Previous High And Low
113+
114+
```python
115+
smc.previous_high_low(ohlc, time_frame = "1D")
116+
```
117+
118+
This method returns the previous high and low of the given time frame.
119+
120+
parameters:<br>
121+
time_frame: str - the time frame to get the previous high and low 15m, 1H, 4H, 1D, 1W, 1M<br>
122+
123+
returns:<br>
124+
PreviousHigh = the previous high<br>
125+
PreviousLow = the previous low<br>
126+
112127
## Contributing
113128

114129
This project is still in BETA so please feel free to contribute to the project. By creating your own indicators or improving the existing ones. If you are stuggling to find something to do then please check out the issues tab for requested changes.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import codecs
33
import os
44

5-
VERSION = '0.0.15'
5+
VERSION = '0.0.16'
66
DESCRIPTION = 'Getting indicators based on smart money concepts or ICT'
77

88
# read the contents of the README file

smartmoneyconcepts/smc.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pandas as pd
33
import numpy as np
44
from pandas import DataFrame, Series
5+
from datetime import datetime
56

67

78
def inputvalidator(input_="ohlc"):
@@ -50,7 +51,7 @@ def decorate(cls):
5051

5152
@apply(inputvalidator(input_="ohlc"))
5253
class smc:
53-
__version__ = "0.0.15"
54+
__version__ = "0.0.16"
5455

5556
@classmethod
5657
def fvg(cls, ohlc: DataFrame) -> Series:
@@ -642,3 +643,53 @@ def liquidity(cls, ohlc: DataFrame, swing_highs_lows: DataFrame, range_percent:f
642643
liquidity_swept = pd.Series(liquidity_swept, name="Swept")
643644

644645
return pd.concat([liquidity, level, liquidity_end, liquidity_swept], axis=1)
646+
647+
@classmethod
648+
def previous_high_low(cls, ohlc: DataFrame, time_frame: str = "1D") -> Series:
649+
"""
650+
Previous High Low
651+
This method returns the previous high and low of the given time frame.
652+
653+
parameters:
654+
time_frame: str - the time frame to get the previous high and low 15m, 1H, 4H, 1D, 1W, 1M
655+
656+
returns:
657+
PreviousHigh = the previous high
658+
PreviousLow = the previous low
659+
"""
660+
661+
ohlc.index = pd.to_datetime(ohlc.index)
662+
663+
resampled_ohlc = ohlc.resample(time_frame).agg(
664+
{
665+
"open": "first",
666+
"high": "max",
667+
"low": "min",
668+
"close": "last",
669+
"volume": "sum",
670+
}
671+
)
672+
673+
# for every candle in ohlc add a new column with the previous high and low
674+
# Fix: Import the datetime module
675+
previous_high = np.zeros(len(ohlc), dtype=np.float32)
676+
previous_low = np.zeros(len(ohlc), dtype=np.float32)
677+
678+
for i in range(len(ohlc)):
679+
current_time = ohlc.index[i]
680+
# get the 1st high where the current time is greater than the time from the resampled ohlc
681+
previous_high_index = resampled_ohlc["high"].where(
682+
resampled_ohlc.index < current_time
683+
).last_valid_index()
684+
previous_high[i] = resampled_ohlc["high"][previous_high_index] if previous_high_index is not None else np.nan
685+
# get the 1st low where the current time is greater than the time from the resampled ohlc
686+
previous_low_index = resampled_ohlc["low"].where(
687+
resampled_ohlc.index < current_time
688+
).last_valid_index()
689+
previous_low[i] = resampled_ohlc["low"][previous_low_index] if previous_low_index is not None else np.nan
690+
691+
previous_high = pd.Series(previous_high, name="PreviousHigh")
692+
previous_low = pd.Series(previous_low, name="PreviousLow")
693+
694+
return pd.concat([previous_high, previous_low], axis=1)
695+

tests/SMC.test_binance.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,50 @@ def add_liquidity(fig, liquidity_data):
260260
)
261261
return fig
262262

263+
def add_previous_high_low(fig, previous_high_low_data):
264+
high = previous_high_low_data["PreviousHigh"]
265+
low = previous_high_low_data["PreviousLow"]
266+
# draw a line horizontally for each high where the highs are the same consecutively
267+
for i in range(len(high)-1):
268+
if high.iloc[i] == high.iloc[i+1]:
269+
fig.add_trace(
270+
go.Scatter(
271+
x=[df.index[i], df.index[i+1]],
272+
y=[high.iloc[i], high.iloc[i+1]],
273+
mode="lines",
274+
line=dict(
275+
color="lightblue",
276+
),
277+
)
278+
)
279+
# draw a line horizontally for each low where the lows are the same consecutively
280+
for i in range(len(low)-1):
281+
if low.iloc[i] == low.iloc[i+1]:
282+
fig.add_trace(
283+
go.Scatter(
284+
x=[df.index[i], df.index[i+1]],
285+
y=[low.iloc[i], low.iloc[i+1]],
286+
mode="lines",
287+
line=dict(
288+
color="lightblue",
289+
),
290+
)
291+
)
292+
293+
return fig
294+
263295
fvg_data = smc.fvg(df)
264296
swing_highs_lows_data = smc.swing_highs_lows(df, swing_length=50)
265297
bos_choch_data = smc.bos_choch(df, swing_highs_lows_data)
266298
ob_data = smc.ob(df, swing_highs_lows_data)
267299
liquidity_data = smc.liquidity(df, swing_highs_lows_data)
300+
previous_high_low_data = smc.previous_high_low(df, time_frame="1W")
268301
fig = add_FVG(fig, fvg_data)
269302
fig = add_swing_highs_lows(fig, swing_highs_lows_data)
270303
fig = add_bos_choch(fig, bos_choch_data)
271304
fig = add_OB(fig, ob_data)
272305
fig = add_liquidity(fig, liquidity_data)
306+
fig = add_previous_high_low(fig, previous_high_low_data)
273307

274308
fig.update_layout(xaxis_rangeslider_visible=False)
275309
fig.update_layout(showlegend=False)

tests/test_binance.png

6.41 KB
Loading

0 commit comments

Comments
 (0)