OPEN-SOURCE SCRIPT
EMA Slope First Flip Signal

1. EMA Calculation:
It calculates 4 Exponential Moving Averages:
EMA 5 – very fast (captures short-term moves)
EMA 9 – fast
EMA 21 – medium
EMA 50 – long-term trend
2. Slope Calculation:
It finds the slope of each EMA using:
pinescript
Copy
Edit
slopeX = emaX - emaX[1]
This checks if the EMA is rising (slope > 0) or falling (slope < 0).
3. Trend Condition:
Buy condition: All 4 EMA slopes must be positive
Sell condition: All 4 EMA slopes must be negative
4. First Signal Logic:
It only flashes a signal when the direction changes.
For example:
If EMAs were not all rising before, but now all are → BUY
If EMAs were not all falling before, but now all are → SELL
Prevents repeated signals during the same trend.
5. Safe Boolean Handling:
Uses bool(condition[1]) to access previous state without error
Avoids using nz() or na() on booleans (which causes errors)
📈 What You See on Chart:
✅ Green "BUY" label below candle when all EMA slopes turn positive for the first time
❌ Red "SELL" label above candle when all EMA slopes turn negative for the first time
🔄 No duplicate signals in the same direction — only on reversal
It calculates 4 Exponential Moving Averages:
EMA 5 – very fast (captures short-term moves)
EMA 9 – fast
EMA 21 – medium
EMA 50 – long-term trend
2. Slope Calculation:
It finds the slope of each EMA using:
pinescript
Copy
Edit
slopeX = emaX - emaX[1]
This checks if the EMA is rising (slope > 0) or falling (slope < 0).
3. Trend Condition:
Buy condition: All 4 EMA slopes must be positive
Sell condition: All 4 EMA slopes must be negative
4. First Signal Logic:
It only flashes a signal when the direction changes.
For example:
If EMAs were not all rising before, but now all are → BUY
If EMAs were not all falling before, but now all are → SELL
Prevents repeated signals during the same trend.
5. Safe Boolean Handling:
Uses bool(condition[1]) to access previous state without error
Avoids using nz() or na() on booleans (which causes errors)
📈 What You See on Chart:
✅ Green "BUY" label below candle when all EMA slopes turn positive for the first time
❌ Red "SELL" label above candle when all EMA slopes turn negative for the first time
🔄 No duplicate signals in the same direction — only on reversal
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.