Pine Script v4 · Strategy
Bollinger Bands strategy with RSI and MACD v1.0
A Pine Script v4 strategy built on RSI (close, 14), MACD (close, 12) and BB (close, 20). It has 6 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 3 contracts and pays no commission. 22 settings; defaults include BB source close, Bollinger Bands SMA length 20 and Bollinger Bands StdDev length 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 Bollinger Bands strategy with RSI and MACD v1.0 repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Bollinger Bands strategy with RSI and MACD v1.0 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.
- Can Bollinger Bands strategy with RSI and MACD v1.0 be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does Bollinger Bands strategy with RSI and MACD v1.0 use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Bollinger Bands strategy with RSI and MACD v1.0 run in Zeal Terminal?
- Yes
- It compiles in 54 ms and runs 1,000 bars in 113 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Plot
- Plot
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- 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, $100,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
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses long =, which v5 renamed line 92
long = on line 92, 120. v5 writes it as direction = strategy.long or strategy.short.
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 rsi → ta.rsi, macd → ta.macd, bb → ta.bb, tostring → str.tostring, max → math.max, min → math.min.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 5: input(type = input.source) → input.source()
- Line 6: input(type = input.integer) → input.int()
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.float) → input.float()
- Line 9: input(type = input.float) → input.float()
26 more edits
- Line 10: input(type = input.integer) → input.int()
- Line 11: input(type = input.integer) → input.int()
- Line 12: input(type = input.bool) → input.bool()
- Line 13: input(type = input.source) → input.source()
- Line 14: input(type = input.integer) → input.int()
- Line 15: input(type = input.integer) → input.int()
- Line 16: input(type = input.integer) → input.int()
- Line 17: input(type = input.bool) → input.bool()
- Line 18: input(type = input.bool) → input.bool()
- Line 19: input(type = input.source) → input.source()
- Line 20: input(type = input.integer) → input.int()
- Line 21: input(type = input.integer) → input.int()
- Line 22: input(type = input.integer) → input.int()
- Line 23: input(type = input.bool) → input.bool()
- Line 24: input(type = input.float) → input.float()
- Line 25: input(type = input.bool) → input.bool()
- Line 26: input(type = input.float) → input.float()
- Line 27: rsi → ta.rsi
- Line 28: macd → ta.macd
- Line 32: bb → ta.bb
- Line 91: tostring → str.tostring
- Line 92: long = true → direction = strategy.long
- Line 103: max → math.max
- Line 119: tostring → str.tostring
- Line 120: long = false → direction = strategy.short
- Line 130: min → math.min
⋯// © juliangonzaconde-//@version=4+//@version=5strategy(title="BB-strategy", calc_on_every_tick=true,max_labels_count=500, max_bars_back = 1000, overlay=true, pyramiding=5, default_qty_value=3,initial_capital=100000, currency=currency.EUR)-bbSource = input(title="BB source", type=input.source, defval=close)+bbSource = input.source(title="BB source", defval=close)-smaLength = input(title="Bollinger Bands SMA length", type=input.integer, defval=20)+smaLength = input.int(title="Bollinger Bands SMA length", defval=20)-stdLength = input(title="Bollinger Bands StdDev length", type=input.integer, defval=2)+stdLength = input.int(title="Bollinger Bands StdDev length", defval=2)-longTrailPerc = input(title="Trail Long Loss (%) (min: 0.0)", type=input.float, minval=0.0, step=0.1, defval=4.0) * 0.01+longTrailPerc = input.float(title="Trail Long Loss (%) (min: 0.0)", minval=0.0, step=0.1, defval=4.0) * 0.01-shortTrailPerc = input(title="Trail Short Loss (%) (min: 0.0)", type=input.float, minval=0.0, step=0.1, defval=4.0) * 0.01+shortTrailPerc = input.float(title="Trail Short Loss (%) (min: 0.0)", minval=0.0, step=0.1, defval=4.0) * 0.01-maxOrders= input(title="Maximum orders (min: 1, max: 3)", type=input.integer,minval=1, maxval=3, defval=2)+maxOrders= input.int(title="Maximum orders (min: 1, max: 3)",minval=1, maxval=3, defval=2)-posSize= input(title="Position size (min: 1, max: 100)", type=input.integer, minval=1, maxval=100, defval=2)+posSize= input.int(title="Position size (min: 1, max: 100)", minval=1, maxval=100, defval=2)-rsiChosen= input(title="Use RSI", type=input.bool, defval=false)+rsiChosen= input.bool(title="Use RSI", defval=false)-rsiSource = input(title="RSI source", type=input.source, defval=close)+rsiSource = input.source(title="RSI source", defval=close)-rsiPeriod= input(title="RSI period", type=input.integer, minval=1, maxval=1000, defval=14)+rsiPeriod= input.int(title="RSI period", minval=1, maxval=1000, defval=14)-rsiBuy = input(title="RSI value for buy", type=input.integer, minval=1, maxval=99, defval=30)+rsiBuy = input.int(title="RSI value for buy", minval=1, maxval=99, defval=30)-rsiSell = input(title="RSI value for sell", type=input.integer, minval=1, maxval=99, defval=70)+rsiSell = input.int(title="RSI value for sell", minval=1, maxval=99, defval=70)-macdChosen= input(title="Use MACD", type=input.bool, defval=false)+macdChosen= input.bool(title="Use MACD", defval=false)-macdVariation= input(title="Use MACD variation (Use MACD must be selected)", type=input.bool, defval=false)+macdVariation= input.bool(title="Use MACD variation (Use MACD must be selected)", defval=false)-macdSource = input(title="MACD source", type=input.source, defval=close)+macdSource = input.source(title="MACD source", defval=close)-fastLen= input(title="MACD fast length", type=input.integer, minval=1, maxval=1000, defval=12)+fastLen= input.int(title="MACD fast length", minval=1, maxval=1000, defval=12)-slowLen= input(title="MACD slow length", type=input.integer, minval=1, maxval=1000, defval=26)+slowLen= input.int(title="MACD slow length", minval=1, maxval=1000, defval=26)-signLen= input(title="MACD signal length", type=input.integer, minval=1, maxval=1000, defval=9)+signLen= input.int(title="MACD signal length", minval=1, maxval=1000, defval=9)-longMaxTpChosen= input(title="Use maximum TP long", type=input.bool, defval=true)+longMaxTpChosen= input.bool(title="Use maximum TP long", defval=true)-longMaxTp= input(title="Maximum take profit long (%) (min: 0.5, max: 30.0)", type=input.float, minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01+longMaxTp= input.float(title="Maximum take profit long (%) (min: 0.5, max: 30.0)", minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01-shortMaxTpChosen= input(title="Use maximum TP short", type=input.bool, defval=true)+shortMaxTpChosen= input.bool(title="Use maximum TP short", defval=true)-shortMaxTp= input(title="Maximum take profit short (%) (min: 0.5, max: 30.0)", type=input.float, minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01+shortMaxTp= input.float(title="Maximum take profit short (%) (min: 0.5, max: 30.0)", minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01-rsiValue= rsi(rsiSource, rsiPeriod)+rsiValue= ta.rsi(rsiSource, rsiPeriod)-[macdLine, signalLine, histLine] = macd(macdSource, fastLen, slowLen, signLen)+[macdLine, signalLine, histLine] = ta.macd(macdSource, fastLen, slowLen, signLen)plot(macdLine, color=color.red)⋯plot(histLine, color=color.yellow, style=plot.style_histogram)-[middle, upper, lower] = bb(bbSource, smaLength, stdLength)+[middle, upper, lower] = ta.bb(bbSource, smaLength, stdLength)var idsLong = array.new_string(maxOrders,na)⋯ - id := tostring(time)+ id := str.tostring(time)- strategy.entry(id=id, long=true, qty=posSize)+ strategy.entry(id=id, direction = strategy.long, qty=posSize) array.set(idsLong, index, id)⋯ stopValue = close * (1 - longTrailPerc)- longStopPrice := max(stopValue, array.get(stopLong,i))+ longStopPrice := math.max(stopValue, array.get(stopLong,i)) array.set(stopLong,i,longStopPrice)⋯if (isShort)- id := tostring(time)+ id := str.tostring(time)- strategy.entry(id=id, long=false, qty=posSize)+ strategy.entry(id=id, direction = strategy.short, qty=posSize) array.set(idsShort, indexSell, id)⋯ stopValue = close * (1 + shortTrailPerc)- shortStopPrice := min(stopValue, array.get(stopShort,i))+ shortStopPrice := math.min(stopValue, array.get(stopShort,i)) array.set(stopShort,i,shortStopPrice)⋯Clean v5
VerifiedNothing to change.
Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L72:C7: cannot compare to 'na' with '==' or '!=' (it does not behave as expected). Use the na(x) function instead. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © juliangonzaconde-//@version=5+//@version=6strategy(title="BB-strategy", calc_on_every_tick=true,max_labels_count=500, max_bars_back = 1000, overlay=true, pyramiding=5, default_qty_value=3,initial_capital=100000, currency=currency.EUR)⋯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 |
|---|---|---|---|
| BB source | close | source | middle upper lower |
| Bollinger Bands SMA length | 20 | int (written input.integer) | middle upper lower |
| Bollinger Bands StdDev length | 2 | int (written input.integer) | middle upper lower |
| Trail Long Loss (%) (min: 0.0) | 4.0 | float · min 0.0 · step 0.1 | stopValue |
| Trail Short Loss (%) (min: 0.0) | 4.0 | float · min 0.0 · step 0.1 | stopValue |
| Maximum orders (min: 1, max: 3) | 2 | int (written input.integer) · min 1 · max 3 | idsLong idsShort stopLong stopShort longPartialClose shortPartialClose opTp opTpShort line 70 line 77 line 101 line 128 |
| Position size (min: 1, max: 100) | 2 | int (written input.integer) · min 1 · max 100 | entry id close id |
| Use RSI | false | bool | line 60 line 65 |
| RSI source | close | source | rsiValue |
| RSI period | 14 | int (written input.integer) · min 1 · max 1000 | rsiValue |
| RSI value for buy | 30 | int (written input.integer) · min 1 · max 99 | line 61 |
| RSI value for sell | 70 | int (written input.integer) · min 1 · max 99 | line 66 |
| Use MACD | false | bool | line 44 line 48 line 53 line 57 |
| Use MACD variation (Use MACD must be selected) | false | bool | line 42 line 51 |
| MACD source | close | source | macdLine signalLine histLine |
| MACD fast length | 12 | int (written input.integer) · min 1 · max 1000 | macdLine signalLine histLine |
| MACD slow length | 26 | int (written input.integer) · min 1 · max 1000 | macdLine signalLine histLine |
| MACD signal length | 9 | int (written input.integer) · min 1 · max 1000 | macdLine signalLine histLine |
| Use maximum TP long | true | bool | close id line 105 |
| Maximum take profit long (%) (min: 0.5, max: 30.0) | 3.0 | float · min 0.5 · max 30.0 · step 0.1 | tp |
| Use maximum TP short | true | bool | close id line 132 |
| Maximum take profit short (%) (min: 0.5, max: 30.0) | 3.0 | float · min 0.5 · max 30.0 · step 0.1 | tp |
What it calculates
- RSI (
rsi,ta.rsiin v5)RSI source (close), RSI period (14)rsiValue · line 27 - MACD (
macd,ta.macdin v5)MACD source (close), MACD fast length (12), MACD slow length (26), MACD signal length (9)macdLine · line 28 - MACD (
macd,ta.macdin v5)MACD source (close), MACD fast length (12), MACD slow length (26), MACD signal length (9)signalLine · line 28 - MACD (
macd,ta.macdin v5)MACD source (close), MACD fast length (12), MACD slow length (26), MACD signal length (9)histLine · line 28 - BB (
bb,ta.bbin v5)BB source (close), Bollinger Bands SMA length (20), Bollinger Bands StdDev length (2)middle · line 32 - BB (
bb,ta.bbin v5)BB source (close), Bollinger Bands SMA length (20), Bollinger Bands StdDev length (2)upper · line 32 - BB (
bb,ta.bbin v5)BB source (close), Bollinger Bands SMA length (20), Bollinger Bands StdDev length (2)lower · line 32
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 id long | (isLong) | On | 92 |
| Close id | ((longMaxTpChosen and array.get(opTp,i)<= close) or close >= upper or close <= array.get(stopLong,i)) and array.get(idsLong,i) != na | On | 107 |
| Close id | close > middle and array.get(idsLong,i) != na and not array.get(longPartialClose,i) | On | 115 |
| Open id short | (isShort) | On | 120 |
| Close id | ((shortMaxTpChosen and array.get(opTpShort,i) >= close) or close <= lower or close >= array.get(stopShort,i)) and array.get(idsShort,i) != na | On | 134 |
| Close id | close < middle and not array.get(shortPartialClose,i) and array.get(idsShort,i) != na | On | 140 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Settings heatmap
Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (Bollinger Bands SMA length 10, Bollinger Bands StdDev length 1) made 0.0% before 26 Dec 2025 and 0.0% after it, against 0.0% for the defaults. 0% of the 24 combinations made money in-sample and 0% out-of-sample.
| Bollinger Bands StdDev length ↓ · Bollinger Bands SMA length → | 10 | 12 | 14 | 17 | 20 | 24 | 28 | 34 |
|---|---|---|---|---|---|---|---|---|
| 1 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 2 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 3 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| Bollinger Bands StdDev length ↓ · Bollinger Bands SMA length → | 10 | 12 | 14 | 17 | 20 | 24 | 28 | 34 |
|---|---|---|---|---|---|---|---|---|
| 1 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 2 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
| 3 | 0% | 0% | 0% | 0% | 0% | 0% | 0% | 0% |
Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| Bollinger Bands strategy with RSI and MACD v1.0 this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| BankNifty_Bearish_Intraday by makarandpatil | macd rsi bb | v5 strategy | No trades on this market | ||
| BankNifty_Bullish_Intraday by makarandpatil | macd rsi bb | v5 strategy | No trades on this market | ||
| RSI of MACD Strategy [Long only] by mohanee | macd rsi | v4 strategy | No trades on this market | ||
| PowerX by jwitt98 by jwitt98 | rsi macd | v5 strategy | +107.9% | 78 | 107.8% |
| 3RSI 3CCI BB 5orders DCA strategy+ by rrolik66 | rsi bb | v5 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 54 ms | With a workaround 1,000 bars in 113 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 92, 120) so the engine trades the way the script says. 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. 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/// © juliangonzaconde//@version=4strategy(title="BB-strategy", calc_on_every_tick=true,max_labels_count=500, max_bars_back = 1000, overlay=true, pyramiding=5, default_qty_value=3,initial_capital=100000, currency=currency.EUR)bbSource = input(title="BB source", type=input.source, defval=close)smaLength = input(title="Bollinger Bands SMA length", type=input.integer, defval=20)stdLength = input(title="Bollinger Bands StdDev length", type=input.integer, defval=2)longTrailPerc = input(title="Trail Long Loss (%) (min: 0.0)", type=input.float, minval=0.0, step=0.1, defval=4.0) * 0.01shortTrailPerc = input(title="Trail Short Loss (%) (min: 0.0)", type=input.float, minval=0.0, step=0.1, defval=4.0) * 0.01maxOrders= input(title="Maximum orders (min: 1, max: 3)", type=input.integer,minval=1, maxval=3, defval=2)posSize= input(title="Position size (min: 1, max: 100)", type=input.integer, minval=1, maxval=100, defval=2)rsiChosen= input(title="Use RSI", type=input.bool, defval=false)rsiSource = input(title="RSI source", type=input.source, defval=close)rsiPeriod= input(title="RSI period", type=input.integer, minval=1, maxval=1000, defval=14)rsiBuy = input(title="RSI value for buy", type=input.integer, minval=1, maxval=99, defval=30)rsiSell = input(title="RSI value for sell", type=input.integer, minval=1, maxval=99, defval=70)macdChosen= input(title="Use MACD", type=input.bool, defval=false)macdVariation= input(title="Use MACD variation (Use MACD must be selected)", type=input.bool, defval=false)macdSource = input(title="MACD source", type=input.source, defval=close)fastLen= input(title="MACD fast length", type=input.integer, minval=1, maxval=1000, defval=12)slowLen= input(title="MACD slow length", type=input.integer, minval=1, maxval=1000, defval=26)signLen= input(title="MACD signal length", type=input.integer, minval=1, maxval=1000, defval=9)longMaxTpChosen= input(title="Use maximum TP long", type=input.bool, defval=true)longMaxTp= input(title="Maximum take profit long (%) (min: 0.5, max: 30.0)", type=input.float, minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01shortMaxTpChosen= input(title="Use maximum TP short", type=input.bool, defval=true)shortMaxTp= input(title="Maximum take profit short (%) (min: 0.5, max: 30.0)", type=input.float, minval=0.5, step=0.1, defval=3.0 ,maxval=30.0) * 0.01rsiValue= rsi(rsiSource, rsiPeriod)[macdLine, signalLine, histLine] = macd(macdSource, fastLen, slowLen, signLen)plot(macdLine, color=color.red)plot(signalLine, color=color.green)plot(histLine, color=color.yellow, style=plot.style_histogram)[middle, upper, lower] = bb(bbSource, smaLength, stdLength)var idsLong = array.new_string(maxOrders,na)var idsShort = array.new_string(maxOrders,na)var stopLong = array.new_float(maxOrders, 0.0)var stopShort = array.new_float(maxOrders,999999.0)var longPartialClose = array.new_bool(maxOrders,false)var shortPartialClose = array.new_bool(maxOrders,false)var opTp = array.new_float(maxOrders, 0.0)var opTpShort = array.new_float(maxOrders, 0.0)canBuyMacd = trueif macdVariation canBuyMacd := histLine[2]<histLine[1] and histLine[1] < histLine if not macdChosen canBuyMacd := trueelse canBuyMacd := histLine[2]<0 and histLine[1] >= 0 and histLine >= 0 if not macdChosen canBuyMacd := truecanSellMacd = trueif macdVariation canSellMacd := histLine[2]> histLine[1] and histLine[1]> histLine if not macdChosen canSellMacd := trueelse canSellMacd := histLine[2]>0 and histLine[1] <= 0 and histLine <= 0 if not macdChosen canSellMacd := truecanBuyRsi = trueif rsiChosen if rsiValue > rsiBuy canBuyRsi := false canSellRsi = trueif rsiChosen if rsiValue < rsiSell canSellRsi := falsecanBuy = falseint index = nafor i = 0 to (maxOrders - 1) val=array.get(idsLong,i) if val==na canBuy := true index := icanSell = falseint indexSell= nafor i = 0 to (maxOrders - 1) val=array.get(idsShort,i) if val==na canSell := true indexSell := ibool isLong = close[2] < lower[2] and close[1] > close[2] and close > close[1] and canBuy and close[3] > close[2] and close < middle and (canBuyRsi and canBuyMacd)bool isShort = close[2] > upper[2] and close[1] < close[2] and close < close[1] and canSell and close[3] < close[2] and close > middle and (canSellRsi and canSellMacd)longStopPrice = 0.0 shortStopPrice = 0.0tp=0.0id = ""if (isLong) id := tostring(time) strategy.entry(id=id, long=true, qty=posSize) array.set(idsLong, index, id) stopValue = close * (1 - longTrailPerc) longStopPrice := stopValue array.set(stopLong, index, longStopPrice) tp := close * (1 + longMaxTp) array.set(opTp,index,tp) for i = 0 to (maxOrders - 1) stopValue = close * (1 - longTrailPerc) longStopPrice := max(stopValue, array.get(stopLong,i)) array.set(stopLong,i,longStopPrice) if ((longMaxTpChosen and array.get(opTp,i)<= close) or close >= upper or close <= array.get(stopLong,i)) and array.get(idsLong,i) != na id := array.get(idsLong,i) strategy.close(id=id, qty=posSize) array.set(idsLong,i,na) array.set(stopLong,i,0.0) array.set(longPartialClose,i,false) if close > middle and array.get(idsLong,i) != na and not array.get(longPartialClose,i) id := array.get(idsLong,i) x=array.get(idsLong,i) strategy.close(id=id, qty=posSize/2) array.set(longPartialClose,i,true) if (isShort) id := tostring(time) strategy.entry(id=id, long=false, qty=posSize) array.set(idsShort, indexSell, id) stopValue = close * (1 + shortTrailPerc) shortStopPrice := stopValue array.set(stopShort, indexSell, shortStopPrice) tp := close * (1 - shortMaxTp) array.set(opTpShort,indexSell,tp) for i = 0 to (maxOrders - 1) stopValue = close * (1 + shortTrailPerc) shortStopPrice := min(stopValue, array.get(stopShort,i)) array.set(stopShort,i,shortStopPrice) if ((shortMaxTpChosen and array.get(opTpShort,i) >= close) or close <= lower or close >= array.get(stopShort,i)) and array.get(idsShort,i) != na id := array.get(idsShort,i) strategy.close(id=id, qty=posSize) array.set(idsShort,i,na) array.set(stopShort,i,999999.0) array.set(shortPartialClose,i,false) if close < middle and not array.get(shortPartialClose,i) and array.get(idsShort,i) != na id := array.get(idsShort,i) strategy.close(id=id, qty=posSize/2) array.set(shortPartialClose,i,true) plot(middle, color=color.yellow)plot(upper, color=color.yellow)plot(lower, color=color.yellow)