RSI Strategy Tradingview

Viewed 588

I am trying to write some code with the aim that when the latest RSI is above 40 to show the long strategy. Both the weekly RSI and monthly RSI should be above 60.

study("rsi", overlay=true)

rsisource= input( title="rsisource", type=input.source, defval=close)

rsilength= input( title="rsisource", type=input.integer, defval=14)


rsivalue= rsi(rsisource, rsilength)

lrsi= input( title="dt", type=input.resolution, defval="D")

wrsi= input( title="dt", type=input.resolution, defual="W")

mrsi= input( title="dt", type=input.resolution, defual="M")



llr=crossover (rsivalue and lrsi, 40)
 
wwr=(rsivalue and wrsi > 60) 

mmr=(rsivalue and mrsi > 60) 

l1=(rsivalue and lrsi > 60) 


if (llr and wwr and mmr)

strategy.entry("buy", strategy.long)

if (l1)

strategy.close("sell")
1 Answers

You have to use the security() function to get higher time frames.

Probably you want something like this: (buy when rsi cross 40 line and D, W, M rsi are above 40 / sell when rsi cross 60)

//@version=4
strategy("My Script")

rsi = rsi(close, 14)

rsi_d = security(syminfo.tickerid, "D", rsi)
rsi_w = security(syminfo.tickerid, "W", rsi)
rsi_m = security(syminfo.tickerid, "M", rsi)

if crossover(rsi, 40) and rsi_d > 40 and rsi_w > 40 and rsi_m > 40
    strategy.entry("buy", strategy.long)

if crossover(rsi, 60)
    strategy.close("buy")
Related