Skip to content

Commit 390b431

Browse files
committed
Add market structure break indicators
1 parent 70852ca commit 390b431

6 files changed

Lines changed: 845 additions & 331 deletions

File tree

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pip install pyindicators
4848
* [Fair Value Gap (FVG)](#fair-value-gap-fvg)
4949
* [Order Blocks](#order-blocks)
5050
* [Market Structure Break](#market-structure-break)
51+
* [Market Structure CHoCH/BOS](#market-structure-chochbos)
5152
* [Pattern recognition](#pattern-recognition)
5253
* [Detect Peaks](#detect-peaks)
5354
* [Detect Bullish Divergence](#detect-bullish-divergence)
@@ -1191,6 +1192,84 @@ The `market_structure_ob` function additionally returns:
11911192

11921193
![MARKET_STRUCTURE](https://github.com/coding-kitties/PyIndicators/blob/main/static/images/indicators/market_structure_ob.png)
11931194

1195+
#### Market Structure CHoCH/BOS
1196+
1197+
Market Structure CHoCH/BOS (Fractal) is a Smart Money Concept indicator that uses fractal detection to identify swing points and distinguishes between two types of structure breaks:
1198+
1199+
**CHoCH (Change of Character):** A trend reversal signal that occurs when price breaks a swing point in the **opposite direction** of the current trend.
1200+
- Bullish CHoCH: Trend was bearish, price breaks above swing high (reversal to bullish)
1201+
- Bearish CHoCH: Trend was bullish, price breaks below swing low (reversal to bearish)
1202+
1203+
**BOS (Break of Structure):** A trend continuation signal that occurs when price breaks a swing point in the **same direction** as the current trend.
1204+
- Bullish BOS: Trend is bullish, price breaks above swing high (continuation)
1205+
- Bearish BOS: Trend is bearish, price breaks below swing low (continuation)
1206+
1207+
This indicator also tracks dynamic support and resistance levels based on the swing structure.
1208+
1209+
```python
1210+
def market_structure_choch_bos(
1211+
data: Union[PdDataFrame, PlDataFrame],
1212+
length: int = 5,
1213+
high_column: str = 'High',
1214+
low_column: str = 'Low',
1215+
close_column: str = 'Close',
1216+
choch_bullish_column: str = 'choch_bullish',
1217+
choch_bearish_column: str = 'choch_bearish',
1218+
bos_bullish_column: str = 'bos_bullish',
1219+
bos_bearish_column: str = 'bos_bearish',
1220+
support_column: str = 'support_level',
1221+
resistance_column: str = 'resistance_level',
1222+
support_broken_column: str = 'support_broken',
1223+
resistance_broken_column: str = 'resistance_broken',
1224+
trend_column: str = 'market_trend'
1225+
) -> Union[PdDataFrame, PlDataFrame]:
1226+
```
1227+
1228+
Example
1229+
1230+
```python
1231+
import pandas as pd
1232+
from pyindicators import (
1233+
market_structure_choch_bos,
1234+
choch_bos_signal,
1235+
get_choch_bos_stats
1236+
)
1237+
1238+
# Create sample OHLC data
1239+
df = pd.DataFrame({
1240+
'High': [...],
1241+
'Low': [...],
1242+
'Close': [...]
1243+
})
1244+
1245+
# Detect CHoCH and BOS signals
1246+
df = market_structure_choch_bos(df, length=5)
1247+
print(df[['choch_bullish', 'choch_bearish', 'bos_bullish', 'bos_bearish', 'market_trend']])
1248+
1249+
# Generate trading signals
1250+
# 2 = bullish CHoCH (strong reversal), 1 = bullish BOS (continuation)
1251+
# -1 = bearish BOS (continuation), -2 = bearish CHoCH (strong reversal)
1252+
df = choch_bos_signal(df)
1253+
reversal_signals = df[abs(df['structure_signal']) == 2]
1254+
1255+
# Get statistics
1256+
stats = get_choch_bos_stats(df)
1257+
print(f"Total reversals (CHoCH): {stats['total_choch']}")
1258+
print(f"Total continuations (BOS): {stats['total_bos']}")
1259+
```
1260+
1261+
The function returns:
1262+
- `choch_bullish` / `choch_bearish`: 1 when CHoCH detected (trend reversal)
1263+
- `bos_bullish` / `bos_bearish`: 1 when BOS detected (trend continuation)
1264+
- `support_level` / `resistance_level`: Current S/R level prices
1265+
- `support_broken` / `resistance_broken`: 1 when S/R level is broken
1266+
- `market_trend`: Current trend direction (1=bullish, -1=bearish, 0=neutral)
1267+
1268+
**Trading Strategy:**
1269+
- CHoCH signals are stronger (trend reversals) - good for counter-trend entries
1270+
- BOS signals are trend confirmations - good for trend-following entries
1271+
- Use support/resistance levels for stop loss placement
1272+
11941273
### Pattern Recognition
11951274

11961275
#### Detect Peaks

pyindicators/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
fair_value_gap, fvg_signal, fvg_filled,
1616
order_blocks, ob_signal, get_active_order_blocks,
1717
market_structure_break, market_structure_ob,
18-
msb_signal, ob_quality_signal, get_market_structure_stats
18+
msb_signal, ob_quality_signal, get_market_structure_stats,
19+
market_structure_choch_bos, choch_bos_signal, get_choch_bos_stats
1920
)
2021
from .exceptions import PyIndicatorException
2122
from .date_range import DateRange
@@ -92,5 +93,8 @@ def get_version():
9293
'market_structure_ob',
9394
'msb_signal',
9495
'ob_quality_signal',
95-
'get_market_structure_stats'
96+
'get_market_structure_stats',
97+
'market_structure_choch_bos',
98+
'choch_bos_signal',
99+
'get_choch_bos_stats'
96100
]

pyindicators/indicators/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from .order_blocks import order_blocks, ob_signal, get_active_order_blocks
3434
from .market_structure import (
3535
market_structure_break, market_structure_ob,
36-
msb_signal, ob_quality_signal, get_market_structure_stats
36+
msb_signal, ob_quality_signal, get_market_structure_stats,
37+
market_structure_choch_bos, choch_bos_signal, get_choch_bos_stats
3738
)
3839

3940
__all__ = [
@@ -93,5 +94,8 @@
9394
'market_structure_ob',
9495
'msb_signal',
9596
'ob_quality_signal',
96-
'get_market_structure_stats'
97+
'get_market_structure_stats',
98+
'market_structure_choch_bos',
99+
'choch_bos_signal',
100+
'get_choch_bos_stats'
97101
]

0 commit comments

Comments
 (0)