Ready to go further
Next: test a rule without fooling yourself
The backtesting guide covers costs, untouched data and the common ways a good-looking report turns out to be a fit to the past.
What is included
- What it is, in plain words
- The formula or mechanism, with exact arithmetic
- How it hits grid, martingale and DCA bots
- A check you can run before you switch a bot on
Who it is for
- People who run or plan to run a trading bot
- Anyone reading a backtest or a bot sales page
- Readers who want the numbers, not the pitch
- Not advice, and not a signal service
Key facts
- Pine Script is the name of the scripting language used on one popular charting site. It runs on that site's chart, on the bars the chart already holds. Menus and features there change, so check the current documentation.
- There are two kinds of script. An indicator draws something on the chart. A strategy also places pretend orders, and the site then reports a made-up trade list and results.
- A chart backtest is a guess about what would have happened. It usually fills at a bar price, has no queue, and may start with costs set to zero or to small defaults.
- In our made-up example, 40 trades that earned 12.00 points before costs earned 4.00 after a plausible cost of 0.20 a trade. Double the cost and the total is -4.00.
- A script that looks good on a chart is a draft of an idea. It still needs fresh data, honest costs and a test on a demo account. This is education, not advice.
What a script language for a charting site is
A charting site shows prices as bars or candles. A script language lets a user add their own calculations to that chart: a moving average, a band, a count of something. The script is small. It does not connect to a broker on its own, and it does not run on your computer. It runs on the site's servers against the history the chart has loaded, and it sees one bar at a time, from the oldest to the newest.
That design makes it quick to try an idea. It also means the language is shaped around bars. It thinks in closes, highs and lows. It has no idea what the order book looked like in between.
Indicator scripts and strategy scripts
The two kinds do different jobs, and mixing them up causes confusion.
- An indicator script calculates numbers and draws them. It never trades, even in pretend. You use it to look at something, such as a relative strength index or Bollinger Bands.
- A strategy script declares rules for entering and exiting. The site then simulates those orders on the history and builds a report: a list of trades, a profit figure, a drawdown figure. That report is what people mean by a chart backtest.
Both use the same ideas: inputs the user can change, values worked out from past bars, and conditions that become true or false on each bar. The names and exact syntax vary and change between versions, so this page describes ideas, not code you can paste.
The shape of a strategy script
Most strategy scripts have the same four parts: settings (costs, starting capital, order size), inputs (the numbers a user may change), calculations (averages, bands, counts) and rules (when to enter and when to exit). Here is a tiny made-up sketch in plain pseudo-code. It is not real syntax and not a rule to trade.
// A made-up strategy sketch in plain pseudo-code. Not real syntax.
name: "Example crossover"
inputs: fast = 10, slow = 30
each new bar:
fastAvg = average of last 10 closes
slowAvg = average of last 30 closes
if fastAvg crosses above slowAvg:
enter long at the next bar open
if fastAvg crosses below slowAvg:
exit the long
Read it as a sentence: when the faster average rises through the slower one, buy at the next bar's open, and sell when it falls back through. This is the same idea as an EMA crossover. The sketch is short on purpose. The hard part is not writing the rule, it is knowing whether the rule's result means anything.
Why chart backtests flatter results
A chart is a record, not a market. The strategy report is built by replaying that record and assuming orders happen where the script says. Several quiet assumptions push the result up.
- Fills at bar prices. The simulator fills an order at a close or an open. A real order meets the spread and may suffer slippage. At fast moments, the price you wanted is not there.
- No queue. A limit order in a real market waits behind other orders. A chart often assumes it filled the moment price touched the level, even if only one unit traded there.
- Costs at defaults. Commission and slippage settings may start at zero or at small values. Each trade in a real account pays its own costs. Many small trades add up. Check what your chart uses, because defaults vary and change.
- Bars hide the path. A bar shows an open, high, low and close. It does not show which of the high or low came first. A stop and a target inside one bar cannot be ordered correctly from that bar alone.
- Look-ahead and repainting. A script that uses a value which is only final at the close of a bar, but acts as if it knew it at the open, is using the future. See look-ahead bias.
- Tuning to the past. Changing inputs until the report looks good is fitting, not finding. See overfitting.
A worked example with made-up numbers
Suppose a strategy report shows 40 trades and an average of 0.30 points before costs, a total of +12.00. Now add three costs a real account might pay on each trade (made-up values): a spread of 0.10, a commission of 0.04 and slippage of 0.06. That is 0.20 a trade, or 8.00 over 40 trades. Everything in the table below was calculated by us on 29 Oct 2026 and the figures are made up.
| Case | Cost a trade | Total cost | Net result | Net a trade |
|---|---|---|---|---|
| Chart default, no costs | 0.00 | 0.00 | +12.00 | +0.30 |
| Spread, commission, slippage | 0.20 | 8.00 | +4.00 | +0.10 |
| Same costs doubled | 0.40 | 16.00 | -4.00 | -0.10 |
The idea did not change. Only the honesty of the costs did. The edge per trade fell from 0.30 to 0.10, and a little more cost turned it negative. A rule that trades often is the most exposed, because costs are paid per trade and an average gain per trade can be small.
How to read a strategy report with care
- Set costs yourself. Enter a spread, commission and slippage you have checked for the market you plan to use, then run it again.
- Count the trades. A report built on a few trades says little. See backtesting.
- Look at the worst stretch, not only the total. A rule that ends up well may have been unbearable on the way. See drawdown.
- Test on data it was not built on, then on a demo account. See forward testing and walk-forward testing.
- Be suspicious of a smooth curve. It often means a rule fitted to history, not one with an edge.
What a script cannot tell you
It cannot tell you how your broker will fill, how your platform will behave in a fast market, or whether the pattern will still exist next month. It does not know your risk limits. It is a first sketch, and the checks above are the real work.
Keep Going
Related Tools and Guides
Keep exploring
FAQ
Frequently Asked Questions
What is Pine Script?
It is the name of the scripting language used on one popular charting site. It lets a user draw their own indicators and test simple rules on the chart. Its details change over time, so use the site's own documentation.
What is the difference between an indicator and a strategy script?
An indicator calculates and draws values and never places orders. A strategy script also declares entries and exits, and the site simulates them to build a report of trades and results.
Can a script trade for me?
A script on a chart does not send orders to a broker by itself. Other parts, such as alerts and a separate program, would be needed. Each added part is another place for something to fail.
Why do chart backtests look better than live results?
They usually fill at bar prices, ignore queues, and may use zero or small costs. They also invite tuning to the past. Adding honest costs and testing on new data brings the numbers down to earth.
Does a strategy report prove a rule works?
No. It shows what the rule would have done on one stretch of history under the report's assumptions. It does not show the future or the effect of real fills.
Are the numbers on this page real?
Example numbers are made up to show how the maths works, and the page says so. A figure only counts as exact when the page says it was calculated on a stated date. Nothing you enter in a calculator leaves your browser.
Can I use this to decide whether to run a bot?
It can show whether a bot's numbers hold together and how much an account can lose. It cannot say what the market will do, and it is not advice. Use it to ask better questions, then read the terms of your own broker or exchange.
Is this financial advice?
No. It is general education. The numbers are made up, and it does not consider your situation.
Know the Risk Before You Run It
Free calculators and guides, added every week. Leave your details and we will email you when new ones go up.
Trading carries a risk of loss