here you have
//-----------------------------------------------------------------//
//PRC_Market Structure Trend Matrix [BigBeluga]
//version = 0
//13.05.2026
//Ivan Gonzalez @ www.prorealcode.com
//Sharing ProRealTime knowledge
//-----------------------------------------------------------------//
//-----Inputs------------------------------------------------------//
msLen = 10
atrLength = 14
atrMult = 4.0
targetStepMult = 2.0
showHistory = 1
showStop = 1
maxHistorical = 50
//-----------------------------------------------------------------//
//-----Seeds (variables recursivas)--------------------------------//
once direction = 0
once phVal = high
once plVal = low
once phIdx = barindex
once plIdx = barindex
once havePh = 0
once havePl = 0
once atrTS = close
once entryPrice = close
once currentTarget = 0
once haveTarget = 0
once trendStart = barindex
once nEvents = 0
once nTargets = 0
//-----------------------------------------------------------------//
//-----Pivot detection (ventana centrada)--------------------------//
phCand = high[msLen]
plCand = low[msLen]
isPH = 1
isPL = 1
for k = 0 to 2*msLen do
if k <> msLen then
if high[k] >= phCand then
isPH = 0
endif
if low[k] <= plCand then
isPL = 0
endif
endif
next
if barindex >= 2*msLen then
if isPH = 1 then
phVal = phCand
phIdx = barindex - msLen
havePh = 1
endif
if isPL = 1 then
plVal = plCand
plIdx = barindex - msLen
havePl = 1
endif
endif
//-----------------------------------------------------------------//
//-----ATR---------------------------------------------------------//
atr = averagetruerange[atrLength]
//-----------------------------------------------------------------//
//-----ChoCh up (close cruza phVal estando en bajista)-------------//
if havePh = 1 and direction = 0 and close crosses over phVal then
direction = 1
atrTS = close - atr * atrMult
entryPrice = phVal
currentTarget = entryPrice + atr * targetStepMult
haveTarget = 1
trendStart = barindex
// registrar evento para dibujo historico
$chochOrigin[nEvents] = phIdx
$chochEnd[nEvents] = barindex
$chochLevel[nEvents] = phVal
$chochType[nEvents] = 1
nEvents = nEvents + 1
endif
//-----------------------------------------------------------------//
//-----ChoCh down (close cruza plVal estando en alcista)-----------//
if havePl = 1 and direction = 1 and close crosses under plVal then
direction = 0
atrTS = close + atr * atrMult
entryPrice = plVal
currentTarget = entryPrice - atr * targetStepMult
haveTarget = 1
trendStart = barindex
$chochOrigin[nEvents] = plIdx
$chochEnd[nEvents] = barindex
$chochLevel[nEvents] = plVal
$chochType[nEvents] = -1
nEvents = nEvents + 1
endif
//-----------------------------------------------------------------//
//-----Trailing stop + escalado de targets-------------------------//
if direction = 1 then
atrTS = max(atrTS, close - atr * atrMult)
if haveTarget = 1 and high >= currentTarget then
perc = (currentTarget - entryPrice) / entryPrice * 100
$tgtStart[nTargets] = trendStart
$tgtHit[nTargets] = barindex
$tgtLevel[nTargets] = currentTarget
$tgtPerc[nTargets] = round(perc * 100) / 100
$tgtType[nTargets] = 1
nTargets = nTargets + 1
currentTarget = currentTarget + atr * targetStepMult
endif
else
atrTS = min(atrTS, close + atr * atrMult)
if haveTarget = 1 and low <= currentTarget then
perc = (currentTarget - entryPrice) / entryPrice * 100
$tgtStart[nTargets] = trendStart
$tgtHit[nTargets] = barindex
$tgtLevel[nTargets] = currentTarget
$tgtPerc[nTargets] = round(perc * 100) / 100
$tgtType[nTargets] = -1
nTargets = nTargets + 1
currentTarget = currentTarget - atr * targetStepMult
endif
endif
//-----------------------------------------------------------------//
//-----Color direccional (RGB)-------------------------------------//
if direction = 1 then
rc = 52
gc = 230
bc = 126
else
rc = 255
gc = 82
bc = 241
endif
//-----------------------------------------------------------------//
//-----Fill close <-> trailing stop--------------------------------//
if showStop = 1 then
colorbetween(close, atrTS, rc, gc, bc, 75)
endif
//-----------------------------------------------------------------//
//-----Dibujos historicos (islastbarupdate)------------------------//
if islastbarupdate then
// Lineas + etiquetas ChoCh (ultimos maxHistorical)
if showHistory = 1 and nEvents > 0 then
startE = max(0, nEvents - maxHistorical)
for i = startE to nEvents - 1 do
co = $chochOrigin[i]
ce = $chochEnd[i]
cv = $chochLevel[i]
ct = $chochType[i]
cm = (co + ce) / 2
if ct = 1 then
drawsegment(co, cv, ce, cv) coloured(52,230,126,255)
drawtext("ChoCh up", cm, cv) coloured(52,230,126)
else
drawsegment(co, cv, ce, cv) coloured(255,82,241,255)
drawtext("ChoCh dn", cm, cv) coloured(255,82,241)
endif
next
endif
// Lineas de targets alcanzados + porcentajes
if showHistory = 1 and nTargets > 0 then
startT = max(0, nTargets - maxHistorical)
for j = startT to nTargets - 1 do
ts = $tgtStart[j]
th = $tgtHit[j]
tv = $tgtLevel[j]
tp = $tgtPerc[j]
tt = $tgtType[j]
if tt = 1 then
drawsegment(ts, tv, th, tv) coloured(52,230,126,200) style(dottedline)
drawtext("+#tp#%", ts, tv) coloured(52,230,126)
else
drawsegment(ts, tv, th, tv) coloured(255,82,241,200) style(dottedline)
drawtext("#tp#%", ts, tv) coloured(255,82,241)
endif
next
endif
// Target actual (en curso)
if haveTarget = 1 then
drawsegment(trendStart, currentTarget, barindex, currentTarget) coloured(rc,gc,bc,255)
endif
endif
//-----------------------------------------------------------------//
//-----Return------------------------------------------------------//
return atrTS coloured(rc,gc,bc,255*showStop) as "ATR Trailing Stop" style(line,2)