Pine Script v4 · Strategy
[KL] BOLL + MACD Strategy v2 (published)
A Pine Script v4 strategy built on SMA (BOLL_src, BOLL_length), Stdev (BOLL_src, BOLL_length) and EMA (close, MACD_fastLen). It has 5 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 7 settings; defaults include Risk:[Reward] for exit 3 and ATR multiplier for stop loss 2.
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 [KL] BOLL + MACD Strategy v2 (published) 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 [KL] BOLL + MACD Strategy v2 (published) do on BTCUSDT 1D with default settings?
- −32.05%
- From 26 Jan 2024 to 21 Oct 2026, over 28 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can [KL] BOLL + MACD Strategy v2 (published) 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.
- What alerts does [KL] BOLL + MACD Strategy v2 (published) have?
- 1
- Price approached lower band.
- Does [KL] BOLL + MACD Strategy v2 (published) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
BTCUSDT · 1D · default settings
- Basis
- Upper
- Lower
- ATR Buffer
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −32.05%
- −$32,054.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 28
- 12 won · 16 lost
- Profitable
- 42.9%
- of closed trades
- Profit factor
- 0.66
- gross profit ÷ gross loss
- Max drawdown
- 53.99%
- −$53,991.20
One run with the author's default settings, $100,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
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 91, 92. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 16
transp = on line 16, 17, 18. 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 sma → ta.sma, stdev → ta.stdev, ema → ta.ema, atr → ta.atr, max → math.max, abs → math.abs.
- 2 strategy calls use the when = argument (line 91, 92). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 28 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 10: sma → ta.sma
- Line 11: stdev → ta.stdev
- Line 16: transp = 50 → color.new(color.navy, 50)
- Line 17: transp = 50 → color.new(color.navy, 50)
- Line 18: transp = 85 → color.new(#198787, 85)
12 more edits
- Line 24: ema → ta.ema
- Line 24: ema → ta.ema
- Line 25: ema → ta.ema
- Line 28: input(type = input.time) → input.time()
- Line 30: input(type = input.time) → input.time()
- Line 38: atr → ta.atr
- Line 40: atr → ta.atr
- Line 45: max → math.max
- Line 54: atr → ta.atr
- Line 61: abs → math.abs
- Line 61: abs → math.abs
- Line 87: abs → math.abs
⋯// https://www.tradingview.com/script/EyElTlCG-KL-BOLL-MACD-Strategy-v2-published/-//@version=4+//@version=5strategy("[KL] BOLL + MACD Strategy",overlay=true,pyramiding=1)⋯BOLL_mult = 2.0-BOLL_center = sma(BOLL_src, BOLL_length)+BOLL_center = ta.sma(BOLL_src, BOLL_length)-BOLL_dev = BOLL_mult * stdev(BOLL_src, BOLL_length)+BOLL_dev = BOLL_mult * ta.stdev(BOLL_src, BOLL_length)BOLL_upper = BOLL_center + BOLL_dev⋯plot(BOLL_center, "Basis", color=#872323, offset = BOLL_offset)-BOLL_p1 = plot(BOLL_upper, "Upper", color=color.navy, offset = BOLL_offset, transp=50)+BOLL_p1 = plot(BOLL_upper, "Upper", color=color.new(color.navy, 50), offset = BOLL_offset)-BOLL_p2 = plot(BOLL_lower, "Lower", color=color.navy, offset = BOLL_offset, transp=50)+BOLL_p2 = plot(BOLL_lower, "Lower", color=color.new(color.navy, 50), offset = BOLL_offset)-fill(BOLL_p1, BOLL_p2, title = "Background", color=#198787, transp=85)+fill(BOLL_p1, BOLL_p2, title = "Background", color=color.new(#198787, 85))// }⋯MACD_Len = 9-MACD = ema(close, MACD_fastLen) - ema(close, MACD_slowLen)+MACD = ta.ema(close, MACD_fastLen) - ta.ema(close, MACD_slowLen)-aMACD = ema(MACD, MACD_Len)+aMACD = ta.ema(MACD, MACD_Len)MACD_delta = MACD - aMACD// }-backtest_timeframe_start = input(defval = timestamp("01 Nov 2010 13:30 +0000"), title = "Backtest Start Time", type = input.time)+backtest_timeframe_start = input.time(defval = timestamp("01 Nov 2010 13:30 +0000"), title = "Backtest Start Time")USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")-backtest_timeframe_end = input(defval = timestamp("19 Mar 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)+backtest_timeframe_end = input.time(defval = timestamp("19 Mar 2021 19:30 +0000"), title = "Backtest End Time (if checked above)")EXIT1 = input(false, title="Take profit when Risk:Reward met")⋯ATR_multi = input(2, "ATR multiplier for stop loss")-ATR_buffer = atr(ATR_multi_len) * ATR_multi+ATR_buffer = ta.atr(ATR_multi_len) * ATR_multiplotchar(ATR_buffer, "ATR Buffer", "", location = location.top)-risk_reward_buffer = (atr(ATR_multi_len) * ATR_multi) * REWARD_RATIO+risk_reward_buffer = (ta.atr(ATR_multi_len) * ATR_multi) * REWARD_RATIOreward_met_long = low > entry_price + risk_reward_buffer⋯var stop_loss_price = float(0)-stop_loss_price := max(stop_loss_price, close - trailing_SL_buffer)+stop_loss_price := math.max(stop_loss_price, close - trailing_SL_buffer)// plot TSL line⋯var bTouched = false-if low < BOLL_center - atr(BOLL_length)+if low < BOLL_center - ta.atr(BOLL_length) alert("Price approached lower band", alert.freq_once_per_bar)⋯if time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME)- expected_MACD_crossing = MACD > MACD[1] and MACD < aMACD and abs(MACD - aMACD) < abs(MACD[1] - aMACD[1])+ expected_MACD_crossing = MACD > MACD[1] and MACD < aMACD and math.abs(MACD - aMACD) < math.abs(MACD[1] - aMACD[1]) buy_condition = bTouched and expected_MACD_crossing⋯ if EXIT1 or EXIT2 and strategy.position_size > 0- engulfing_bearish = open - close > abs(open[1]-close[1]) and high > high[1] and (open - close)/(high - low) > 0.4+ engulfing_bearish = open - close > math.abs(open[1]-close[1]) and high > high[1] and (open - close)/(high - low) > 0.4 gapped_down = high < low[1] and close < open⋯Same 28 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 29: input() → input.bool()
- Line 31: input() → input.bool()
- Line 32: input() → input.int()
- Line 33: input() → input.bool()
- Line 37: input() → input.int()
- Line 91: when = (EXIT1 and reward_met_long) → if block
- Line 92: when = (EXIT2 and engulfing_bearish) → if block
⋯backtest_timeframe_start = input.time(defval = timestamp("01 Nov 2010 13:30 +0000"), title = "Backtest Start Time")-USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")+USE_ENDTIME = input.bool(false,title="Define backtest end-time (If false, will test up to most recent candle)")backtest_timeframe_end = input.time(defval = timestamp("19 Mar 2021 19:30 +0000"), title = "Backtest End Time (if checked above)")-EXIT1 = input(false, title="Take profit when Risk:Reward met")+EXIT1 = input.bool(false, title="Take profit when Risk:Reward met")-REWARD_RATIO = input(3,title="Risk:[Reward] for exit")+REWARD_RATIO = input.int(3,title="Risk:[Reward] for exit")-EXIT2 = input(false, title="Take profit when candle is bearish")+EXIT2 = input.bool(false, title="Take profit when candle is bearish")// Trailing stop loss {⋯ATR_multi_len = 26-ATR_multi = input(2, "ATR multiplier for stop loss")+ATR_multi = input.int(2, "ATR multiplier for stop loss")ATR_buffer = ta.atr(ATR_multi_len) * ATR_multi⋯ msg = "exit"- strategy.close("Long", when=(EXIT1 and reward_met_long), comment="risk:reward met")+ if (EXIT1 and reward_met_long)+ strategy.close("Long", comment="risk:reward met")- strategy.close("Long", when=(EXIT2 and engulfing_bearish), comment="bearish")+ if (EXIT2 and engulfing_bearish)+ strategy.close("Long", comment="bearish") // CLEAN UP⋯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: 28 before, 26 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.
⋯// https://www.tradingview.com/script/EyElTlCG-KL-BOLL-MACD-Strategy-v2-published/-//@version=5+//@version=6strategy("[KL] BOLL + MACD Strategy",overlay=true,pyramiding=1)⋯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 |
|---|---|---|---|
| Backtest Start Time | timestamp("01 Nov 2010 13:30 +0000") | time | line 60 |
| Define backtest end-time (If false, will test up to most recent candle) | false | bool | line 60 |
| Backtest End Time (if checked above) | timestamp("19 Mar 2021 19:30 +0000") | time | line 60 |
| Take profit when Risk:Reward met | false | bool | close Long close Long line 86 |
| Risk:[Reward] for exit | 3 | int | risk_reward_buffer |
| Take profit when candle is bearish | false | bool | close Long close Long line 86 |
| ATR multiplier for stop loss | 2 | int | ATR_buffer risk_reward_buffer |
What it calculates
- SMA (
sma,ta.smain v5)BOLL_src, BOLL_lengthBOLL_center · line 10 - Stdev (
stdev,ta.stdevin v5)BOLL_src, BOLL_lengthBOLL_dev · line 11 - EMA (
ema,ta.emain v5)close, MACD_fastLenMACD · line 24 - EMA (
ema,ta.emain v5)MACD, MACD_LenaMACD · line 25 - ATR (
atr,ta.atrin v5)ATR_multi_lenATR_buffer · line 38 - ATR (
atr,ta.atrin v5)ATR_multi_lenrisk_reward_buffer · line 40
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 long | buy_condition | On | 72 |
| Close Long | close > entry_price | On | 79 |
| Close Long | close <= entry_price | On | 82 |
| Close Long | (EXIT1 and reward_met_long) | On | 91 |
| Close Long | (EXIT2 and engulfing_bearish) | On | 92 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| (no title) | alert | Price approached lower band | 55 |
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 | −32.1% | +102.5% | 28 | 43% | 0.66 | 54.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +3.4% | +5.5% | 28 | 32% | 1.13 | 15.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −3.7% | +9.8% | 30 | 30% | 0.46 | 3.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.3% | +8.4% | 27 | 33% | 1.26 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.1% | +16.7% | 25 | 40% | 0.59 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 26 | 38% | 1.11 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.1% | +148.3% | 21 | 57% | 1.07 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 28 | 29% | 1.21 | 0.1% | 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 | 50% | −$11,966.10 |
| Sideways market | 12 | 17% | −$54,470.90 |
| Falling market | 8 | 75% | $34,382.60 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
62.3%
The 95th percentile of the worst drawdown when the same 28 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 37.9% to 62.3%; the tick is what actually happened, 50.1%. Order luck alone moves it this much.
All 28 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Mar 2024 69,880.00 | 14 Apr 2024 63,924.50 | 19 | −$5,955.48 |
| long | 21 Apr 2024 64,940.60 | 2 May 2024 58,365.00 | 11 | −$6,575.61 |
| long | 4 May 2024 62,882.00 | 21 Jun 2024 64,870.00 | 48 | $1,987.98 |
| long | 1 Jul 2024 62,772.00 | 5 Jul 2024 57,050.00 | 4 | −$5,721.99 |
| long | 10 Jul 2024 58,050.00 | 3 Aug 2024 61,498.30 | 24 | $3,448.34 |
| long | 9 Aug 2024 61,686.00 | 2 Sep 2024 57,301.80 | 24 | −$4,384.23 |
| long | 10 Sep 2024 57,042.00 | 11 Oct 2024 60,326.40 | 31 | $3,284.39 |
| long | 12 Oct 2024 62,540.00 | 5 Nov 2024 67,850.00 | 24 | $5,310.02 |
| long | 3 Jan 2025 96,984.80 | 10 Jan 2025 92,552.50 | 7 | −$4,432.30 |
| long | 15 Jan 2025 96,560.90 | 3 Feb 2025 97,700.60 | 19 | $1,139.74 |
| long | 13 Feb 2025 97,870.00 | 26 Feb 2025 88,680.40 | 13 | −$9,189.61 |
| long | 3 Mar 2025 94,270.00 | 10 Mar 2025 80,734.50 | 7 | −$13,535.50 |
| long | 13 Mar 2025 83,680.10 | 9 Apr 2025 76,322.40 | 27 | −$7,357.70 |
| long | 10 Apr 2025 82,615.20 | 6 Jun 2025 101,509.00 | 57 | $18,893.50 |
| long | 10 Jun 2025 110,263.00 | 21 Jun 2025 103,298.00 | 11 | −$6,965.04 |
| long | 24 Jun 2025 105,334.00 | 2 Aug 2025 113,298.00 | 39 | $7,963.98 |
| long | 8 Aug 2025 117,472.00 | 16 Aug 2025 117,342.00 | 8 | −$129.98 |
| long | 23 Aug 2025 116,936.00 | 30 Aug 2025 108,377.00 | 7 | −$8,558.60 |
| long | 3 Sep 2025 111,240.00 | 26 Sep 2025 108,994.00 | 23 | −$2,245.52 |
| long | 29 Sep 2025 112,164.00 | 11 Oct 2025 112,774.00 | 12 | $610.53 |
| long | 21 Oct 2025 110,532.00 | 4 Nov 2025 106,583.00 | 14 | −$3,949.04 |
| long | 10 Nov 2025 104,723.00 | 15 Nov 2025 94,594.00 | 5 | −$10,128.90 |
| long | 25 Nov 2025 88,300.00 | 21 Jan 2026 88,427.70 | 57 | $127.65 |
| long | 10 Feb 2026 70,138.00 | 28 Mar 2026 66,407.30 | 46 | −$3,730.72 |
| long | 1 Apr 2026 68,284.50 | 23 May 2026 75,539.50 | 52 | $7,255.01 |
| long | 12 Jun 2026 63,626.00 | 25 Jun 2026 61,078.00 | 13 | −$2,548.00 |
| long | 5 Aug 2026 64,106.60 | 11 Sep 2026 76,568.70 | 37 | $12,462.20 |
| long | 19 Sep 2026 80,883.90 | 9 Oct 2026 81,754.50 | 20 | $870.60 |
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 |
|---|---|---|---|---|---|
| [KL] BOLL + MACD Strategy v2 (published) this page | — | v4 strategy | −32.1% | 28 | 54.0% |
| Trend Following with Bollinger Bands by Robrecht99 | sma stdev ema atr | v5 strategy | +2.5% | 7 | 1.7% |
| [EURUSD60] BB Expansion Strategy by kasaism | sma stdev ema atr | v4 strategy | No trades on this market | ||
| [KL] Bollinger bands + RSI Strategy by DojiEmoji | sma stdev atr ema | v4 strategy | +28.2% | 30 | 14.7% |
| [KL] Bollinger Bands Consolidation Strategy by DojiEmoji | sma stdev atr ema | v4 strategy | +39.1% | 33 | 19.6% |
| Full strategy AllinOne with risk management MACD RSI PSAR ATR MA by SoftKill21 | sma stdev ema atr | v4 strategy | +2,985,110.0% | 3 | 2,350,820.0% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | Yes 1,000 bars in 45 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/// © DojiEmoji// https://www.tradingview.com/script/EyElTlCG-KL-BOLL-MACD-Strategy-v2-published///@version=4strategy("[KL] BOLL + MACD Strategy",overlay=true,pyramiding=1)// BOLL bands {BOLL_length = 20BOLL_src = closeBOLL_mult = 2.0BOLL_center = sma(BOLL_src, BOLL_length)BOLL_dev = BOLL_mult * stdev(BOLL_src, BOLL_length)BOLL_upper = BOLL_center + BOLL_devBOLL_lower = BOLL_center - BOLL_devBOLL_offset = 0plot(BOLL_center, "Basis", color=#872323, offset = BOLL_offset)BOLL_p1 = plot(BOLL_upper, "Upper", color=color.navy, offset = BOLL_offset, transp=50)BOLL_p2 = plot(BOLL_lower, "Lower", color=color.navy, offset = BOLL_offset, transp=50)fill(BOLL_p1, BOLL_p2, title = "Background", color=#198787, transp=85)// }// MACD signals {MACD_fastLen = 12MACD_slowLen = 26MACD_Len = 9MACD = ema(close, MACD_fastLen) - ema(close, MACD_slowLen)aMACD = ema(MACD, MACD_Len)MACD_delta = MACD - aMACD// }backtest_timeframe_start = input(defval = timestamp("01 Nov 2010 13:30 +0000"), title = "Backtest Start Time", type = input.time)USE_ENDTIME = input(false,title="Define backtest end-time (If false, will test up to most recent candle)")backtest_timeframe_end = input(defval = timestamp("19 Mar 2021 19:30 +0000"), title = "Backtest End Time (if checked above)", type = input.time)EXIT1 = input(false, title="Take profit when Risk:Reward met")REWARD_RATIO = input(3,title="Risk:[Reward] for exit")EXIT2 = input(false, title="Take profit when candle is bearish")// Trailing stop loss {var entry_price = float(0)ATR_multi_len = 26ATR_multi = input(2, "ATR multiplier for stop loss")ATR_buffer = atr(ATR_multi_len) * ATR_multiplotchar(ATR_buffer, "ATR Buffer", "", location = location.top)risk_reward_buffer = (atr(ATR_multi_len) * ATR_multi) * REWARD_RATIOreward_met_long = low > entry_price + risk_reward_buffertake_profit_short = low < entry_price - risk_reward_buffervar trailing_SL_buffer = float(0)var stop_loss_price = float(0)stop_loss_price := max(stop_loss_price, close - trailing_SL_buffer)// plot TSL linetrail_profit_line_color = color.greenif strategy.position_size == 0 // make TSL line less visible trail_profit_line_color := color.black stop_loss_price := close - trailing_SL_bufferplot(stop_loss_price,color=trail_profit_line_color)// }var bTouched = falseif low < BOLL_center - atr(BOLL_length) alert("Price approached lower band", alert.freq_once_per_bar) bTouched := trueelse if low > BOLL_center bTouched := false// MAINif time >= backtest_timeframe_start and (time <= backtest_timeframe_end or not USE_ENDTIME) expected_MACD_crossing = MACD > MACD[1] and MACD < aMACD and abs(MACD - aMACD) < abs(MACD[1] - aMACD[1]) buy_condition = bTouched and expected_MACD_crossing //ENTRY { if buy_condition if strategy.position_size == 0 entry_price := close trailing_SL_buffer := ATR_buffer stop_loss_price := close - ATR_buffer msg = "buy" if strategy.position_size > 0 msg := "+" strategy.entry("Long",strategy.long, comment=msg) // } //EXIT: // Case A: if strategy.position_size > 0 and close <= stop_loss_price if close > entry_price strategy.close("Long", comment="take profit [trailing]") stop_loss_price := 0 else if close <= entry_price strategy.close("Long", comment="stop loss") stop_loss_price := 0 // Case B: if EXIT1 or EXIT2 and strategy.position_size > 0 engulfing_bearish = open - close > abs(open[1]-close[1]) and high > high[1] and (open - close)/(high - low) > 0.4 gapped_down = high < low[1] and close < open is_bearish_candle = engulfing_bearish or gapped_down msg = "exit" strategy.close("Long", when=(EXIT1 and reward_met_long), comment="risk:reward met") strategy.close("Long", when=(EXIT2 and engulfing_bearish), comment="bearish") // CLEAN UP if strategy.position_size == 0 stop_loss_price := 0