TradingView Backtesting

Hedge Ratios for TradingView Spreads: Dollars, Beta and Regression

A hedge ratio specifies how much of instrument B offsets a position in A. State its units first: shares per share, dollars per dollar, or contracts per position. Equal shares match quantities; equal dollars match initial notionals; beta sizing targets estimated benchmark exposure; price OLS fits a historical relationship. These objectives produce different positions, and none guarantees a profitable or stable spread.

See the decision guide

Four hedge ratios, four different constraints

Let qA be the number of long A shares, qB the number of short B shares, and h = qB / qA. Prices PA and PB are currency per share. Then long notional is qA × PA and short notional is qB × PB. A positive h describes opposing positions; a negative fitted h implies buying B alongside A in this convention. Do not discard the sign to force a long-short trade.

  • Dollar neutrality is an entry-time constraint unless you rebalance. Rebalancing changes holdings and incurs trading costs.
  • Beta A and beta B must use the same benchmark, return definition, frequency and training dates. A near-zero beta B makes the ratio unstable; beta B = 0 makes this formula undefined.
  • A price regression coefficient is a share ratio in this setup. A regression on percentage returns estimates a return sensitivity, requiring notional-to-share conversion. A log-price coefficient is an elasticity, not a fixed share count.
  • For futures, include the contract multiplier and currency conversion in each contract value. Margin posted is not the contract notional. These share formulas do not directly size options or nonlinear exposures.
MethodShares of B per share of AWhat it matches
Equal sharesh = 1Share counts only; share prices and economic exposure can differ.
Equal dollarsh = PA / PBInitial long and short notionals in a common currency.
Benchmark betah = (beta A / beta B) × (PA / PB)Estimated benchmark exposure: qA × PA × beta A = qB × PB × beta B.
Price OLS with intercepth = sum((B - mean B) × (A - mean A)) / sum((B - mean B)^2)Least-squares residuals of A on B over the training sample.

Recalculate the positions for a $12,000 long leg

This hypothetical example holds 100 shares of A at $120 and prices B at $50. Assume benchmark betas of 1.2 for A and 0.8 for B, and a separately fitted price OLS slope of 2. The beta-neutral dollar ratio is 1.2 / 0.8 = 1.5, so the short leg is $18,000, or 360 B shares. Equal dollars instead requires $12,000 / $50 = 240 shares.

  • Each P&L is 100 × ($126 - $120) - qB × ($52 - $50), with fixed shares and no dividends, fees, financing or borrow charges. These are arithmetic scenarios, not measured returns.
  • The beta-neutral row starts with $30,000 gross exposure: $12,000 long plus $18,000 short. Its zero estimated benchmark exposure does not mean zero capital, zero risk or zero gross exposure.
  • The equal-dollar row retains $12,000 × 1.2 - $12,000 × 0.8 = $4,800 of beta-weighted dollar exposure. Matching dollars does not match benchmark sensitivity.
  • Do not divide P&L by the net notional to calculate a return: that denominator is zero for the dollar-neutral pair. Use a stated capital allocation or margin policy and a complete cash ledger.
Methodh; B shares shortShort notional; net notionalP&L if A = $126 and B = $52
Equal shares1; 100$5,000; +$7,000$600 - $200 = +$400
Equal dollars2.4; 240$12,000; $0$600 - $480 = +$120
Benchmark beta3.6; 360$18,000; -$6,000$600 - $720 = -$120
Price OLS2; 200$10,000; +$2,000$600 - $400 = +$200

Fit price OLS and keep the intercept separate

Use the four synthetic synchronized price pairs below only to check the arithmetic. The training means are mean B = 47.5 and mean A = 105. The cross-deviation sum is 250 and the B squared-deviation sum is 125, so h = 250 / 125 = 2. The intercept is 105 - 2 × 47.5 = 10. NIST documents these slope and intercept estimators. Four observations do not establish a reliable trading relationship.

  • The tradable price combination per A share is A - 2 × B. The fitted residual subtracts another $10 to center that combination. The intercept is not ten extra shares or an order in a third asset; actual funding belongs in the cash ledger.
  • At the separate hypothetical entry prices A = $120 and B = $50, the residual is $10. At A = $126 and B = $52, it is $12. Holding 100 A and short 200 B produces 100 × ($12 - $10) = $200 before costs because the fitted coefficients and holdings stayed fixed.
  • Forcing the regression through zero gives sum(A × B) / sum(B²) = 20,200 / 9,150 = 2.207650, a different hedge. Include or exclude the intercept deliberately and record that choice.
  • Regressing B on A and inverting its slope generally differs from regressing A on B. Document which leg is the dependent variable. A small regression residual or high correlation does not prove cointegration, future mean reversion or executable profit.
Training observationB price ($)A price ($)Residual A - 10 - 2 × B ($)
140922
24598-2
350108-2
4551222

Synchronize observations before estimating beta or OLS

Join the two series on genuinely comparable observation times. Two daily bars with the same date can close hours apart. Use the same trading session and valuation convention, and retain an audit of excluded observations. Training on asynchronous or stale prices can change both the estimated slope and the apparent spread behavior.

  • Save exchange-qualified symbols, interval, timezone, session, currency, adjustment settings and data retrieval date. Convert currencies with a documented contemporaneous FX series before treating notionals as comparable.
  • For prices, choose and document split and dividend adjustments. For beta, calculate aligned simple or excess returns consistently; beta against a benchmark is covariance(asset return, benchmark return) / variance(benchmark return).
  • Require valid observations for both legs and the benchmark where needed. Do not silently forward-fill a holiday or halted instrument and treat it as a fresh executable quote.
  • Reject a zero B-price variance for OLS and a zero benchmark-return variance for beta. Missing values, duplicated timestamps and a near-zero denominator need an explicit exclusion or failure rule.
  • Use enough history for the intended horizon and assess coefficient uncertainty and stability. A longer sample may reduce sampling noise while mixing different regimes; no fixed lookback makes a hedge reliable.

Freeze training estimates, then measure exposure drift

Choose the training cutoff, lookback, intercept policy, model direction and rebalance schedule before examining the holdout. Estimate coefficients using only information available at that cutoff. Apply them to subsequent observations without refitting the past. If a rolling model is intended, every update needs its own earlier estimation window and an executable timing rule.

  • Keep the example beta-neutral holdings fixed at 100 A and short 360 B. After prices move to $126 and $52, unchanged betas imply $12,600 × 1.2 - $18,720 × 0.8 = +$144 of beta-weighted dollar exposure. Price movement alone changes neutrality.
  • If a later, separately estimated beta B is 0.6 while beta A stays 1.2, the same holdings have +$3,888 estimated exposure. Restoring neutrality at those prices requires qB = $12,600 × 1.2 / ($52 × 0.6) = 484.615 shares before rounding. That estimate was unavailable at entry.
  • Save frozen coefficients alongside holdout residuals, gross and net exposure, estimated benchmark exposure and turnover. Apply a predefined drift threshold rather than choosing one after seeing losses.
  • Changing h from one observation to the next changes the plotted residual even if no trades occur. A difference of rolling residuals is not the P&L of fixed holdings. Book the rebalance trades, cash movements, rounding and costs separately.
  • Stress borrow availability, recalls, financing, spread widening, one leg filling before the other and structural breaks. A stable training fit cannot remove these execution risks.

What TradingView and Pineify can verify

TradingView spread charts visualize arithmetic expressions and document differences between real-time and historical spread bars. A chart expression such as a weighted price difference does not specify borrow, margin, two fills or portfolio cash. Native Pine strategy orders use the chart symbol; requesting another symbol supplies data without creating an independently traded second leg.

  • Use synchronized price data outside a trade-list CSV to estimate the ratios above. A trading report lists executed simulated trades; it is not a complete matched price panel for two instruments and a benchmark.
  • For a two-leg backtest, retain the timestamp of each leg, quantity, price, costs and currency, plus cash, financing and open-position valuations. Model failed or partial fills and shared buying power before calling the result a portfolio return.
  • Backtest Deep Report accepts TradingView trade-list CSV data and calculates trade-based performance statistics. Reconcile the imported trade count and net result with the original report. Its current report fields do not provide the OLS fit on this page or beta-neutral sizing, and importing an export does not reconstruct missing second-leg execution.
  • The Pine Script AI Coding Agent can help draft and revise signal or diagnostic code from a specified model. Independently validate the generated source and data timing. A compiled script does not establish statistical validity or a complete two-leg accounting model.
Where Pineify fits

Backtest Deep Report

Review an exported TradingView trade list and reconcile its trade-based results. Keep hedge estimation and any two-leg portfolio ledger separate; the report does not supply the OLS or beta sizing calculations shown here.

Also useful: Pine Script AI Coding Agent. Describe your frozen hedge model and timing rules when drafting a spread diagnostic. Review and validate the code before using its output.

Review Backtest Deep Report

Frequently asked questions

Educational information, not investment advice. All prices, betas and results in the examples are hypothetical. A hedge can lose money, and historical relationships do not guarantee future risk reduction or returns.

Sources and verification

Published by Pineify. Pineify is an independent product and is not affiliated with TradingView. Sources support the technical explanations; examples do not establish trading performance.