Skip to main content
← Back to blog

Pine Script for beginners: your first backtest (and why it can lie)

📅 2026-08-17
✍️ Strategy Arena
tradingview pine script backtest beginner guide overfitting

The big moment: your rule becomes a program

So far you may have followed the path: first chart, alerts, Paper Trading, Bar Replay. At each step one idea returned: a rule written before the event. Pine Script is the logical next step: the rule becomes a program, and TradingView can test it on years of history in one second.

It is intoxicating. It is also the exact place most beginners get trapped. This guide does both: the first script, then the vaccine.

No TradingView account yet? The 7-step Starter path gets you here from zero (disclosed partner link for new accounts, neutral otherwise). Pine Editor is available on the free plan.

Your first script (10 min)

Open Pine Editor (tab at the bottom of the chart), clear the contents and paste this:

//@version=5
strategy("My first strategy - EMA cross",
     overlay=true,
     initial_capital=10000,
     commission_type=strategy.commission.percent,
     commission_value=0.1)

fastEma = ta.ema(close, 20)
slowEma = ta.ema(close, 50)

if ta.crossover(fastEma, slowEma)
    strategy.entry("Buy", strategy.long)

if ta.crossunder(fastEma, slowEma)
    strategy.close("Buy")

plot(fastEma, color=color.new(color.blue, 0))
plot(slowEma, color=color.new(color.orange, 0))

Click “Add to chart”. The rule is readable in plain language: buy when the 20-period average crosses above the 50, exit on the opposite cross. Notice commission_value=0.1: a backtest with no fees is already a lie — every round trip costs, and on strategies that trade often, fees eat everything.

Read the Strategy Tester (the 4 numbers that matter)

The Strategy Tester tab opens with your results. Ignore the temptation to look only at total profit. The useful numbers:

  1. Number of trades: below ~30 trades, results are statistical noise. A “+80%” built on 7 trades says nothing.
  2. Max drawdown: the worst drop from a peak. That is the number that tests your stomach — would you have held −35% without unplugging?
  3. Fees paid: compare them to net profit. If fees eat a notable fraction of the gain, the strategy lives on paper.
  4. Net profit vs Buy & Hold: making +40% while the asset did +90% by simply holding… is not winning.

Why this backtest can lie

Here is the central trap, and it is worth understanding once and for all: your strategy was chosen BECAUSE it worked on that past.

Change 20 and 50 to other values. Try 10/40, 15/60, 25/45… In a few attempts you will find a combo that prints a spectacular result. You did not discover a martingale: you memorized the past. That is overfitting, and it does not forgive.

We showed it at industrial scale: our GPU tested millions of variants and found a strategy at +172% on its training history. On data it had never seen, the same strategy lost 37%. The flattering backtest was not proof. It was a mirror.

Three defense reflexes, from your first script:

  • realistic fees in strategy() — always;
  • no fine-tuning: if the strategy only holds at exactly 20/50 and collapses at 19/52, it does not hold;
  • out-of-sample test: keep a period your script never saw during tuning, and judge it only there.

The real test: sealed data

The third reflex is the hardest to impose on yourself — when you know your data, you cheat without meaning to. That is exactly the service Strategy Arena provides: the free Quick Check first checks Pine compatibility, with no account. Then the independent audit runs your strategy on a sealed dataset it has never seen, with a machine verdict — KEEP or REJECT, and payment never changes the verdict.

Your first EMA-cross script will probably get a REJECT. That is normal, and it is the good news: in ten minutes you will know what months of live trading would have billed you to learn.

Beginner path recap

  1. First day: chart, watchlist, alerts;
  2. Paper Trading: the simulator;
  3. Bar Replay: decide in the fog;
  4. Pine Script: the rule becomes a program (you are here);
  5. The independent test on unseen data — the only judge that does not flatter anyone.

Write your first script tonight. And do not take its word for it.

Open the 7-step Starter path

This guide is part of Track 0 — TradingView Starter in the Academy, Strategy Arena's free course.

Disclosure: Strategy Arena is in the TradingView partner program. If you create a new account through our path and then subscribe on the web, we may receive a commission — at no extra cost to you. Existing accounts use neutral links.

⚠️ Disclaimer — This article is for informational and educational purposes only. It does not constitute investment advice or a buy/sell recommendation. Past performance does not guarantee future results. Strategy Arena is an educational simulator with virtual capital. Always do your own research before making investment decisions.

Enjoyed this article? Share it

𝕏 Share on X ✈️ Telegram