OPEN-SOURCE SCRIPT
Trend Colors + Transition Labels (Clean MAs + Supertrend)

//version=5
indicator("Trend Colors + Transition Labels (Clean MAs + Supertrend)", overlay=true, max_labels_count=500)
//———— Inputs
atrMultST = input.float(3.0, "Supertrend ATR Multiplier", step=0.1)
atrLenST = input.int(10, "Supertrend ATR Length")
lenMAfast = input.int(50, "Fast MA (chart TF)")
lenMAslow = input.int(200, "Slow MA (chart TF)")
htf = input.string("W", "Higher TF for Confirmation (use W on Daily, D on 1H)")
//———— Chart TF signals
maFast = ta.sma(close, lenMAfast)
maSlow = ta.sma(close, lenMAslow)
[st_ctf, dir_ctf] = ta.supertrend(atrMultST, atrLenST) // dir: 1=up, -1=down
ctf_up = dir_ctf == 1
ctf_dn = dir_ctf == -1
above50 = close > maFast
above200 = close > maSlow
maBull = maFast > maSlow
maBear = maFast < maSlow
//———— Higher TF signals
[st_htf, dir_htf] = request.security(syminfo.tickerid, htf, ta.supertrend(atrMultST, atrLenST))
maFast_htf = request.security(syminfo.tickerid, htf, ta.sma(close, lenMAfast))
maSlow_htf = request.security(syminfo.tickerid, htf, ta.sma(close, lenMAslow))
htf_up = dir_htf == 1
htf_dn = dir_htf == -1
htfBull = maFast_htf > maSlow_htf
htfBear = maFast_htf < maSlow_htf
//———— Final states (clear & symmetric)
strongUp = ctf_up and htf_up and maBull and htfBull and above50 and above200
strongDn = ctf_dn and htf_dn and maBear and htfBear and not above50 and not above200
earlyUp = ctf_up and not strongUp and not strongDn
//———— Background colors (GREEN up, RED down, LIGHT GREEN early)
bgcolor(strongUp ? color.new(color.green, 85) :
strongDn ? color.new(color.red, 85) :
earlyUp ? color.new(color.lime, 88) : na)
//———— Transition detection (integer state avoids string quirks)
var int lastState = 0 // 1=up, -1=down, 0=neutral
curState = strongUp ? 1 : strongDn ? -1 : 0
upStarted = curState == 1 and lastState != 1
downStarted = curState == -1 and lastState != -1
//———— Labels at transitions (single-line calls, ASCII only)
if upStarted
label.new(bar_index, low, "Uptrend Starts", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.normal)
if downStarted
label.new(bar_index, high, "Downtrend Starts", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.normal)
//———— Update state
lastState := curState
//———— Visual MAs
plot(maSlow, color=color.orange, title="200 MA")
plot(maFast, color=color.new(color.blue, 0), title="50 MA")
indicator("Trend Colors + Transition Labels (Clean MAs + Supertrend)", overlay=true, max_labels_count=500)
//———— Inputs
atrMultST = input.float(3.0, "Supertrend ATR Multiplier", step=0.1)
atrLenST = input.int(10, "Supertrend ATR Length")
lenMAfast = input.int(50, "Fast MA (chart TF)")
lenMAslow = input.int(200, "Slow MA (chart TF)")
htf = input.string("W", "Higher TF for Confirmation (use W on Daily, D on 1H)")
//———— Chart TF signals
maFast = ta.sma(close, lenMAfast)
maSlow = ta.sma(close, lenMAslow)
[st_ctf, dir_ctf] = ta.supertrend(atrMultST, atrLenST) // dir: 1=up, -1=down
ctf_up = dir_ctf == 1
ctf_dn = dir_ctf == -1
above50 = close > maFast
above200 = close > maSlow
maBull = maFast > maSlow
maBear = maFast < maSlow
//———— Higher TF signals
[st_htf, dir_htf] = request.security(syminfo.tickerid, htf, ta.supertrend(atrMultST, atrLenST))
maFast_htf = request.security(syminfo.tickerid, htf, ta.sma(close, lenMAfast))
maSlow_htf = request.security(syminfo.tickerid, htf, ta.sma(close, lenMAslow))
htf_up = dir_htf == 1
htf_dn = dir_htf == -1
htfBull = maFast_htf > maSlow_htf
htfBear = maFast_htf < maSlow_htf
//———— Final states (clear & symmetric)
strongUp = ctf_up and htf_up and maBull and htfBull and above50 and above200
strongDn = ctf_dn and htf_dn and maBear and htfBear and not above50 and not above200
earlyUp = ctf_up and not strongUp and not strongDn
//———— Background colors (GREEN up, RED down, LIGHT GREEN early)
bgcolor(strongUp ? color.new(color.green, 85) :
strongDn ? color.new(color.red, 85) :
earlyUp ? color.new(color.lime, 88) : na)
//———— Transition detection (integer state avoids string quirks)
var int lastState = 0 // 1=up, -1=down, 0=neutral
curState = strongUp ? 1 : strongDn ? -1 : 0
upStarted = curState == 1 and lastState != 1
downStarted = curState == -1 and lastState != -1
//———— Labels at transitions (single-line calls, ASCII only)
if upStarted
label.new(bar_index, low, "Uptrend Starts", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.normal)
if downStarted
label.new(bar_index, high, "Downtrend Starts", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.normal)
//———— Update state
lastState := curState
//———— Visual MAs
plot(maSlow, color=color.orange, title="200 MA")
plot(maFast, color=color.new(color.blue, 0), title="50 MA")
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.