Skip to content

Commit abc218b

Browse files
committed
Add order blocks indicators
1 parent 4b2b9be commit abc218b

6 files changed

Lines changed: 988 additions & 3 deletions

File tree

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pip install pyindicators
4646
* [Golden Zone](#golden-zone)
4747
* [Golden Zone Signal](#golden-zone-signal)
4848
* [Fair Value Gap (FVG)](#fair-value-gap-fvg)
49+
* [Order Blocks](#order-blocks)
4950
* [Pattern recognition](#pattern-recognition)
5051
* [Detect Peaks](#detect-peaks)
5152
* [Detect Bullish Divergence](#detect-bullish-divergence)
@@ -1018,6 +1019,77 @@ The `fvg_filled` function detects when FVGs have been mitigated:
10181019

10191020
![FAIR_VALUE_GAP](https://github.com/coding-kitties/PyIndicators/blob/main/static/images/indicators/fair_value_gap.png)
10201021

1022+
#### Order Blocks
1023+
1024+
Order Blocks are zones where institutional traders (banks, hedge funds) placed large orders, causing significant price moves. They represent areas of supply and demand imbalance that often act as support/resistance when price returns.
1025+
1026+
**Bullish Order Block:** The last bearish candle before a strong upward move. When price returns to this zone, it often bounces up (support).
1027+
1028+
**Bearish Order Block:** The last bullish candle before a strong downward move. When price returns to this zone, it often reverses down (resistance).
1029+
1030+
**Breaker Blocks:** When an Order Block is broken (invalidated), it becomes a breaker block and may act as the opposite type of support/resistance.
1031+
1032+
```python
1033+
def order_blocks(
1034+
data: Union[PdDataFrame, PlDataFrame],
1035+
swing_length: int = 10,
1036+
use_body: bool = False,
1037+
high_column: str = 'High',
1038+
low_column: str = 'Low',
1039+
open_column: str = 'Open',
1040+
close_column: str = 'Close',
1041+
bullish_ob_column: str = 'bullish_ob',
1042+
bearish_ob_column: str = 'bearish_ob',
1043+
bullish_ob_top_column: str = 'bullish_ob_top',
1044+
bullish_ob_bottom_column: str = 'bullish_ob_bottom',
1045+
bearish_ob_top_column: str = 'bearish_ob_top',
1046+
bearish_ob_bottom_column: str = 'bearish_ob_bottom',
1047+
bullish_breaker_column: str = 'bullish_breaker',
1048+
bearish_breaker_column: str = 'bearish_breaker'
1049+
) -> Union[PdDataFrame, PlDataFrame]:
1050+
```
1051+
1052+
Example
1053+
1054+
```python
1055+
import pandas as pd
1056+
from pyindicators import order_blocks, ob_signal, get_active_order_blocks
1057+
1058+
# Create sample OHLC data
1059+
df = pd.DataFrame({
1060+
'Open': [100, 102, 101, 105, 110, 108, 112, 115, 113, 118],
1061+
'High': [103, 104, 106, 112, 115, 112, 118, 120, 117, 122],
1062+
'Low': [99, 100, 100, 104, 108, 106, 110, 113, 111, 116],
1063+
'Close': [102, 101, 105, 110, 108, 110, 115, 113, 116, 120]
1064+
})
1065+
1066+
# Detect Order Blocks
1067+
df = order_blocks(df, swing_length=5)
1068+
print(df[['bullish_ob', 'bearish_ob', 'bullish_ob_top', 'bullish_ob_bottom']])
1069+
1070+
# Generate signals when price enters an OB zone
1071+
df = ob_signal(df)
1072+
print(df['ob_signal']) # 1 = in bullish zone, -1 = in bearish zone, 0 = outside
1073+
1074+
# Get currently active Order Blocks
1075+
active = get_active_order_blocks(df, max_bullish=3, max_bearish=3)
1076+
print(f"Active bullish OBs: {len(active['bullish'])}")
1077+
print(f"Active bearish OBs: {len(active['bearish'])}")
1078+
```
1079+
1080+
The function returns columns for:
1081+
- `bullish_ob` / `bearish_ob`: 1 when Order Block is detected, 0 otherwise
1082+
- `bullish_ob_top` / `bullish_ob_bottom`: Zone boundaries for bullish OBs
1083+
- `bearish_ob_top` / `bearish_ob_bottom`: Zone boundaries for bearish OBs
1084+
- `bullish_breaker` / `bearish_breaker`: 1 when OB is broken (becomes breaker block)
1085+
1086+
The `ob_signal` function generates signals:
1087+
- **1**: Price is within a bullish OB zone (potential long entry)
1088+
- **-1**: Price is within a bearish OB zone (potential short entry)
1089+
- **0**: Price is outside any OB zone
1090+
1091+
![ORDER_BLOCKS](https://github.com/coding-kitties/PyIndicators/blob/main/static/images/indicators/order_blocks.png)
1092+
10211093
### Pattern Recognition
10221094

10231095
#### Detect Peaks

pyindicators/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
fibonacci_retracement, fibonacci_retracement_levels, fibonacci_extension,
1313
moving_average_envelope, sma_envelope, ema_envelope,
1414
golden_zone, golden_zone_signal,
15-
fair_value_gap, fvg_signal, fvg_filled
15+
fair_value_gap, fvg_signal, fvg_filled,
16+
order_blocks, ob_signal, get_active_order_blocks
1617
)
1718
from .exceptions import PyIndicatorException
1819
from .date_range import DateRange
@@ -81,5 +82,8 @@ def get_version():
8182
'golden_zone_signal',
8283
'fair_value_gap',
8384
'fvg_signal',
84-
'fvg_filled'
85+
'fvg_filled',
86+
'order_blocks',
87+
'ob_signal',
88+
'get_active_order_blocks'
8589
]

pyindicators/indicators/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
sma_envelope, ema_envelope
3131
from .golden_zone import golden_zone, golden_zone_signal
3232
from .fair_value_gap import fair_value_gap, fvg_signal, fvg_filled
33+
from .order_blocks import order_blocks, ob_signal, get_active_order_blocks
3334

3435
__all__ = [
3536
'sma',
@@ -80,5 +81,8 @@
8081
'golden_zone_signal',
8182
'fair_value_gap',
8283
'fvg_signal',
83-
'fvg_filled'
84+
'fvg_filled',
85+
'order_blocks',
86+
'ob_signal',
87+
'get_active_order_blocks'
8488
]

0 commit comments

Comments
 (0)