Pine Script v4 · Strategy
Inside Bar Strategy w- SL
A Pine Script v4 strategy built on Lowest (10), Highest (10) and Valuewhen (bought, LL). It has 10 order rules, 8 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays 0.04 commission. 17 settings; defaults include Strategy Direction 0, # Of Inside Bars in pattern 1 and MA Length 65.
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 Inside Bar Strategy w- SL repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 5 of 5 trades.
- How did Inside Bar Strategy w- SL do on BTCUSDT 1D with default settings?
- −61.27%
- From 26 Jan 2024 to 21 Oct 2026, over 25 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Inside Bar Strategy w- SL 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 Inside Bar Strategy w- SL use a stop loss?
- Yes
- strategy.exit sets a stop on line 154, 155.
- Does Inside Bar Strategy w- SL run in Zeal Terminal?
- Yes
- It compiles in 44 ms and runs 1,000 bars in 103 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- SL
- SSL
- TP
- STP
- Bullish Setup
- Bearish Setup
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −61.27%
- −$6,126.87
- Buy and hold
- +102.5%
- same period
- Closed trades
- 25
- 4 won · 21 lost
- Profitable
- 16.0%
- of closed trades
- Profit factor
- 0.45
- gross profit ÷ gross loss
- Max drawdown
- 64.96%
- −$6,880.04
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
2 settings do nothing
“-----------------Strategy Inputs-------------------”, “-----------------General Inputs-------------------” are declared but never read.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
Uses an argument v6 removed
when = on line 117, 118, 120, 121, 124, 125, 127, 128, 154, 155. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 109
transp = on line 109, 110. v5 folds the transparency into the colour with color.new(c, transp).
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 lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr, sma → ta.sma, barssince → ta.barssince.
- 10 strategy calls use the when = argument (line 117, 118, 120, 121, 124, 125, 127, 128, 154, 155). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 25 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 19: //@version=4 → //@version=5
- Line 28: input(type = input.integer) → input.int()
- Line 32: input(type = input.integer) → input.int()
- Line 51: lowest → ta.lowest
- Line 52: highest → ta.highest
- Line 53: valuewhen → ta.valuewhen
14 more edits
- Line 54: valuewhen → ta.valuewhen
- Line 60: atr → ta.atr
- Line 63: valuewhen → ta.valuewhen
- Line 64: valuewhen → ta.valuewhen
- Line 70: valuewhen → ta.valuewhen
- Line 71: valuewhen → ta.valuewhen
- Line 75: sma → ta.sma
- Line 76: sma → ta.sma
- Line 79: barssince → ta.barssince
- Line 80: barssince → ta.barssince
- Line 109: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 110: transp = 100 removed: the call sets no colour to fold it into, so its default colour loses the transparency
- Line 134: valuewhen → ta.valuewhen
- Line 146: valuewhen → ta.valuewhen
⋯// © tweakerID-//@version=4+//@version=5strategy("Inside Bar Strategy w/ SL", ⋯ slippage=0)-direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)+direction = input.int(0, title = "Strategy Direction", minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))⋯title1=input(true, "-----------------Strategy Inputs-------------------") -i_NBars = input(defval=1, type=input.integer, title="# Of Inside Bars in pattern", options=[1, 2, 3, 4])+i_NBars = input.int(defval=1, title="# Of Inside Bars in pattern", options=[1, 2, 3, 4])i_BarsDirection = input(false, title="Only trade using complete bullish or bearish patterns")⋯// Price Action Stop and Take Profit-LL=(lowest(i_SPL))*(1-i_PercIncrement)+LL=(ta.lowest(i_SPL))*(1-i_PercIncrement)-HH=(highest(i_SPL))*(1+i_PercIncrement)+HH=(ta.highest(i_SPL))*(1+i_PercIncrement)-LL_price = valuewhen(bought, LL, 0)+LL_price = ta.valuewhen(bought, LL, 0)-HH_price = valuewhen(bought, HH, 0)+HH_price = ta.valuewhen(bought, HH, 0)entry_LL_price = strategy.position_size > 0 ? LL_price : na ⋯// ATR Stop-ATR=atr(i_ATR)*i_ATRMult+ATR=ta.atr(i_ATR)*i_ATRMultATRLong = ohlc4 - ATRATRShort = ohlc4 + ATR-ATRLongStop = valuewhen(bought, ATRLong, 0)+ATRLongStop = ta.valuewhen(bought, ATRLong, 0)-ATRShortStop = valuewhen(bought, ATRShort, 0)+ATRShortStop = ta.valuewhen(bought, ATRShort, 0)LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na ⋯// Strategy Stop-float LongStop = valuewhen(bought,low[1],0)*(1-i_PercIncrement)+float LongStop = ta.valuewhen(bought,low[1],0)*(1-i_PercIncrement)-float ShortStop = valuewhen(bought,high[1],0)*(1+i_PercIncrement)+float ShortStop = ta.valuewhen(bought,high[1],0)*(1+i_PercIncrement)float StratTP = na⋯/////////////////////// STRATEGY LOGIC /////////////////////////////////////////-MAFilter=close > sma(close, i_MALen)+MAFilter=close > ta.sma(close, i_MALen)-plot(i_MAFilter ? sma(close, i_MALen) : na)+plot(i_MAFilter ? ta.sma(close, i_MALen) : na)bullBar=close > openbearBar=close < open-contbullBar=barssince(not bullBar) >= (i_NBars+1)+contbullBar=ta.barssince(not bullBar) >= (i_NBars+1)-contbearBar=barssince(not bearBar) >= (i_NBars+1)+contbearBar=ta.barssince(not bearBar) >= (i_NBars+1)InsideBar(NBars) =>⋯//Debugging Plots-plot(contbullBar ? 1:0, transp=100, title="contbullBar")+plot(contbullBar ? 1:0, title="contbullBar")-plot(contbearBar ? 1:0, transp=100, title="contbearBar")+plot(contbearBar ? 1:0, title="contbearBar")//Trading Inputs⋯//TrailingStop-dif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0))+dif=(ta.valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_price⋯var Ststop = float(na)-Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 +Sdif=strategy.position_avg_price-(ta.valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))⋯Same 25 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 31: input() → input.bool()
- Line 33: input() → input.bool()
- Line 34: input() → input.bool()
- Line 35: input() → input.int()
- Line 37: input() → input.bool()
- Line 39: input() → input.bool()
19 more edits
- Line 40: input() → input.string()
- Line 41: input() → input.int()
- Line 42: input() → input.int()
- Line 43: input() → input.int()
- Line 44: input() → input.int()
- Line 45: input() → input.int()
- Line 46: input() → input.bool()
- Line 112: input() → input.bool()
- Line 113: input() → input.bool()
- Line 117: when = SELL and strategy.position_size == 0 → if block
- Line 118: when = BUY and strategy.position_size == 0 → if block
- Line 120: when = SELL → if block
- Line 121: when = BUY → if block
- Line 124: when = BUY and strategy.position_size == 0 → if block
- Line 125: when = SELL and strategy.position_size == 0 → if block
- Line 127: when = BUY → if block
- Line 128: when = SELL → if block
- Line 154: when = i_SL → if block
- Line 155: when = i_SL → if block
⋯/////////////////////// STRATEGY INPUTS ////////////////////////////////////////-title1=input(true, "-----------------Strategy Inputs-------------------") +title1=input.bool(true, "-----------------Strategy Inputs-------------------") i_NBars = input.int(defval=1, title="# Of Inside Bars in pattern", options=[1, 2, 3, 4])-i_BarsDirection = input(false, title="Only trade using complete bullish or bearish patterns")+i_BarsDirection = input.bool(false, title="Only trade using complete bullish or bearish patterns")-i_MAFilter = input(true, title="Use MA Trend Filter")+i_MAFilter = input.bool(true, title="Use MA Trend Filter")-i_MALen = input(65, title="MA Length")+i_MALen = input.int(65, title="MA Length")/////////////////////// BACKTESTER /////////////////////////////////////////////-title2=input(true, "-----------------General Inputs-------------------") +title2=input.bool(true, "-----------------General Inputs-------------------") // Backtester General Inputs-i_SL=input(true, title="Use Stop Loss and Take Profit")+i_SL=input.bool(true, title="Use Stop Loss and Take Profit")-i_SLType=input(defval="ATR Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])+i_SLType=input.string(defval="ATR Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])-i_SPL=input(defval=10, title="Swing Point Lookback")+i_SPL=input.int(defval=10, title="Swing Point Lookback")-i_PercIncrement=input(defval=1, step=.1, title="Swing Point SL Perc Increment")*0.01+i_PercIncrement=input.int(defval=1, step=.1, title="Swing Point SL Perc Increment")*0.01-i_ATR = input(14, title="ATR Length")+i_ATR = input.int(14, title="ATR Length")-i_ATRMult = input(5, step=.1, title="ATR Multiple")+i_ATRMult = input.int(5, step=.1, title="ATR Multiple")-i_TPRRR = input(2, step=.1, title="Take Profit Risk Reward Ratio")+i_TPRRR = input.int(2, step=.1, title="Take Profit Risk Reward Ratio")-TS=input(false, title="Trailing Stop")+TS=input.bool(false, title="Trailing Stop")// Bought and Sold Boolean Signal⋯//Trading Inputs-DPR=input(true, "Allow Direct Position Reverse")+DPR=input.bool(true, "Allow Direct Position Reverse")-reverse=input(false, "Reverse Trades")+reverse=input.bool(false, "Reverse Trades")// Entries⋯ if not DPR- strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0)+ if SELL and strategy.position_size == 0+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0)+ if BUY and strategy.position_size == 0+ strategy.entry("short", strategy.short) else - strategy.entry("long", strategy.long, when=SELL)+ if SELL+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=BUY)+ if BUY+ strategy.entry("short", strategy.short)else if not DPR - strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0)+ if BUY and strategy.position_size == 0+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0)+ if SELL and strategy.position_size == 0+ strategy.entry("short", strategy.short) else- strategy.entry("long", strategy.long, when=BUY)+ if BUY+ strategy.entry("long", strategy.long)- strategy.entry("short", strategy.short, when=SELL)+ if SELL+ strategy.entry("short", strategy.short)SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStop⋯ Ststop := na-strategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL, when=i_SL)+if i_SL+ strategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL)-strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL, when=i_SL)+if i_SL+ strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL)/////////////////////// PLOTS //////////////////////////////////////////////////⋯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: 25 before, 63 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.
⋯// © tweakerID-//@version=5+//@version=6strategy("Inside Bar Strategy w/ SL", ⋯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 |
|---|---|---|---|
| Strategy Direction | 0 | int (written input.integer) · min -1 · max 1 | line 29 |
| -----------------Strategy Inputs------------------- | true | bool | unused |
| # Of Inside Bars in pattern | 1 | int (written input.integer) · options [1, 2, 3, 4] | contbullBar contbearBar insideBar |
| Only trade using complete bullish or bearish patterns | false | bool | bullInsideBar bearInsideBar |
| Use MA Trend Filter | true | bool | bullInsideBar bearInsideBar plot |
| MA Length | 65 | int | MAFilter plot |
| -----------------General Inputs------------------- | true | bool | unused |
| Use Stop Loss and Take Profit | true | bool | exit TP & SL SL SSL TP STP |
| Type Of Stop | ATR Stop | string · options ["Strategy Stop", "Swing Lo/Hi", "ATR Stop"] | SL SSL TP STP |
| Swing Point Lookback | 10 | int | LL HH |
| Swing Point SL Perc Increment | 1 | int · step .1 | LL HH LongStop ShortStop |
| ATR Length | 14 | int | ATR |
| ATR Multiple | 5 | int · step .1 | ATR |
| Take Profit Risk Reward Ratio | 2 | int · step .1 | tp stp ATRtp ATRstp |
| Trailing Stop | false | bool | exit TP & SL SL SSL |
| Allow Direct Position Reverse | true | bool | entry long entry short entry long entry short entry long entry short entry long entry short line 116 line 123 |
| Reverse Trades | false | bool | line 115 |
What it calculates
- Lowest (
lowest,ta.lowestin v5)Swing Point Lookback (10)LL · line 51 - Highest (
highest,ta.highestin v5)Swing Point Lookback (10)HH · line 52 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, LL, 0LL_price · line 53 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, HH, 0HH_price · line 54 - ATR (
atr,ta.atrin v5)ATR Length (14)ATR · line 60 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, ATRLong, 0ATRLongStop · line 63 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, ATRShort, 0ATRShortStop · line 64 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, low[1], 0LongStop · line 70 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, high[1], 0ShortStop · line 71
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.
The script has 72 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | SELL and strategy.position_size == 0 | On | 117 |
| Open short short | BUY and strategy.position_size == 0 | On | 118 |
| Open long long | SELL | On | 120 |
| Open short short | BUY | On | 121 |
| Open long long | BUY and strategy.position_size == 0 | On | 124 |
| Open short short | SELL and strategy.position_size == 0 | On | 125 |
| Open long long | BUY | On | 127 |
| Open short short | SELL | On | 128 |
| Exit TP & SL stop and limit | i_SL | On | 154 |
| Exit TP & SL stop and limit | i_SL | On | 155 |
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 | −61.3% | +102.5% | 25 | 16% | 0.45 | 65.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +6.2% | +5.5% | 19 | 37% | 1.19 | 14.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −9.2% | +9.8% | 18 | 33% | 0.91 | 75.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −1.3% | +8.4% | 26 | 31% | 0.98 | 31.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −76.8% | +16.7% | 22 | 32% | 0.29 | 85.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +44.6% | +21.5% | 20 | 45% | 2.15 | 16.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −22.7% | +148.3% | 18 | 44% | 0.81 | 51.9% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −39.5% | +17.1% | 27 | 19% | 0.24 | 42.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 | 8 | 13% | −$2,958.65 |
| Sideways market | 13 | 23% | −$1,288.93 |
| Falling market | 4 | 0% | −$1,877.74 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
77.8%
The 95th percentile of the worst drawdown when the same 25 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 61.2% to 77.8%; the tick is what actually happened, 63.4%. Order luck alone moves it this much.
All 25 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 30 Mar 2024 69,850.50 | 15 Apr 2024 65,661.90 | 16 | $591.67 |
| long | 15 Apr 2024 65,661.90 | 27 Apr 2024 63,770.00 | 12 | −$313.50 |
| short | 27 Apr 2024 63,770.00 | 25 May 2024 68,550.00 | 28 | −$778.95 |
| long | 25 May 2024 68,550.00 | 20 Jun 2024 64,974.40 | 26 | −$502.92 |
| short | 20 Jun 2024 64,974.40 | 29 Jul 2024 68,249.90 | 39 | −$460.90 |
| long | 29 Jul 2024 68,249.90 | 5 Aug 2024 55,587.80 | 7 | −$1,589.80 |
| short | 10 Aug 2024 60,838.00 | 25 Aug 2024 64,157.00 | 15 | −$384.47 |
| long | 25 Aug 2024 64,157.00 | 1 Sep 2024 58,974.00 | 7 | −$535.11 |
| short | 1 Sep 2024 58,974.00 | 22 Sep 2024 63,349.00 | 21 | −$452.04 |
| long | 22 Sep 2024 63,349.00 | 11 Nov 2024 85,339.90 | 50 | $1,929.83 |
| long | 7 Dec 2024 99,740.80 | 12 Jan 2025 94,600.00 | 36 | −$392.45 |
| short | 12 Jan 2025 94,600.00 | 19 Jan 2025 104,556.00 | 7 | −$754.43 |
| long | 19 Jan 2025 104,556.00 | 5 Feb 2025 97,763.10 | 17 | −$417.97 |
| short | 5 Feb 2025 97,763.10 | 14 May 2025 104,104.00 | 98 | −$390.08 |
| long | 14 May 2025 104,104.00 | 15 Sep 2025 115,268.00 | 124 | $590.43 |
| short | 15 Sep 2025 115,268.00 | 21 Sep 2025 115,686.00 | 6 | −$27.16 |
| long | 21 Sep 2025 115,686.00 | 28 Sep 2025 109,636.00 | 7 | −$324.40 |
| short | 28 Sep 2025 109,636.00 | 3 Oct 2025 122,543.00 | 5 | −$686.26 |
| long | 5 Oct 2025 122,391.00 | 10 Oct 2025 110,121.00 | 5 | −$515.09 |
| short | 12 Oct 2025 110,644.00 | 5 Feb 2026 63,352.70 | 116 | $1,956.65 |
| short | 8 Feb 2026 69,289.40 | 24 Apr 2026 78,257.50 | 75 | −$852.10 |
| long | 24 Apr 2026 78,257.50 | 3 Jun 2026 65,676.00 | 40 | −$919.23 |
| short | 7 Jun 2026 60,884.60 | 9 Aug 2026 64,962.60 | 63 | −$323.42 |
| long | 9 Aug 2026 64,962.60 | 13 Aug 2026 63,480.00 | 4 | −$105.05 |
| short | 13 Aug 2026 63,480.00 | 20 Aug 2026 70,274.90 | 7 | −$468.57 |
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 |
|---|---|---|---|---|---|
| Inside Bar Strategy w- SL this page | — | v4 strategy | −61.3% | 25 | 65.0% |
| Positive Bars - Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.3% | 7 | 54.7% |
| Big Bar Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −56.6% | 13 | 57.1% |
| SuperTrend Multi Time Frame Long and Short Trading Strategy by ranga_technologies | atr highest lowest valuewhen | v5 strategy | No trades on this market | ||
| Modulo Logic + EMA Strat by tweakerID | lowest highest valuewhen atr | v4 strategy | −50.8% | 16 | 54.7% |
| Delayed RSI Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.4% | 7 | 42.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 44 ms | Yes 1,000 bars in 103 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 at https://mozilla.org/MPL/2.0/// From "Day Trading Cryptocurrency // Strategies, Tactics, Mindset, and Tools Required To Build Your // New Income Stream"// by Phil C. Senior// "Inside bars are a two -bar pattern. They can indicate either a continuation of the // existing move or a reversal. A continuation occurs when there is no significant // support or resistance level in sight, while a reversal occurs close to a strong sup- // port or resistance level...// ...A lot of traders are aware of inside bars but few manage to make money with // them. Why is this so? It goes back to interpreting price action. A lot of traders look // to trade in geometric ways. What I mean is that they search for fancy shapes on a // chart and think that this is what represents true price action. // This is not the case. A shape is just a shape. The formation by itself means // nothing unless underlying order flow backs it up. This is why it’s extremely impor- // tant that you look for inside bars when a trend is already in place. The best place to // look for them is in the beginning of trends."// © tweakerID//@version=4strategy("Inside Bar Strategy w/ SL", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, initial_capital=10000, commission_value=0.04, calc_on_every_tick=false, slippage=0)direction = input(0, title = "Strategy Direction", type=input.integer, minval=-1, maxval=1)strategy.risk.allow_entry_in(direction == 0 ? strategy.direction.all : (direction < 0 ? strategy.direction.short : strategy.direction.long))/////////////////////// STRATEGY INPUTS ////////////////////////////////////////title1=input(true, "-----------------Strategy Inputs-------------------") i_NBars = input(defval=1, type=input.integer, title="# Of Inside Bars in pattern", options=[1, 2, 3, 4])i_BarsDirection = input(false, title="Only trade using complete bullish or bearish patterns")i_MAFilter = input(true, title="Use MA Trend Filter")i_MALen = input(65, title="MA Length")/////////////////////// BACKTESTER /////////////////////////////////////////////title2=input(true, "-----------------General Inputs-------------------") // Backtester General Inputsi_SL=input(true, title="Use Stop Loss and Take Profit")i_SLType=input(defval="ATR Stop", title="Type Of Stop", options=["Strategy Stop", "Swing Lo/Hi", "ATR Stop"])i_SPL=input(defval=10, title="Swing Point Lookback")i_PercIncrement=input(defval=1, step=.1, title="Swing Point SL Perc Increment")*0.01i_ATR = input(14, title="ATR Length")i_ATRMult = input(5, step=.1, title="ATR Multiple")i_TPRRR = input(2, step=.1, title="Take Profit Risk Reward Ratio")TS=input(false, title="Trailing Stop")// Bought and Sold Boolean Signalbought = strategy.position_size > strategy.position_size[1] or strategy.position_size < strategy.position_size[1]// Price Action Stop and Take ProfitLL=(lowest(i_SPL))*(1-i_PercIncrement)HH=(highest(i_SPL))*(1+i_PercIncrement)LL_price = valuewhen(bought, LL, 0)HH_price = valuewhen(bought, HH, 0)entry_LL_price = strategy.position_size > 0 ? LL_price : na entry_HH_price = strategy.position_size < 0 ? HH_price : na tp=strategy.position_avg_price + (strategy.position_avg_price - entry_LL_price)*i_TPRRRstp=strategy.position_avg_price - (entry_HH_price - strategy.position_avg_price)*i_TPRRR// ATR StopATR=atr(i_ATR)*i_ATRMultATRLong = ohlc4 - ATRATRShort = ohlc4 + ATRATRLongStop = valuewhen(bought, ATRLong, 0)ATRShortStop = valuewhen(bought, ATRShort, 0)LongSL_ATR_price = strategy.position_size > 0 ? ATRLongStop : na ShortSL_ATR_price = strategy.position_size < 0 ? ATRShortStop : na ATRtp=strategy.position_avg_price + (strategy.position_avg_price - LongSL_ATR_price)*i_TPRRRATRstp=strategy.position_avg_price - (ShortSL_ATR_price - strategy.position_avg_price)*i_TPRRR// Strategy Stopfloat LongStop = valuewhen(bought,low[1],0)*(1-i_PercIncrement)float ShortStop = valuewhen(bought,high[1],0)*(1+i_PercIncrement)float StratTP = nafloat StratSTP = na/////////////////////// STRATEGY LOGIC /////////////////////////////////////////MAFilter=close > sma(close, i_MALen)plot(i_MAFilter ? sma(close, i_MALen) : na)bullBar=close > openbearBar=close < opencontbullBar=barssince(not bullBar) >= (i_NBars+1)contbearBar=barssince(not bearBar) >= (i_NBars+1)InsideBar(NBars) => Inside1Bar=high < high[1] and low > low[1] Inside2Bar=high < high[2] and low > low[2] and Inside1Bar Inside3Bar=high < high[3] and low > low[3] and Inside1Bar and Inside2Bar Inside4Bar=high < high[4] and low > low[4] and Inside1Bar and Inside2Bar and Inside3Bar if NBars == 1 inside1Bar=Inside1Bar [inside1Bar] else if NBars == 2 inside2Bar=Inside2Bar [inside2Bar] else if NBars == 3 inside3Bar=Inside3Bar [inside3Bar] else if NBars == 4 inside4Bar=Inside4Bar [inside4Bar] else [na][insideBar] = InsideBar(i_NBars) bullInsideBar=bar_index > 40 and insideBar and bullBar and (i_BarsDirection ? contbullBar : true) and (i_MAFilter ? MAFilter : true)bearInsideBar=bar_index > 40 and insideBar and bearBar and (i_BarsDirection ? contbearBar : true) and (i_MAFilter ? not MAFilter : true)BUY = bullInsideBarSELL = bearInsideBar//Debugging Plotsplot(contbullBar ? 1:0, transp=100, title="contbullBar")plot(contbearBar ? 1:0, transp=100, title="contbearBar")//Trading InputsDPR=input(true, "Allow Direct Position Reverse")reverse=input(false, "Reverse Trades")// Entriesif reverse if not DPR strategy.entry("long", strategy.long, when=SELL and strategy.position_size == 0) strategy.entry("short", strategy.short, when=BUY and strategy.position_size == 0) else strategy.entry("long", strategy.long, when=SELL) strategy.entry("short", strategy.short, when=BUY)else if not DPR strategy.entry("long", strategy.long, when=BUY and strategy.position_size == 0) strategy.entry("short", strategy.short, when=SELL and strategy.position_size == 0) else strategy.entry("long", strategy.long, when=BUY) strategy.entry("short", strategy.short, when=SELL)SL= i_SLType == "Swing Lo/Hi" ? entry_LL_price : i_SLType == "ATR Stop" ? LongSL_ATR_price : LongStopSSL= i_SLType == "Swing Lo/Hi" ? entry_HH_price : i_SLType == "ATR Stop" ? ShortSL_ATR_price : ShortStopTP= i_SLType == "Swing Lo/Hi" ? tp : i_SLType == "ATR Stop" ? ATRtp : StratTPSTP= i_SLType == "Swing Lo/Hi" ? stp : i_SLType == "ATR Stop" ? ATRstp : StratSTP//TrailingStopdif=(valuewhen(strategy.position_size>0 and strategy.position_size[1]<=0, high,0)) -strategy.position_avg_pricetrailOffset = strategy.position_avg_price - SLvar tstop = float(na)if strategy.position_size > 0 tstop := high- trailOffset - dif if tstop<tstop[1] tstop:=tstop[1]else tstop := naStrailOffset = SSL - strategy.position_avg_pricevar Ststop = float(na)Sdif=strategy.position_avg_price-(valuewhen(strategy.position_size<0 and strategy.position_size[1]>=0, low,0))if strategy.position_size < 0 Ststop := low+ StrailOffset + Sdif if Ststop>Ststop[1] Ststop:=Ststop[1]else Ststop := nastrategy.exit("TP & SL", "long", limit=TP, stop=TS? tstop : SL, when=i_SL)strategy.exit("TP & SL", "short", limit=STP, stop=TS? Ststop : SSL, when=i_SL)/////////////////////// PLOTS //////////////////////////////////////////////////plot(i_SL and strategy.position_size > 0 and not TS ? SL : i_SL and strategy.position_size > 0 and TS ? tstop : na , title='SL', style=plot.style_cross, color=color.red)plot(i_SL and strategy.position_size < 0 and not TS ? SSL : i_SL and strategy.position_size < 0 and TS ? Ststop : na , title='SSL', style=plot.style_cross, color=color.red)plot(i_SL and strategy.position_size > 0 ? TP : na, title='TP', style=plot.style_cross, color=color.green)plot(i_SL and strategy.position_size < 0 ? STP : na, title='STP', style=plot.style_cross, color=color.green)// Draw price action setup arrowsplotshape(BUY ? 1 : na, style=shape.triangleup, location=location.belowbar, color=color.green, title="Bullish Setup", size=size.auto)plotshape(SELL ? 1 : na, style=shape.triangledown, location=location.abovebar, color=color.red, title="Bearish Setup", size=size.auto)