Reputation: 3
I'm trying to plot a buy signal every time my RSI oversold level cross occurred at least 3 times within a period of maximum 30 bars.
I made a lot of research but I am a newbie. I tried some codes with valuewhen and barssince but I didn't find the right way for the solution.
Here is my pine script with my RSI overbought and oversold levels plots, it works well, no problem:
RSIPeriod = input(13, "RSI Period")
BuyAlertLevel = input(-34, "RSI Buy Alert")
SellAlertLevel = input(34, "RSI Sell Alert")
RSIHistoModify = input(1.618, "RSI Fib Factor")
xPrice2 = close[1]
RSIMain = (ta.rsi(xPrice2, RSIPeriod) - 50) * RSIHistoModify
Buysignal = ta.crossunder(RSIMain, BuyAlertLevel)
Sellsignal = ta.crossover(RSIMain, SellAlertLevel)
show_rsi_signals = input(true, "RSI Signals")
plotchar(show_rsi_signals ? Buysignal: na, title="RSI Buy", char="•", location=location.belowbar, size=size.normal, color=color.rgb(255, 255, 255, 85))
plotchar(show_rsi_signals ? Sellsignal: na, title="RSI Sell", char="•", location=location.abovebar, size=size.normal, color=color.rgb(255, 255, 255, 85))
Lastly I tried somethings with ta.valuewhen to check if the cross occured at least 3 times during 30 bars but it didn't work:
rsibuy1 = ta.valuewhen(Buysignal, close, 3) <= 30
label.new(rsibuy1 ? bar_index:na, low, text='B', color=color.rgb(255, 255, 255, 100), style=label.style_label_up, textcolor=color.rgb(0, 194, 120, 80), size=size.small)
My RSI overbought and oversold cross plots are ok no problem with that. I only need to understand how to check x amount of crosses within x amount of bars to plot it with another buy signal.
I am using Pine Script v5.
Can anybody help? Thanks in advance.
Upvotes: 0
Views: 894
Reputation: 368
I have used for loop to look for true conditions, here you can see that I incremented the count
variable every time I found the cross over Buysignal
true.
In similar way you can use a for loop for sell/cross under signals in last number_of_bar_to_lookfor
bars.
The final signal is the bgcolor()
bgcolor(n_rsi_cross_found and not n_rsi_cross_found[1]?color.green:na,title='n number of cross found')
Hope this is helpful.
//@version=5
indicator("My script")
RSIPeriod = input(13, "RSI Period")
BuyAlertLevel = input(-34, "RSI Buy Alert")
SellAlertLevel = input(34, "RSI Sell Alert")
RSIHistoModify = input(1.618, "RSI Fib Factor")
xPrice2 = close[1]
RSIMain = (ta.rsi(xPrice2, RSIPeriod) - 50) * RSIHistoModify
plot(RSIMain)
Buysignal = ta.crossunder(RSIMain, BuyAlertLevel)
Sellsignal = ta.crossover(RSIMain, SellAlertLevel)
number_of_bar_to_lookfor=input.int(30,'Input Number of bars you want to look for',group='Setting for the Logic')
number_of_cross_happened=input.int(3,'Input Number of cross to happen',group='Setting for the Logic')
count=0
for i=0 to number_of_bar_to_lookfor
if Buysignal[i]
count+=1
n_rsi_cross_found=count>=number_of_cross_happened
bgcolor(n_rsi_cross_found and not n_rsi_cross_found[1]?color.green:na,title='n number of cross found')
show_rsi_signals = input(true, "RSI Signals")
plotchar(show_rsi_signals and Buysignal?RSIMain: na, title="RSI Buy", char="•", location=location.absolute, size=size.normal, color=color.rgb(255, 255, 255, 85))
plotchar(show_rsi_signals and Sellsignal?RSIMain: na, title="RSI Sell", char="•", location=location.absolute, size=size.normal, color=color.rgb(255, 255, 255, 85))
Upvotes: 1