本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("ms1 - Marci Silfran (Full-Day Trendlines & Multi-Timeframe Matrix)", overlay=true, max_lines_count=500)
// Inputs
lookbackBars = input.int(2, "Candle Lookback Window", options=[2, 3, 4, 5, 6, 8], group="Calculation Settings")
tablePosition = input.string("Top Right", "Table Position", options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group="Table Settings")
colorDowntrend = input.color(color.red, "Downtrend Color", group="Style Settings")
colorUptrend = input.color(color.green, "Uptrend Color", group="Style Settings")
var string tablePos = position.top_right
if tablePosition == "Top Left"
tablePos := position.top_left
else if tablePosition == "Bottom Right"
tablePos := position.bottom_right
else if tablePosition == "Bottom Left"
tablePos := position.bottom_left
// Core calculation function for secure multi-timeframe requests
f_getTimeframeStats(int lbBars) =>
var int lastTrend = 0
var int totalSignals = 0
var int successfulHits = 0
var float activeTargetPrice = na
var float activeAnchorPrice = na
var int activeDirection = 0
if activeDirection == 1
if high >= activeTargetPrice
successfulHits += 1
activeDirection := 0
else if low < activeAnchorPrice
activeDirection := 0
else if activeDirection == -1
if low <= activeTargetPrice
successfulHits += 1
activeDirection := 0
else if high > activeAnchorPrice
activeDirection := 0
if bar_index >= (lbBars - 1)
int anchorOffset = lbBars - 1
isHH = ta.highestbars(high, lbBars) == -anchorOffset
isLL = ta.lowestbars(low, lbBars) == -anchorOffset
isLH = (high[anchorOffset] > high[anchorOffset + 1]) and (high[anchorOffset] > high[anchorOffset - 1]) and not isHH
isDowntrendValid = (isHH or isLH) and (high[anchorOffset] > high[0]) and (close[0] <= close[1])
isUptrendValid = isLL and (low[anchorOffset] < low[0]) and (close[0] >= close[1])
if isDowntrendValid and lastTrend != -1
lastTrend := -1
totalSignals += 1
float extremeLow = low[0]
for i = 0 to anchorOffset
if low[i] < extremeLow
extremeLow := low[i]
float distanceA = high[anchorOffset] - extremeLow
activeTargetPrice := high[0] - distanceA
activeAnchorPrice := high[anchorOffset]
activeDirection := -1
else if isUptrendValid and lastTrend != 1
lastTrend := 1
totalSignals += 1
float extremeHigh = high[0]
for i = 0 to anchorOffset
if high[i] > extremeHigh
extremeHigh := high[i]
float distanceA = extremeHigh - low[anchorOffset]
activeTargetPrice := low[0] + distanceA
activeAnchorPrice := low[anchorOffset]
activeDirection := 1
[totalSignals, successfulHits]
// Secure multi-timeframe requests
[t1_tot, t1_win] = request.security(syminfo.tickerid, "1", f_getTimeframeStats(lookbackBars))
[t2_tot, t2_win] = request.security(syminfo.tickerid, "2", f_getTimeframeStats(lookbackBars))
[t3_tot, t3_win] = request.security(syminfo.tickerid, "3", f_getTimeframeStats(lookbackBars))
[t5_tot, t5_win] = request.security(syminfo.tickerid, "5", f_getTimeframeStats(lookbackBars))
[t10_tot, t10_win] = request.security(syminfo.tickerid, "10", f_getTimeframeStats(lookbackBars))
[t15_tot, t15_win] = request.security(syminfo.tickerid, "15", f_getTimeframeStats(lookbackBars))
// Full-day historical chart rendering
var int chartLastTrend = 0
if bar_index >= (lookbackBars - 1)
int anchorOffset = lookbackBars - 1
isHH = ta.highestbars(high, lookbackBars) == -anchorOffset
isLL = ta.lowestbars(low, lookbackBars) == -anchorOffset
isLH = (high[anchorOffset] > high[anchorOffset + 1]) and (high[anchorOffset] > high[anchorOffset - 1]) and not isHH
isDowntrendValid = (isHH or isLH) and (high[anchorOffset] > high[0]) and (close[0] <= close[1])
isUptrendValid = isLL and (low[anchorOffset] < low[0]) and (close[0] >= close[1])
if isDowntrendValid and chartLastTrend != -1
chartLastTrend := -1
float extremeLow = low[0]
for i = 0 to anchorOffset
if low[i] < extremeLow
extremeLow := low[i]
float distanceA = high[anchorOffset] - extremeLow
float supportPrice = high[0] - distanceA
line.new(x1=bar_index - anchorOffset, y1=high[anchorOffset], x2=bar_index, y2=high[0], xloc=xloc.bar_index, color=colorDowntrend, width=2)
line.new(x1=bar_index - anchorOffset, y1=supportPrice, x2=bar_index + 3, y2=supportPrice, xloc=xloc.bar_index, color=colorDowntrend, style=line.style_dashed, width=2)
else if isUptrendValid and chartLastTrend != 1
chartLastTrend := 1
float extremeHigh = high[0]
for i = 0 to anchorOffset
if high[i] > extremeHigh
extremeHigh := high[i]
float distanceA = extremeHigh - low[anchorOffset]
float resistancePrice = low[0] + distanceA
line.new(x1=bar_index - anchorOffset, y1=low[anchorOffset], x2=bar_index, y2=low[0], xloc=xloc.bar_index, color=colorUptrend, width=2)
line.new(x1=bar_index - anchorOffset, y1=resistancePrice, x2=bar_index + 3, y2=resistancePrice, xloc=xloc.bar_index, color=colorUptrend, style=line.style_dashed, width=2)
// Table Display Helper
f_addRow(table t, int rowIdx, string tfLabel, int total, int wins) =>
float rate = total > 0 ? (wins / float(total)) * 100.0 : 0.0
string rateStr = str.tostring(rate, "#.##") + "% (" + str.tostring(wins) + "/" + str.tostring(total) + ")"
color bgCol = rate >= 50 ? color.new(color.green, 20) : color.new(color.red, 20)
table.cell(t, 0, rowIdx, tfLabel, text_color=color.white)
table.cell(t, 1, rowIdx, rateStr, bgcolor=bgCol, text_color=color.white)
// Multi-Timeframe Performance Matrix Table Display
var table matrixTable = table.new(tablePos, 2, 7, bgcolor=color.black, border_width=1, border_color=color.gray)
if barstate.islast
table.cell(matrixTable, 0, 0, "Timeframe", bgcolor=color.navy, text_color=color.white)
table.cell(matrixTable, 1, 0, "Success (" + str.tostring(lookbackBars) + "-Bar)", bgcolor=color.navy, text_color=color.white)
f_addRow(matrixTable, 1, "1 Min", t1_tot, t1_win)
f_addRow(matrixTable, 2, "2 Min", t2_tot, t2_win)
f_addRow(matrixTable, 3, "3 Min", t3_tot, t3_win)
f_addRow(matrixTable, 4, "5 Min", t5_tot, t5_win)
f_addRow(matrixTable, 5, "10 Min", t10_tot, t10_win)
f_addRow(matrixTable, 6, "15 Min", t15_tot, t15_win)