Jayadev Rana Get a custom script
Free Pine Script Indicators / EMA Trend Strategy
Free strategy · Pine Script v6

EMA Trend Strategy

The EMA trend idea as a backtestable strategy: realistic commission and slippage, ATR stop-loss and take-profit, and JSON alert messages ready for webhook automation.

StrategyPine Script v6Overlay + Strategy TesterFree

Get the codeDownload .pine.txt

Snapshots

Backtest on OANDA:SPX500USD 4h (2013 to Sep 2026): 427 trades, 43.8% winners, profit factor 0.953, total P&L -$112.
Backtest on OANDA:SPX500USD 4h (2013 to Sep 2026): 427 trades, 43.8% winners, profit factor 0.953, total P&L -$112.
List of trades: every entry and exit with its profit or loss.
List of trades: every entry and exit with its profit or loss.
Strategy properties: capital, order size, 0.05% commission, 2 ticks slippage, on-bar-close execution.
Strategy properties: capital, order size, 0.05% commission, 2 ticks slippage, on-bar-close execution.
The complete strategy in the Pine Editor.
The complete strategy in the Pine Editor.

How it works

  1. Honest result: about +$298 before costs, but ~$411 of commission turned it into a -$112 loss. Test costs before you trust any backtest.
  2. Each trade risks 2 ATR (stop) to make 3 ATR (target), sized at 10% of equity.
  3. For automation, create a strategy alert and put {{strategy.order.alert_message}} in the message box; your webhook receives {"side":"buy","symbol":"SPX500USD"}.

Settings

SettingDefaultWhat it does
Initial capital10,000Starting equity
Order size10% of equityPer trade
Commission0.05% per orderBroker cost
Slippage2 ticksExecution cost
Stop / Target2 ATR / 3 ATRSet in strategy.exit

Source code

In TradingView: open the Pine Editor, create a new strategy, paste the code, then click "Add to chart".

ema-trend-strategy.pine.txtGitHubDownload
//@version=6
strategy("EMA Trend Strategy", overlay = true, initial_capital = 10000,
  default_qty_type = strategy.percent_of_equity, default_qty_value = 10,
  commission_type = strategy.commission.percent, commission_value = 0.05,
  slippage = 2)

fastEma  = ta.ema(close, 9)
slowEma  = ta.ema(close, 21)
trendEma = ta.ema(close, 200)
atr      = ta.atr(14)

longSignal  = ta.crossover(fastEma, slowEma) and close > trendEma
shortSignal = ta.crossunder(fastEma, slowEma) and close < trendEma

buyMsg  = '{"side":"buy","symbol":"{{ticker}}"}'
sellMsg = '{"side":"sell","symbol":"{{ticker}}"}'

if longSignal
    strategy.entry("Long", strategy.long, alert_message = buyMsg)
if shortSignal
    strategy.entry("Short", strategy.short, alert_message = sellMsg)

if strategy.position_size > 0
    strategy.exit("Long exit", "Long",
      stop  = strategy.position_avg_price - 2 * atr,
      limit = strategy.position_avg_price + 3 * atr)
if strategy.position_size < 0
    strategy.exit("Short exit", "Short",
      stop  = strategy.position_avg_price + 2 * atr,
      limit = strategy.position_avg_price - 3 * atr)

plot(fastEma, "Fast EMA", color.teal)
plot(slowEma, "Slow EMA", color.orange)
plot(trendEma, "Trend EMA", color.gray, 2)

Watch it built

This script is written line by line in the TradingView Masterclass video.

Need a custom indicator or strategy?

Non-repainting Pine Script v6, backtested with real costs, alert and webhook ready. Fixed quote within 24 hours.

Free and open source under the Mozilla Public License 2.0. Educational content only, not financial advice. Backtest results are historical and include the costs stated; past performance does not predict future results. © Jayadev Rana · Privacy · Terms