CoinScriptBacktest
Backtest
Test your script's signals as a strategy on the chart's candles, read the metrics, equity curve and trade list, and optimize up to two parameters.
The Backtest tab in the MantaAi panel turns your script's signals into a strategy and works out what it would have made on the chart's candles.
- A
signal()withpos: 'below'is a long entry. - A
signal()withpos: 'above'is a short entry.
The backtest cannot test a script without signal(). It then reports: “No signal() in the script. Long entries come from signal(…, { pos: 'below' }), short entries from pos: 'above'.”
What the backtest calculates on
The backtest takes exactly the candles the chart has currently loaded: the same symbol, the same timeframe. The top of the tab shows the script's name and the period, for example “5000 candles · 1h”. If you scroll further back on the chart, it loads older candles and the backtest calculates over the longer period. In replay it ends at the replay point.
It recalculates by itself as soon as you change the code, the direction or the exit, or a new candle arrives. A run may take up to 2 seconds.
The rules
- Entry and exit happen at the candle's close.
- There is only ever one position. Further entry signals in the same direction are ignored while it is open.
- Each trade costs 0.05% in fees per side, so 0.1% in total. They are already deducted from each trade's result.
- Each trade uses the entire capital, without leverage. The results compound: a trade with +2% after one with +2% gives about +4.04%.
- On the last candle, the backtest no longer opens anything.
Direction and exit
| Direction | Meaning |
|---|---|
| Long + short | Both sides are traded. |
| Long only | Long entries only. Short signals can still close a long position (with the Opposite signal exit). |
| Short only | Short entries only, the other way around. |
| Exit | Meaning |
|---|---|
| Opposite signal | A long closes on the next short signal, a short on the next long signal. With Long + short, the opposite direction is opened at the same time. |
| After 10 candles | The position closes at the close 10 candles after entry. |
| TP 2% · SL 1% | Take profit 2% and stop loss 1% from the entry price, checked against the high and low of the following candles. Exit exactly at that level. |
With TP 2% · SL 1%, the backtest does not know what happened first within a candle. If a candle touches both levels, the stop loss counts. With After 10 candles and TP 2% · SL 1%, opposite signals do not close a position.
The terminal remembers the direction and exit in your settings.
The metrics
| Metric | Meaning |
|---|---|
| Net | The strategy's result over the whole period, after fees |
| Buy & hold | Price change from the first to the last close, for comparison |
| Trades | Number of closed trades |
| Win rate | Share of trades that closed in profit after fees |
| Profit factor | Sum of all profits divided by the sum of all losses. Above 1 the strategy makes money, ∞ means no losing trade |
| Max drawdown | Largest decline of the equity curve from a high, including open positions |
Without closed trades, win rate and profit factor show “–”.
Equity curve
The Strategy curve shows the capital candle by candle, with open positions valued at the respective close. It is turquoise if the strategy ends in profit, otherwise red. Buy & hold runs alongside it as a dashed line, and a horizontal line marks the starting value. Below it, as a reminder: “Fees 0.05% per side · entry and exit at candle close”.
Trade list
At the bottom are the last 40 closed trades, newest first:
| Column | Content |
|---|---|
| # | Running number |
| Side | Long or short |
| Entry | Date and time of entry, with entry and exit price below |
| Duration | Holding time in candles, for example “7 K.” |
| Result | The trade's result in percent, after fees |
If there are none, it says “No closed trades in this period.”
Optimization
Optimization tries many values for your inputs and shows which combination would have done best. It is included from the Pro plan up.
- The script needs at least one
input(), for exampleconst len = input('Länge', 21). The first two number inputs are optimized. - For each input, the terminal suggests From, To and Step around the current value. You can change them, and both comma and period work. The top shows how many combinations this gives.
- Under Sort by, choose what “best” means: Net, Profit factor, Win rate or Drawdown (smallest first).
- Click Start optimization. The progress runs along, for example “Optimizing … 120 / 400”.
Up to 30 values are possible per input, and up to 400 combinations in total. Direction and exit apply as set above.
Reading the result
- Heatmap (with two inputs): Each cell is a combination, rows are the second input, columns the first. Turquoise means profit, red loss, the stronger the larger. The best combination has a border. When you hover over a cell, you see the values, net and number of trades.
- Ranking: the best eight combinations with net, trades and profit factor (PF).
- Apply writes a row's values as new defaults into your
input()calls, for example “Parameters applied: Schnell 12 · Langsam 30”.
This works if the default is a number directly in the call, as in input('Schnell', 9). If you then change the code, the direction or the exit, the result disappears because it no longer fits. Combinations where the script throws an error are missing from the result.
What the backtest does not model
- No slippage and no spread: orders fill exactly at the close or at the TP or SL level.
- No funding payments, no leverage, no position sizes, no partial fills.
- Signals on the running candle can still change until the candle closes.
delta,cvd,buyVolandsellVolcan differ slightly in the backtest from the values on the chart.- A good result in the past is no guarantee for the future.