Write
Strategies
Turn signals into simulated trades with strategy(), read the backtest panel, and understand what each metric does and does not tell you.
An indicator draws. A strategy also places simulated orders, and Tradequix shows the resulting backtest: trades on the chart and a results panel with profit, drawdown and win rate.
Declare strategy(...) instead of indicator(...), then use strategy.entry, strategy.close and strategy.exit.
//@version=6strategy("EMA cross with stops", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 10)fast = ta.ema(close, 12)slow = ta.ema(close, 26)atr = ta.atr(14)plot(fast, "Fast", color = color.teal)plot(slow, "Slow", color = color.red)if ta.crossover(fast, slow) strategy.entry("Long", strategy.long)if ta.crossunder(fast, slow) strategy.entry("Short", strategy.short)strategy.exit("Long exit", "Long", stop = close - 2 * atr, limit = close + 3 * atr)strategy.exit("Short exit", "Short", stop = close + 2 * atr, limit = close - 3 * atr)Orders#
| Call | Does |
|---|---|
strategy.entry(id, direction) | Opens or adds to a position; an opposite entry reverses it |
strategy.close(id) | Closes the position opened by entry id at market |
strategy.close_all() | Closes everything |
strategy.exit(id, from_entry, stop, limit) | Attaches a stop loss and/or take profit to an entry |
strategy.order(...) | A raw order, without entry's reversal logic |
Orders fill on the next bar's open by default, which is how a real order placed at the close would behave.
Settings that change results#
Arguments to strategy() that matter most: initial_capital, default_qty_type and default_qty_value (position size), pyramiding (how many entries may stack), commission_type / commission_value, and slippage. Leave commission at zero and a scalping strategy will look far better than it is.
Reading the results panel#
| Metric | Meaning |
|---|---|
| Net profit | The result of closed trades |
| Return | Net profit as a percentage of initial capital |
| Max drawdown | The largest fall from a peak in equity, in money and percent |
| Trades | How many trades closed |
| Win rate | The share of closed trades that made money |
| Gross +/− | Total won and total lost, separately |
| Sharpe / Sortino | Risk-adjusted return, computed from monthly returns |
Each trade's entry and exit is also drawn on the chart, so you can see where the strategy acted.
Before you trust a backtest#
- Run it on more than one pair and more than one timeframe.
- Add commission and slippage that match how you would actually trade.
- Check it does not use future data. A strategy that reads
closeand enters on the same bar's close, or usesrequest.securitywith lookahead on the current bar, can look perfect and never work live. - History on a short timeframe is short. A great result over 300 bars of 5-second data is a few minutes of market.