PINE LIBRARY

RSMPatternLib

26
Library "RSMPatternLib"
RSM Pattern Library - All chart patterns from PATTERNS.md
Implements: Candlestick patterns, Support/Resistance, Gaps, Triangles, Volume Divergence, and more
ALL PATTERNS ARE OWN IMPLEMENTATION - No external dependencies

EDGE CASES HANDLED:
- Zero/tiny candle bodies
- Missing volume data
- Low bar count scenarios
- Integer division issues
- Price normalization for different instruments

bullishEngulfing(minBodyRatio, minPrevBodyRatio)
  Detects Bullish Engulfing pattern
  Parameters:
    minBodyRatio (float): Minimum body size as ratio of total range (default 0.3)
    minPrevBodyRatio (float): Minimum previous candle body ratio to filter dojis (default 0.1)
  Returns: bool True when bullish engulfing detected
EDGE CASES: Handles doji previous candle, zero range, tiny bodies

bearishEngulfing(minBodyRatio, minPrevBodyRatio)
  Detects Bearish Engulfing pattern
  Parameters:
    minBodyRatio (float): Minimum body size as ratio of total range (default 0.3)
    minPrevBodyRatio (float): Minimum previous candle body ratio to filter dojis (default 0.1)
  Returns: bool True when bearish engulfing detected
EDGE CASES: Handles doji previous candle, zero range, tiny bodies

doji(maxBodyRatio, minRangeAtr)
  Detects Doji candle (indecision)
  Parameters:
    maxBodyRatio (float): Maximum body size as ratio of total range (default 0.1)
    minRangeAtr (float): Minimum range as multiple of ATR to filter flat candles (default 0.3)
  Returns: bool True when doji detected
EDGE CASES: Filters out no-movement bars, handles zero range

shootingStar(wickMultiplier, maxLowerWickRatio, minBodyAtrRatio)
  Detects Shooting Star (bearish reversal)
  Parameters:
    wickMultiplier (float): Upper wick must be at least this times the body (default 2.0)
    maxLowerWickRatio (float): Lower wick max as ratio of body (default 0.5)
    minBodyAtrRatio (float): Minimum body size as ratio of ATR (default 0.1)
  Returns: bool True when shooting star detected
EDGE CASES: Handles zero body (uses range-based check), tiny bodies

hammer(wickMultiplier, maxUpperWickRatio, minBodyAtrRatio)
  Detects Hammer (bullish reversal)
  Parameters:
    wickMultiplier (float): Lower wick must be at least this times the body (default 2.0)
    maxUpperWickRatio (float): Upper wick max as ratio of body (default 0.5)
    minBodyAtrRatio (float): Minimum body size as ratio of ATR (default 0.1)
  Returns: bool True when hammer detected
EDGE CASES: Handles zero body (uses range-based check), tiny bodies

invertedHammer(wickMultiplier, maxLowerWickRatio)
  Detects Inverted Hammer (bullish reversal after downtrend)
  Parameters:
    wickMultiplier (float): Upper wick must be at least this times the body (default 2.0)
    maxLowerWickRatio (float): Lower wick max as ratio of body (default 0.5)
  Returns: bool True when inverted hammer detected
EDGE CASES: Same as shootingStar but requires bullish close

hangingMan(wickMultiplier, maxUpperWickRatio)
  Detects Hanging Man (bearish reversal after uptrend)
  Parameters:
    wickMultiplier (float): Lower wick must be at least this times the body (default 2.0)
    maxUpperWickRatio (float): Upper wick max as ratio of body (default 0.5)
  Returns: bool True when hanging man detected
NOTE: Identical to hammer - context (uptrend) determines meaning

morningStar(requireGap, minAvgBars)
  Detects Morning Star (3-candle bullish reversal)
  Parameters:
    requireGap (bool): Whether to require gap between candles (default false for crypto/forex)
    minAvgBars (int): Minimum bars for average body calculation (default 14)
  Returns: bool True when morning star pattern detected
EDGE CASES: Gap is optional, handles low bar count, uses shifted average

eveningStar(requireGap, minAvgBars)
  Detects Evening Star (3-candle bearish reversal)
  Parameters:
    requireGap (bool): Whether to require gap between candles (default false for crypto/forex)
    minAvgBars (int): Minimum bars for average body calculation (default 14)
  Returns: bool True when evening star pattern detected
EDGE CASES: Gap is optional, handles low bar count

gapUp()
  Detects Gap Up
  Returns: bool True when current bar opens above previous bar's high

gapDown()
  Detects Gap Down
  Returns: bool True when current bar opens below previous bar's low

gapSize()
  Returns gap size in price
  Returns: float Gap size (positive for gap up, negative for gap down, 0 for no gap)

gapPercent()
  Returns gap size as percentage
  Returns: float Gap size as percentage of previous close

gapType(volAvgLen, breakawayMinPct, highVolMult)
  Classifies gap type based on volume
  Parameters:
    volAvgLen (int): Length for volume average (default 20)
    breakawayMinPct (float): Minimum gap % for breakaway (default 1.0)
    highVolMult (float): Volume multiplier for high volume (default 1.5)
  Returns: string Gap type: "Breakaway", "Common", "Continuation", or "None"
EDGE CASES: Handles missing volume data, low bar count

swingHigh(leftBars, rightBars)
  Detects swing high using pivot
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
  Returns: float Swing high price or na

swingLow(leftBars, rightBars)
  Detects swing low using pivot
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
  Returns: float Swing low price or na

higherHigh(leftBars, rightBars, lookback)
  Checks if current swing high is higher than previous swing high
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): How many bars back to search for previous pivot (default 50)
  Returns: bool True when higher high pattern detected
EDGE CASES: Searches backwards for pivots instead of using var (library-safe)

higherLow(leftBars, rightBars, lookback)
  Checks if current swing low is higher than previous swing low
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): How many bars back to search for previous pivot (default 50)
  Returns: bool True when higher low pattern detected

lowerHigh(leftBars, rightBars, lookback)
  Checks if current swing high is lower than previous swing high
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): How many bars back to search for previous pivot (default 50)
  Returns: bool True when lower high pattern detected

lowerLow(leftBars, rightBars, lookback)
  Checks if current swing low is lower than previous swing low
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): How many bars back to search for previous pivot (default 50)
  Returns: bool True when lower low pattern detected

bullishTrend(leftBars, rightBars, lookback)
  Detects Bullish Trend (HH + HL within lookback)
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): Lookback period (default 50)
  Returns: bool True when making higher highs AND higher lows

bearishTrend(leftBars, rightBars, lookback)
  Detects Bearish Trend (LH + LL within lookback)
  Parameters:
    leftBars (int): Bars to left for pivot (default 5)
    rightBars (int): Bars to right for pivot (default 5)
    lookback (int): Lookback period (default 50)
  Returns: bool True when making lower highs AND lower lows

nearestResistance(lookback, leftBars, rightBars)
  Finds nearest resistance level above current price
  Parameters:
    lookback (int): Number of bars to look back (default 50)
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
  Returns: float Nearest resistance level or na
EDGE CASES: Pre-computes pivots, handles bounds properly

nearestSupport(lookback, leftBars, rightBars)
  Finds nearest support level below current price
  Parameters:
    lookback (int): Number of bars to look back (default 50)
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
  Returns: float Nearest support level or na

resistanceBreakout(lookback, leftBars, rightBars)
  Detects resistance breakout
  Parameters:
    lookback (int): Number of bars to look back (default 50)
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
  Returns: bool True when price breaks above resistance
EDGE CASES: Uses previous bar's resistance to avoid lookahead

supportBreakdown(lookback, leftBars, rightBars)
  Detects support breakdown
  Parameters:
    lookback (int): Number of bars to look back (default 50)
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
  Returns: bool True when price breaks below support

bullishVolumeDivergence(leftBars, rightBars, lookback)
  Detects Bullish Volume Divergence (price makes lower low, volume decreases)
  Parameters:
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
    lookback (int): Bars to search for previous pivot (default 50)
  Returns: bool True when bullish volume divergence detected
EDGE CASES: Library-safe (no var), searches for previous pivot

bearishVolumeDivergence(leftBars, rightBars, lookback)
  Detects Bearish Volume Divergence (price makes higher high, volume decreases)
  Parameters:
    leftBars (int): Pivot left bars (default 5)
    rightBars (int): Pivot right bars (default 5)
    lookback (int): Bars to search for previous pivot (default 50)
  Returns: bool True when bearish volume divergence detected

rangeContracting(lookback)
  Detects if price is in a contracting range (triangle formation)
  Parameters:
    lookback (int): Bars to analyze (default 20)
  Returns: bool True when range is contracting
EDGE CASES: Uses safe integer division, checks minimum lookback

ascendingTriangle(lookback, flatTolerance)
  Detects Ascending Triangle (flat top, rising bottom)
  Parameters:
    lookback (int): Bars to analyze (default 20)
    flatTolerance (float): Max normalized slope for "flat" line (default 0.002)
  Returns: bool True when ascending triangle detected
EDGE CASES: Safe division, normalized slope, minimum lookback

descendingTriangle(lookback, flatTolerance)
  Detects Descending Triangle (falling top, flat bottom)
  Parameters:
    lookback (int): Bars to analyze (default 20)
    flatTolerance (float): Max normalized slope for "flat" line (default 0.002)
  Returns: bool True when descending triangle detected

symmetricalTriangle(lookback, minSlope)
  Detects Symmetrical Triangle (converging trend lines)
  Parameters:
    lookback (int): Bars to analyze (default 20)
    minSlope (float): Minimum normalized slope magnitude (default 0.0005)
  Returns: bool True when symmetrical triangle detected

doubleBottom(tolerance, minSpanBars, lookback)
  Detects Double Bottom (W pattern) - OWN IMPLEMENTATION
Two swing lows at similar price levels with a swing high between them
  Parameters:
    tolerance (float): Max price difference between lows as % (default 3)
    minSpanBars (int): Minimum bars between the two lows (default 5)
    lookback (int): Max bars to search for pattern (default 100)
  Returns: bool True when double bottom detected

doubleTop(tolerance, minSpanBars, lookback)
  Detects Double Top (M pattern) - OWN IMPLEMENTATION
Two swing highs at similar price levels with a swing low between them
  Parameters:
    tolerance (float): Max price difference between highs as % (default 3)
    minSpanBars (int): Minimum bars between the two highs (default 5)
    lookback (int): Max bars to search for pattern (default 100)
  Returns: bool True when double top detected

tripleBottom(tolerance, minSpanBars, lookback)
  Detects Triple Bottom - OWN IMPLEMENTATION
Three swing lows at similar price levels
  Parameters:
    tolerance (float): Max price difference between lows as % (default 3)
    minSpanBars (int): Minimum total bars for pattern (default 10)
    lookback (int): Max bars to search for pattern (default 150)
  Returns: bool True when triple bottom detected

tripleTop(tolerance, minSpanBars, lookback)
  Detects Triple Top - OWN IMPLEMENTATION
Three swing highs at similar price levels
  Parameters:
    tolerance (float): Max price difference between highs as % (default 3)
    minSpanBars (int): Minimum total bars for pattern (default 10)
    lookback (int): Max bars to search for pattern (default 150)
  Returns: bool True when triple top detected

bearHeadShoulders()
  Detects Bearish Head and Shoulders (OWN IMPLEMENTATION)
Head is higher than both shoulders, shoulders roughly equal, with valid neckline
STRICT VERSION - requires proper structure, neckline, and minimum span
  Returns: bool True when bearish H&S detected

bullHeadShoulders()
  Detects Bullish (Inverse) Head and Shoulders (OWN IMPLEMENTATION)
Head is lower than both shoulders, shoulders roughly equal, with valid neckline
STRICT VERSION - requires proper structure, neckline, and minimum span
  Returns: bool True when bullish H&S detected

bearAscHeadShoulders()
  Detects Bearish Ascending Head and Shoulders (variant)
  Returns: bool True when pattern detected

bullAscHeadShoulders()
  Detects Bullish Ascending Head and Shoulders (variant)
  Returns: bool True when pattern detected

bearDescHeadShoulders()
  Detects Bearish Descending Head and Shoulders (variant)
  Returns: bool True when pattern detected

bullDescHeadShoulders()
  Detects Bullish Descending Head and Shoulders (variant)
  Returns: bool True when pattern detected

isSwingLow()
  Re-export: Detects swing low
  Returns: bool True when swing low detected

isSwingHigh()
  Re-export: Detects swing high
  Returns: bool True when swing high detected

swingHighPrice(idx)
  Re-export: Gets swing high price at index
  Parameters:
    idx (int): Index (0 = most recent)
  Returns: float Swing high price

swingLowPrice(idx)
  Re-export: Gets swing low price at index
  Parameters:
    idx (int): Index (0 = most recent)
  Returns: float Swing low price

swingHighBarIndex(idx)
  Re-export: Gets swing high bar index
  Parameters:
    idx (int): Index (0 = most recent)
  Returns: int Bar index of swing high

swingLowBarIndex(idx)
  Re-export: Gets swing low bar index
  Parameters:
    idx (int): Index (0 = most recent)
  Returns: int Bar index of swing low

cupBottom(smoothLen, minDepthAtr, maxDepthAtr)
  Detects Cup and Handle pattern formation
Uses price acceleration and depth analysis
  Parameters:
    smoothLen (int): Smoothing length for price (default 10)
    minDepthAtr (float): Minimum cup depth as ATR multiple (default 1.0)
    maxDepthAtr (float): Maximum cup depth as ATR multiple (default 5.0)
  Returns: bool True when potential cup bottom detected
EDGE CASES: Added depth filter, ATR validation

cupHandle(lookback, maxHandleRetraceRatio)
  Detects potential handle formation after cup
  Parameters:
    lookback (int): Bars to look back for cup (default 30)
    maxHandleRetraceRatio (float): Maximum handle retracement of cup depth (default 0.5)
  Returns: bool True when handle pattern detected

bullishPatternCount()
  Returns count of bullish patterns detected
  Returns: int Number of bullish patterns currently active

bearishPatternCount()
  Returns count of bearish patterns detected
  Returns: int Number of bearish patterns currently active

detectedPatterns()
  Returns string description of detected patterns
  Returns: string Comma-separated list of detected patterns

Thông báo miễn trừ trách nhiệm

Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.