本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("HTF Fractals", overlay=true, max_lines_count=500)
// === Inputs ===
htf = input.timeframe("D", "Higher Timeframe")
barsEachSide = input.int(2, "Bars each side", minval=1, maxval=20)
maxLevels = input.int(8, "Max levels to show", minval=1, maxval=50)
showHighs = input.bool(true, "Show High Fractals")
showLows = input.bool(true, "Show Low Fractals")
highColor = input.color(color.red, "High Fractal Color")
lowColor = input.color(color.green, "Low Fractal Color")
lineWidth = input.int(1, "Line Width", minval=1, maxval=4)
extendRight = input.bool(true, "Extend lines to the right")
// === Fractal function ===
f_getFractals() =>
bool isHigh = true
bool isLow = true
for i = 1 to barsEachSide
isHigh := isHigh and high[barsEachSide] > high[barsEachSide + i] and high[barsEachSide] > high[barsEachSide - i]
isLow := isLow and low[barsEachSide] < low[barsEachSide + i] and low[barsEachSide] < low[barsEachSide - i]
[isHigh ? high[barsEachSide] : na, isLow ? low[barsEachSide] : na]
// Request HTF data
[htfHighFractal, htfLowFractal] = request.security(syminfo.tickerid, htf, f_getFractals(), lookahead = barmerge.lookahead_off)
// === Arrays ===
var float[] highLevels = array.new_float()
var float[] lowLevels = array.new_float()
var line[] highLines = array.new_line()
var line[] lowLines = array.new_line()
// Add high fractal
if showHighs and not na(htfHighFractal)
bool isNew = array.size(highLevels) == 0 or htfHighFractal != array.get(highLevels, array.size(highLevels) - 1)
if isNew
array.push(highLevels, htfHighFractal)
if array.size(highLevels) > maxLevels
array.shift(highLevels)
if array.size(highLines) > 0
line.delete(array.shift(highLines))
// Add low fractal
if showLows and not na(htfLowFractal)
bool isNew = array.size(lowLevels) == 0 or htfLowFractal != array.get(lowLevels, array.size(lowLevels) - 1)
if isNew
array.push(lowLevels, htfLowFractal)
if array.size(lowLevels) > maxLevels
array.shift(lowLevels)
if array.size(lowLines) > 0
line.delete(array.shift(lowLines))
// Draw lines
if barstate.islast
// High lines
if array.size(highLines) != array.size(highLevels)
for ln in highLines
line.delete(ln)
array.clear(highLines)
for lvl in highLevels
ln = line.new(bar_index - 1, lvl, bar_index, lvl, color=highColor, width=lineWidth, extend=extendRight ? extend.right : extend.none)
array.push(highLines, ln)
else if array.size(highLines) > 0
for i = 0 to array.size(highLines) - 1
line.set_xy1(array.get(highLines, i), bar_index - 100, array.get(highLevels, i))
line.set_xy2(array.get(highLines, i), bar_index, array.get(highLevels, i))
line.set_extend(array.get(highLines, i), extendRight ? extend.right : extend.none)
// Low lines
if array.size(lowLines) != array.size(lowLevels)
for ln in lowLines
line.delete(ln)
array.clear(lowLines)
for lvl in lowLevels
ln = line.new(bar_index - 1, lvl, bar_index, lvl, color=lowColor, width=lineWidth, extend=extendRight ? extend.right : extend.none)
array.push(lowLines, ln)
else if array.size(lowLines) > 0
for i = 0 to array.size(lowLines) - 1
line.set_xy1(array.get(lowLines, i), bar_index - 100, array.get(lowLevels, i))
line.set_xy2(array.get(lowLines, i), bar_index, array.get(lowLevels, i))
line.set_extend(array.get(lowLines, i), extendRight ? extend.right : extend.none)