Example output
Here’s a complete Pine Script code snippet for your requested backtesting strategy in TradingView:
```pinescript
//@version=5
strategy("Trend Following EMA Crossover", shorttitle="EMA Crossover", overlay=true)
// === Input Parameters ===
fastLength = input.int(50, title="Fast EMA Length", minval=1)
slowLength = input.int(200, title="Slow EMA Length", minval=1)
riskReward = input.float(2.0, title="Risk/Reward Ratio", minval=1.0)
stopLossPercent = input.float(1.0, title="Stop Loss %", minval=0.1) / 100
takeProfitPercent = input.float(2.0, title="Take Profit %", minval=0.1) / 100
// === EMA Calculation ===
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)
// === Entry and Exit Conditions ===
longCondition = ta.crossover(fastEMA, slowEMA)
shortCondition = ta.crossunder(fastEMA, slowEMA)
// Track trade entry price
var float entryPrice = na
if (longCondition)
strategy.entry("Long", strategy.long)
entryPrice := close
if (shortCondition)
strategy.close("Long")
entryPrice := na
// === Stop Loss and Take Profit ===
if not na(entryPrice)
stopLossLevel = entryPrice * (1 - stopLossPercent)
takeProfitLevel = entryPrice * (1 + takeProfitPercent)
strategy.exit("Exit TP/SL", "Long", stop=stopLossLevel, limit=takeProfitLevel)
// === Visual Markers ===
plotshape(series=longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// === Performance Metrics ===
var float totalNetProfit = na
var float winRate = na
var float profitFactor = na
if barstate.islastconfirmedhistory
totalNetProfit := strategy.netprofit
trades = strategy.closedtrades
winningTrades = trades.size() - trades.losses
winRate := na(winningTrades) ? na : (winningTrades / trades.size()) * 100
profitFactor := trades.grossprofit / trades.grossloss
// === Display Metrics ===
plot(totalNetProfit, title="Total Net Profit", color=color.blue)
plot(winRate, title="Win Rate", color=color.purple)
plot(profitFactor, title="Profit Factor", color=color.orange)
// === Comments ===
// 1. Configurable inputs allow tuning of EMA lengths, stop loss, take profit, and risk/reward ratio.
// 2. Entry occurs when the 50 EMA crosses above the 200 EMA (bullish trend).
// 3. Exit occurs on reverse crossover or based on configurable stop loss and take profit levels.
// 4. Visual markers indicate buy (green) and sell (red) signals on the chart.
// 5. Key performance metrics (win rate, profit factor, total net profit) are calculated and plotted.
```
### How It Works:
1. **Entry Rules**: A long position is entered when the 50 EMA crosses above the 200 EMA.
2. **Exit Rules**: The position is closed either when the 50 EMA crosses below the 200 EMA or when stop loss/take profit levels are hit.
3. **Configurable Parameters**: Parameters like EMA lengths, stop loss %, and take profit % are adjustable in the script’s settings.
4. **Visual Markers**: Buy and sell signals are marked on the chart for easy interpretation.
5. **Performance Metrics**: Metrics like total net profit, win rate, and profit factor are displayed on the chart as plots.
You can copy this code into TradingView's Pine Script editor to test and refine the strategy.