本页源码来自 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/
// © NJ_Algo
//@version=6
indicator("Multi-Timeframe Z-Score Trend [NJ]", "MTZST", overlay = false, behind_chart = false)
// ███╗ ██╗ ██╗ █████╗ ██╗ ██████╗ ██████╗
// ████╗ ██║ ██║ ██╔══██╗██║ ██╔════╝ ██╔═══██╗
// ██╔██╗ ██║ ██║ ███████║██║ ██║ ███╗██║ ██║
// ██║╚██╗██║ ██║ ██╔══██║██║ ██║ ██║██║ ██║
// ██║ ╚████║ ██╗ ██║ ██║ ██║██║ ██║ ██║██║ ██║
// ██║ ╚███║ ╚████╔╝ ██║ ██║███████╗╚██████╔╝╚██████╔╝
// ╚═╝ ╚══╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝
// ✧═════════════════✧
// ║ INPUTS ║
// ✧═════════════════✧
const string g1 = "✧ DISPLAY ✧"
dispMode = input.string("Average", "Display mode", options = ["Average", "All"], group = g1)
colorMode = input.string("Colored", "Color mode", options = ["Colored", "Neutral"], group = g1)
dispCandles = input.bool(true, "Color candles", group = g1)
const string g2 = "✧ CALCULATION ✧"
zsSrc = input.source(high, "Z-Score source", group = g2)
zsLen = input.int(88, "Z-Score length", group = g2)
longThr = input.float(2.0, "Long threshold", step = 0.05, group = g2)
shortThr = input.float(-1.0, "Short threshold", step = 0.05, group = g2)
// ✧══════════════════════✧
// ║ CALCULATION ║
// ✧══════════════════════✧
// Z-Score function
zScore() =>
mean = ta.sma(zsSrc, zsLen)
dev = ta.stdev(zsSrc, zsLen)
zScore = dev > 0 ? (zsSrc - mean) / dev : na
// Anti-repainting request security function
f_zs(tf) =>
htf = timeframe.in_seconds(tf) > timeframe.in_seconds(timeframe.period)
request.security(syminfo.tickerid, tf, zScore()[htf ? 1 : 0], lookahead = htf ? barmerge.lookahead_on : barmerge.lookahead_off)
// Get multi-timeframe Z-Scores
zs1 = f_zs("180") // 3H
zs2 = f_zs("360") // 6H
zs3 = f_zs("480") // 8H
zs4 = f_zs("720") // 12H
// Average Z-Score
avgZs = (zs1 + zs2 + zs3 + zs4) / 4
// ✧═══════════════════════✧
// ║ FINAL SIGNAL ║
// ✧═══════════════════════✧
var int NJ = 0
if ta.crossover(avgZs, longThr)
NJ := 1
if ta.crossunder(avgZs, shortThr)
NJ := -1
// ✧════════════════✧
// ║ PLOTS ║
// ✧════════════════✧
// Colors
longCol = colorMode == "Colored" ? color.rgb(0, 136, 100) : colorMode == "Neutral" ? color.rgb(179, 179, 179) : na
shortCol = colorMode == "Colored" ? color.rgb(120, 0, 32) : colorMode == "Neutral" ? color.rgb(60, 60, 60) : na
sigCol = NJ == 1 ? longCol : NJ == -1 ? shortCol : na
// Color candles
plotcandle(
open, high, low, close,
"Colored candles",
dispCandles ? sigCol : na,
dispCandles ? sigCol : na,
bordercolor = dispCandles ? sigCol : na,
display = display.all - display.status_line - display.price_scale,
force_overlay = true)
// Display score and Z-Scores
plot(dispMode == "Average" ? avgZs : na, "Average", sigCol, 2)
plot(dispMode == "All" ? zs1 : na, "3H", sigCol, 1)
plot(dispMode == "All" ? zs2 : na, "6H", sigCol, 1)
plot(dispMode == "All" ? zs3 : na, "8H", sigCol, 1)
plot(dispMode == "All" ? zs4 : na, "12H", sigCol, 1)
hline(longThr, "Long threshold", longCol)
hline(shortThr, "Short threshold", shortCol)