Ecco!!
//----------------------------------------------
//PRC_FVG Fair value MS
//version = 0
//01.04.24
//Iván González @ www.prorealcode.com
//Sharing ProRealTime knowledge
//----------------------------------------------
// User Inputs
//----------------------------------------------
ONCE fvgTog = 1 // 1="Structural", 2="All", 3="None"
// --- Colores FVG ---
ONCE upColR = 58
ONCE upColG = 170
ONCE upColB = 69
ONCE downColR = 255
ONCE downColG = 3
ONCE downColB = 62
ONCE upFillColR = 0
ONCE upFillColG = 0
ONCE upFillColB = 0
ONCE upFillColA = 100
ONCE downFillColR = 0
ONCE downFillColG = 0
ONCE downFillColB = 0
ONCE downFillColA = 100
// --- Áreas de Interés (AOI) ---
ONCE aoiNum = 3 // Número de AOIs a extender
ONCE bullColorR = 58
ONCE bullColorG = 170
ONCE bullColorB = 69
ONCE bullColorA = 20
ONCE bearColorR = 255
ONCE bearColorG = 3
ONCE bearColorB = 62
ONCE bearColorA = 20
ONCE bullFillColorR = 58
ONCE bullFillColorG = 170
ONCE bullFillColorB = 69
ONCE bullFillColorA = 20
ONCE bearFillColorR = 255
ONCE bearFillColorG = 3
ONCE bearFillColorB = 62
ONCE bearFillColorA = 20
ONCE textTog = 0 // 0=false, 1=true
ONCE textColorR = 255
ONCE textColorG = 255
ONCE textColorB = 255
ONCE textColorA = 60
ONCE textSize = 10
// --- Líneas de Reflexión ---
ONCE refTog = 1 // 0=false, 1=true
ONCE refDownColR = 255
ONCE refDownColG = 60
ONCE refDownColB = 0
ONCE refUpColR = 0
ONCE refUpColG = 77
ONCE refUpColB = 146
ONCE refCount = 3
// --- Estructura de Mercado (MS) ---
ONCE msTog = 1 // 0=false, 1=true
ONCE msStyle = 1 // 1=Solid, 2=Dashed, 3=Dotted
ONCE mssBullTog = 1 // 0=false, 1=true
ONCE mssBullStyle = 2 // 1=Solid, 2=Dashed, 3=Dotted
ONCE mssBullColR = 58
ONCE mssBullColG = 170
ONCE mssBullColB = 69
ONCE mssBearTog = 1 // 0=false, 1=true
ONCE mssBearStyle = 2 // 1=Solid, 2=Dashed, 3=Dotted
ONCE mssBearColR = 255
ONCE mssBearColG = 3
ONCE mssBearColB = 62
// --- Sesión ---
ONCE seshinStart = 093000
ONCE seshinEnd = 160000
ONCE stog = 1 // 0=false, 1=true
ONCE sstyle = 2 // 1=Solid, 2=Dashed, 3=Dotted
ONCE scR = 128
ONCE scG = 128
ONCE scB = 128
ONCE scA = 50
ONCE etog = 1 // 0=false, 1=true
ONCE estyle = 2 // 1=Solid, 2=Dashed, 3=Dotted
ONCE ecR = 255
ONCE ecG = 3
ONCE ecB = 62
ONCE ecA = 50
//----------------------------------------------
// Variables y Arrays
//----------------------------------------------
ONCE dir = 0
ONCE mssdir = 0
ONCE bullaoicheck = 0
ONCE bearaoicheck = 0
ONCE hstPrice = 0
ONCE hstBar = 0
ONCE lstPrice = 0
ONCE lstBar = 0
ONCE pivhiPrice = 0
ONCE pivhiBar = 0
ONCE pivloPrice = 0
ONCE pivloBar = 0
ONCE phaoiTopPrice = high
ONCE phaoiTopBar = 0
ONCE phaoiBotPrice = high
ONCE phaoiBotBar = 0
ONCE plaoiTopPrice = low
ONCE plaoiTopBar = 0
ONCE plaoiBotPrice = low
ONCE plaoiBotBar = 0
ONCE pivloclaimed = 0
ONCE pivhiclaimed = 0
// Arrays para AOIs Bullish
ONCE bullAoisCount = 0
// Arrays para AOIs Bearish
ONCE bearAoisCount = 0
// Arrays para Líneas de Reflexión
ONCE rValsCount = 0
fvgup = (low > high[2]) AND (close[1] > high[2])
fvgdown = (high < low[2]) AND (close[1] < low[2])
ctop = MAX(open, close)
cbot = MIN(open, close)
//----------------------------------------------
// Lógica FVG y Estructura
//----------------------------------------------
IF fvgTog = 2 THEN // "All"
IF fvgdown THEN
DRAWRECTANGLE(barindex[2], low[2], barindex, high) COLOURED(downColR, downColG, downColB)
$fvgTop[fvgCount+1] = high
$fvgBottom[fvgCount+1] = low[2]
$fvgLeft[fvgCount+1] = barindex[2]
$fvgType[fvgCount+1] = -1
fvgCount = fvgCount + 1
ENDIF
IF fvgup THEN
DRAWRECTANGLE(barindex[2], high[2], barindex, low) COLOURED(upColR, upColG, upColB)
$fvgTop[fvgCount+1] = low
$fvgBottom[fvgCount+1] = high[2]
$fvgLeft[fvgCount+1] = barindex[2]
$fvgType[fvgCount+1] = 1
fvgCount = fvgCount + 1
ENDIF
ENDIF
IF dir = 0 THEN
IF fvgup THEN
dir = 1
hstPrice = high
hstBar = barindex
lstPrice = low
lstBar = barindex
ENDIF
IF fvgdown THEN
dir = -1
hstPrice = high
hstBar = barindex
lstPrice = low
lstBar = barindex
ENDIF
ENDIF
IF dir = 1 THEN
IF high >= hstPrice THEN
hstPrice = high
hstBar = barindex
lstPrice = low
lstBar = barindex
elsif low <= lstPrice THEN
lstPrice = low
lstBar = barindex
ENDIF
IF fvgdown THEN
dir = -1
pivhiPrice = hstPrice
pivhiBar = hstBar
pivhiclaimed = 0
IF hstPrice > pivhiPrice[1] THEN
IF (pivhiPrice >= phaoiTopPrice AND bearaoicheck) OR NOT bearaoicheck THEN
phaoiTopPrice = pivhiPrice
phaoiTopBar = pivhiBar
phaoiBotPrice = low[2]
phaoiBotBar = barindex
bearaoicheck = 1
ENDIF
ENDIF
hstPrice = high
hstBar = barindex
// lst se mantiene
IF msTog THEN
DRAWSEGMENT(pivhiBar, pivhiPrice, pivloBar, pivloPrice) COLOURED(128,128,128,60) STYLE(line, 2)
ENDIF
IF fvgTog = 1 THEN // "Structural"
DRAWRECTANGLE(barindex[2], low[2], barindex, high) COLOURED(downColR, downColG, downColB, downFillColA)
$fvgTop[fvgCount+1] = high
$fvgBottom[fvgCount+1] = low[2]
$fvgLeft[fvgCount+1] = barindex[2]
$fvgType[fvgCount+1] = -1
fvgCount = fvgCount + 1
ENDIF
ENDIF
ENDIF
IF dir = -1 THEN
IF low <= lstPrice THEN
hstPrice = high
hstBar = barindex
lstPrice = low
lstBar = barindex
ENDIF
IF high >= hstPrice THEN
hstPrice = high
hstBar = barindex
ENDIF
IF fvgup THEN
dir = 1
pivloPrice = lstPrice
pivloBar = lstBar
pivloclaimed = 0
IF lstPrice < pivloPrice[1] THEN
IF (pivloPrice <= plaoiBotPrice AND bullaoicheck) OR NOT bullaoicheck THEN
plaoiTopPrice = high[2]
plaoiTopBar = barindex
plaoiBotPrice = pivloPrice
plaoiBotBar = pivloBar
bullaoicheck = 1
ENDIF
ENDIF
// hst se mantiene
lstPrice = low
lstBar = barindex
IF msTog THEN
DRAWSEGMENT(pivhiBar, pivhiPrice, pivloBar, pivloPrice) COLOURED(128,128,128,60) STYLE(line, 2)
ENDIF
IF fvgTog = 1 THEN // "Structural"
DRAWRECTANGLE(barindex[2], high[2], barindex, low) COLOURED(upColR, upColG, upColB, upFillColA)
$fvgTop[fvgCount+1] = low
$fvgBottom[fvgCount+1] = high[2]
$fvgLeft[fvgCount+1] = barindex[2]
$fvgType[fvgCount+1] = 1
fvgCount = fvgCount + 1
ENDIF
ENDIF
ENDIF
//----------------------------------------------
// --- Detección de Ruptura de Estructura (MSS) y Activación de AOIs ---
//----------------------------------------------
// --- Ruptura Alcista ---
IF close CROSSES OVER pivhiPrice AND pivhiPrice > 0 THEN
DRAWSEGMENT(pivhiBar, pivhiPrice, barindex, pivhiPrice) STYLE(dottedline) COLOURED(mssBullColR, mssBullColG, mssBullColB)
// ACTIVAMOS Y GUARDAMOS EL AOI ALCISTA
IF bullaoicheck THEN
cumvol = 0
FOR i = plaoiTopBar DOWNTO plaoiBotBar DO
cumvol = cumvol + volume[barindex-i]
NEXT
// Guardamos los datos del AOI en los arrays para dibujarlos al final
$bullAoisTop[bullAoisCount] = plaoiTopPrice
$bullAoisBottom[bullAoisCount] = plaoiBotPrice
$bullAoisLeftBar[bullAoisCount] = plaoiBotBar
$bullAoisRightBar[bullAoisCount] = barindex
$bullAoisVolume[bullAoisCount] = cumvol
$bullAoisBreak[bullAoisCount] = 0
bullAoisCount = bullAoisCount + 1
// Reseteamos el check para que este AOI no se guarde dos veces
bullaoicheck = 0
// Guardamos el nivel del pivote bajo para la Línea de Reflexión
$rValsPrice[rValsCount] = plaoiBotPrice // Precio del pivote bajo
$rValsBar[rValsCount] = plaoiBotBar // Barindex del pivote bajo
rValsCount = rValsCount + 1
ENDIF
// --- Ruptura Bajista ---
ELSIF close CROSSES UNDER pivloPrice AND pivloPrice > 0 THEN
// Dibujamos la línea de ruptura
DRAWSEGMENT(pivloBar, pivloPrice, barindex, pivloPrice) STYLE(dottedline,3) COLOURED(mssBearColR, mssBearColG, mssBearColB)
// ACTIVAMOS Y GUARDAMOS EL AOI BAJISTA
IF bearaoicheck THEN
// Calculamos el volumen
cumvol = 0
FOR i = phaoiBotBar DOWNTO phaoiTopBar DO
cumvol = cumvol + volume[barindex-i]
NEXT
// Guardamos los datos del AOI en los arrays
$bearAoisTop[bearAoisCount] = phaoiTopPrice
$bearAoisBottom[bearAoisCount] = phaoiBotPrice
$bearAoisLeftBar[bearAoisCount] = phaoiTopBar
$bearAoisRightBar[bearAoisCount] = barindex
$bearAoisVolume[bearAoisCount] = cumvol
$bearAoisBreak[bearAoisCount] = 0
bearAoisCount = bearAoisCount + 1
// Reseteamos el check
bearaoicheck = 0
// Guardamos el nivel del pivote alto para la Línea de Reflexión
$rValsPrice[rValsCount] = phaoiTopPrice // Precio del pivote alto
$rValsBar[rValsCount] = phaoiTopBar // Barindex del pivote alto
rValsCount = rValsCount + 1
ENDIF
ENDIF
//----------------------------------------------
// Draw AOIs
//----------------------------------------------
if islastbarupdate then
for i=bearAoisCount downto 1 do
dist = barindex-$bearAoisLeftBar[i]
if $bearAoisBreak[i] = 0 then
for j=dist downto 0 do
if high[j] > $bearAoisTop[i] and $bearAoisBreak[i] = 0 then
$bearAoisBreak[i] = 1
$bearAoisRightBar[i] = barindex[j]
break
endif
next
endif
if $bearAoisBreak[i] = 1 then
drawrectangle($bearAoisLeftBar[i],$bearAoisBottom[i],$bearAoisRightBar[i],$bearAoisTop[i])coloured(downColR,downColG,downColB)fillcolor(downColR,downColG,downColB,50)
else
drawrectangle($bearAoisLeftBar[i],$bearAoisBottom[i],barindex,$bearAoisTop[i])coloured(downColR,downColG,downColB)fillcolor(downColR,downColG,downColB,50)
endif
next
for i=bullAoisCount downto 1 do
dist = barindex-$bullAoisLeftBar[i]
if $bullAoisBreak[i] = 0 then
for j=dist downto 0 do
if low[j] < $bullAoisBottom[i] and $bullAoisBreak[i] = 0 then
$bullAoisBreak[i] = 1
$bullAoisRightBar[i] = barindex[j]
break
endif
next
endif
if $bullAoisBreak[i] = 1 then
drawrectangle($bullAoisLeftBar[i],$bullAoisBottom[i],$bullAoisRightBar[i],$bullAoisTop[i])coloured(upColR,upColG,upColB)fillcolor(upColR,upColG,upColB,50)
else
drawrectangle($bullAoisLeftBar[i],$bullAoisBottom[i],barindex,$bullAoisTop[i])coloured(upColR,upColG,upColB)fillcolor(upColR,upColG,upColB,50)
endif
next
// --- DIBUJO DE LÍNEAS DE REFLEXIÓN ---
IF refTog THEN
// Calculamos el precio medio suavizado
threshold = averagetruerange[14]
ONCE rope = close
move = close - rope
rope = rope + MAX(ABS(move) - threshold, 0) * SGN(move)
mean = rope
// Controlamos que el array no supere refCount
WHILE rValsCount > refCount DO
// Simular un desplazamiento (shift) para eliminar el elemento más antiguo
FOR i = 0 TO rValsCount - 2 DO
$rValsPrice[i] = $rValsPrice[i+1]
$rValsBar[i] = $rValsBar[i+1]
NEXT
rValsCount = rValsCount - 1
WEND
// Dibujamos las líneas
FOR i = 0 TO rValsCount - 1 DO
valPrice = $rValsPrice[i]
valBar = $rValsBar[i]
// Lógica de color
IF mean < valPrice THEN
// Resistencia
DRAWSEGMENT(valBar, valPrice, barindex + 1, valPrice) COLOURED(refDownColR, refDownColG, refDownColB) STYLE(line, 2)
ELSE
// Soporte
DRAWSEGMENT(valBar, valPrice, barindex + 1, valPrice) COLOURED(refUpColR, refUpColG, refUpColB) STYLE(line, 2)
ENDIF
NEXT
ENDIF
endif
return