Fakeout & Swing High/Low Signals [JPT]

Jos-ProTrader · study · 334 行 · 点赞 408 · 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("Fakeout & Swing High/Low Signals [JPT]",
     shorttitle="Fakeout Signals [JPT]",
     overlay=true,
     max_lines_count=300,
     max_labels_count=300)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 01. SETTINGS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
groupSwing = "01. Swing Detection"
 
pivotLen = input.int(16, "Pivot Strength", minval=2, group=groupSwing)
showLabels = input.bool(true, "Show Swing Labels", group=groupSwing)
showLevels = input.bool(true, "Show Swing Levels", group=groupSwing)
extendLevels = input.bool(false, "Extend Swing Levels", group=groupSwing)
maxLevels = input.int(20, "Maximum Stored Levels", minval=5, maxval=100, group=groupSwing)
 
groupStrength = "02. Swing Strength"
 
atrLen = input.int(14, "ATR Length", minval=1, group=groupStrength)
atrFactor = input.float(1.0, "ATR Strength", step=0.1, group=groupStrength)
wickFactor = input.float(0.40, "Minimum Wick %", step=0.05, group=groupStrength)
bodyFactor = input.float(0.50, "Minimum Body %", step=0.05, group=groupStrength)
 
groupFake = "03. Fake Breakout Detection"
 
showFakeSignals = input.bool(true, "Show Fake Breakout Signals", group=groupFake)
requireCandleColor = input.bool(false, "Require Reversal Candle Color", group=groupFake)
minWickPercent = input.float(0.25, "Minimum Sweep Wick %", step=0.05, group=groupFake)
atrSweepFilter = input.bool(false, "Enable ATR Sweep Filter", group=groupFake)
minSweepATR = input.float(0.10, "Minimum Sweep ATR", step=0.05, group=groupFake)
 
groupVisual = "04. Visual Settings"
 
showBreakMarkers = input.bool(true, "Show Break Markers", group=groupVisual)
showStrengthLabels = input.bool(true, "Show Strength Labels", group=groupVisual)
showCurrentSwing = input.bool(true, "Show Current Swing Lines", group=groupVisual)
 
groupAlerts = "05. Alerts"
 
enableAlerts = input.bool(true, "Enable Alert Conditions", group=groupAlerts)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 02. INDICATORS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
atr = ta.atr(atrLen)
 
pivotHigh = ta.pivothigh(high, pivotLen, pivotLen)
pivotLow = ta.pivotlow(low, pivotLen, pivotLen)
 
bodySize = math.abs(close - open)
rangeSize = high - low
 
upperWick = high - math.max(open, close)
lowerWick = math.min(open, close) - low
 
bodyPercent = rangeSize > 0 ? bodySize / rangeSize : 0.0
upperPercent = rangeSize > 0 ? upperWick / rangeSize : 0.0
lowerPercent = rangeSize > 0 ? lowerWick / rangeSize : 0.0
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 03. STORAGE
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
var float lastSwingHigh = na
var float lastSwingLow = na
 
var float previousSwingHigh = na
var float previousSwingLow = na
 
var int lastHighBar = na
var int lastLowBar = na
 
var bool highBroken = false
var bool lowBroken = false
 
var line[] highLines = array.new_line()
var line[] lowLines = array.new_line()
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 04. SWING HIGH DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if not na(pivotHigh)
 
    swingBar = bar_index - pivotLen
 
    distanceHigh = na(previousSwingHigh) ? atr[pivotLen] :
         math.abs(pivotHigh - previousSwingHigh)
 
    scoreHigh = 0
 
    if distanceHigh > atr[pivotLen] * atrFactor
        scoreHigh += 1
 
    if upperPercent[pivotLen] > wickFactor
        scoreHigh += 1
 
    if bodyPercent[pivotLen] > bodyFactor
        scoreHigh += 1
 
    strengthHigh = scoreHigh == 3 ? "Strong High" :
         scoreHigh == 2 ? "Normal High" : "Weak High"
 
    highColor = scoreHigh == 3 ? color.red :
         scoreHigh == 2 ? color.orange : color.gray
 
    lastSwingHigh := pivotHigh
    lastHighBar := swingBar
    highBroken := false
 
    if showLabels and showStrengthLabels
        label.new(
             swingBar,
             pivotHigh,
             strengthHigh,
             style=label.style_label_down,
             color=highColor,
             textcolor=color.white,
             size=size.tiny)
 
    if showLevels
        newHighLine = line.new(
             swingBar,
             pivotHigh,
             bar_index,
             pivotHigh,
             color=color.red,
             width=2,
             extend=extendLevels ? extend.right : extend.none)
 
        array.push(highLines, newHighLine)
 
        if array.size(highLines) > maxLevels
            line.delete(array.shift(highLines))
 
    previousSwingHigh := pivotHigh
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 05. SWING LOW DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
if not na(pivotLow)
 
    swingBar = bar_index - pivotLen
 
    distanceLow = na(previousSwingLow) ? atr[pivotLen] :
         math.abs(pivotLow - previousSwingLow)
 
    scoreLow = 0
 
    if distanceLow > atr[pivotLen] * atrFactor
        scoreLow += 1
 
    if lowerPercent[pivotLen] > wickFactor
        scoreLow += 1
 
    if bodyPercent[pivotLen] > bodyFactor
        scoreLow += 1
 
    strengthLow = scoreLow == 3 ? "Strong Low" :
         scoreLow == 2 ? "Normal Low" : "Weak Low"
 
    lowColor = scoreLow == 3 ? color.lime :
         scoreLow == 2 ? color.yellow : color.gray
 
    lastSwingLow := pivotLow
    lastLowBar := swingBar
    lowBroken := false
 
    if showLabels and showStrengthLabels
        label.new(
             swingBar,
             pivotLow,
             strengthLow,
             style=label.style_label_up,
             color=lowColor,
             textcolor=color.black,
             size=size.tiny)
 
    if showLevels
        newLowLine = line.new(
             swingBar,
             pivotLow,
             bar_index,
             pivotLow,
             color=color.lime,
             width=2,
             extend=extendLevels ? extend.right : extend.none)
 
        array.push(lowLines, newLowLine)
 
        if array.size(lowLines) > maxLevels
            line.delete(array.shift(lowLines))
 
    previousSwingLow := pivotLow
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 06. SWING BREAK DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
isAboveSwingHigh = not na(lastSwingHigh) and high > lastSwingHigh
isBelowSwingLow = not na(lastSwingLow) and low < lastSwingLow
 
bullishBreak = not na(lastSwingHigh) and close > lastSwingHigh and not highBroken
bearishBreak = not na(lastSwingLow) and close < lastSwingLow and not lowBroken
 
if bullishBreak
    highBroken := true
 
if bearishBreak
    lowBroken := true
 
plotshape(
     showBreakMarkers and bullishBreak,
     title="Bullish Swing Break",
     style=shape.circle,
     location=location.abovebar,
     color=color.lime,
     size=size.tiny)
 
plotshape(
     showBreakMarkers and bearishBreak,
     title="Bearish Swing Break",
     style=shape.circle,
     location=location.belowbar,
     color=color.red,
     size=size.tiny)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 07. FAKE BREAKOUT DETECTION
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
// Minimum sweep distance
 
bullSweepDistance = not na(lastSwingLow) ? lastSwingLow - low : 0.0
bearSweepDistance = not na(lastSwingHigh) ? high - lastSwingHigh : 0.0
 
bullSweepValid = not atrSweepFilter or bullSweepDistance >= atr * minSweepATR
bearSweepValid = not atrSweepFilter or bearSweepDistance >= atr * minSweepATR
 
// Bullish liquidity sweep and reclaim
 
bullishFakeBreakout =
     not na(lastSwingLow) and
     low < lastSwingLow and
     close > lastSwingLow and
     lowerPercent >= minWickPercent and
     bullSweepValid and
     (not requireCandleColor or close > open) and
     barstate.isconfirmed
 
// Bearish liquidity sweep and rejection
 
bearishFakeBreakout =
     not na(lastSwingHigh) and
     high > lastSwingHigh and
     close < lastSwingHigh and
     upperPercent >= minWickPercent and
     bearSweepValid and
     (not requireCandleColor or close < open) and
     barstate.isconfirmed
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 08. REVERSAL SIGNALS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plotshape(
     showFakeSignals and bullishFakeBreakout,
     title="Bullish Fake Breakout",
     style=shape.labelup,
     location=location.belowbar,
     color=color.lime,
     text="BUY",
     textcolor=color.black,
     size=size.small)
 
plotshape(
     showFakeSignals and bearishFakeBreakout,
     title="Bearish Fake Breakout",
     style=shape.labeldown,
     location=location.abovebar,
     color=color.red,
     text="SELL",
     textcolor=color.white,
     size=size.small)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 09. CURRENT SWING LEVELS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
plot(
     showCurrentSwing ? lastSwingHigh : na,
     title="Current Swing High",
     color=color.new(color.red, 15),
     linewidth=2,
     style=plot.style_linebr)
 
plot(
     showCurrentSwing ? lastSwingLow : na,
     title="Current Swing Low",
     color=color.new(color.lime, 15),
     linewidth=2,
     style=plot.style_linebr)
 
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 10. ALERT CONDITIONS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 
alertcondition(
     enableAlerts and bullishFakeBreakout,
     title="Bullish Fake Breakout",
     message="JPT: Bullish Fake Breakout detected. Price swept below swing low and reclaimed it.")
 
alertcondition(
     enableAlerts and bearishFakeBreakout,
     title="Bearish Fake Breakout",
     message="JPT: Bearish Fake Breakout detected. Price swept above swing high and rejected it.")
 
alertcondition(
     enableAlerts and bullishBreak,
     title="Bullish Swing Break",
     message="JPT: Bullish Swing High Break detected.")
 
alertcondition(
     enableAlerts and bearishBreak,
     title="Bearish Swing Break",
     message="JPT: Bearish Swing Low Break detected.")

← 返回列表