本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Multi-MA Trend Lines – Pro", shorttitle="MMA Pro", overlay=true, max_labels_count=50)
// ============================================================================
// INPUTS
// ============================================================================
const string G_MA = "1. Moving Averages"
const string G_TR = "2. Trend Detection"
const string G_COL = "3. Trend Line Colors"
const string G_LBL = "4. MA Labels"
const string G_DASH = "5. Dashboard"
maType = input.string("EMA", "MA type", options=["SMA", "EMA", "WMA", "RMA", "HMA", "VWMA"], group=G_MA, tooltip="Applied to all nine moving averages.")
src = input.source(close, "Source", group=G_MA, tooltip="Price source used for every MA.")
en1 = input.bool(true, "MA 1", inline="m1", group=G_MA, tooltip="Enable / disable each MA slot and edit its length.")
len1 = input.int(20, "Length", minval=1, inline="m1", group=G_MA)
en2 = input.bool(false, "MA 2", inline="m2", group=G_MA)
len2 = input.int(30, "Length", minval=1, inline="m2", group=G_MA)
en3 = input.bool(true, "MA 3", inline="m3", group=G_MA)
len3 = input.int(50, "Length", minval=1, inline="m3", group=G_MA)
en4 = input.bool(false, "MA 4", inline="m4", group=G_MA)
len4 = input.int(60, "Length", minval=1, inline="m4", group=G_MA)
en5 = input.bool(false, "MA 5", inline="m5", group=G_MA)
len5 = input.int(70, "Length", minval=1, inline="m5", group=G_MA)
en6 = input.bool(false, "MA 6", inline="m6", group=G_MA)
len6 = input.int(90, "Length", minval=1, inline="m6", group=G_MA)
en7 = input.bool(true, "MA 7", inline="m7", group=G_MA)
len7 = input.int(100, "Length", minval=1, inline="m7", group=G_MA)
en8 = input.bool(true, "MA 8", inline="m8", group=G_MA)
len8 = input.int(200, "Length", minval=1, inline="m8", group=G_MA)
en9 = input.bool(false, "MA 9", inline="m9", group=G_MA)
len9 = input.int(500, "Length", minval=1, inline="m9", group=G_MA)
tolPct = input.float(0.0, "Flat tolerance (%)", minval=0.0, step=0.01, group=G_TR, tooltip="MA moves smaller than this % of the previous MA value count as Neutral.")
confirmBars = input.int(1, "Confirmation bars", minval=1, group=G_TR, tooltip="A new state must persist this many closed bars before it becomes active. 1 = confirm at bar close.")
bullColor = input.color(#00C853, "Bullish", group=G_COL)
bearColor = input.color(#FF1744, "Bearish", group=G_COL)
neutralColor = input.color(#808080, "Neutral", group=G_COL)
lineWidth = input.int(2, "Line width", minval=1, maxval=5, group=G_COL)
showLabels = input.bool(true, "Show MA labels", group=G_LBL)
lblSizeIn = input.string("Small", "Label size", options=["Tiny", "Small", "Normal", "Large"], group=G_LBL)
showDash = input.bool(true, "Show dashboard", group=G_DASH)
dashPosIn = input.string("Top Right", "Dashboard position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=G_DASH)
dashSizeIn = input.string("Small", "Dashboard text size", options=["Tiny", "Small", "Normal", "Large"], group=G_DASH)
// ============================================================================
// HELPER FUNCTIONS
// ============================================================================
// Moving average engine. All variants are calculated on every bar so ta.*
// history stays consistent, then the selected one is returned.
f_ma(string t, float s, int len) =>
float vSma = ta.sma(s, len)
float vEma = ta.ema(s, len)
float vWma = ta.wma(s, len)
float vRma = ta.rma(s, len)
float vHma = ta.hma(s, len)
float vVwma = ta.vwma(s, len)
float result = switch t
"SMA" => vSma
"EMA" => vEma
"WMA" => vWma
"RMA" => vRma
"HMA" => vHma
"VWMA" => vVwma
=> vEma
result
// Trend state with confirmation. Returns 1 = bullish, -1 = bearish, 0 = neutral,
// na = not enough data. State only updates on a closed bar.
f_state(float ma, float tol, int need) =>
var int confirmed = 0
var int candidate = 0
var int count = 0
float prev = ma[1]
bool valid = not na(ma) and not na(prev)
if valid and barstate.isconfirmed
float thr = math.abs(prev) * tol / 100.0
int raw = ma > prev + thr ? 1 : ma < prev - thr ? -1 : 0
if raw == candidate
count += 1
else
candidate := raw
count := 1
if count >= need
confirmed := candidate
int out = valid ? confirmed : na
out
// True only on the confirmed bar where the state changes to `target`.
f_turn(int st, int target) =>
barstate.isconfirmed and not na(st) and not na(st[1]) and st == target and st[1] != target
// Counts and the shortest-length enabled MA's state.
f_summary(array<int> lens, array<int> sts) =>
int bull = 0
int bear = 0
int neu = 0
int total = 0
int fastLen = 0
int fastSt = na
for i = 0 to array.size(sts) - 1
int s = array.get(sts, i)
if not na(s)
total += 1
if s == 1
bull += 1
else if s == -1
bear += 1
else
neu += 1
int ln = array.get(lens, i)
if na(fastSt) or ln < fastLen
fastLen := ln
fastSt := s
[bull, bear, neu, total, fastSt]
// 1 = Strong Bullish, -1 = Strong Bearish, 2 = Mixed, 0 = Insufficient data.
f_overall(int bull, int bear, int total, int fastSt) =>
int res = 0
if total >= 3
if bull >= 0.75 * total and fastSt == 1
res := 1
else if bear >= 0.75 * total and fastSt == -1
res := -1
else
res := 2
res
// 1 = Potential Correction, 2 = Potential Bounce, 0 = none.
f_context(int bull, int bear, int total, int fastSt) =>
int res = 0
if total >= 3
int baseTotal = total - 1
int baseBull = bull - (fastSt == 1 ? 1 : 0)
int baseBear = bear - (fastSt == -1 ? 1 : 0)
if fastSt == -1 and baseBull >= 0.75 * baseTotal
res := 1
else if fastSt == 1 and baseBear >= 0.75 * baseTotal
res := 2
res
f_size(string s) =>
switch s
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
=> size.small
f_pos(string p) =>
switch p
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
f_stateTxt(int s) =>
s == 1 ? "Bullish" : s == -1 ? "Bearish" : s == 0 ? "Neutral" : "Warming up"
f_stateCol(int s) =>
s == 1 ? bullColor : s == -1 ? bearColor : neutralColor
f_row(table t, int row, string l, string r, color rc, string sz) =>
table.cell(t, 0, row, l, text_color=color.white, text_halign=text.align_left, text_size=sz)
table.cell(t, 1, row, r, text_color=rc, text_halign=text.align_right, text_size=sz)
// ============================================================================
// MOVING AVERAGE CALCULATIONS (always calculated; masked when disabled)
// ============================================================================
float r1 = f_ma(maType, src, len1)
float r2 = f_ma(maType, src, len2)
float r3 = f_ma(maType, src, len3)
float r4 = f_ma(maType, src, len4)
float r5 = f_ma(maType, src, len5)
float r6 = f_ma(maType, src, len6)
float r7 = f_ma(maType, src, len7)
float r8 = f_ma(maType, src, len8)
float r9 = f_ma(maType, src, len9)
int s1 = f_state(r1, tolPct, confirmBars)
int s2 = f_state(r2, tolPct, confirmBars)
int s3 = f_state(r3, tolPct, confirmBars)
int s4 = f_state(r4, tolPct, confirmBars)
int s5 = f_state(r5, tolPct, confirmBars)
int s6 = f_state(r6, tolPct, confirmBars)
int s7 = f_state(r7, tolPct, confirmBars)
int s8 = f_state(r8, tolPct, confirmBars)
int s9 = f_state(r9, tolPct, confirmBars)
float m1 = en1 ? r1 : na
float m2 = en2 ? r2 : na
float m3 = en3 ? r3 : na
float m4 = en4 ? r4 : na
float m5 = en5 ? r5 : na
float m6 = en6 ? r6 : na
float m7 = en7 ? r7 : na
float m8 = en8 ? r8 : na
float m9 = en9 ? r9 : na
int st1 = en1 ? s1 : na
int st2 = en2 ? s2 : na
int st3 = en3 ? s3 : na
int st4 = en4 ? s4 : na
int st5 = en5 ? s5 : na
int st6 = en6 ? s6 : na
int st7 = en7 ? s7 : na
int st8 = en8 ? s8 : na
int st9 = en9 ? s9 : na
// ============================================================================
// PLOTS (exactly nine, global scope, ternary colors)
// ============================================================================
plot(m1, "MA Slot 1", color = st1 == 1 ? bullColor : st1 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m2, "MA Slot 2", color = st2 == 1 ? bullColor : st2 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m3, "MA Slot 3", color = st3 == 1 ? bullColor : st3 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m4, "MA Slot 4", color = st4 == 1 ? bullColor : st4 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m5, "MA Slot 5", color = st5 == 1 ? bullColor : st5 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m6, "MA Slot 6", color = st6 == 1 ? bullColor : st6 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m7, "MA Slot 7", color = st7 == 1 ? bullColor : st7 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m8, "MA Slot 8", color = st8 == 1 ? bullColor : st8 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
plot(m9, "MA Slot 9", color = st9 == 1 ? bullColor : st9 == -1 ? bearColor : neutralColor, linewidth = lineWidth)
// ============================================================================
// TREND ALIGNMENT ANALYSIS
// ============================================================================
array<int> lens = array.from(len1, len2, len3, len4, len5, len6, len7, len8, len9)
array<int> sts = array.from(st1, st2, st3, st4, st5, st6, st7, st8, st9)
[bullN, bearN, neuN, totN, fastSt] = f_summary(lens, sts)
int overall = f_overall(bullN, bearN, totN, fastSt)
int ctx = f_context(bullN, bearN, totN, fastSt)
// MA stacking uses slots 1 (20), 3 (50), 7 (100), 8 (200)
bool stackReady = not na(m1) and not na(m3) and not na(m7) and not na(m8)
bool stackBull = stackReady and m1 > m3 and m3 > m7 and m7 > m8
bool stackBear = stackReady and m1 < m3 and m3 < m7 and m7 < m8
// ============================================================================
// ALERTS (18 MA transition alerts + 2 context alerts = 20)
// ============================================================================
bool bull1 = f_turn(st1, 1)
bool bear1 = f_turn(st1, -1)
bool bull2 = f_turn(st2, 1)
bool bear2 = f_turn(st2, -1)
bool bull3 = f_turn(st3, 1)
bool bear3 = f_turn(st3, -1)
bool bull4 = f_turn(st4, 1)
bool bear4 = f_turn(st4, -1)
bool bull5 = f_turn(st5, 1)
bool bear5 = f_turn(st5, -1)
bool bull6 = f_turn(st6, 1)
bool bear6 = f_turn(st6, -1)
bool bull7 = f_turn(st7, 1)
bool bear7 = f_turn(st7, -1)
bool bull8 = f_turn(st8, 1)
bool bear8 = f_turn(st8, -1)
bool bull9 = f_turn(st9, 1)
bool bear9 = f_turn(st9, -1)
alertcondition(bull1, "MA Slot 1 Bullish", "MA Slot 1 (default 20) turned Bullish (confirmed)")
alertcondition(bear1, "MA Slot 1 Bearish", "MA Slot 1 (default 20) turned Bearish (confirmed)")
alertcondition(bull2, "MA Slot 2 Bullish", "MA Slot 2 (default 30) turned Bullish (confirmed)")
alertcondition(bear2, "MA Slot 2 Bearish", "MA Slot 2 (default 30) turned Bearish (confirmed)")
alertcondition(bull3, "MA Slot 3 Bullish", "MA Slot 3 (default 50) turned Bullish (confirmed)")
alertcondition(bear3, "MA Slot 3 Bearish", "MA Slot 3 (default 50) turned Bearish (confirmed)")
alertcondition(bull4, "MA Slot 4 Bullish", "MA Slot 4 (default 60) turned Bullish (confirmed)")
alertcondition(bear4, "MA Slot 4 Bearish", "MA Slot 4 (default 60) turned Bearish (confirmed)")
alertcondition(bull5, "MA Slot 5 Bullish", "MA Slot 5 (default 70) turned Bullish (confirmed)")
alertcondition(bear5, "MA Slot 5 Bearish", "MA Slot 5 (default 70) turned Bearish (confirmed)")
alertcondition(bull6, "MA Slot 6 Bullish", "MA Slot 6 (default 90) turned Bullish (confirmed)")
alertcondition(bear6, "MA Slot 6 Bearish", "MA Slot 6 (default 90) turned Bearish (confirmed)")
alertcondition(bull7, "MA Slot 7 Bullish", "MA Slot 7 (default 100) turned Bullish (confirmed)")
alertcondition(bear7, "MA Slot 7 Bearish", "MA Slot 7 (default 100) turned Bearish (confirmed)")
alertcondition(bull8, "MA Slot 8 Bullish", "MA Slot 8 (default 200) turned Bullish (confirmed)")
alertcondition(bear8, "MA Slot 8 Bearish", "MA Slot 8 (default 200) turned Bearish (confirmed)")
alertcondition(bull9, "MA Slot 9 Bullish", "MA Slot 9 (default 500) turned Bullish (confirmed)")
alertcondition(bear9, "MA Slot 9 Bearish", "MA Slot 9 (default 500) turned Bearish (confirmed)")
bool ctxCorrection = barstate.isconfirmed and ctx == 1 and ctx[1] != 1
bool ctxBounce = barstate.isconfirmed and ctx == 2 and ctx[1] != 2
alertcondition(ctxCorrection, "Potential Correction started", "Potential Correction: fastest MA turned Bearish while the longer MAs remain Bullish (not a sell signal)")
alertcondition(ctxBounce, "Potential Bounce started", "Potential Bounce: fastest MA turned Bullish while the longer MAs remain Bearish (not a buy signal)")
// ============================================================================
// LABELS + DASHBOARD (last bar only)
// ============================================================================
var array<color> idColors = array.from(#FF9800, #FFB74D, #FFEB3B, #FFC107, #F48FB1, #EC407A, #FF4081, #2962FF, #00BFFF)
var array<color> txtColors = array.from(color.black, color.black, color.black, color.black, color.black, color.black, color.black, color.white, color.black)
var array<label> labels = array.new<label>(9)
string lblSize = f_size(lblSizeIn)
string dashSize = f_size(dashSizeIn)
var table dash = table.new(f_pos(dashPosIn), 2, 18, bgcolor=#1E222D, frame_color=#363A45, frame_width=1, border_color=#363A45, border_width=1)
if barstate.islast
array<float> mas = array.from(m1, m2, m3, m4, m5, m6, m7, m8, m9)
array<bool> ens = array.from(en1, en2, en3, en4, en5, en6, en7, en8, en9)
// ----- Labels: one persistent label per MA, updated in place -----
for i = 0 to 8
float v = array.get(mas, i)
label lbl = array.get(labels, i)
if showLabels and not na(v)
int lx = bar_index + 2 + (i % 3) * 3
string txt = "MA " + str.tostring(array.get(lens, i))
if na(lbl)
lbl := label.new(lx, v, txt, xloc=xloc.bar_index, style=label.style_label_left, color=array.get(idColors, i), textcolor=array.get(txtColors, i), size=lblSize)
array.set(labels, i, lbl)
else
label.set_xy(lbl, lx, v)
label.set_text(lbl, txt)
label.set_size(lbl, lblSize)
else if not na(lbl)
label.delete(lbl)
array.set(labels, i, na)
// ----- Price position -----
int above = 0
int below = 0
for i = 0 to 8
float v = array.get(mas, i)
if not na(v)
if close > v
above += 1
else if close < v
below += 1
// ----- Dashboard -----
if showDash
string overallTxt = overall == 1 ? "Strong Bullish" : overall == -1 ? "Strong Bearish" : overall == 2 ? "Mixed trend" : "Insufficient data (need 3+ MAs)"
color overallCol = overall == 1 ? bullColor : overall == -1 ? bearColor : overall == 2 ? color.yellow : color.gray
string ctxTxt = ctx == 1 ? "Potential Correction" : ctx == 2 ? "Potential Bounce" : "None"
color ctxCol = ctx == 1 ? color.orange : ctx == 2 ? #2962FF : color.gray
string stackTxt = not stackReady ? "N/A (needs 20/50/100/200)" : stackBull ? "Bullish (20>50>100>200)" : stackBear ? "Bearish (20<50<100<200)" : "None"
color stackCol = stackBull ? bullColor : stackBear ? bearColor : color.gray
f_row(dash, 0, "Multi-MA Trend Lines – Pro", "", color.white, dashSize)
f_row(dash, 1, "Context", ctxTxt, ctxCol, dashSize)
for i = 0 to 8
if array.get(ens, i)
int s = array.get(sts, i)
f_row(dash, 2 + i, "MA " + str.tostring(array.get(lens, i)), f_stateTxt(s), f_stateCol(s), dashSize)
else
f_row(dash, 2 + i, "", "", color.gray, dashSize)
f_row(dash, 11, "Overall trend", overallTxt, overallCol, dashSize)
f_row(dash, 12, "Bullish MAs", str.tostring(bullN), bullColor, dashSize)
f_row(dash, 13, "Bearish MAs", str.tostring(bearN), bearColor, dashSize)
f_row(dash, 14, "Neutral MAs", str.tostring(neuN), neutralColor, dashSize)
f_row(dash, 15, "Enabled MAs", str.tostring(totN), color.white, dashSize)
f_row(dash, 16, "Price above / below", str.tostring(above) + " / " + str.tostring(below), color.white, dashSize)
f_row(dash, 17, "MA stacking", stackTxt, stackCol, dashSize)