Trading8 min read
How to turn a Pine Script strategy into Hyperliquid orders
Follow each order or match the position: how a Pine Script strategy becomes Hyperliquid orders through a webhook, with worked examples, costs and caps.
Dylan Shilts, Founder of Hypr Markets
Published
On this page
A Pine Script strategy on TradingView can trade a Hyperliquid account when its order-fill alerts go to a webhook that places the orders. With Webhooks for TradingView on a Hypr Markets terminal there are 2 ways to do it: follow each order, which copies every fill with {{strategy.order.action}} and {{strategy.order.contracts}}, or match the position, which sends {{strategy.market_position}} and {{strategy.position_size}} and moves the account to the strategy's position on every alert. Matching the position corrects a missed alert on the next one.
This post is for traders who have a strategy that backtests well on TradingView and want to know what changes when it trades for real on Hyperliquid. It compares the two templates with worked examples, shows what fees and slippage do to a backtest's edge, explains the caps that keep a strategy inside your limits, and ends with a careful order for going live. Nothing here is financial advice: a strategy that made money in a backtest can lose money live.
How does a Pine Script strategy send orders to Hyperliquid?
A Pine Script strategy sends orders to Hyperliquid through alerts on its order fills. In TradingView you create one alert on the strategy with the condition "order fills", and TradingView fires it every time the strategy fills an order in its simulation on the chart. The alert carries a message, and the strategy's placeholders in that message are filled in with the details of the fill. TradingView's page on variables in alerts lists them.
The webhook does the rest. It reads the message, maps {{ticker}} to a Hyperliquid market, checks the limits and signs the order with the trade-only TradingView key on your own account. So the strategy keeps running on TradingView's servers and decides when to trade, and the terminal decides whether each order is allowed. The step-by-step setup guide covers the key, the webhook and the alert dialog.
Follow each order or match the position?
The follow-each-order template sends what the strategy just did, while the match-the-position template sends where the strategy should be. Both read the same TradingView order fills, and on a normal day they place the same Hyperliquid orders. The difference shows when an alert is missed or the client also trades by hand.
| Follow each order | Match the position | |
|---|---|---|
| Message carries | {{strategy.order.action}}, {{strategy.order.contracts}} | {{strategy.market_position}}, {{strategy.position_size}} |
| Each alert means | "Buy or sell this many coins" | "Be long, short or flat this size" |
| After a missed alert | The account stays out of step until you fix it | The next alert puts it right |
| After a manual trade | The strategy's next order adds to or reduces your trade | The next alert moves the account back to the strategy |
| Alert log reads as | One alert, one order | One alert, the difference only |
Follow each order is easy to read, and it suits a strategy where every order matters on its own, such as one that scales in at fixed levels. Match the position suits almost everything else, because a strategy's real intent is its position, and TradingView itself says alert delivery can occasionally fail.
How does match-the-position work, with numbers?
Match-the-position compares the strategy's position in each alert with the account's actual position on Hyperliquid and sends only what is needed to close the gap. Take an example strategy on BTC that sizes 0.01 BTC, with BTC at an example price of $95,000, so 0.01 BTC is worth $950.
- The strategy goes long. The alert says long 0.01; the account is flat, so the webhook buys 0.01 BTC.
- The strategy takes half off. The alert says long 0.005; the account is long 0.01, so the webhook sells 0.005 with a reduce-only order.
- The strategy flips short. The alert says short 0.01; the account is long 0.005, so the webhook closes the 0.005 long first, then sells 0.01.
- An alert is lost, then the strategy exits. The next alert says flat; the account is short 0.01, so the webhook buys 0.01 reduce-only, and the account is flat as the strategy is.
If a change is worth less than $10, Hyperliquid's minimum order value, the alert is logged as "Nothing to do" rather than as an error, so a strategy that trims by tiny amounts does not fill the log with refusals. A stop-loss or take-profit in the message is placed after an opening step, on the whole position.
What changes between a backtest and live orders?
A TradingView backtest counts what you tell it to count. Unless the strategy's properties include commission and slippage, the backtest fills every order at the chart's price for free. Live orders on Hyperliquid pay three costs on every fill: Hyperliquid's own fee, which is 0.045% taker and 0.015% maker on perps at the base tier according to its fee schedule, the terminal's trading fee of up to 0.1%, and the distance between the chart's price and the price the order actually fills at.
Say, as an example, that a strategy earns 0.30% gross per round trip in its backtest and trades with market orders.
| Cost per round trip (example) | Amount |
|---|---|
| Hyperliquid taker fee, opening and closing (2 × 0.045%) | 0.09% |
| Terminal trading fee at an example 0.05%, opening and closing | 0.10% |
| Slippage at an example 0.05% per fill | 0.10% |
| What is left of the 0.30% gross | 0.01% |
That edge is close to nothing, before funding on positions held across hours. The fee calculator works out the exact fees for your size and tier, and the funding calculator shows what holding costs. Put the costs into the strategy's properties, then backtest again.
Prices can differ too. A strategy on a BINANCE:BTCUSDT.P chart decides on that exchange's candles, while the order fills on Hyperliquid's book. Charting Hyperliquid's own market on TradingView, which lists Hyperliquid as a data source, keeps the signal and the fill on the same book.
How do limits keep a strategy in check?
Every alert passes three layers of limits, and the tightest one wins. The webhook's own limits come first: largest single order, total per day, orders per minute, the markets allowed and the highest slippage. The terminal's limits come next, set by the operator for the whole site. Then the policy signer checks the terminal's limits again before it signs, and writes a signed receipt for every step.
The limits are hard. An order above the per-order limit is refused, not shrunk to fit, because a strategy that thinks it holds 0.1 BTC while the account holds 0.02 is worse than one that stops. The daily limit counts the value of opening orders over 24 hours. The per-minute limit stops a strategy stuck in a loop. Set the webhook's limits around the strategy's real size: a little above its normal order, and a daily limit that covers a busy day but not a runaway one.
Two more controls help strategies in particular. With ts set to {{timenow}}, an alert older than the webhook's maximum age is refused, so a backlog of delayed alerts cannot trade on stale prices. And because an id built from {{strategy.order.id}} and {{timenow}} is new for each fill, a resent alert with the same id is ignored.
How should strategy quantity be sized?
The generator reads the strategy's quantity as coins of the Hyperliquid market: {{strategy.order.contracts}} and {{strategy.position_size}} go into the message as coins. On a crypto perpetual chart a strategy's quantity is usually in coins already, and 1000PEPE tickers map to Hyperliquid's kPEPE market, which also counts in thousands of PEPE.
When a strategy sizes in something else, such as a futures contract with a multiplier, or when you want the live size to differ from the backtest, give the message a fixed size instead: size_usd for a dollar amount or size_pct for a share of the account value. With match-the-position and a fixed size, the alert still follows long, short and flat, at the size you chose.
A careful order for going live
- Add commission and slippage to the strategy's properties and backtest again.
- Create a webhook just for this strategy, with a per-order limit a little above its normal order and a small daily limit.
- Generate the message with the match-the-position template, keep id and ts, and run the Test button.
- Create the strategy alert on order fills with the webhook URL and the message.
- Watch the first alerts in the alert log next to the strategy's list of trades. Every Refused or Partly done line has a reason.
- Raise the limits only after live fills match what you expected.
Pause the webhook at any time, or stop everything in one step; your positions and stops stay as they are, and you close them in the terminal as usual.
Frequently asked questions
Do I need to change my Pine Script code?
Usually not. Strategy alerts on order fills work with any strategy, and the message uses TradingView's built-in strategy placeholders. Only a strategy that calls alert() with its own message text needs that text replaced by a message from the generator.
What happens if TradingView misses an alert?
With follow-each-order, the account stays out of step until you correct it in the terminal or the strategy trades again. With match-the-position, the next alert compares the strategy's position with the account's and sends the difference, so the account catches up on its own.
Can a strategy use leverage through the webhook?
Yes. Add leverage to the message, a whole number from 1 to 50, and it is set before the order, never above the terminal's highest leverage. Leverage multiplies losses as well as gains, so size the webhook's limits for the worst day, not the average one.
Can I run several strategies on one account?
Yes, with one webhook per strategy, each with its own limits and log. Be careful with match-the-position on the same market: two strategies that each target their own BTC position will move the one shared BTC position back and forth. Give each strategy its own market, or use follow-each-order for one of them.
Does a webhook strategy trade HIP-3 markets?
Not yet. HIP-3 tickers such as xyz:TSLA are recognised and refused, because webhook keys trade the main Hyperliquid perpetuals for now. Spot markets are not offered either.