# ThinkScript Architect - Dynamic Pivot Scan
# Author: TheInvestorToolkit.com
# Description: Scans for stocks interacting with Pivot Levels (S1-S5, R1-R5)
# --- INSTALLATION ---
# 1. Copy this code.
# 2. Go to Studies > Edit Studies > "Create..."
# 3. Paste the code and name it "PivotScan". Click OK.
# 4. In the Scanner, Add Filter > Study > Search for "PivotScan".
# 5. Click the Pencil Icon to set your parameters.
# CRITICAL SETUP: Set your Stock Hacker Aggregation to the timeframe you want to check (e.g., 'Quarter', 'Month', 'Week').
declare upper;
# --- Inputs ---
input pivotSide = {default Support, Resistance};
input pivotLevel = {default "1", "2", "3", "4", "5"};
# Scan Mode:
# "Touched / Breached" = Did price hit this level at ANY time? (Includes breaks and bounces)
# "Holding Beyond" = Breakout State. Price is currently beyond the level.
# "Reclaimed / Reversal" = The Wick Play. Price hit the level but closed back inside (Bounce/Reject).
# "Near Level" = Proximity scan.
input ScanMode = {default "Touched / Breached", "Holding Beyond", "Reclaimed / Reversal", "Near Level"};
input percentThreshold = 2.0; # Percent range for "Near Level" logic
# --- Data Fetch ---
# We use [1] to get the full High/Low/Close of the COMPLETED previous Aggregation.
def PH = high[1];
def PL = low[1];
def PC = close[1];
# --- Pivot Math —
def P = (PH + PL + PC) / 3;
# --- Level Calculations ---
def R1 = P * 2 - PL;
def S1 = P * 2 - PH;
def R2 = P + (PH - PL);
def S2 = P - (PH - PL);
def R3 = P * 2 + (PH - 2 * PL);
def S3 = P * 2 - (2 * PH - PL);
def R4 = P * 3 + (PH - 3 * PL);
def S4 = P * 3 - (3 * PH - PL);
def R5 = P * 4 + (PH - 4 * PL);
def S5 = P * 4 - (4 * PH - PL);
# --- Target Level Selection ---
def targetLevel;
if (pivotSide == pivotSide.Support) {
if (pivotLevel == pivotLevel."1") {
targetLevel = S1;
} else if (pivotLevel == pivotLevel."2") {
targetLevel = S2;
} else if (pivotLevel == pivotLevel."3") {
targetLevel = S3;
} else if (pivotLevel == pivotLevel."4") {
targetLevel = S4;
} else {
targetLevel = S5;
}
} else {
if (pivotLevel == pivotLevel."1") {
targetLevel = R1;
} else if (pivotLevel == pivotLevel."2") {
targetLevel = R2;
} else if (pivotLevel == pivotLevel."3") {
targetLevel = R3;
} else if (pivotLevel == pivotLevel."4") {
targetLevel = R4;
} else {
targetLevel = R5;
}
}
# --- Smart Scan Logic —
def isSupport = pivotSide == pivotSide.Support;
# 1. Event: Touched / Breached
# Did it hit the level at all?
def eventTouch = if isSupport
then low <= targetLevel
else high >= targetLevel;
# 2. State: Holding Beyond (Breakout/Breakdown)
# Did it hit AND stay there?
def isBeyond = if isSupport
then close < targetLevel
else close > targetLevel;
# 3. Setup: Reclaimed / Reversal (The Wick)
# Did it touch the level BUT close back inside?
# Support: Low went below, but Close is above.
# Resistance: High went above, but Close is below.
def isReclaimed = if isSupport
then low <= targetLevel and close > targetLevel
else high >= targetLevel and close < targetLevel;
# 4. Setup: Near Level
# Proximity Check
def dist = if isSupport then AbsValue(low - targetLevel) else AbsValue(high - targetLevel);
def isNear = (dist <= (targetLevel * (percentThreshold / 100))) or isBeyond;
# --- Final Output —
plot Scan = if ScanMode == ScanMode."Touched / Breached" then eventTouch
else if ScanMode == ScanMode."Holding Beyond" then isBeyond
else if ScanMode == ScanMode."Reclaimed / Reversal" then isReclaimed
else isNear;