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提供或認可的金融、投資、交易或其他類型的意見或建議。請在使用條款閱讀更多資訊。