The coding for RSI TP is based on a lookback period of 29 (see line 53 and 73), if you change this value, for example to the first top (bottom) the calculation becomes different so as the results. Maybe if backtest there would be a variable number instead of 29 it could be optimised.
Here is the coding:
// --- settings
//Zigo 21/12/2020
RSIp=n //RSI period
once obLevel=ob //overbought level
once osLevel=os //oversold level
minimalBars=5 //minimal count of bars where RSI is ob or os
// --- end of settings
irsi = rsi[RSIp]
ob = irsi>obLevel
os = irsi<osLevel
if ob then
if not ob[1] then
maxrsi = 0
maxprice = 0
firstobbar = barindex
endif
maxrsi=max(maxrsi,irsi)
maxprice=max(maxprice,high)
if maxrsi<>maxrsi[1] then
maxrsibar=barindex
endif
endif
if os then
if not os[1] then
minrsi = 100
minprice = close*100
firstosbar = barindex
endif
minrsi=min(minrsi,irsi)
minprice=min(minprice,low)
if minrsi<>minrsi[1] then
minrsibar=barindex
endif
endif
Once divsell=0
if irsi crosses under obLevel then
//verif divergence
div = maxprice>oldmaxprice and maxrsi<oldmaxrsi and (barindex-firstobbar)>=minimalBars
if div then
s=(High[0]+low[0])/2
drawsegment(oldmaxrsibar,oldmaxRSI,maxrsibar,maxRSI) coloured(255,255,0, 255)
DRAWRECTANGLE(barindex, low-AverageTrueRange[14](close), barindex[1], low[1]-3*AverageTrueRange[14](close))coloured(255,55,25,255)
DRAWTEXT("Start Div RSI Down", barindex, High+2.5*AverageTrueRange[14](close),dialog, standard, 15)coloured(205,170,120,255)
DRAWTEXT("Enter Short = #s#", barindex, High+ 1.5*AverageTrueRange[14](close),dialog, standard, 15)coloured(205,170,120,255)
TPSh= (Lowest[29](low)+ (maxprice-oldmaxprice))
DRAWHLINE(TPsh)coloured(255,0,0,35)
DRAWTEXT("TargetLine = #TPSh#", barindex, TPSh-0.5*AverageTrueRange[14](close),dialog, standard, 12)coloured(205,205,0)
divsell=s
endif
oldmaxrsi = maxrsi
oldmaxprice = maxprice
oldmaxrsibar = maxrsibar
endif
Once divbuy=0
if irsi crosses over osLevel then
//verif divergence
div = minprice<oldminprice and minrsi>oldminrsi and (barindex-firstosbar)>=minimalBars
if div then
b=(open)
drawsegment(oldminrsibar,oldminrsi,minrsibar,minrsi) coloured(0,200,0)
DRAWRECTANGLE(barindex, High+AverageTrueRange[14](close), barindex[1], high[1]+4*AverageTrueRange[14](close))coloured(0,125,175,255)
DRAWTEXT("Start Div RSI Up", barindex, Low-2*AverageTrueRange[14](close), dialog, standard,15)coloured(125,255,125,255)
DRAWTEXT("Enter Long <= #b#", barindex, low-1.5*AverageTrueRange[14](close),dialog, standard, 15)coloured(0,255,0,255)
TPL=(Highest[29](High)- (minprice - oldminprice))
DRAWHLINE(TPL)coloured(0,255,0,35)
DRAWTEXT("TargetLine = #TPL#", barindex, TPL+0.5*AverageTrueRange[14](close),dialog, standard, 12)coloured(0,255,0,255)
divbuy=b
endif
oldminrsi = minrsi
oldminprice = minprice
oldminrsibar = minrsibar
endif
return