COPY AND PASTE INTO TRADINGVIEW
//@version=5
strategy("15min Buy/Sell Strategy with Backtest", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Restrict to 15-minute timeframe
if (timeframe.period != "15")
runtime.error("Please use this strategy on the 15-minute chart only.")
// === INPUTS ===
fastEMA = input.int(9, title="Fast EMA")
slowEMA = input.int(21, title="Slow EMA")
rsiPeriod = input.int(14, title="RSI Period")
rsiOverbought = input.int(70, title="RSI Overbought Level")
rsiOversold = input.int(30, title="RSI Oversold Level")
takeProfitPerc = input.float(2, title="Take Profit (%)", step=0.1)
stopLossPerc = input.float(1.0, title="Stop Loss (%)", step=0.1)
// === CALCULATIONS ===
emaFast = ta.ema(close, fastEMA)
emaSlow = ta.ema(close, slowEMA)
rsi = ta.rsi(close, rsiPeriod)
// === ENTRY CONDITIONS ===
longCondition = ta.crossover(emaFast, emaSlow) and rsi < rsiOverbought
shortCondition = ta.crossunder(emaFast, emaSlow) and rsi > rsiOversold
// === STRATEGY ENTRY ===
if (longCondition)
strategy.entry("Buy", strategy.long)
if (shortCondition)
strategy.entry("Sell", strategy.short)
// === STRATEGY EXIT ===
strategy.exit("Exit Buy", from_entry="Buy", profit=takeProfitPerc * syminfo.pointvalue * close / 100, loss=stopLossPerc * syminfo.pointvalue * close / 100)
strategy.exit("Exit Sell", from_entry="Sell", profit=takeProfitPerc * syminfo.pointvalue * close / 100, loss=stopLossPerc * syminfo.pointvalue * close / 100)
// === PLOTTING ===
plot(emaFast, color=color.orange, title="Fast EMA")
plot(emaSlow, color=color.blue, title="Slow EMA")
plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
