High vs Previous Low % + Slope Signal

hasanaksoy199264 · study · 54 行 · 点赞 18 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("High vs Previous Low % + Slope Signal", shorttitle = "HPL Slope", overlay = true, max_labels_count = 500)
 
// ── Inputs ──
barCount   = input.int(50, "Number of bars to display", minval = 5, maxval = 500)
textSize   = input.string(size.small, "Label text size", options = [size.tiny, size.small, size.normal])
posColor   = input.color(color.green, "Positive color")
negColor   = input.color(color.red, "Negative color")
highColor  = input.color(color.new(color.aqua, 0), "High line color")
lowColor   = input.color(color.new(color.orange, 0), "Low line color")
multiplier = input.float(2.0, "Slope change threshold (K × std)", minval = 0.5, step = 0.1,
     tooltip = "A signal is triggered when the absolute slope change exceeds the mean + K × standard deviation.")
 
// ── Period: last N bars ──
inRange = bar_index > last_bar_index - barCount
 
// ── Calculation: (high - previous low) / previous low * 100 ──
pct = low[1] > 0 ? (high - low[1]) / low[1] * 100 : na
 
// ── Line slopes (% change per bar) ──
slopeHigh = high[1] > 0 ? (high - high[1]) / high[1] * 100 : na
slopeLow  = low[1]  > 0 ? (low  - low[1])  / low[1]  * 100 : na
slope     = (slopeHigh + slopeLow) / 2   // combined slope of both lines
 
// ── Slope change compared to the previous bar ──
slopeChange = slope - slope[1]
absChange   = math.abs(slopeChange)
 
// ── Threshold: mean + K × std over the last N bars ──
threshold = ta.sma(absChange, barCount) + multiplier * ta.stdev(absChange, barCount)
 
hasSignal   = inRange and not na(absChange) and absChange > threshold
longSignal  = hasSignal and slope > 0
shortSignal = hasSignal and slope < 0
 
// ── Thin lines connecting highs and lows ──
plot(inRange ? high : na, "High line", color = highColor, linewidth = 1)
plot(inRange ? low  : na, "Low line",  color = lowColor,  linewidth = 1)
 
// ── Percentage labels ──
if inRange and not na(pct)
    label.new(bar_index, low, str.tostring(pct, "#.##") + "%",
         style = label.style_label_up,
         color = color.new(color.gray, 100),
         textcolor = pct >= 0 ? posColor : negColor,
         size = textSize)
 
// ── LONG / SHORT markers ──
plotshape(longSignal,  "LONG",  shape.triangleup,   location.belowbar, color = color.lime, size = size.small, text = "LONG")
plotshape(shortSignal, "SHORT", shape.triangledown, location.abovebar, color = color.red,  size = size.small, text = "SHORT")
 
// ── Alerts ──
alertcondition(longSignal,  "LONG signal",  "Sharp slope change, direction up: LONG")
alertcondition(shortSignal, "SHORT signal", "Sharp slope change, direction down: SHORT")

← 返回列表