Pivot Breakout/Retest Signals[JPT]

Jos-ProTrader · study · 95 行 · 点赞 702 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Jos-ProTrader
 
//@version=6
indicator("Pivot Breakout/Retest Signals[JPT]", overlay=true, max_labels_count=500)
 
// ===== Inputs =====
pivotLen = input.int(14, "Pivot Length", minval=2)
retestTolerance = input.float(0.3, "Retest Tolerance (%)", step=0.1) / 100
emaFilter = input.bool(true, "Use EMA Trend Filter")
emaLength = input.int(50, "EMA Length")
 
// ===== EMA =====
ema = ta.ema(close, emaLength)
plot(emaFilter ? ema : na, color=color.orange, linewidth=2)
 
// ===== Pivot Detection =====
pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
 
var float lastPivotHigh = na
var float lastPivotLow = na
 
if not na(pivotHigh)
    lastPivotHigh := pivotHigh
 
if not na(pivotLow)
    lastPivotLow := pivotLow
 
plot(lastPivotHigh, color=color.red, linewidth=2, style=plot.style_linebr)
plot(lastPivotLow, color=color.lime, linewidth=2, style=plot.style_linebr)
 
// ===== Breakout Detection =====
bullBreakout = ta.crossover(close, lastPivotHigh)
bearBreakout = ta.crossunder(close, lastPivotLow)
 
// ===== Store Breakout Levels =====
var float bullLevel = na
var float bearLevel = na
 
var bool waitingBullRetest = false
var bool waitingBearRetest = false
 
if bullBreakout
    bullLevel := lastPivotHigh
    waitingBullRetest := true
 
if bearBreakout
    bearLevel := lastPivotLow
    waitingBearRetest := true
 
// ===== Retest Logic =====
bullRetest =
     waitingBullRetest and
     low <= bullLevel * (1 + retestTolerance) and
     low >= bullLevel * (1 - retestTolerance) and
     close > bullLevel
 
bearRetest =
     waitingBearRetest and
     high >= bearLevel * (1 - retestTolerance) and
     high <= bearLevel * (1 + retestTolerance) and
     close < bearLevel
 
// ===== EMA Filter =====
buySignal = bullRetest and (not emaFilter or close > ema)
sellSignal = bearRetest and (not emaFilter or close < ema)
 
// ===== Reset =====
if buySignal
    waitingBullRetest := false
 
if sellSignal
    waitingBearRetest := false
 
// ===== Plot Signals =====
plotshape(buySignal,
     title="Long",
     location=location.belowbar,
     style=shape.triangleup,
     color=color.lime,
     size=size.large,
     text="Long")
 
plotshape(sellSignal,
     title="Short",
     location=location.abovebar,
     style=shape.triangledown,
     color=color.red,
     size=size.large,
     text="Short")
 
// ===== Alerts =====
alertcondition(buySignal, title="Buy Alert", message="Pivot Breakout Retest BUY")
alertcondition(sellSignal, title="Sell Alert", message="Pivot Breakout Retest SELL")

← 返回列表