Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /@version=5
- strategy(
- "Learning Scripts RSI",
- overlay=true,
- initial_capital=1000,
- default_qty_type=strategy.percent_of_equity,
- default_qty_value=1,
- commission_type=strategy.commission.percent,
- commission_value=0.05
- )
- // Variables
- //// Inputs
- // 1. Get RSI Long and short variables
- rsiLength = input.int(14, "RSI Length", group="RSI")
- rsiLongThreshold = input.int(70, "RSI Long Threshold", group="RSI")
- rsiShortThreshold = input.int(30, "RSI Short Threshold", group="RSI")
- // 2. Changed EMAs to inputs so we can change them as well
- fastEmaLength = input.int(12, "Fast EMA Length", group="EMA")
- slowEmaLength = input.int(21, "Slow EMA Length", group="EMA")
- stopLossPercentage = input.float(3, "Stop Loss Percentage", group="Exits") * 0.01
- takeProfitPercentage = input.float(3, "Take Profit Percentage", group="Exits") * 0.01
- //// Bands
- fastEma = ta.ema(close, fastEmaLength)
- slowEma = ta.ema(close, slowEmaLength)
- //// Exit lines
- var line stopLossLine = na
- var line takeProfitLine = na
- if strategy.position_size == 0
- stopLossLine := na
- takeProfitLine := na
- else
- line.set_x2(stopLossLine, bar_index)
- line.set_x2(takeProfitLine, bar_index)
- // Logic
- // 3. Get RSI and check if it is within the threshold
- rsi = ta.rsi(close, rsiLength)
- rsiWithinThreshold = rsi <= rsiLongThreshold and rsi >= rsiShortThreshold
- //// Longs
- // 4. Check if rsi is within threshold before buys and sells
- if strategy.position_size == 0 and ta.crossover(fastEma, slowEma) and rsiWithinThreshold
- stopLoss = close * (1 - stopLossPercentage)
- takeProfit = close * (1 + takeProfitPercentage)
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- strategy.entry("Long", strategy.long)
- strategy.exit("SL / TP", "Long", stop=stopLoss, limit=takeProfit)
- //// Shorts
- // 4. Check if RSI is within threshold before buys and sells
- if strategy.position_size != 0 and ta.crossunder(fastEma, slowEma) and rsiWithinThreshold
- stopLoss = close * (1 - stopLossPercentage)
- takeProfit = close * (1 + takeProfitPercentage)
- stopLossLine := line.new(bar_index, stopLoss, bar_index, stopLoss, color=color.red)
- takeProfitLine := line.new(bar_index, takeProfit, bar_index, takeProfit, color=color.green)
- strategy.entry("Short", strategy.short)
- strategy.exit("SL / TP", "Short", stop=stopLoss, limit=takeProfit)
- // Plots
- bandColor = fastEma > slowEma ? color.green : color.red
- plot(fastEma, color=bandColor, linewidth=1)
- plot(slowEma, color=bandColor, linewidth=3)
- // 5. Base rsi color on band color
- rsiColor = rsiWithinThreshold ? bandColor : color.white
- plot(rsi, color=rsiColor, display=display.status_line) // Display it only in the status line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement