Hello, below mentioned (in the code section) is the code for a RSI and its related Bollinger Band (BB). Help is needed for plotting divergence (defined by (1) ZSR (defined in the PRT code) touching LB (defined in the PRT code) and then price going down to make a lower low but ZSR staying above LBB, (2) ZSR touching UB and then price going up to make a higher high but ZSR staying below UBB).
ZMYR = RSI[21](close)
ZSR = RSI[2](ZMYR)-50
M=wilderAVERAGE[18](ZSR)
SD=STD[18](ZSR)
UB=M+1.2*SD
LB=M-1.2*SD
Return zsr as "MOMENTUM", ub as "OverBought", lb as "OverSold"
Hello,
Anyone up for the challenge?
Regards,
I think that your description is upside down.. Or is it me? Well, here is the code I made from what I understood from it:
ZMYR = RSI[21](close)
ZSR = RSI[2](ZMYR)-50
M=wilderAVERAGE[18](ZSR)
SD=STD[18](ZSR)
UB=M+1.2*SD
LB=M-1.2*SD
//trough
trough=zsr>zsr[1] and zsr[1]<zsr[2]
////osc reset
//if zsr>lb then
//oscLL=1000
//endif
//oscillator below
if zsr[1]<lb then
if trough and zsr[1]<oscll then
oscll=zsr[1]
oscbar=barindex[1]
endif
endif
//osc above lbb
if zsr>lb then
//trough
if trough then
if low[1]<low[barindex-oscbar] and zsr[1]>oscLL and oscbar>0 then
drawarrowup(barindex[1],zsr[1]) coloured(0,255,0)
oscbar=0
oscll=1000
endif
endif
endif
Return zsr as "MOMENTUM", ub as "OverBought", lb as "OverSold"
Many thanks Nicolas. Your time and effort is really appreciated.
The code is only printing UP arrow for (1) Bullish divergence, however it is not printing DOWN red arrow for (2) Bearish divergence as shown with dates for EURUSD daily chart.
Also some of the UP Arrows are missing on valid setup as shown with dates for EURUSD daily chart.
Appreciate the corrected code please.
Thank you again.
Best regards,
You did not mention anything about bearish divergences, that’s why I did not code it..
Here is a version with some fixes, please confirm bullish version is ok.
ZMYR = RSI[21](close)
ZSR = RSI[2](ZMYR)-50
M=wilderAVERAGE[18](ZSR)
SD=STD[18](ZSR)
UB=M+1.2*SD
LB=M-1.2*SD
//trough
trough=zsr>zsr[1] and zsr[1]<zsr[2]
//reset on new cross
if zsr crosses under lb then
oscbar=0
oscll=1000
endif
//oscillator below
if zsr[1]<lb then
if trough and zsr[1]<=oscll then
oscll=zsr[1]
oscbar=barindex[1]
endif
endif
//osc above lbb
if zsr>lb then
//trough
if trough then
if low[1]<low[barindex-oscbar] and zsr[1]>oscLL and oscbar>0 then
drawarrowup(barindex[1],zsr[1]) coloured(0,255,0)
drawsegment(oscbar,oscll,barindex[1],zsr[1]) coloured(0,255,0)
oscbar=0
oscll=1000
endif
endif
endif
Return zsr as "MOMENTUM", ub as "OverBought", lb as "OverSold"
Hello Nicolas, many thanks for the fix. the bullish divergence is working fine now.
I actually mentioned initially for the bearish divergence, although in brief, keeping in mind your coding knowledge level as very high, as well, as, ” (2) ZSR touching UB and then price going up to make a higher high but ZSR staying below UBB). ”
Appreciate if you can incorporate the the code for bearish divergence please.
Many thanks.
Best regards,
Here is the complete version that include the bullish and bearish divergences.
ZMYR = RSI[21](close)
ZSR = RSI[2](ZMYR)-50
M=wilderAVERAGE[18](ZSR)
SD=STD[18](ZSR)
UB=M+1.2*SD
LB=M-1.2*SD
//bullish divergences
//trough
trough=zsr>zsr[1] and zsr[1]<zsr[2]
//reset on new cross
if zsr crosses under lb then
oscbar=0
oscll=1000
endif
//oscillator below
if zsr[1]<lb then
if trough and zsr[1]<=oscll then
oscll=zsr[1]
oscbar=barindex[1]
endif
endif
//osc above lbb
if zsr>lb then
//trough
if trough then
if low[1]<low[barindex-oscbar] and zsr[1]>oscLL and oscbar>0 then
drawarrowup(barindex[1],zsr[1]) coloured(69,139,116)
drawsegment(oscbar,oscll,barindex[1],zsr[1]) coloured(69,139,116)
oscbar=0
oscll=1000
endif
endif
endif
//bearish divergences
//peak
peak=zsr<zsr[1] and zsr[1]>zsr[2]
//reset on new cross
if zsr crosses over UB then
oscbarr=0
oschh=0
endif
//oscillator above
if zsr[1]>UB then
if peak and zsr[1]>=oschh then
oschh=zsr[1]
oscbarr=barindex[1]
endif
endif
//osc below ub
if zsr<ub then
//peak
if peak then
if high[1]>high[barindex-oscbarr] and zsr[1]<oscHH and oscbarr>0 then
drawarrowdown(barindex[1],zsr[1]) coloured(255,0,0)
drawsegment(oscbarr,oschh,barindex[1],zsr[1]) coloured(255,0,0)
oscbarr=0
oschh=0
endif
endif
endif
Return zsr style(line,3) as "MOMENTUM", ub coloured(190,190,190) style(dottedline,2) as "OverBought" , lb coloured(190,190,190) style(dottedline,2) as "OverSold"
Many many thanks dear Nicolas.
Much appreciate your fast and reliable response, as always.
Its all fine now.
Best regards,
Hello Nicolas,
I can not create Alert on this divergence. Is it possible to plot the divergence as value return so that Alert can be set please.
Your help is much appreciated, Nicolas.
Best regards,
Try this. It returns a 1 for a buy signal and a -1 for a sell signal.
ZMYR = RSI[21](close)
ZSR = RSI[2](ZMYR)-50
M=wilderAVERAGE[18](ZSR)
SD=STD[18](ZSR)
UB=M+1.2*SD
LB=M-1.2*SD
result = 0
//bullish divergences
//trough
trough=zsr>zsr[1] and zsr[1]<zsr[2]
//reset on new cross
if zsr crosses under lb then
oscbar=0
oscll=1000
endif
//oscillator below
if zsr[1]<lb then
if trough and zsr[1]<=oscll then
oscll=zsr[1]
oscbar=barindex[1]
endif
endif
//osc above lbb
if zsr>lb then
//trough
if trough then
if low[1]<low[barindex-oscbar] and zsr[1]>oscLL and oscbar>0 then
result = 1
oscbar=0
oscll=1000
endif
endif
endif
//bearish divergences
//peak
peak=zsr<zsr[1] and zsr[1]>zsr[2]
//reset on new cross
if zsr crosses over UB then
oscbarr=0
oschh=0
endif
//oscillator above
if zsr[1]>UB then
if peak and zsr[1]>=oschh then
oschh=zsr[1]
oscbarr=barindex[1]
endif
endif
//osc below ub
if zsr<ub then
//peak
if peak then
if high[1]>high[barindex-oscbarr] and zsr[1]<oscHH and oscbarr>0 then
result = -1
oscbarr=0
oschh=0
endif
endif
endif
Return result
Hello Vonashi,
Thank you for your prompt response, however your code is not after Nicolas’s last post which was recent. We have to build an alert on that.
Your code is not running and giving the enclosed error.
Regards,
You must have done a bad copy and paste because it works just fine for me. Use the button in the top right of the PRT code window to highlight it all and then hit CTRL+C.
if they are unused, then delete all their references in code, simple as 1.2.3. 😆
Many thanks Vonasi.
Much appreciate your time, effort and helping attitude.
Thank you again for your prompt response.
Regards,
Yes Nicolas. You are correct.