Skip to main content
Turn a signal into a strategy and see what it would have made after it paid to trade. Declare strategy() in place of indicator(), say on each closed bar what position you want, and the engine fills it, charges what the venue would have charged, and keeps the account.
The chart shows each fill as an arrow on the price and the equity curve in a pane of its own, beside buy-and-hold. Report in the legend opens the Strategy report.

Targets

strategy.target(fraction) is the position you want as a fraction of equity: 1 fully long, −1 fully short, 0 flat. strategy.targetUsd(usd) takes a notional instead, and strategy.targetSize(coins) a size in coins. All are held to leverage times equity. Nothing makes a strategy trade both ways: one that only ever asks for 0 to 1 is long only, and one that asks for −1 to 0 is short only.
  • A target applies on closed bars only. By default it fills at the next bar’s open, so a script never trades on a close it hasn’t seen. fill: 'close' fills at the signal bar’s own close, the shortcut many testers take.
  • The same target again holds the position as it is: calling strategy.target(1) every bar doesn’t trade as equity moves. A new value trades to it.
  • An order under $10 is not sent, as the venue would refuse it.
strategy.position, strategy.equity, strategy.cash, strategy.closedTrades and strategy.costs read the account back in a handler, marked at the bar’s close. strategy.position.entries counts the fills that opened or added to the position, and entryBar and entryTime say when it opened.

Stops, limits and exits

A target with a stop or a limit rests until price reaches it, rather than filling at the next open. An exit closes the position at a stop or a limit, whichever price reaches first.
  • A stop fills once price trades through it (above for a buy, below for a sell), at its price, or at the open when a bar opens past it. It pays the taker fee and the slippage.
  • A limit fills once price trades at it, at its price or a better open. It pays the maker fee and no slippage.
  • Inside a bar, price is taken to go from the open to the nearer of the high and the low, then to the other, then to the close, as TradingView’s strategy tester assumes. Each order fills where that path first reaches it. An exit placed with its entry can fill in the bar the entry opens.
  • strategy.exit({ stop, limit }) takes prices. { loss, profit } takes distances from the entry price, placed once the position is open. Both can be given; the one reached first fills. side: 'long' or 'short' keeps an exit to one side.
  • An order placed on a bar’s close can fill from the next bar on. It stays until it fills, another with its id replaces it, strategy.cancel(id) removes it, or, for an exit, the position it closes is closed. A strategy holds at most 16.

What it pays

Each cost is charged on its own and listed in the report, so you can see what took the edge.
  • Fees: the venue’s base tier (Hyperliquid’s rates, twice them on HIP-3, Lighter’s). A market or stop fill pays the taker rate, a limit fill the maker rate. fees: { takerBps, makerBps } sets your own; makerBps defaults to takerBps.
  • Slippage: what a market order of the fill’s size paid against the order book at the signal bar’s close, the book the next open trades into. The archive records what $10k, $100k and $1M cost on each bar it holds (Hyperliquid and HIP-3); sizes between are interpolated, smaller ones run down to half the spread, and larger ones carry the last stretch on (the report counts them). On a bar the archive has no reading for, a fill costs fallbackBps (2). The report says how many fills the book priced and from when. slippage: { bps } uses a flat cost throughout.
  • Funding: every hour a position is open it pays or collects that hour’s rate on its notional, as the venue settles it, from 2025-03-01. A 4h or 1d bar charges each hour inside it. Funding needs a time chart of 1m or longer; on other charts pass funding: false.
  • Liquidation: with leverage above 1, a position whose equity at the bar’s worst price falls below maintenance margin is closed where it would have been (half the initial margin at the market’s highest leverage). An account never goes below zero; the report says what the venue’s backstop took.
A backtest is one path through history. A strategy tuned until it looks good on that path has learned the path. Check it on a stretch it was not tuned on, and score its forecast with ml.score (see statistics and machine learning), before you trust it.

History

from: '2025-03-01' loads the chart’s history back to that date before the run, so the report covers more than the bars on screen. The run takes longer on more history; the legend says how far it has got.

The Strategy report

The report opens in a panel of its own, from Report in the legend or the legend’s menu.
  • Overview: the net result against buy-and-hold, max drawdown, Sharpe, profit factor, trades, win rate and exposure; the equity curve with its drawdown underneath; what each cost took from the gross; and the forecast’s score when the script declares an ml.score.
  • Trades: every round trip with its costs. Click one to bring it into view on the chart.
  • Assumptions: every rule and source the run used: fills, fees, slippage, where the order book and funding start, leverage and maintenance, and how stops and limits fill when the script places them.
Every fill is simulated in the browser and nothing reaches an exchange. The same bars and settings give the same trades on every run.

Portfolios

strategy(name, { portfolio: true }) trades many markets in one account: shared cash, cross margin, every Hyperliquid perp. Set one market’s weight with strategy.target(0.2, { market: 'ETH' }), or the whole book with strategy.targets({ ETH: 0.2, SOL: 0.2 }), where a market left out goes to 0. A weight is a fraction of equity, as one market’s target is, and fills at the open of that market’s next bar.
  • Picking and trading read different data. Picking a universe reads the universe history, one row a day for every perp. Trading reads each market’s own bars at the chart’s interval, the same bars its chart shows: fills, marks, liquidation and the pool’s price reads. On 1D they come in the universe history’s monthly files, which hold exactly the daily bars a chart shows, so a daily run loads in seconds. Intraday each picked market’s bars load on their own: quick over weeks, slower over many months of hourly bars.
  • The universe is picked point in time. universe.topN(n) holds the n perps with the most dollar volume over the last 30 days, among those with 60 days of prices, re-picked every 30 days. universe.rule(select) runs a rule of your own: select(view) reads any market’s days before the pick (markets, listed, closes, volume, openInterest, funding, volatility, historyDays) and returns the members. Both pick from every perp that traded then, delisted ones included, so a run never holds a coin for what it later became. pool.view reads the same days as of now.
  • The pool’s price reads use the members’ bars. pool.close(m), closes, ret, volatility, beta, resid (the move a reference such as BTC does not explain) and volume count bars of the pool’s interval, the chart’s by default; universe.topN(25, { interval: '1d' }) reads daily bars on an intraday chart. A bar is read once it has closed. Intraday a market’s bars load the first time it is picked; until they land its reads are na and the report says it is loading.
  • It runs on any time chart from 1m to 1W. On a 4h chart it trades every 4 hours; the universe is still re-picked on the days every sets. The chart shows its own market’s fills, its sleeve of the book, and the whole account’s equity. An intraday run covers the history the chart holds, as any script does: scroll back for more.
  • The costs. Each fill pays the taker fee and a flat 2 bps of slippage (slippage: { bps } sets your own): the archive’s book readings start on 6 Sep 2026, too late for most of a portfolio’s history. Every hour a position is held it pays or collects that day’s mean hourly funding rate. The book is held to leverage at every rebalance; below the sum of its maintenance margins at the bar’s worst prices, the whole account is liquidated.
The Strategy report adds Markets, what each market made after its costs, and Book, the weights after each rebalance beside the universe’s picks.

Next step

See it on real data in RSI PCA forecast, Funding carry and Residual momentum.
Last modified on November 2, 2026