Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

trading YM based on pair trading

A Pine Script v5 strategy built on SMA (ratio, 200), BB (diff, 20) and Crossunder (diff, lower). It has 3 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 2 settings; defaults include Symbol1 YM1! and Symbol2 NQ1!.

Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.

Does trading YM based on pair trading repaint?
No
No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
How did trading YM based on pair trading do on BTCUSDT 1D with default settings?
Not measured
It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its entry conditions never all held on this data. It closed no trades on the other markets either. A backtest, not investment advice.
Does trading YM based on pair trading run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download.
Does trading YM based on pair trading use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does trading YM based on pair trading run in Zeal Terminal?
Yes
It compiles in 28 ms and runs 1,000 bars in 35 ms, in the browser.

BTCUSDT · 1D · default settings

  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,000,000$2,025,273Equity from $1,000,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. The script plots nothing itself; the lines and the lower pane are its own calculations, drawn for reference. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market. The live re-run is not offered here: the script requests YM1! D, NQ1! D, which this page cannot load in your browser.

Needs YM1! D data, not available: not a Binance spot market Needs NQ1! D data, not available: not a Binance spot market Values that depend on it are empty on this chart.

Net profit
0.00%
$0.00
Buy and hold
+102.5%
same period
Closed trades
0
0 won · 0 lost
Profitable
0.0%
of closed trades
Profit factor
—
gross profit ÷ gross loss
Max drawdown
0.00%
$0.00

One run with the author's default settings, $1,000,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.

Review

What a careful reader of this script would point out, found by checking the source and the backtest.

  • Check

    No stop loss

    Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.

  • Check

    No commission

    The backtest assumes free trading. Results on any real exchange will be lower.

  • Note

    2 settings have no label

    In the settings dialog symbol1 (line 5), symbol2 (line 6) show an empty name. The labels on this page come from the variable names.

  • Note

    No slippage

    Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Symbol1 (no label in the script)YM1!symbolsecurity1Close
Symbol2 (no label in the script)NQ1!symbolsecurity2Open security2High security2Low security2Close

What it calculates

  • SMAratio, 200ratioSma · line 13
  • BBdiff, 20, 2middle · line 15
  • BBdiff, 20, 2upper · line 15
  • BBdiff, 20, 2lower · line 15
  • Crossunderdiff, lowerlong · line 20
  • Crossoverdiff, middleexitLong · line 21
  • Crossoverdiff, uppershort · line 22
  • Crossunderdiff, middleexitShort · line 23

Signal flow

How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.

closecloseprice datasymbol1Symbol1setting · YM1!openopenprice datasymbol2Symbol2setting · NQ1!highhighprice datalowlowprice dataratioSmaSMA→ ratioSmamiddleBB→ middleupperBB→ upperlowerBB→ lowerlongCrossunder→ longexitLongCrossover→ exitLongshortCrossover→ shortexitShortCrossunder→ exitShortsecurity1Closesecurity1Closevaluesecurity2Opensecurity2Openvaluesecurity2Highsecurity2Highvaluesecurity2Lowsecurity2Lowvaluesecurity2Closesecurity2Closevalueratioratiovaluediffdiffvalueentry:enter longOpen enter longstrategy.entryentry:enter shortOpen enter shortstrategy.entryclose_all:close_all strategy.close_allplot:16plotplotplot:17plotplotplot:18plotplotplot:19plotplot

Order rules

OrderFires whenBy defaultLine
Open enter long longlong and strategy.position_size == 0On25
Open enter short shortshort and strategy.position_size == 0On27
Close (exitLong and strategy.position_size > 0) or (exitShort and strategy.position_size < 0)On29

Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.

Does it repaint?

No

No repainting patterns in the source.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • ClearHigher-timeframe data5 request.security call(s), none with lookahead on.
  • ClearFuture-bar referencesNo negative history offsets such as close[-1].
  • ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
  • ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.

The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.

Other markets

Not run on other markets: the script requests YM1! D, NQ1! D, and that data is not available here (not a Binance spot market; not a Binance spot market). Its results would rest on empty values.

Similar scripts

Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.

ScriptSharesTypeNetTradesMax drawdown
trading YM based on pair trading this page—v5 strategy0.0%00.0%
BB and KC Strategy by jensenvilhelmbb smav5 strategy+0.0%220.1%
TrendMaAlignmentStrategy - Long term trades by Trendoscopesma bbv4 strategy−22.0%846.3%
BuyTheDip by Trendoscopesma bbv4 strategy+4.6%28.3%
Strategy Oil Z Score by jroche1973bb smav4 strategyNo trades on this market
Bollinger -B Candles Strategy by Trendoscopesma bbv4 strategy+17.3%295.1%

Upgrade and export

Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.

  • No v6-only features. v6 is not needed to run this script.

Clean v5

Verified

Nothing to change.

Does it run in Pine Script v6?

Not tested

Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// © hkorange2007-//@version=5+//@version=6strategy("pair trading")⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 28 msWith a workaround 1,000 bars in 35 msNot measured
PineTSNot testedNot testedNot measured

The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.

“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.

Source

Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © hkorange2007//@version=5strategy("pair trading")symbol1 = input.symbol("YM1!")symbol2 = input.symbol("NQ1!")security1Close = request.security(symbol1, timeframe.period, close)security2Open = request.security(symbol2, timeframe.period, open)security2High = request.security(symbol2, timeframe.period, high)security2Low = request.security(symbol2, timeframe.period, low)security2Close = request.security(symbol2, timeframe.period, close)ratio = security1Close / security2CloseratioSma = ta.sma(ratio, 200)diff = security1Close - security2Close * ratioSma[middle, upper, lower] = ta.bb(diff, 20, 2)plot(diff)plot(middle, color = color.orange)plot(upper, color = color.orange)plot(lower, color = color.orange)long = ta.crossunder(diff, lower)exitLong = ta.crossover(diff, middle)short = ta.crossover(diff, upper)exitShort = ta.crossunder(diff, middle)if long and strategy.position_size == 0    strategy.entry("enter long", strategy.long, 1) if short and strategy.position_size == 0    strategy.entry("enter short", strategy.short, 1)     if (exitLong and strategy.position_size > 0) or (exitShort and strategy.position_size < 0)    strategy.close_all()