本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MTF Panel — Candle Alignment", overlay = true, dynamic_requests = true)
language = input.string("English", "Language / Idioma", options = ["English", "Português"], group = "Language / Idioma")
f_tr(string en, string pt) =>
language == "Português" ? pt : en
string g1 = "Visible timeframes / Tempos visíveis"
b1 = input.bool(true, "Show / Mostrar", inline = "1", group = g1)
t1 = input.timeframe("1", "TF / Tempo 1", inline = "1", group = g1)
b2 = input.bool(true, "Show / Mostrar", inline = "2", group = g1)
t2 = input.timeframe("5", "TF / Tempo 2", inline = "2", group = g1)
b3 = input.bool(true, "Show / Mostrar", inline = "3", group = g1)
t3 = input.timeframe("15", "TF / Tempo 3", inline = "3", group = g1)
b4 = input.bool(true, "Show / Mostrar", inline = "4", group = g1)
t4 = input.timeframe("30", "TF / Tempo 4", inline = "4", group = g1)
b5 = input.bool(true, "Show / Mostrar", inline = "5", group = g1)
t5 = input.timeframe("60", "TF / Tempo 5", inline = "5", group = g1)
b6 = input.bool(true, "Show / Mostrar", inline = "6", group = g1)
t6 = input.timeframe("240", "TF / Tempo 6", inline = "6", group = g1)
b7 = input.bool(true, "Show / Mostrar", inline = "7", group = g1)
t7 = input.timeframe("1D", "TF / Tempo 7", inline = "7", group = g1)
string g2 = "Conclusion rules / Regras da conclusão"
threshold = input.int(60, "Minimum majority / Maioria mínima (%)", minval = 51, maxval = 100, group = g2)
leaders = input.int(2, "Highest TFs that must agree / Maiores tempos que devem concordar", minval = 1, maxval = 7, group = g2)
minCount = input.int(3, "Minimum valid TFs / Mínimo de tempos válidos", minval = 2, maxval = 7, group = g2)
confirmCurrent = input.bool(true, "Confirm with current candles / Confirmar com candles atuais", group = g2)
showNote = input.bool(false, "Show footer note / Mostrar nota de rodapé", group = "Visual")
font = input.string("Small", "Text size / Tamanho do texto", options = ["Small", "Normal", "Large"], group = "Visual")
f_dir(float a, float b) =>
na(a) or na(b) ? int(na) : a > b ? 1 : a < b ? -1 : 0
f_label(string tf) =>
float seconds = timeframe.in_seconds(tf)
seconds < 3600 ? str.tostring(seconds / 60, "#.##") + " min" : seconds < 86400 ? str.tostring(seconds / 3600, "#.##") + " h" : tf
color buyBlue = color.rgb(25, 100, 210)
color waitYellow = color.rgb(255, 210, 60)
color green = color.rgb(18, 110, 72)
color red = color.rgb(160, 48, 55)
color neutral = color.rgb(65, 73, 87)
color base = color.rgb(25, 29, 38)
color header = color.rgb(40, 49, 66)
f_color(int d) =>
na(d) ? neutral : d == 1 ? green : d == -1 ? red : neutral
var table panel = table.new(position.top_right, 4, showNote ? 11 : 10, bgcolor = base, frame_color = color.gray, frame_width = 1, border_color = color.rgb(55, 61, 73), border_width = 1)
if barstate.isfirst
table.merge_cells(panel, 0, 8, 3, 8)
table.merge_cells(panel, 0, 9, 3, 9)
if showNote
table.merge_cells(panel, 0, 10, 3, 10)
// Requests execute throughout history, ensuring every selected context is initialized.
array<string> times = array.from(t1, t2, t3, t4, t5, t6, t7)
array<bool> enabled = array.from(b1, b2, b3, b4, b5, b6, b7)
array<float> seconds = array.new_float()
array<int> closedDirs = array.new_int(7, int(na))
array<int> currentDirs = array.new_int(7, int(na))
array<bool> valid = array.new_bool(7, false)
array<string> seen = array.new_string()
for i = 0 to 6
string tf = array.get(times, i)
float secs = timeframe.in_seconds(tf)
array.push(seconds, secs)
bool duplicate = array.includes(seen, str.tostring(secs))
if array.get(enabled, i) and not duplicate
array.push(seen, str.tostring(secs))
if secs >= timeframe.in_seconds()
// Confirmed values: offset + lookahead_on prevents future leakage.
[prevOpen, prevClose] = request.security(syminfo.tickerid, tf, [open[1], close[1]], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)
// Developing value is deliberately live and can change before closure.
liveClose = request.security(syminfo.tickerid, tf, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off)
int closedDir = f_dir(prevClose, prevOpen)
int currentDir = f_dir(liveClose, prevClose)
array.set(closedDirs, i, closedDir)
array.set(currentDirs, i, currentDir)
array.set(valid, i, not na(closedDir) and not na(currentDir))
array<int> order = array.sort_indices(seconds, order.descending)
int count = 0
int up = 0
int down = 0
int liveUp = 0
int liveDown = 0
bool topUp = true
bool topDown = true
int topCount = 0
for rank = 0 to 6
int i = array.get(order, rank)
if array.get(valid, i)
int d = array.get(closedDirs, i)
int c = array.get(currentDirs, i)
count += 1
up += d == 1 ? 1 : 0
down += d == -1 ? 1 : 0
liveUp += c == 1 ? 1 : 0
liveDown += c == -1 ? 1 : 0
if topCount < leaders
topUp := topUp and d == 1
topDown := topDown and d == -1
topCount += 1
int required = int(math.ceil(count * threshold / 100.0))
bool enough = count >= minCount and topCount == leaders
bool buyBase = enough and up >= required and topUp
bool sellBase = enough and down >= required and topDown
bool buy = buyBase and (not confirmCurrent or liveUp >= required)
bool sell = sellBase and (not confirmCurrent or liveDown >= required)
string conclusion = count < minCount ? f_tr("WAIT\ntoo few valid timeframes", "AGUARDAR\npoucos tempos válidos") : topCount < leaders ? f_tr("WAIT\nnot enough higher timeframes", "AGUARDAR\nfaltam tempos maiores") : buy ? f_tr("↑↑ LOOK FOR BUY\nbullish alignment", "↑↑ PROCURAR COMPRA\nalinhamento de alta") : sell ? f_tr("↓↓ LOOK FOR SELL\nbearish alignment", "↓↓ PROCURAR VENDA\nalinhamento de baixa") : buyBase or sellBase ? f_tr("↔ WAIT\ncurrent candles unconfirmed", "↔ AGUARDAR\ncandles atuais sem confirmação") : f_tr("↔ WAIT\ntimeframes not aligned", "↔ AGUARDAR\ntempos sem alinhamento")
if barstate.islast
string sizeText = font == "Small" ? size.small : font == "Large" ? size.large : size.normal
table.clear(panel, 0, 0, 3, 7)
table.cell(panel, 0, 0, f_tr("TF ↓", "TF ↓"), text_color = color.white, bgcolor = header, text_size = sizeText)
table.cell(panel, 1, 0, f_tr("Last closed", "Fechado"), text_color = color.white, bgcolor = header, text_size = sizeText)
table.cell(panel, 2, 0, f_tr("Current\nvs prev. close", "Atual\n× fech. anterior"), text_color = color.white, bgcolor = header, text_size = sizeText)
table.cell(panel, 3, 0, f_tr("Alignment", "Alinhamento"), text_color = color.white, bgcolor = header, text_size = sizeText)
int row = 1
array<string> displayed = array.new_string()
for rank = 0 to 6
int i = array.get(order, rank)
string key = str.tostring(array.get(seconds, i))
if array.get(enabled, i) and not array.includes(displayed, key)
array.push(displayed, key)
bool ok = array.get(valid, i)
int d = array.get(closedDirs, i)
int c = array.get(currentDirs, i)
bool lower = array.get(seconds, i) < timeframe.in_seconds()
string closedText = not ok ? (lower ? f_tr("TF below chart TF", "TF menor que o gráfico") : f_tr("No data", "Sem dados")) : d == 1 ? f_tr("↑ POSITIVE", "↑ POSITIVO") : d == -1 ? f_tr("↓ NEGATIVE", "↓ NEGATIVO") : "↔ DOJI"
string liveText = not ok ? "—" : c == 1 ? f_tr("ABOVE ↑", "ACIMA ↑") : c == -1 ? f_tr("BELOW ↓", "ABAIXO ↓") : f_tr("EQUAL ↔", "IGUAL ↔")
string agreement = not ok ? f_tr("Excluded", "Excluído") : d == 1 and c == 1 ? f_tr("↑↑ BULLISH", "↑↑ ALTA") : d == -1 and c == -1 ? f_tr("↓↓ BEARISH", "↓↓ BAIXA") : f_tr("↔ MIXED", "↔ MISTO")
table.cell(panel, 0, row, f_label(array.get(times, i)), text_color = color.white, bgcolor = header, text_size = sizeText)
table.cell(panel, 1, row, closedText, text_color = color.white, bgcolor = f_color(d), text_size = sizeText)
table.cell(panel, 2, row, liveText, text_color = color.white, bgcolor = f_color(c), text_size = sizeText)
table.cell(panel, 3, row, agreement, text_color = color.white, bgcolor = ok and d == c ? f_color(d) : neutral, text_size = sizeText)
row += 1
table.cell(panel, 0, 8, conclusion, text_color = buy or sell ? color.white : color.black, bgcolor = buy ? buyBlue : sell ? red : waitYellow, text_size = sizeText)
string tally = f_tr("Closed: ", "Fechados: ") + str.tostring(up) + "↑ / " + str.tostring(down) + f_tr("↓\nCurrent: ", "↓\nAtuais: ") + str.tostring(liveUp) + "↑ / " + str.tostring(liveDown) + f_tr("↓\nValid: ", "↓\nVálidos: ") + str.tostring(count) + f_tr(" | Required: ", " | Mínimo: ") + str.tostring(required)
table.cell(panel, 0, 9, tally, text_color = color.white, bgcolor = base, text_size = sizeText)
if showNote
table.cell(panel, 0, 10, f_tr("Live candles can change\nPrefer 1 min chart • Context only", "Candle atual pode mudar\nPrefira 1 min • Apenas contexto"), text_color = color.silver, bgcolor = base, text_size = sizeText)