Skip to content

Commit

Permalink
Merge pull request #15 from joshyattridge/previous_candles_high_low
Browse files Browse the repository at this point in the history
Previous candles high low
  • Loading branch information
joshyattridge authored Mar 18, 2024
2 parents a1f5951 + a643d06 commit eeb7128
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.

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

## Installation

Expand Down Expand Up @@ -109,6 +109,21 @@ Level = the level of the liquidity<br>
End = the index of the last liquidity level<br>
Swept = the index of the candle that swept the liquidity<br>

### Previous High And Low

```python
smc.previous_high_low(ohlc, time_frame = "1D")
```

This method returns the previous high and low of the given time frame.

parameters:<br>
time_frame: str - the time frame to get the previous high and low 15m, 1H, 4H, 1D, 1W, 1M<br>

returns:<br>
PreviousHigh = the previous high<br>
PreviousLow = the previous low<br>

## Contributing

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.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import codecs
import os

VERSION = '0.0.15'
VERSION = '0.0.16'
DESCRIPTION = 'Getting indicators based on smart money concepts or ICT'

# read the contents of the README file
Expand Down
53 changes: 52 additions & 1 deletion smartmoneyconcepts/smc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pandas as pd
import numpy as np
from pandas import DataFrame, Series
from datetime import datetime


def inputvalidator(input_="ohlc"):
Expand Down Expand Up @@ -50,7 +51,7 @@ def decorate(cls):

@apply(inputvalidator(input_="ohlc"))
class smc:
__version__ = "0.0.15"
__version__ = "0.0.16"

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

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

@classmethod
def previous_high_low(cls, ohlc: DataFrame, time_frame: str = "1D") -> Series:
"""
Previous High Low
This method returns the previous high and low of the given time frame.
parameters:
time_frame: str - the time frame to get the previous high and low 15m, 1H, 4H, 1D, 1W, 1M
returns:
PreviousHigh = the previous high
PreviousLow = the previous low
"""

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

previous_high = pd.Series(previous_high, name="PreviousHigh")
previous_low = pd.Series(previous_low, name="PreviousLow")

return pd.concat([previous_high, previous_low], axis=1)

34 changes: 34 additions & 0 deletions tests/SMC.test_binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,50 @@ def add_liquidity(fig, liquidity_data):
)
return fig

def add_previous_high_low(fig, previous_high_low_data):
high = previous_high_low_data["PreviousHigh"]
low = previous_high_low_data["PreviousLow"]
# draw a line horizontally for each high where the highs are the same consecutively
for i in range(len(high)-1):
if high.iloc[i] == high.iloc[i+1]:
fig.add_trace(
go.Scatter(
x=[df.index[i], df.index[i+1]],
y=[high.iloc[i], high.iloc[i+1]],
mode="lines",
line=dict(
color="lightblue",
),
)
)
# draw a line horizontally for each low where the lows are the same consecutively
for i in range(len(low)-1):
if low.iloc[i] == low.iloc[i+1]:
fig.add_trace(
go.Scatter(
x=[df.index[i], df.index[i+1]],
y=[low.iloc[i], low.iloc[i+1]],
mode="lines",
line=dict(
color="lightblue",
),
)
)

return fig

fvg_data = smc.fvg(df)
swing_highs_lows_data = smc.swing_highs_lows(df, swing_length=50)
bos_choch_data = smc.bos_choch(df, swing_highs_lows_data)
ob_data = smc.ob(df, swing_highs_lows_data)
liquidity_data = smc.liquidity(df, swing_highs_lows_data)
previous_high_low_data = smc.previous_high_low(df, time_frame="1W")
fig = add_FVG(fig, fvg_data)
fig = add_swing_highs_lows(fig, swing_highs_lows_data)
fig = add_bos_choch(fig, bos_choch_data)
fig = add_OB(fig, ob_data)
fig = add_liquidity(fig, liquidity_data)
fig = add_previous_high_low(fig, previous_high_low_data)

fig.update_layout(xaxis_rangeslider_visible=False)
fig.update_layout(showlegend=False)
Expand Down
Binary file modified tests/test_binance.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eeb7128

Please sign in to comment.