//@version=5 strategy(title = "RSI versus SMA - Enry - V5", shorttitle = "RSI vs SMA - Enry - V5", overlay = false, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 25, currency = currency.USD) // Description: // - It's RSI versus a Simple Moving Average.. Not sure it really needs much more description. // - Should not repaint - Automatically offsets by 1 bar if anything other than "open" selected as RSI source. // === INPUTS === // rsi rsiSource = input(defval = open, title = "RSI Source") rsiLength = input(defval = 8, title = "RSI Length") // sma maLength = input(defval = 34, title = "MA Period") // invert trade direction tradeInvert = input(defval = false, title = "Invert Trade Direction?") // risk management useStop = input(defval = false, title = "Use Initial Stop Loss?") slPoints = input(defval = 25, title = "Initial Stop Loss Points") useTS = input(defval = true, title = "Use Trailing Stop?") tslPoints = input(defval = 120, title = "Trail Points") useTSO = input(defval = false, title = "Use Offset For Trailing Stop?") tslOffset = input(defval = 20, title = "Trail Offset Points") // === /INPUTS === // === BASE FUNCTIONS === // delay for direction change actions switchDelay(exp, len) => average = len >= 2 ? math.sum(exp, len) / len : exp[1] up = exp > average down = exp < average state = up ? true : down ? false : up[1] // === /BASE FUNCTIONS === // === SERIES and VAR === // rsi shunt = rsiSource == open ? 0 : 1 rsiUp = ta.rma(math.max(ta.change(rsiSource[shunt]), 0), rsiLength) rsiDown = ta.rma(-math.min(ta.change(rsiSource[shunt]), 0), rsiLength) rsi = (rsiDown == 0 ? 100 : rsiUp == 0 ? 0 : 100 - (100 / (1 + rsiUp / rsiDown))) - 50 // shifted 50 points to make 0 median // sma of rsi rsiMa = ta.sma(rsi, maLength) // self explanatory.. tradeDirection = tradeInvert ? 0 <= rsiMa ? true : false : 0 >= rsiMa ? true : false // === /SERIES === // === PLOTTING === barcolor(color = tradeDirection ? color.green : color.red, title = "Bar Colours") // hlines medianLine = hline(0, title = 'Median', color = color.orange, linestyle = hline.style_dotted, linewidth = 1) limitUp = hline(25, title = 'Limit Up', color = color.silver, linestyle = hline.style_dotted, linewidth = 1) limitDown = hline(-25, title = 'Limit Down', color = color.silver, linestyle = hline.style_dotted, linewidth = 1) // rsi and ma rsiLine = plot(rsi, title = 'RSI', color = color.purple, linewidth = 2, style = plot.style_line, transp = 50) areaLine = plot(rsiMa, title = 'Area MA', color = color.silver, linewidth = 1, style = plot.style_area, transp = 70) // === /PLOTTING === goLong() => not tradeDirection[1] and tradeDirection killLong() => tradeDirection[1] and not tradeDirection if(goLong()) strategy.entry(id = "Buy", direction = strategy.long) if(killLong()) strategy.close(id = "Buy") goShort() => tradeDirection[1] and not tradeDirection killShort() => not tradeDirection[1] and tradeDirection if(goShort()) strategy.entry(id = "Sell", direction = strategy.short) if(killShort()) strategy.close(id = "Sell") if (useStop) strategy.exit("XSL", from_entry = "Buy", loss = slPoints) strategy.exit("XSS", from_entry = "Sell", loss = slPoints) // if we're using the trailing stop if (useTS and useTSO) // with offset strategy.exit("XSL", from_entry = "Buy", trail_points = tslPoints, trail_offset = tslOffset) strategy.exit("XSS", from_entry = "Sell", trail_points = tslPoints, trail_offset = tslOffset) if (useTS and not useTSO) // without offset strategy.exit("XSL", from_entry = "Buy", trail_points = tslPoints, trail_offset = na) strategy.exit("XSS", from_entry = "Sell", trail_points = tslPoints, trail_offset = na)