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
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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Symbol1 (no label in the script) | YM1! | symbol | security1Close |
| Symbol2 (no label in the script) | NQ1! | symbol | security2Open security2High security2Low security2Close |
What it calculates
- SMAratio, 200
ratioSma · line 13 - BBdiff, 20, 2
middle · line 15 - BBdiff, 20, 2
upper · line 15 - BBdiff, 20, 2
lower · line 15 - Crossunderdiff, lower
long · line 20 - Crossoverdiff, middle
exitLong · line 21 - Crossoverdiff, upper
short · line 22 - Crossunderdiff, middle
exitShort · 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open enter long long | long and strategy.position_size == 0 | On | 25 |
| Open enter short short | short and strategy.position_size == 0 | On | 27 |
| Close | (exitLong and strategy.position_size > 0) or (exitShort and strategy.position_size < 0) | On | 29 |
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.
- 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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| trading YM based on pair trading this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| BB and KC Strategy by jensenvilhelm | bb sma | v5 strategy | +0.0% | 22 | 0.1% |
| TrendMaAlignmentStrategy - Long term trades by Trendoscope | sma bb | v4 strategy | −22.0% | 8 | 46.3% |
| BuyTheDip by Trendoscope | sma bb | v4 strategy | +4.6% | 2 | 8.3% |
| Strategy Oil Z Score by jroche1973 | bb sma | v4 strategy | No trades on this market | ||
| Bollinger -B Candles Strategy by Trendoscope | sma bb | v4 strategy | +17.3% | 29 | 5.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
VerifiedNothing 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
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | With a workaround 1,000 bars in 35 ms | Not measured |
| PineTS | Not tested | Not tested | Not 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()