Pine Script v4 · Strategy
[LunaOwl] Support Resistance Strategy V4
A Pine Script v4 strategy built on Valuewhen or Cross or SMA or Highest (high, LB), Valuewhen or Cross or SMA or Lowest (low, LB) and Crossover (close, R). It has 2 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 commits 100% of equity and pays 0.05% commission. 2 settings; defaults include 回溯期數 50.
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 [LunaOwl] Support Resistance Strategy V4 repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
- How did [LunaOwl] Support Resistance Strategy V4 do on BTCUSDT 1D with default settings?
- −1.28%
- From 26 Jan 2024 to 21 Oct 2026, over 7 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [LunaOwl] Support Resistance Strategy V4 be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does [LunaOwl] Support Resistance Strategy V4 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does [LunaOwl] Support Resistance Strategy V4 run in Zeal Terminal?
- Yes
- It compiles in 8 ms and runs 1,000 bars in 24 ms, in the browser.
BTCUSDT · 1D · default settings
- 壓力
- 支撐
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −1.28%
- −$128.17
- Buy and hold
- +102.5%
- same period
- Closed trades
- 7
- 3 won · 4 lost
- Profitable
- 42.9%
- of closed trades
- Profit factor
- 0.98
- gross profit ÷ gross loss
- Max drawdown
- 25.50%
- −$3,170.27
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
Uses an argument v6 removed
when = on line 50, 51. The upgraded version below moves each call inside an if block.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames valuewhen → ta.valuewhen, cross → ta.cross, sma → ta.sma, highest → ta.highest, lowest → ta.lowest, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 50, 51). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 5: //@version=4 → //@version=5
- Line 37: input(type = input.integer) → input.int()
- Line 38: input(type = input.bool) → input.bool()
- Line 40: valuewhen → ta.valuewhen
- Line 40: cross → ta.cross
- Line 40: sma → ta.sma
7 more edits
- Line 40: highest → ta.highest
- Line 41: valuewhen → ta.valuewhen
- Line 41: cross → ta.cross
- Line 41: sma → ta.sma
- Line 41: lowest → ta.lowest
- Line 45: crossover → ta.crossover
- Line 46: crossunder → ta.crossunder
⋯//-//@version=4+//@version=5//⋯//==設定輸入==//-LB = input(50, title = "回溯期數", type = input.integer)+LB = input.int(50, title = "回溯期數")-button = input(true, title = "請問要繪製K線麼?", type = input.bool)+button = input.bool(true, title = "請問要繪製K線麼?")//==支撐壓力線==//-R = valuewhen(cross(sma(close, LB),close), highest(high, LB), 1)+R = ta.valuewhen(ta.cross(ta.sma(close, LB),close), ta.highest(high, LB), 1)-S = valuewhen(cross(close,sma(close, LB)), lowest( low, LB), 1)+S = ta.valuewhen(ta.cross(close,ta.sma(close, LB)), ta.lowest( low, LB), 1)plot(R, title = "壓力", color = color.green)⋯//==定義輸出結果==//-Trend_up = crossover(close, R) ? 1 : 0+Trend_up = ta.crossover(close, R) ? 1 : 0-Trend_dn = crossunder(close, S) ? -1 : 0+Trend_dn = ta.crossunder(close, S) ? -1 : 0//==設定出場規則==//⋯Same 7 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 50: when = Enter → if block
- Line 51: when = Exit → if block
⋯Exit = Trend_dn == -1 and Trend_dn[1] == 0 ? Trend_dn : na-strategy.entry("多", strategy.long, when = Enter)+if Enter+ strategy.entry("多", strategy.long)-strategy.entry("空", strategy.short, when = Exit)+if Exit+ strategy.entry("空", strategy.short)//==繪製燭台==//⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 7 before, 19 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯//-//@version=5+//@version=6//⋯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 |
|---|---|---|---|
| 回溯期數 | 50 | int (written input.integer) | R S |
| 請問要繪製K線麼? | true | bool | 燭台顏色 |
What it calculates
- Valuewhen / Cross / SMA / Highest (
valuewhen,ta.valuewhenin v5)cross(sma(close, LB),close), highest(high, LB), 1R · line 40 - Valuewhen / Cross / SMA / Lowest (
valuewhen,ta.valuewhenin v5)cross(close,sma(close, LB)), lowest( low, LB), 1S · line 41 - Crossover (
crossover,ta.crossoverin v5)close, RTrend_up · line 45 - Crossunder (
crossunder,ta.crossunderin v5)close, STrend_dn · line 46
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 多 long | Enter | On | 50 |
| Open 空 short | Exit | On | 51 |
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, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- 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
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −1.3% | +102.5% | 7 | 43% | 0.98 | 25.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.1% | +5.5% | 7 | 43% | 0.99 | 13.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.6% | +9.8% | 7 | 43% | 0.99 | 61.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −19.9% | +8.4% | 8 | 38% | 0.58 | 30.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +4.8% | +16.7% | 4 | 50% | 1.16 | 33.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +15.7% | +21.5% | 6 | 50% | 1.62 | 16.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −55.1% | +148.3% | 8 | 25% | 0.27 | 58.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +12.6% | +17.1% | 4 | 50% | 1.91 | 15.2% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 0 | — | $0.00 |
| Sideways market | 5 | 60% | $4,077.22 |
| Falling market | 2 | 0% | −$4,200.45 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
43.5%
The 95th percentile of the worst drawdown when the same 7 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 21.8% to 43.5%; the tick is what actually happened, 23.7%. Order luck alone moves it this much.
All 7 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 8 Jul 2024 55,857.80 | 16 Oct 2024 67,074.20 | 100 | −$2,018.03 |
| long | 16 Oct 2024 67,074.20 | 26 Feb 2025 88,680.30 | 133 | $2,562.56 |
| short | 26 Feb 2025 88,680.30 | 29 Apr 2025 95,011.20 | 62 | −$763.70 |
| long | 29 Apr 2025 95,011.20 | 5 Nov 2025 101,497.00 | 190 | $657.63 |
| short | 5 Nov 2025 101,497.00 | 18 Apr 2026 77,072.10 | 164 | $2,502.82 |
| long | 18 Apr 2026 77,072.10 | 4 Jun 2026 64,142.70 | 47 | −$2,182.42 |
| short | 4 Jun 2026 64,142.70 | 20 Aug 2026 69,334.80 | 77 | −$882.09 |
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 |
|---|---|---|---|---|---|
| [LunaOwl] Support Resistance Strategy V4 this page | — | v4 strategy | −1.3% | 7 | 25.5% |
| Trend Bounce [racer8] by racer8 | highest lowest sma valuewhen | v4 strategy | +2.4% | 22 | 32.2% |
| TradeIQ - Crazy Scalping Trading Strategy [Kaspricci] by Kaspricci | highest lowest valuewhen sma cross | v5 strategy | +1,326.3% | 6 | 1,046.4% |
| Chandelier + BB + EMAS by juanchez | highest lowest sma cross | v4 strategy | −20.6% | 10 | 38.6% |
| TradingGroundhog - Strategy & Fractal V1 by UnknownUnicorn27666852 | highest lowest valuewhen sma | v4 strategy | −2.9% | 8 | 5.9% |
| CITI Moku Strategy(Free) by @MiaIsTheNewBird by CITIAlgo | cross highest lowest valuewhen | v5 strategy | −8.7% | 48 | 16.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 8 ms | Yes 1,000 bars in 24 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“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. PineTS runs Pine Script v5 and v6 only.
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//-- 開源代碼受Mozilla公眾授權條款2.0版規範, 網址是https://mozilla.org/MPL/2.0/////@version=4//// 作品: [LunaOwl] 支撐壓力策略第4版// 英文: [LunaOwl] Support Resistance Strategy V4//////////////////////////////////// ~~!!*(๑╹◡╹๑) ** //// 製作: @LunaOwl 彭彭 //// 日期: 2019年03月05日 //// 修改: 2019年04月22日 //// 四版: 2020年06月16日 //// 發表: 2020年06月17日 //////////////////////////////////////一個簡單的支撐阻力突破策略,唯一的差異在於它是PineScript第四版//#_____________________________//==設定策略==//strategy("[LunaOwl] 支撐壓力策略 [回測]", shorttitle = "支撐壓力策略 [回測]", overlay = true, calc_on_order_fills = false, calc_on_every_tick = false, pyramiding = 0, currency = currency.NONE, initial_capital = 10000, slippage = 5, default_qty_value = 100, default_qty_type = strategy.percent_of_equity, commission_type = strategy.commission.percent, commission_value = 0.05 )//==設定輸入==//LB = input(50, title = "回溯期數", type = input.integer)button = input(true, title = "請問要繪製K線麼?", type = input.bool)//==支撐壓力線==//R = valuewhen(cross(sma(close, LB),close), highest(high, LB), 1)S = valuewhen(cross(close,sma(close, LB)), lowest( low, LB), 1)plot(R, title = "壓力", color = color.green)plot(S, title = "支撐", color = color.red)//==定義輸出結果==//Trend_up = crossover(close, R) ? 1 : 0Trend_dn = crossunder(close, S) ? -1 : 0//==設定出場規則==//Enter = Trend_up == 1 and Trend_up[1] == 0 ? Trend_up : naExit = Trend_dn == -1 and Trend_dn[1] == 0 ? Trend_dn : nastrategy.entry("多", strategy.long, when = Enter)strategy.entry("空", strategy.short, when = Exit)//==繪製燭台==//Trend_col = R < close ? color.green : S > close ? color.red : color.orangebarcolor(not button ? na : Trend_col, editable = true, title = "燭台顏色")