本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rachainta987
//@version=6
indicator("Trend Matrix V12", overlay=true)
// Trend Matrix V12
// Multi-layer Trend Structure Framework
// Features:
// • Six customizable Moving Averages
// • Trend Ribbon
// • Market Structure (BOS / CHOCH)
// • Multi-level Cross Signals
// • Trading Session Visualization
// =====================================================
// INPUTS
// =====================================================
len1 = input.int(7, title = 'MA1 Length')
type1 = input.string('EMA', title = 'MA1 Type', options = ['SMA', 'EMA', 'SMMA'])
show1 = input.bool(true, title = 'Show MA1')
len2 = input.int(20, title = 'MA2 Length')
type2 = input.string('EMA', title = 'MA2 Type', options = ['SMA', 'EMA', 'SMMA'])
show2 = input.bool(true, title = 'Show MA2')
len3 = input.int(60, title = 'MA3 Length')
type3 = input.string('SMA', title = 'MA3 Type', options = ['SMA', 'EMA', 'SMMA'])
show3 = input.bool(true, title = 'Show MA3')
len4 = input.int(200, title = 'MA4 Length')
type4 = input.string('SMMA', title = 'MA4 Type', options = ['SMA', 'EMA', 'SMMA'])
show4 = input.bool(true, title = 'Show MA4')
len5 = input.int(100, title = 'MA5 Length')
type5 = input.string('EMA', title = 'MA5 Type', options = ['SMA', 'EMA', 'SMMA'])
show5 = input.bool(true, title = 'Show MA5')
len6 = input.int(200, title = 'MA6 Length')
type6 = input.string('EMA', title = 'MA6 Type', options = ['SMA', 'EMA', 'SMMA'])
show6 = input.bool(true, title = 'Show MA6')
swingLen = input.int(2, title = 'Swing Length')
showStructure = input.bool(true, 'Show BOS / CHOCH')
showCrossLevels = input.bool(true, 'Show Cross Levels')
showRibbon = input.bool(true, "Show MA2 ↔ MA3 Ribbon")
// =====================================================
// SESSION MODULE
// =====================================================
showSessions = input.bool(true, 'Show Sessions')
showTokyo = input.bool(true, 'Tokyo')
showLondon = input.bool(true, 'London')
showNewYork = input.bool(true, 'New York')
// =====================================================
// MA CALCULATIONS
// =====================================================
ma1 = type1 == 'SMA' ? ta.sma(close, len1) : type1 == 'EMA' ? ta.ema(close, len1) : ta.rma(close, len1)
ma2 = type2 == 'SMA' ? ta.sma(close, len2) : type2 == 'EMA' ? ta.ema(close, len2) : ta.rma(close, len2)
ma3 = type3 == 'SMA' ? ta.sma(close, len3) : type3 == 'EMA' ? ta.ema(close, len3) : ta.rma(close, len3)
ma4 = type4 == 'SMA' ? ta.sma(close, len4) : type4 == 'EMA' ? ta.ema(close, len4) : ta.rma(close, len4)
ma5 = type5 == 'SMA' ? ta.sma(close, len5) : type5 == 'EMA' ? ta.ema(close, len5) : ta.rma(close, len5)
ma6 = type6 == 'SMA' ? ta.sma(close, len6) : type6 == 'EMA' ? ta.ema(close, len6) : ta.rma(close, len6)
// =====================================================
// PLOTS
// =====================================================
plot(show1 ? ma1 : na, color = color.lime, linewidth = 1)
pMA2 = plot(show2 ? ma2 : na, title="MA2", color=color.blue, linewidth=1)
pMA3 = plot(show3 ? ma3 : na, title="MA3", color=color.rgb(245,18,215), linewidth=2)
plot(show4 ? ma4 : na, color = color.rgb(7, 155, 81), linewidth = 2)
plot(show5 ? ma5 : na, color = color.rgb(238, 6, 6), linewidth = 1)
plot(show6 ? ma6 : na, color = color.rgb(0, 12, 11), linewidth = 1)
//=====================================================
// MA2 ↔ MA3 Ribbon
//=====================================================
fill(
pMA2,
pMA3,
color = showRibbon ?
(ma2 >= ma3 ?
color.new(color.lime, 85) :
color.new(color.red, 85))
: na,
title = "MA2 ↔ MA3 Ribbon"
)
// =====================================================
// THAILAND SESSION TIME (GMT+7)
// =====================================================
hourTH = hour(time, 'Asia/Bangkok')
minuteTH = minute(time, 'Asia/Bangkok')
thaiTime = hourTH * 100 + minuteTH
tokyo = thaiTime >= 800 and thaiTime < 1700
london = thaiTime >= 1500 and thaiTime < 2400
newyork = thaiTime >= 2000 or thaiTime < 500
tokyoOpen = tokyo and not tokyo[1]
londonOpen = london and not london[1]
newyorkOpen = newyork and not newyork[1]
if showSessions and showTokyo and tokyoOpen
line.new(bar_index, low, bar_index, high, extend = extend.both, color = color.rgb(10, 8, 0), style = line.style_dashed)
label.new(bar_index, high, 'TOKYO', style = label.style_none, color = color.rgb(43, 42, 41), textcolor = color.rgb(240, 139, 6))
if showSessions and showLondon and londonOpen
line.new(bar_index, low, bar_index, high, extend = extend.both, color = color.blue, style = line.style_dashed)
label.new(bar_index, high, 'LONDON', style = label.style_none, textcolor = color.blue)
if showSessions and showNewYork and newyorkOpen
line.new(bar_index, low, bar_index, high, extend = extend.both, color = color.rgb(43, 44, 44), style = line.style_dashed)
label.new(bar_index, high, 'NEW YORK', style = label.style_none, color = color.rgb(12, 13, 14), textcolor = color.rgb(19, 18, 18))
// =====================================================
// SIGNALS
// =====================================================
buy1 = ta.crossover(ma1, ma2)
buy2 = ta.crossover(ma1, ma3)
buy3 = ta.crossover(ma2, ma3)
sell1 = ta.crossunder(ma1, ma2)
sell2 = ta.crossunder(ma1, ma3)
sell3 = ta.crossunder(ma2, ma3)
plotshape(buy1, location = location.belowbar, color = color.lime, style = shape.triangleup, text = 'B1', offset = -1)
plotshape(buy2, location = location.belowbar, color = color.green, style = shape.circle, text = 'B2', offset = -1)
plotshape(buy3, location = location.belowbar, color = color.aqua, style = shape.square, text = 'B3', offset = -1)
plotshape(sell1, location = location.abovebar, color = color.red, style = shape.triangledown, text = 'S1', offset = -1)
plotshape(sell2, location = location.abovebar, color = color.maroon, style = shape.circle, text = 'S2', offset = -1)
plotshape(sell3, location = location.abovebar, color = color.orange, style = shape.square, text = 'S3', offset = -1)
// =====================================================
// STRUCTURE (BOS / CHOCH)
// =====================================================
pivotHigh = ta.pivothigh(high, swingLen, swingLen)
pivotLow = ta.pivotlow(low, swingLen, swingLen)
var float lastHigh = na
var float lastLow = na
var int lastHighBar = na
var int lastLowBar = na
if not na(pivotHigh)
lastHigh := pivotHigh
lastHighBar := bar_index[swingLen]
lastHighBar
if not na(pivotLow)
lastLow := pivotLow
lastLowBar := bar_index[swingLen]
lastLowBar
body = math.abs(close - open)
rng = high - low
validMove = rng > 0 ? body / rng > 0.35 : false
bullBreak = not na(lastHigh) and close > lastHigh and close[1] <= lastHigh and validMove
bearBreak = not na(lastLow) and close < lastLow and close[1] >= lastLow and validMove
var int trend = 0
bullChoch = bullBreak and trend <= 0
bullBos = bullBreak and trend == 1
bearChoch = bearBreak and trend >= 0
bearBos = bearBreak and trend == -1
if bullBreak
trend := 1
trend
if bearBreak
trend := -1
trend
// =====================================================
// LINE STORAGE (IMPORTANT FIX)
// =====================================================
var array<line> structureLines = array.new_line()
var array<line> crossLines = array.new_line()
maxLines = input.int(100, 'Max Saved Lines')
if array.size(structureLines) > maxLines
line.delete(array.shift(structureLines))
if array.size(crossLines) > maxLines
line.delete(array.shift(crossLines))
// =====================================================
// STRUCTURE DRAW
// =====================================================
if showStructure and (bullChoch or bullBos)
l = line.new(lastHighBar, lastHigh, bar_index, lastHigh, color = color.green, width = 2)
array.push(structureLines, l)
label.new(lastHighBar + 2, lastHigh, bullBos ? 'bos' : 'choch', textcolor = color.green, style = label.style_none)
if showStructure and (bearChoch or bearBos)
l = line.new(lastLowBar, lastLow, bar_index, lastLow, color = color.red, width = 2)
array.push(structureLines, l)
label.new(lastLowBar + 2, lastLow, bearBos ? 'bos' : 'choch', textcolor = color.red, style = label.style_none)
// =====================================================
// CROSS LEVELS DRAW
// =====================================================
if showCrossLevels and (buy1 or sell1)
l = line.new(bar_index - 3, ma2, bar_index + 1, ma2, color = buy1 ? color.rgb(57, 6, 240) : color.rgb(3, 0, 3), width = 3)
array.push(crossLines, l)
if showCrossLevels and (buy2 or sell2)
l = line.new(bar_index - 5, ma3, bar_index + 1, ma3, color = buy2 ? color.rgb(6, 174, 240) : color.rgb(5, 0, 0), width = 3)
array.push(crossLines, l)
// =====================================================
// TOGGLE CLEANUP (FIXED)
// =====================================================
if not showStructure and array.size(structureLines) > 0
for i = 0 to array.size(structureLines) - 1 by 1
line.delete(array.get(structureLines, i))
array.clear(structureLines)
if not showCrossLevels and array.size(crossLines) > 0
for i = 0 to array.size(crossLines) - 1 by 1
line.delete(array.get(crossLines, i))
array.clear(crossLines)
// =====================================================
// SESSION BACKGROUND
// =====================================================
sessionColor = showLondon and showNewYork and london and newyork ? color.new(color.purple, 85) : showTokyo and showLondon and tokyo and london ? color.new(color.teal, 85) : showTokyo and tokyo ? color.new(#f39d2d, 85) : showLondon and london ? color.new(color.blue, 85) : showNewYork and newyork ? color.new(#887979, 85) : na
bgcolor(showSessions ? sessionColor : na)