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")
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。
开源脚本
本着TradingView的真正精神,此脚本的创建者将其开源,以便交易者可以查看和验证其功能。向作者致敬!虽然您可以免费使用它,但请记住,重新发布代码必须遵守我们的网站规则。
免责声明
这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。