本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// © Mohd Asfa Asyari / asfaasyari@gmail.com
indicator("Tweezers Top & Bottom Rev1", overlay=true)
// --- Pattern Logic Inputs ---
int allowedThreshold = input.int(0, title="Allowed Ticks Threshold", minval=0, tooltip="Sets the maximum allowed difference in price ticks between the two candle highs/lows. Set to 0 for an exact price match. Higher values allow for slight imperfections in the pattern.")
// --- Visual Preferences ---
bool showShape = input.bool(true, title="Show Triangle Shapes & Text", tooltip="Toggle to show or hide the 'TB' and 'TT' visual markers on the chart. Note: Mobile and webhook alerts will still function even if this is turned off.")
bool showEma = input.bool(false, title="Show EMA Line", tooltip="Toggle to display the EMA trend filter line on the chart. You can change its color, thickness, and style in the 'Style' tab.")
// --- Timeframe Filter Inputs ---
int minTfInput = input.int(5, title="Minimum Timeframe (Minutes)", minval=1, tooltip="Hides visual signals and disables alerts on timeframes lower than this value. For example, entering '5' means the indicator will ignore 1-minute, 3-minute, and all seconds-based charts.")
// --- Trend & Volume Filtering Inputs ---
bool useTrendFilter = input.bool(false, title="Enable Trend Filtering?", tooltip="When enabled, Tweezer Bottoms will only trigger if the price is below the EMA (downtrend), and Tweezer Tops will only trigger if the price is above the EMA (uptrend).")
int emaLength = input.int(50, title="Trend Filter EMA Length", minval=1, tooltip="The length of the Exponential Moving Average (EMA) used to determine the current trend direction. Only active if 'Enable Trend Filtering' is checked.")
bool useVolumeFilter = input.bool(false, title="Enable Volume Confirmation?", tooltip="When enabled, patterns will only trigger if the second candle has a higher trading volume than the first candle, indicating stronger momentum behind the reversal.")
// Calculate threshold values based on asset minimum tick sizes
float tickSize = syminfo.mintick
float priceTolerance = allowedThreshold * tickSize
// Timeframe verification logic (Blocks seconds charts and intraday charts under the minTfInput)
bool isValidTimeframe = not timeframe.isseconds and (not timeframe.isintraday or timeframe.multiplier >= minTfInput)
// Trend Detection helper (Only actively blocks signals if user enables the option)
float emaFilter = ta.ema(close, emaLength)
bool isUptrend = not useTrendFilter or (close > emaFilter)
bool isDowntrend = not useTrendFilter or (close < emaFilter)
// Volume Confirmation helper
bool hasVolumeConfirm = not useVolumeFilter or (volume > volume[1])
// --- Candle Color Logic ---
bool isBullish = close > open
bool isBearish = close < open
// --- Core Pattern Definitions ---
// 1. Tweezer Bottom: Bearish candle followed by Bullish candle with matching lows + valid TF + volume confirm
bool isTweezerBottom = math.abs(low - low[1]) <= priceTolerance and isBearish[1] and isBullish and isDowntrend and isValidTimeframe and hasVolumeConfirm
// 2. Tweezer Top: Bullish candle followed by Bearish candle with matching highs + valid TF + volume confirm
bool isTweezerTop = math.abs(high - high[1]) <= priceTolerance and isBullish[1] and isBearish and isUptrend and isValidTimeframe and hasVolumeConfirm
// --- Visual Plotting ---
// Plot EMA Line (Formatting is managed natively in the Style tab)
plot(showEma ? emaFilter : na, title="Trend Filter EMA", color=color.blue, linewidth=1)
// Plots both the triangle and the text ONLY when showShape is ticked (true)
plotshape(isTweezerBottom and showShape, title="Tweezer Bottom", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny, text="TB", textcolor=color.green)
plotshape(isTweezerTop and showShape, title="Tweezer Top", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny, text="TT", textcolor=color.red)
// --- Mobile Notification Alerts ---
// 1. Individual Alert for Tweezer Bottom
alertcondition(isTweezerBottom,
title="Tweezer Bottom Formed",
message="Bullish Reversal: Tweezer Bottom detected on {{exchange}}:{{ticker}} ({{interval}} timeframe).\nPrice: {{close}}\nTime: {{time}}")
// 2. Individual Alert for Tweezer Top
alertcondition(isTweezerTop,
title="Tweezer Top Formed",
message="Bearish Reversal: Tweezer Top detected on {{exchange}}:{{ticker}} ({{interval}} timeframe).\nPrice: {{close}}\nTime: {{time}}")
// 3. Combined Static Alert for Both (TB and TT)
alertcondition(isTweezerBottom or isTweezerTop,
title="TB and TT Formed",
message="Tweezer Reversal detected on {{exchange}}:{{ticker}} ({{interval}} timeframe).\nPrice: {{close}}\nTime: {{time}}")