Institutional Pivot Matrix-(For Dark Background)

Cut and Paste into Think or Swim-from declare lower;

declare upper;

# --- 1) Inputs ---
input barsBack = 280;
input lockLevels = yes;
input extendFullChart = yes;
input showZones = yes;
input showTargets = yes;
input showTimeTurns = yes;
input showLabels = yes;

# Zone retracement (50% - 62%)
input zoneTopPct = 0.50;
input zoneBotPct = 0.62;

# --- 78.6% Retracement Line ---
input show78Line = yes;
input retrace78Pct = 0.786;  

# Time Turns
input pct45  = 0.45;
input pct90  = 0.90;
input pct180 = 1.80;

# Gann PRICE Levels
input showGannPrice = yes;
input showGannPriceLabels = yes;
input g45mult  = 0.25;   
input g90mult  = 0.50;   
input g180mult = 1.00;   
input g360mult = 2.00;   

def bn = BarNumber();
def lastBar = HighestAll(if !IsNaN(close) then bn else Double.NaN);
def scanStart = lastBar - barsBack;

# --- 2) Anchor Logic ---
def winHigh_raw = HighestAll(if bn >= scanStart then high else Double.NaN);
def winLow_raw  = LowestAll(if bn >= scanStart then low else Double.NaN);
def winHigh = if lockLevels then HighestAll(winHigh_raw) else winHigh_raw;
def winLow  = if lockLevels then HighestAll(winLow_raw)  else winLow_raw;
def hiBar = HighestAll(if high == winHigh and bn >= scanStart then bn else 0);
def loBar = HighestAll(if low == winLow and bn >= scanStart then bn else 0);
def isBullish = loBar < hiBar;
def rng = winHigh - winLow;

# --- 3) Zone & Target Math ---
def bz1 = if isBullish then winHigh - (rng * zoneTopPct) else winLow + (rng * zoneTopPct);
def bz2 = if isBullish then winHigh - (rng * zoneBotPct) else winLow + (rng * zoneBotPct);
def zoneHi = Max(bz1, bz2);
def zoneLo = Min(bz1, bz2);
def trigger = if isBullish then zoneLo else zoneHi;

def T62_val  = if isBullish then trigger + (rng * 0.618) else trigger - (rng * 0.618);
def T78_val  = if isBullish then trigger + (rng * 0.786) else trigger - (rng * 0.786);
def T100_val = if isBullish then trigger + (rng * 1.000) else trigger - (rng * 1.000);

# --- 4) Visual Plots - DARK MODE ADJUSTED ---
plot BullZoneHi = if isBullish and showZones and bn >= scanStart then HighestAll(zoneHi) else Double.NaN;
plot BullZoneLo = if isBullish and showZones and bn >= scanStart then HighestAll(zoneLo) else Double.NaN;
plot BearZoneHi = if !isBullish and showZones and bn >= scanStart then HighestAll(zoneHi) else Double.NaN;
plot BearZoneLo = if !isBullish and showZones and bn >= scanStart then HighestAll(zoneLo) else Double.NaN;

BullZoneHi.SetDefaultColor(CreateColor(0, 200, 0)); # Bright Green
BullZoneLo.SetDefaultColor(CreateColor(0, 200, 0));
BearZoneHi.SetDefaultColor(CreateColor(220, 0, 0)); # Bright Red
BearZoneLo.SetDefaultColor(CreateColor(220, 0, 0));

# Subtle clouds for dark background
AddCloud(BullZoneHi, BullZoneLo, CreateColor(0, 50, 0), CreateColor(0, 50, 0));
AddCloud(BearZoneHi, BearZoneLo, CreateColor(60, 0, 0), CreateColor(60, 0, 0));

def Retr78_raw = if isBullish then winHigh - (rng * retrace78Pct) else winLow + (rng * retrace78Pct);
def Retr78_val = HighestAll(Retr78_raw);
plot Retrace78 = if show78Line and showZones and bn >= scanStart then Retr78_val else Double.NaN;
Retrace78.AssignValueColor(if isBullish then Color.GREEN else Color.RED);
Retrace78.SetStyle(Curve.LONG_DASH);
Retrace78.SetLineWeight(2);

# --- 5) Target Hierarchy - ELECTRIC BLUE ---
plot Target62  = if showTargets and bn >= scanStart then HighestAll(T62_val) else Double.NaN;
plot Target78  = if showTargets and bn >= scanStart then HighestAll(T78_val) else Double.NaN;
plot Target100 = if showTargets and bn >= scanStart then HighestAll(T100_val) else Double.NaN;

Target62.SetDefaultColor(Color.CYAN);  Target62.SetLineWeight(1);
Target78.SetDefaultColor(Color.CYAN);  Target78.SetLineWeight(2);
Target100.SetDefaultColor(Color.CYAN); Target100.SetLineWeight(3);

# --- 6) Gann PRICE Levels - VIBRANT RED ---
def gannAnchor = if isBullish then winLow else winHigh;
def G45_val  = HighestAll(if isBullish then gannAnchor + (rng * g45mult)  else gannAnchor - (rng * g45mult));
def G90_val  = HighestAll(if isBullish then gannAnchor + (rng * g90mult)  else gannAnchor - (rng * g90mult));
def G180_val = HighestAll(if isBullish then gannAnchor + (rng * g180mult) else gannAnchor - (rng * g180mult));
def G360_val = HighestAll(if isBullish then gannAnchor + (rng * g360mult) else gannAnchor - (rng * g360mult));

plot Gann45  = if showGannPrice and bn >= scanStart then G45_val  else Double.NaN;
plot Gann90  = if showGannPrice and bn >= scanStart then G90_val  else Double.NaN;
plot Gann180 = if showGannPrice and bn >= scanStart then G180_val else Double.NaN;
plot Gann360 = if showGannPrice and bn >= scanStart then G360_val else Double.NaN;

Gann45.SetDefaultColor(Color.ORANGE); # Orange/Red for contrast
Gann90.SetDefaultColor(Color.ORANGE);
Gann180.SetDefaultColor(Color.ORANGE);
Gann360.SetDefaultColor(Color.ORANGE);
Gann45.SetStyle(Curve.SHORT_DASH);
Gann180.SetLineWeight(2);

# --- 7) Time Series - LIGHT GRAY (Black won't show) ---
def off45  = Round(barsBack * pct45, 0);
def off90  = Round(barsBack * pct90, 0);
def off180 = Round(barsBack * pct180, 0);
def turnAnchor = if isBullish then hiBar else loBar;

AddVerticalLine(showTimeTurns and bn == turnAnchor + off45,  "T+" + off45,  Color.LIGHT_GRAY);
AddVerticalLine(showTimeTurns and bn == turnAnchor + off90,  "T+" + off90,  Color.LIGHT_GRAY);
AddVerticalLine(showTimeTurns and bn == turnAnchor + off180, "T+" + off180, Color.LIGHT_GRAY);

# --- 8) Labels ---
AddLabel(showLabels, if isBullish then "BULLISH SETUP" else "BEARISH SETUP", if isBullish then Color.GREEN else Color.RED);
AddLabel(showZones, (if isBullish then "BUY ZONE: " else "SELL ZONE: ") + Round(zoneLo, 2) + " - " + Round(zoneHi, 2), if isBullish then Color.GREEN else Color.RED);
AddLabel(show78Line, "78.6% RETRACE: " + Round(Retr78_val, 2), if isBullish then Color.GREEN else Color.RED);
AddLabel(showGannPriceLabels, "GANN Price Levels Active", Color.ORANGE);
AddLabel(showTargets, "T100 TARGET: " + Round(T100_val, 2), Color.CYAN);