OPEN-SOURCE SCRIPT
ORDER BLCOK custom strategy

# OB Matrix Strategy - Documentation
**Version:** 1.0
**Author:** HPotter
**Date:** 31/07/2017
The **OB Matrix Strategy** is based on the identification of **bullish and bearish Order Blocks** and the management of conditional orders with multiple Take Profit (TP) and Stop Loss (SL) levels. It uses trend filters, ATR, and percentage-based risk management.
---
## 1. Main Parameters
### Strategy
- `initial_capital`: 50
- `default_qty_type`: percentage of capital
- `default_qty_value`: 10
### Money Management
- `rr_threshold`: minimum Risk/Reward threshold to open a trade
- `risk_percent`: percentage of capital to risk per trade (default 2%)
- `maxPendingBars`: maximum number of bars for a pending order
- `maxBarsOpen`: maximum number of bars for an open position
- `qty_tp1`, `qty_tp2`, `qty_tp3`: quantity percentages for multiple TPs
---
## 2. Order Block Identification
### Order Block Parameters
- `obLookback`: number of bars to identify an Order Block
- `obmode`: method to calculate the block (`Full` or `Breadth`)
- `obmiti`: method to determine block mitigation (`Close`, `Wick`, `Avg`)
- `obMaxBlocks`: maximum number of Order Blocks displayed
### Main Variables
- `bullBlocks`: array of bullish blocks
- `bearBlocks`: array of bearish blocks
- `last_bull_volume`, `last_bear_volume`: volume of the last block
- `dom_block`: dominant block type (Bullish/Bearish/None)
- `block_strength`: block strength (normalized volume)
- `price_distance`: distance between current price and nearest block
---
## 3. Visual Parameters
- `Width`: line thickness for swing high/low
- `amountOfBoxes`: block grid segments
- `showBorder`: show block borders
- `borderWidth`: width of block borders
- `showVolume`: display volume inside blocks
- `volumePosition`: vertical position of volume text
Customizable colors:
- `obHighVolumeColor`, `obLowVolumeColor`, `obBearHighVolumeColor`, `obBearLowVolumeColor`
- `obBullBorderColor`, `obBearBorderColor`
- `obBullFillColor`, `obBearFillColor`
- `volumeTextColor`
---
## 4. Screener Table
- `showScreener`: display the screener table
- `tablePosition`: table position (`Top Left`, `Top Right`, `Bottom Left`, `Bottom Right`)
- `tableSize`: table size (`Small`, `Normal`, `Large`)
The table shows:
- Symbol, Timeframe
- Type and status of Order Block
- Number of retests
- Bullish and bearish volumes
---
## 5. Trend Filters
- EMA as a trend filter (`emaPeriod`, default 223)
- `bullishTrend` if close > EMA
- `bearishTrend` if close < EMA
---
## 6. ATR and Swing Points
- ATR calculated with a customizable period (`atrLength`)
- Swing High/Low for SL/TP calculation
- `f_getSwingTargets` function to calculate SL and TP based on direction
---
## 7. Trade Logic
### Buy Limit on Bullish OB
- Conditions:
- New bullish block
- Uptrend
- RR > threshold (`rr_threshold`)
- SL: `bullishOBPrice * (1 - atr * atrMultiplier)`
- Multiple TPs: TP1 (50%), TP2 (80%), TP3 (100% max)
- Quantity calculation based on percentage risk
### Sell Limit on Bearish OB
- Conditions:
- New bearish block
- Downtrend
- RR > threshold (`rr_threshold`)
- SL: `bearishOBPrice * (1 + atr * atrMultiplier)`
- Multiple TPs: TP1 (50%), TP2 (80%), TP3 (100% max)
- Quantity calculation based on percentage risk
---
## 8. Order Management and Timeout
- Close pending orders after `maxPendingBars` bars
- Close open positions after `maxBarsOpen` bars
- Label management for open orders
---
## 9. Alert Conditions
- `bull_touch`: price inside maximum bullish volume zone
- `bear_touch`: price inside maximum bearish volume zone
- `bull_reject`: confirmation of bullish zone rejection
- `bear_reject`: confirmation of bearish zone rejection
- `new_bull`: new bullish block
- `new_bear`: new bearish block
---
## 10. Level Calculation
- Swing levels based on selected timeframe (`SelectPeriod`)
- `xHigh` and `xLow` for S1 and R1 calculation
- Levels plotted on chart
---
## 11. Take Profit / Stop Loss
- Extended horizontal lines (`extendBars`) to visualize TP and SL
- Customizable colors (`tpColor`, `slColor`)
---
## 12. Notes
- Complete script based on Pine Script v5
- Advanced graphical management with boxes, lines, labels
- Dynamically displays volumes and Order Blocks
- Integrated internal screener
---
### End of Documentation
**Version:** 1.0
**Author:** HPotter
**Date:** 31/07/2017
The **OB Matrix Strategy** is based on the identification of **bullish and bearish Order Blocks** and the management of conditional orders with multiple Take Profit (TP) and Stop Loss (SL) levels. It uses trend filters, ATR, and percentage-based risk management.
---
## 1. Main Parameters
### Strategy
- `initial_capital`: 50
- `default_qty_type`: percentage of capital
- `default_qty_value`: 10
### Money Management
- `rr_threshold`: minimum Risk/Reward threshold to open a trade
- `risk_percent`: percentage of capital to risk per trade (default 2%)
- `maxPendingBars`: maximum number of bars for a pending order
- `maxBarsOpen`: maximum number of bars for an open position
- `qty_tp1`, `qty_tp2`, `qty_tp3`: quantity percentages for multiple TPs
---
## 2. Order Block Identification
### Order Block Parameters
- `obLookback`: number of bars to identify an Order Block
- `obmode`: method to calculate the block (`Full` or `Breadth`)
- `obmiti`: method to determine block mitigation (`Close`, `Wick`, `Avg`)
- `obMaxBlocks`: maximum number of Order Blocks displayed
### Main Variables
- `bullBlocks`: array of bullish blocks
- `bearBlocks`: array of bearish blocks
- `last_bull_volume`, `last_bear_volume`: volume of the last block
- `dom_block`: dominant block type (Bullish/Bearish/None)
- `block_strength`: block strength (normalized volume)
- `price_distance`: distance between current price and nearest block
---
## 3. Visual Parameters
- `Width`: line thickness for swing high/low
- `amountOfBoxes`: block grid segments
- `showBorder`: show block borders
- `borderWidth`: width of block borders
- `showVolume`: display volume inside blocks
- `volumePosition`: vertical position of volume text
Customizable colors:
- `obHighVolumeColor`, `obLowVolumeColor`, `obBearHighVolumeColor`, `obBearLowVolumeColor`
- `obBullBorderColor`, `obBearBorderColor`
- `obBullFillColor`, `obBearFillColor`
- `volumeTextColor`
---
## 4. Screener Table
- `showScreener`: display the screener table
- `tablePosition`: table position (`Top Left`, `Top Right`, `Bottom Left`, `Bottom Right`)
- `tableSize`: table size (`Small`, `Normal`, `Large`)
The table shows:
- Symbol, Timeframe
- Type and status of Order Block
- Number of retests
- Bullish and bearish volumes
---
## 5. Trend Filters
- EMA as a trend filter (`emaPeriod`, default 223)
- `bullishTrend` if close > EMA
- `bearishTrend` if close < EMA
---
## 6. ATR and Swing Points
- ATR calculated with a customizable period (`atrLength`)
- Swing High/Low for SL/TP calculation
- `f_getSwingTargets` function to calculate SL and TP based on direction
---
## 7. Trade Logic
### Buy Limit on Bullish OB
- Conditions:
- New bullish block
- Uptrend
- RR > threshold (`rr_threshold`)
- SL: `bullishOBPrice * (1 - atr * atrMultiplier)`
- Multiple TPs: TP1 (50%), TP2 (80%), TP3 (100% max)
- Quantity calculation based on percentage risk
### Sell Limit on Bearish OB
- Conditions:
- New bearish block
- Downtrend
- RR > threshold (`rr_threshold`)
- SL: `bearishOBPrice * (1 + atr * atrMultiplier)`
- Multiple TPs: TP1 (50%), TP2 (80%), TP3 (100% max)
- Quantity calculation based on percentage risk
---
## 8. Order Management and Timeout
- Close pending orders after `maxPendingBars` bars
- Close open positions after `maxBarsOpen` bars
- Label management for open orders
---
## 9. Alert Conditions
- `bull_touch`: price inside maximum bullish volume zone
- `bear_touch`: price inside maximum bearish volume zone
- `bull_reject`: confirmation of bullish zone rejection
- `bear_reject`: confirmation of bearish zone rejection
- `new_bull`: new bullish block
- `new_bear`: new bearish block
---
## 10. Level Calculation
- Swing levels based on selected timeframe (`SelectPeriod`)
- `xHigh` and `xLow` for S1 and R1 calculation
- Levels plotted on chart
---
## 11. Take Profit / Stop Loss
- Extended horizontal lines (`extendBars`) to visualize TP and SL
- Customizable colors (`tpColor`, `slColor`)
---
## 12. Notes
- Complete script based on Pine Script v5
- Advanced graphical management with boxes, lines, labels
- Dynamically displays volumes and Order Blocks
- Integrated internal screener
---
### End of Documentation
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.
Mã nguồn mở
Theo đúng tinh thần TradingView, người tạo ra tập lệnh này đã biến tập lệnh thành mã nguồn mở để các nhà giao dịch có thể xem xét và xác minh công năng. Xin dành lời khen tặng cho tác giả! Mặc dù bạn có thể sử dụng miễn phí, nhưng lưu ý nếu đăng lại mã, bạn phải tuân theo Quy tắc nội bộ của chúng tôi.
Thông báo miễn trừ trách nhiệm
Thông tin và ấn phẩm không có nghĩa là và không cấu thành, tài chính, đầu tư, kinh doanh, hoặc các loại lời khuyên hoặc khuyến nghị khác được cung cấp hoặc xác nhận bởi TradingView. Đọc thêm trong Điều khoản sử dụng.