# Market_Structure_Trend_Matrix
# Converted from "Market Structure Trend Matrix [BigBeluga]": https://www.tradingview.com/script/EdtttXPC-Market-Structure-Trend-Matrix-BigBeluga/
# Converted by Chewie 5/5/2026
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# INPUTS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
input msLen = 10;
input atrLength = 14;
input atrMult = 4.0;
input TatrMult = 2.0;
input targetStepMult = 2.0;
input showStop = yes;
input bubble = yes;
input alerts = yes;
# Colors β defined as defs using CreateColor so they can be reused anywhere
# Bull = green (52, 230, 126) | Bear = magenta (255, 82, 241)
DefineGlobalColor("Bull", CreateColor(52, 230, 126));
DefineGlobalColor("Bear", CreateColor(255, 82, 241));
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PIVOT DETECTION
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def ph = if high[msLen] == Highest(high, 2 * msLen + 1)[msLen] then high[msLen] else Double.NaN;
def pl = if low[msLen] == Lowest(low, 2 * msLen + 1)[msLen] then low[msLen] else Double.NaN;
def phVal = if !IsNaN(ph) then ph else phVal[1];
def plVal = if !IsNaN(pl) then pl else plVal[1];
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ATR
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def atr = Average(TrueRange(high, close, low), atrLength);
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# DIRECTION & STRUCTURE BREAKS (ChoCh)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def bullBreak = close crosses above phVal;
def bearBreak = close crosses below plVal;
def direction;
def entryPrice;
if bullBreak and !direction[1] {
direction = 1;
entryPrice = phVal;
} else if bearBreak and direction[1] {
direction = 0;
entryPrice = plVal;
} else {
direction = direction[1];
entryPrice = entryPrice[1];
}
def directionChanged = direction != direction[1];
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ATR TRAILING STOP
# Bull: ratchet up β max(prev, close - atr*mult)
# Bear: ratchet down β min(prev, close + atr*mult)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def atrTS;
if bullBreak and !direction[1] {
atrTS = close - atr * atrMult;
} else if bearBreak and direction[1] {
atrTS = close + atr * atrMult;
} else if direction {
atrTS = Max(CompoundValue(1, atrTS[1], close - atr * atrMult),
close - atr * atrMult);
} else {
atrTS = Min(CompoundValue(1, atrTS[1], close + atr * atrMult),
close + atr * atrMult);
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PLOTS β TRAILING STOP
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
plot TrailingStop = if showStop and !directionChanged then atrTS else Double.NaN;
TrailingStop.SetLineWeight(2);
TrailingStop.AssignValueColor(if direction then GlobalColor("Bull") else GlobalColor("Bear"));
plot PriceLine = if showStop then close else Double.NaN;
PriceLine.Hide();
AddCloud(PriceLine, TrailingStop, GlobalColor("Bull"), GlobalColor("Bear"));
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TARGET LEVELS
# Projected from entryPrice at atrMult * targetStepMult increments.
# Held until the next direction change clears them.
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def targetStep = atr * TatrMult * targetStepMult;
def t1;
def t2;
def t3;
if directionChanged {
t1 = if direction then entryPrice + 1 * targetStep
else entryPrice - 1 * targetStep;
t2 = if direction then entryPrice + 2 * targetStep
else entryPrice - 2 * targetStep;
t3 = if direction then entryPrice + 3 * targetStep
else entryPrice - 3 * targetStep;
} else {
t1 = CompoundValue(1, t1[1], Double.NaN);
t2 = CompoundValue(1, t2[1], Double.NaN);
t3 = CompoundValue(1, t3[1], Double.NaN);
}
plot Target1 = t1;
plot Target2 = t2;
plot Target3 = t3;
Target1.SetStyle(Curve.SHORT_DASH);
Target2.SetStyle(Curve.SHORT_DASH);
Target3.SetStyle(Curve.SHORT_DASH);
Target1.SetLineWeight(1);
Target2.SetLineWeight(1);
Target3.SetLineWeight(2);
Target1.AssignValueColor(if direction then GlobalColor("Bull") else GlobalColor("Bear"));
Target2.AssignValueColor(if direction then GlobalColor("Bull") else GlobalColor("Bear"));
Target3.AssignValueColor(if direction then GlobalColor("Bull") else GlobalColor("Bear"));
AddChartBubble(directionChanged, if direction then t1 else t1, "T1", Color.WHITE, direction);
AddChartBubble(directionChanged, if direction then t2 else t2, "T2", Color.WHITE, direction);
AddChartBubble(directionChanged, if direction then t3 else t3, "T3", Color.WHITE, direction);
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PLOTS β ChoCh SIGNALS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
plot ChoCh_Bull = if bullBreak and !direction[1] then low else Double.NaN;
ChoCh_Bull.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
ChoCh_Bull.SetDefaultColor(GlobalColor("Bull"));
ChoCh_Bull.SetLineWeight(3);
plot ChoCh_Bear = if bearBreak and direction[1] then high else Double.NaN;
ChoCh_Bear.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ChoCh_Bear.SetDefaultColor(GlobalColor("Bear"));
ChoCh_Bear.SetLineWeight(3);
AddChartBubble(bubble and bullBreak and !direction[1], low, "ChoChUP", GlobalColor("Bull"), no);
AddChartBubble(bubble and bearBreak and direction[1], high, "ChoChDN", GlobalColor("Bear"), yes);
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ALERTS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Alert(alerts and bullBreak and !direction[1], "ChoCh Up - Bullish Structure Break", Alert.BAR, Sound.Chimes);
Alert(alerts and bearBreak and direction[1], "ChoCh Down - Bearish Structure Break", Alert.BAR, Sound.Bell);