OPEN-SOURCE SCRIPT
Patterns Strategy v1

### 1. What the strategy does
This is a **price-action pattern strategy** that detects four patterns:
* **Bullish Engulfing** → long setup
* **Bearish Engulfing** → short setup
* **Morning Star** → long setup
* **Evening Star** → short setup
The strategy then creates an entry trigger, stop loss, and profit targets for the detected pattern.
---
### 2. Bullish Engulfing
For a bullish engulfing setup, the script looks for:
* Previous candle is bearish.
* Current candle is bullish.
* Current candle's body is large enough according to the minimum-body setting.
* The current candle completely engulfs the previous candle's body when strict engulfing is enabled.
* The current close can also be required to finish beyond the previous candle's open.
Once detected:
**Entry:** above the engulfing candle's high
**SL:** below the engulfing candle's low, with optional use of both candles
**Target:** normally 1.5R for TP1 and 2R for the final target.
---
### 3. Bearish Engulfing
The opposite structure is used:
* Previous candle is bullish.
* Current candle is bearish.
* Current candle has sufficient body strength.
* Current candle engulfs the previous candle's body.
* The close can be required to finish below the previous candle's open.
**Entry:** below the engulfing candle's low
**SL:** above the engulfing candle's high
**Target:** 1.5R and 2R by default.
---
### 4. Morning Star
The strategy uses a **three-candle pattern**:
1. Strong bearish first candle.
2. Small-body middle candle.
3. Strong bullish third candle.
4. Third candle must recover at least the configured penetration percentage of the first candle's body.
The default settings require:
* Middle candle body ≤ 35% of its range.
* Outer candles body ≥ 55%.
* Third candle penetration = 50%.
**Entry:** above the third candle's high
**SL:** below the entire three-candle pattern
**Target:** 1.5R / 2R.
---
### 5. Evening Star
This is the reverse of the Morning Star:
1. Strong bullish first candle.
2. Small middle candle.
3. Strong bearish third candle.
4. Third candle penetrates sufficiently into the first candle.
**Entry:** below the third candle's low
**SL:** above the entire pattern
**Target:** 1.5R / 2R.
---
## 6. Entry mechanism
There are **two entry modes**.
### Stop Order
The strategy places a resting order at the breakout level.
For example:
**Bullish pattern → price must break the trigger above the pattern.**
### Market on Break
The strategy waits until price actually breaks the trigger and then enters on the next bar.
So the two modes behave differently in backtesting.
There is also an **entry buffer**, meaning the trigger can be placed a few ticks beyond the pattern high/low.
---
## 7. Pending setup expiration
After a pattern appears, the setup does **not remain active forever**.
Default:
**5 bars**
If price doesn't trigger the entry within those bars, the pending setup is cancelled.
There is also an option to cancel the setup if the stop-loss level breaks before the entry occurs.
---
## 8. Stop-loss protection
The strategy has a minimum stop-distance rule.
If the calculated SL is too close to the entry, it automatically pushes the SL farther away to satisfy the minimum number of ticks.
This prevents extremely small stop distances.
---
## 9. Profit management
The default structure is:
**Entry → 1.5R TP1 → 2R final TP**
At TP1:
* 50% of the position is closed by default.
* The remaining position continues toward TP2.
* Stop loss can automatically move toward break-even.
The break-even adjustment has its own tick offset.
So conceptually:
**Risk = 1R**
If your entry-to-SL distance is 20 points:
* TP1 = +30 points
* TP2 = +40 points
with the default 1.5R / 2R settings.
---
## 10. Daily trading restrictions
The script has several controls:
* Maximum trades per day
* Maximum number of losses per day
* Only the first pattern of the day
* Long trades on/off
* Short trades on/off
* Trading session restriction
* Close trade at session end
* EMA trend filter
The default maximum trades per day is **1**.
This is important because even if several patterns appear, the strategy can prevent additional trades once the daily limit has been reached.
---
## 11. EMA trend filter
The strategy can optionally use an EMA as a directional filter.
Default EMA:
**200 EMA**
When enabled:
* Long setups require price above the EMA.
* Short setups require price below the EMA.
When disabled, the EMA does not filter the trades.
---
## 12. Position sizing
There are two approaches.
### Fixed lots
The strategy trades a fixed number of lots.
Default:
**1 lot × 65 units = 65 units**
### Risk-based sizing
You can instead specify how much of your equity you want to risk per trade.
The position size is then calculated from:
**Account equity + risk percentage + distance to SL + lot size**
It rounds the quantity to whole lots.
---
## 13. White candle feature
This is one of the important visual features of your script.
When a valid pattern is detected, the pattern candle can be painted **white**.
For two-candle patterns:
**Both candles can be highlighted.**
For three-candle patterns:
**All three candles can be highlighted.**
You can independently control this feature from the Visuals settings.
---
## 14. Chart markings
The script can display:
* Bullish triangle
* Bearish triangle
* Pattern name
* Entry/trigger level
* Entry price
* Stop loss
* TP1
* TP2
* EMA when trend filtering is enabled
The levels make it possible to visually follow the entire trade from setup → entry → SL/TP.
---
## 15. Dashboard
The top-right dashboard shows the current strategy state.
It can tell you:
* **LONG**
* **SHORT**
* **Pending Long**
* **Pending Short**
* **Flat**
It also displays:
* Trades today
* Losses today
* Last pattern
* Current 1R risk
* Net profit
When DEBUG is enabled, it additionally shows:
* Signals detected
* Orders placed
* Fills
* Expired setups
* Invalidated setups
* Blocked setups
* Last reason a setup was skipped.
---
## 16. Alerts
The script has alerts for each individual pattern:
* Bullish Engulfing
* Bearish Engulfing
* Morning Star
* Evening Star
It also has general long/short setup alerts.
The alert message can include the pattern, trigger price and SL.
---
### Overall flow
The strategy essentially works like this:
**Find pattern → check filters → calculate trigger → calculate SL → create pending setup → wait for breakout → enter → manage TP1 → move SL toward BE → target TP2 or SL → enforce daily limits.**
So this is **not simply a candle-pattern indicator**. It is a complete **backtesting/trading strategy with order management, risk sizing, daily limits, visual signals, dashboard and alerts**.
This is a **price-action pattern strategy** that detects four patterns:
* **Bullish Engulfing** → long setup
* **Bearish Engulfing** → short setup
* **Morning Star** → long setup
* **Evening Star** → short setup
The strategy then creates an entry trigger, stop loss, and profit targets for the detected pattern.
---
### 2. Bullish Engulfing
For a bullish engulfing setup, the script looks for:
* Previous candle is bearish.
* Current candle is bullish.
* Current candle's body is large enough according to the minimum-body setting.
* The current candle completely engulfs the previous candle's body when strict engulfing is enabled.
* The current close can also be required to finish beyond the previous candle's open.
Once detected:
**Entry:** above the engulfing candle's high
**SL:** below the engulfing candle's low, with optional use of both candles
**Target:** normally 1.5R for TP1 and 2R for the final target.
---
### 3. Bearish Engulfing
The opposite structure is used:
* Previous candle is bullish.
* Current candle is bearish.
* Current candle has sufficient body strength.
* Current candle engulfs the previous candle's body.
* The close can be required to finish below the previous candle's open.
**Entry:** below the engulfing candle's low
**SL:** above the engulfing candle's high
**Target:** 1.5R and 2R by default.
---
### 4. Morning Star
The strategy uses a **three-candle pattern**:
1. Strong bearish first candle.
2. Small-body middle candle.
3. Strong bullish third candle.
4. Third candle must recover at least the configured penetration percentage of the first candle's body.
The default settings require:
* Middle candle body ≤ 35% of its range.
* Outer candles body ≥ 55%.
* Third candle penetration = 50%.
**Entry:** above the third candle's high
**SL:** below the entire three-candle pattern
**Target:** 1.5R / 2R.
---
### 5. Evening Star
This is the reverse of the Morning Star:
1. Strong bullish first candle.
2. Small middle candle.
3. Strong bearish third candle.
4. Third candle penetrates sufficiently into the first candle.
**Entry:** below the third candle's low
**SL:** above the entire pattern
**Target:** 1.5R / 2R.
---
## 6. Entry mechanism
There are **two entry modes**.
### Stop Order
The strategy places a resting order at the breakout level.
For example:
**Bullish pattern → price must break the trigger above the pattern.**
### Market on Break
The strategy waits until price actually breaks the trigger and then enters on the next bar.
So the two modes behave differently in backtesting.
There is also an **entry buffer**, meaning the trigger can be placed a few ticks beyond the pattern high/low.
---
## 7. Pending setup expiration
After a pattern appears, the setup does **not remain active forever**.
Default:
**5 bars**
If price doesn't trigger the entry within those bars, the pending setup is cancelled.
There is also an option to cancel the setup if the stop-loss level breaks before the entry occurs.
---
## 8. Stop-loss protection
The strategy has a minimum stop-distance rule.
If the calculated SL is too close to the entry, it automatically pushes the SL farther away to satisfy the minimum number of ticks.
This prevents extremely small stop distances.
---
## 9. Profit management
The default structure is:
**Entry → 1.5R TP1 → 2R final TP**
At TP1:
* 50% of the position is closed by default.
* The remaining position continues toward TP2.
* Stop loss can automatically move toward break-even.
The break-even adjustment has its own tick offset.
So conceptually:
**Risk = 1R**
If your entry-to-SL distance is 20 points:
* TP1 = +30 points
* TP2 = +40 points
with the default 1.5R / 2R settings.
---
## 10. Daily trading restrictions
The script has several controls:
* Maximum trades per day
* Maximum number of losses per day
* Only the first pattern of the day
* Long trades on/off
* Short trades on/off
* Trading session restriction
* Close trade at session end
* EMA trend filter
The default maximum trades per day is **1**.
This is important because even if several patterns appear, the strategy can prevent additional trades once the daily limit has been reached.
---
## 11. EMA trend filter
The strategy can optionally use an EMA as a directional filter.
Default EMA:
**200 EMA**
When enabled:
* Long setups require price above the EMA.
* Short setups require price below the EMA.
When disabled, the EMA does not filter the trades.
---
## 12. Position sizing
There are two approaches.
### Fixed lots
The strategy trades a fixed number of lots.
Default:
**1 lot × 65 units = 65 units**
### Risk-based sizing
You can instead specify how much of your equity you want to risk per trade.
The position size is then calculated from:
**Account equity + risk percentage + distance to SL + lot size**
It rounds the quantity to whole lots.
---
## 13. White candle feature
This is one of the important visual features of your script.
When a valid pattern is detected, the pattern candle can be painted **white**.
For two-candle patterns:
**Both candles can be highlighted.**
For three-candle patterns:
**All three candles can be highlighted.**
You can independently control this feature from the Visuals settings.
---
## 14. Chart markings
The script can display:
* Bullish triangle
* Bearish triangle
* Pattern name
* Entry/trigger level
* Entry price
* Stop loss
* TP1
* TP2
* EMA when trend filtering is enabled
The levels make it possible to visually follow the entire trade from setup → entry → SL/TP.
---
## 15. Dashboard
The top-right dashboard shows the current strategy state.
It can tell you:
* **LONG**
* **SHORT**
* **Pending Long**
* **Pending Short**
* **Flat**
It also displays:
* Trades today
* Losses today
* Last pattern
* Current 1R risk
* Net profit
When DEBUG is enabled, it additionally shows:
* Signals detected
* Orders placed
* Fills
* Expired setups
* Invalidated setups
* Blocked setups
* Last reason a setup was skipped.
---
## 16. Alerts
The script has alerts for each individual pattern:
* Bullish Engulfing
* Bearish Engulfing
* Morning Star
* Evening Star
It also has general long/short setup alerts.
The alert message can include the pattern, trigger price and SL.
---
### Overall flow
The strategy essentially works like this:
**Find pattern → check filters → calculate trigger → calculate SL → create pending setup → wait for breakout → enter → manage TP1 → move SL toward BE → target TP2 or SL → enforce daily limits.**
So this is **not simply a candle-pattern indicator**. It is a complete **backtesting/trading strategy with order management, risk sizing, daily limits, visual signals, dashboard and alerts**.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.