Ahora entiendo. El cálculo del retroceso no suele hacerse así, no es un porcentaje del precio, sino un porcentaje del rango de precios que había en el periodo elegido.
Si el precio, en un período determinado, ha hecho una subida de 500 puntos y luego vuelve a 400 puntos, ha hecho un retroceso del 80%, independientemente del instrumento, de lo contrario, 400 puntos serían diferentes en SP o en DOW JONES o en EURUSD.
De todos modos, aquí está el cálculo como lo desea:
DEFPARAM DrawOnLastBarOnly = True
Trend = 0
p = 250 //250 periods
HH = highest[p](high)
LL = lowest[p](low)
//Diff1 = abs(HH - LL)
FOR i = 0 TO (p - 1)
IF high[i] = HH THEN
Trend = 1
MyBar = BarIndex[i]
BREAK
ENDIF
IF low[i] = LL THEN
Trend = -1
//MyBar = BarIndex[i]
HH = HH[1]
LL = LL[1]
Bx = Bx[1]
Rg = Rg[1]
BREAK
ENDIF
NEXT
IF Trend = 1 THEN
bx = BarIndex
Retracement = lowest[max(1,BarIndex - MyBar)](low)
Diff2 = HH - Retracement
//PerCent = round(Diff2 * 100 / Diff1,1)
PerCent = round(Diff2 * 100 / HH,2)
Rg = average[p,0](Range) * 2.0
ENDIF
DrawSegment(MyBar,HH,Bx,HH) coloured("Blue")
DrawSegment(MyBar,LL,Bx,LL) coloured("Blue")
IF Trend = 1 THEN
DrawText("PullBack % : #PerCent#" ,Bx,HH + Rg*1.0) coloured("Blue",255)
DrawText(" LOW retrace: #Retracement#",Bx,HH + Rg*1.5) coloured("Blue",255)
DrawText(" HH : #HH#" ,Bx,HH + Rg*2.0) coloured("Blue",255)
DrawText(" LL : #LL#" ,Bx,HH + Rg*2.5) coloured("Blue",255)
ENDIF
RETURN
Este es el screener:
p = 250 //250 periods
PerCent = 30.0 //30.0 minimum % of retracement
HH = highest[p](high)
LL = lowest[p](low)
Trend = 0
Signal = 0
IF close[p] > 0 THEN
FOR i = 0 TO (p - 1)
IF high[i] = HH THEN
MyBar = BarIndex[i]
Trend = 1
BREAK
ENDIF
IF low[i] = LL THEN
Trend = -1
BREAK
ENDIF
NEXT
ENDIF
IF Trend = 1 THEN
Retracement = lowest[max(1,BarIndex - MyBar)](low)
Diff2 = HH - Retracement
PC = round(Diff2 * 100 / HH,2)
Signal = (close <= Retracement)
ENDIF
SCREENER[Signal AND (PC >= PerCent)](PC AS "Pullback %")