13
//version=5
strategy("Intraday Buy/Sell with Target & SL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// INPUTS
rsiLength = input(14, "RSI Length")
rsiOverbought = input(70, "Overbought")
rsiOversold = input(30, "Oversold")
maLength = input(20, "Moving Average Length")
targetPercent = input(0.5, "Target %") / 100
stopLossPercent = input(0.3, "Stop Loss %") / 100

// INDICATORS
rsi = ta.rsi(close, rsiLength)
ma = ta.sma(close, maLength)

// BUY CONDITION: Price > MA and RSI < 30 (oversold)
longCondition = close > ma and rsi < rsiOversold
if (longCondition)
strategy.entry("Buy", strategy.long)
strategy.exit("TP/SL", from_entry="Buy", profit=targetPercent * close, loss=stopLossPercent * close)

// SELL CONDITION: Price < MA and RSI > 70 (overbought)
shortCondition = close < ma and rsi > rsiOverbought
if (shortCondition)
strategy.entry("Sell", strategy.short)
strategy.exit("TP/SL", from_entry="Sell", profit=targetPercent * close, loss=stopLossPercent * close)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.