Pine Script v5 · Strategy
Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN]
A Pine Script v5 strategy built on EMA (close, 42), SMA (close, 42) and Stdev (close, 42). It has 9 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a profit target and no stop loss. Each trade commits 100.0% of equity and pays 0.075 commission. 29 settings; defaults include Bollinger Length 42, Bollinger Source close and Base Multiplier 2.0.
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 Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] do on BTCUSDT 1D with default settings?
- −115.13%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- What alerts does Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] have?
- 6
- Buy; Buy; Buy; Sell; and more.
- Does Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] run in Pine Script v6?
- No
- The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L194:C7: cannot use 'int' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download.
- Does Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] use a stop loss?
- No
- Its exits set a profit target but no stop.
BTCUSDT · 1D · default settings
- Sell
- Buy
- Plot
- Up Trend
- Down Trend
- Winning trade
- Losing trade
- Equity
- Buy and hold
| Start: long1713052800000 |
| End: long1774828800000 |
| Net Profit: -115.13% |
| Percent Per Day -0.16102% |
| Percent Profitable: 16.67% |
| Profit Factor: 0 |
| Total Trades: 6 |
| Max Wins In A Row: 1 |
| Max Losses In A Row: 4 |
- Net profit
- −115.13%
- −$23,026.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 1 won · 5 lost
- Profitable
- 16.7%
- of closed trades
- Profit factor
- 0.00
- gross profit ÷ gross loss
- Max drawdown
- 123.30%
- −$24,660.00
One run with the author's default settings, $20,000 starting capital, commission as the script sets it. A short 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.
-
Likely bug
ta.sma(hl2, nLengthFast) ignores the settings line 45
Line 45 hard-codes hl2 where line 33 uses “Bollinger Source”. Changing those settings moves one band but not this one.
ta.sma(hl2, nLengthFast)ta.sma(bb_source, nLengthFast) -
Likely bug
ta.sma(hl2, nLengthSlow) ignores the settings line 46
Line 46 hard-codes hl2 where line 33 uses “Bollinger Source”. Changing those settings moves one band but not this one.
ta.sma(hl2, nLengthSlow)ta.sma(bb_source, nLengthSlow) -
Check
No stop loss
strategy.exit is used without a stop, loss or trailing stop.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
2 settings have no label
In the settings dialog startPeriodTime (line 128), endPeriodTime (line 130) 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.
-
Note
Uses an argument v6 removed
when = on line 190, 191, 192. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Use EMA for Bollinger Band | false | bool | bb_basis |
| Bollinger Length | 42 | int · min 1 | ema_1 sma_1 dev |
| Bollinger Source | close | source | ema_1 sma_1 fast_ma dev |
| Base Multiplier | 2.0 | float · min 0.5 · max 10 | bb_dev_inner |
| Fast EMA length | 10 | int · min 2 | fast_ma |
| Awesome Length Slow | 77 | int · min 1 | xSMA2_hl2 |
| Awesome Length Fast | 10 | int · min 1 | xSMA1_hl2 |
| ATR Length | 10 | int | supertrend direction |
| Factor | 3.0 | float · step 0.01 | supertrend direction |
| ADX | |||
| Average Directional Index (ADX) | false | bool | adxsig isADXEnabledAndAboveThreshold |
| ADX Smoothing | 14 | int | adx adxsig |
| DI Length | 14 | int | adxplus adxminus adxsig |
| ADX Threshold | 25 | int | isADXEnabledAndAboveThreshold |
| Date Range | |||
| Start | true | bool | start |
| Start Period Time (no label in the script) | timestamp('1 Jan 2019') | time | start |
| End | true | bool | end |
| End Period Time (no label in the script) | timestamp('31 Dec 2030') | time | end |
| Trade Direction | |||
| Trade Direction | Long and Short | string · options ['Long and Short', 'Long Only', 'Short Only'] | entry Long entry Short line 178 line 181 |
| Take Profit | |||
| Take Profit 1 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP1 |
| % Of Position | 100 | int · min 0 | exit TP1 |
| Take Profit 2 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP2 |
| % Of Position | 100 | int · min 0 | exit TP2 |
| Take Profit 3 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP3 |
| % Of Position | 100 | int · min 0 | exit TP3 |
| Take Profit 4 - Target % | 100 | float · min 0.0 · step 0.5 | exit TP4 |
| Stop Loss | |||
| Stop Loss (%) | 999 | float · min 0.01 | slLongClose slShortClose |
| Leverage | |||
| Leverage | 1 | float · step .5 | contracts |
| ProfitView Alert Syntax | |||
| Alert Syntax Prefix | CRYPTANEX_99FTX_Strategy-Name-Here | string | alertSyntaxBase |
| Dashboard | |||
| Show Dashboard | true | bool | line 201 |
What it calculates
- EMABollinger Source (close), Bollinger Length (42)
ema_1 · line 32 - SMABollinger Source (close), Bollinger Length (42)
sma_1 · line 33 - EMABollinger Source (close), Fast EMA length (10)
fast_ma · line 35 - StdevBollinger Source (close), Bollinger Length (42)
dev · line 38 - SMAhl2, Awesome Length Fast (10)
xSMA1_hl2 · line 45 - SMAhl2, Awesome Length Slow (77)
xSMA2_hl2 · line 46 - SupertrendFactor (3.0), ATR Length (10)
supertrend · line 88 - SupertrendFactor (3.0), ATR Length (10)
direction · line 88 - Changehigh
adxup · line 111 - Changelow
adxdown · line 112 - RMAta.tr, len
adxtruerange · line 115 - RMAmath.abs(adxplus - adxminus) / (adxsum == 0 ? 1 : adxsum), ADX Smoothing (14)
adx · line 122
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 94 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 | longConditionCalc and tradeDirection != 'Short Only' and isInLongPosition == false | On | 179 |
| Open Short short | shortConditionCalc and tradeDirection != 'Long Only' and isInShortPosition == false | On | 182 |
| Exit TP1 limit | calcPeriod | On | 186 |
| Exit TP2 limit | calcPeriod | On | 187 |
| Exit TP3 limit | calcPeriod | On | 188 |
| Exit TP4 limit | calcPeriod | On | 189 |
| Close Long | slLongCloseclose < strategy.position_avg_price * (1 - stoplossPercent) | On | 190 |
| Close Short | slShortCloseclose > strategy.position_avg_price * (1 + stoplossPercent) | On | 191 |
| Close | closeLongCondition or closeShortCondition | On | 192 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Alerts
| Alert | Call | Message | Line |
|---|---|---|---|
| Buy | alertcondition | Buy | 78 |
| Buy | alertcondition | Buy | 79 |
| Buy | alertcondition | Buy | 80 |
| Sell | alertcondition | Sell | 81 |
| (no title) | alert | alertSyntaxBase + 'side:long' | 180 |
| (no title) | alert | alertSyntaxBase + 'side:short' | 183 |
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 recalculationuses varip. 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 | −115.1% | +102.5% | 6 | 17% | 0.00 | 123.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.2% | +5.5% | 9 | 33% | 1.01 | 25.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −97.3% | +9.8% | 4 | 0% | 0.00 | 103.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −48.6% | +8.4% | 10 | 20% | 0.10 | 50.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −82.4% | +16.7% | 6 | 33% | 0.10 | 94.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −46.1% | +21.5% | 7 | 29% | 0.20 | 58.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −31.5% | +148.3% | 3 | 33% | 0.03 | 122.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −5.6% | +17.1% | 9 | 22% | 0.79 | 18.6% | 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 | 1 | 0% | −$1,036.77 |
| Sideways market | 4 | 25% | −$21,989.60 |
| Falling market | 1 | 0% | −$0.01 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
119.8%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 115.1% to 119.8%; the tick is what actually happened, 115.1%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 14 Apr 2024 63,924.50 | 21 Jul 2024 67,140.00 | 98 | −$1,036.77 |
| long | 21 Jul 2024 67,140.00 | 6 Aug 2024 54,018.80 | 16 | −$3,734.75 |
| short | 6 Aug 2024 54,018.80 | 2 Oct 2025 118,595.00 | 422 | −$18,254.90 |
| long | 2 Oct 2025 118,595.00 | 17 Oct 2025 108,194.00 | 15 | −$0.01 |
| short | 17 Oct 2025 108,194.00 | 14 Mar 2026 70,930.00 | 148 | $0.04 |
| long | 14 Mar 2026 70,930.00 | 30 Mar 2026 66,010.90 | 16 | −$0.01 |
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 |
|---|---|---|---|---|---|
| Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN] this page | — | v5 strategy | −115.1% | 6 | 123.3% |
| Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic by exlux99 | sma stdev ema rma | v5 strategy | +1.9% | 15 | 3.2% |
| Best TradingView Strategy - For NASDAQ and DOW30 and other Index by The_Bigger_Bull | sma stdev rma ema | v5 strategy | +2.2% | 17 | 3.4% |
| Volume Weighted Bollinger Bands Strategy by Ankit_1618 | ema sma stdev | v4 strategy | +10.0% | 15 | 27.5% |
| BB21_MA200_Strategy by mohanee | ema sma stdev | v4 strategy | No trades on this market | ||
| Optimized Keltner Channels SL-TP Strategy for BTC by chanu_lev10k | sma ema rma | v4 strategy | −19.4% | 36 | 70.0% |
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.
- 3 strategy calls use the when = argument (line 190, 191, 192). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 6 settings use the untyped input() (line 21, 23, 86, 107, 108, 109). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 21: input() → input.bool()
- Line 23: input() → input.source()
- Line 86: input() → input.int()
- Line 107: input() → input.int()
- Line 108: input() → input.int()
- Line 109: input() → input.int()
3 more edits
- Line 190: when = slLongClose → if block
- Line 191: when = slShortClose → if block
- Line 192: when = closeLongCondition or closeShortCondition → if block
⋯// Bollinger Bands Inputs-bb_use_ema = input(false, title='Use EMA for Bollinger Band')+bb_use_ema = input.bool(false, title='Use EMA for Bollinger Band')bb_length = input.int(42, minval=1, title='Bollinger Length')-bb_source = input(close, title='Bollinger Source')+bb_source = input.source(close, title='Bollinger Source')bb_mult = input.float(2.0, title='Base Multiplier', minval=0.5, maxval=10)⋯//indicator("Supertrend", overlay=true, timeframe="", timeframe_gaps=true)-atrPeriod = input(10, "ATR Length")+atrPeriod = input.int(10, "ATR Length")factor = input.float(3.0, "Factor", step = 0.01)⋯adxEnabled = input.bool(defval = false , title = "Average Directional Index (ADX)", tooltip = "", group ="ADX" ) -adxlen = input(14, title="ADX Smoothing", group="ADX")+adxlen = input.int(14, title="ADX Smoothing", group="ADX")-adxdilen = input(14, title="DI Length", group="ADX")+adxdilen = input.int(14, title="DI Length", group="ADX")-adxabove = input(25, title="ADX Threshold", group="ADX")+adxabove = input.int(25, title="ADX Threshold", group="ADX")adxdirmov(len) =>⋯ strategy.exit('TP4', profit=per(tp4))- strategy.close('Long', qty_percent=100, comment='SL Long', when=slLongClose)+ if slLongClose+ strategy.close('Long', qty_percent=100, comment='SL Long')- strategy.close('Short', qty_percent=100, comment='SL Short', when=slShortClose)+ if slShortClose+ strategy.close('Short', qty_percent=100, comment='SL Short')- strategy.close_all(when=closeLongCondition or closeShortCondition, comment='Close Postion')+ if closeLongCondition or closeShortCondition+ strategy.close_all(comment='Close Postion')/// Dashboard⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L194:C7: cannot use 'int' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © myn-//@version=5+//@version=6strategy('Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN]', max_bars_back=5000, overlay=true, pyramiding=0, initial_capital=20000, currency='USD', default_qty_type=strategy.percent_of_equity, default_qty_value=100.0, commission_value=0.075, use_bar_magnifier = true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 116 ms | Yes 1,000 bars in 148 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © myn//@version=5strategy('Strategy Myth-Busting #3 - BB_BUY+SuperTrend - [MYN]', max_bars_back=5000, overlay=true, pyramiding=0, initial_capital=20000, currency='USD', default_qty_type=strategy.percent_of_equity, default_qty_value=100.0, commission_value=0.075, use_bar_magnifier = true)///Derek-Multiple-Take-Profit-Strategy-Template-v5-2022-04-03.pine// 04-03-2022 Added ADX // 08-17-2022 Added Dashboard///////////////////////////////////////* Put your strategy logic below *///////////////////////////////////////// Q0txMfU623Y// Trading Rules//long condition when BB_BUY indicates buy signal and SuperTrend is green//short condition when BB_BUY indicates Sell signal and SuperTrend is red //@version=5// BB_Buy and sell strategy logic//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░//indicator(shorttitle='BB_Buy and Sell', title='BB_Buy and Sell', overlay=true)// === INPUTS ===// Bollinger Bands Inputsbb_use_ema = input(false, title='Use EMA for Bollinger Band')bb_length = input.int(42, minval=1, title='Bollinger Length')bb_source = input(close, title='Bollinger Source')bb_mult = input.float(2.0, title='Base Multiplier', minval=0.5, maxval=10)// EMA inputsfast_ma_len = input.int(10, title='Fast EMA length', minval=2)// Awesome InputsnLengthSlow = input.int(77, minval=1, title='Awesome Length Slow')nLengthFast = input.int(10, minval=1, title='Awesome Length Fast')// === /INPUTS ===// Breakout Indicator Inputsema_1 = ta.ema(bb_source, bb_length)sma_1 = ta.sma(bb_source, bb_length)bb_basis = bb_use_ema ? ema_1 : sma_1fast_ma = ta.ema(bb_source, fast_ma_len)// Deviation// * I'm sure there's a way I could write some of this cleaner, but meh.dev = ta.stdev(bb_source, bb_length)bb_dev_inner = bb_mult * dev// Upper bandsinner_high = bb_basis + bb_dev_inner// Lower Bandsinner_low = bb_basis - bb_dev_inner// Calculate Awesome OscillatorxSMA1_hl2 = ta.sma(hl2, nLengthFast)xSMA2_hl2 = ta.sma(hl2, nLengthSlow)xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2// Calculate direction of AOAO = xSMA1_SMA2 >= 0 ? xSMA1_SMA2 > xSMA1_SMA2[1] ? 1 : 2 : xSMA1_SMA2 > xSMA1_SMA2[1] ? -1 : -2// Deternine if we are currently LONGisLong = falseisLong := nz(isLong[1], false)// Determine if we are currently SHORTisShort = falseisShort := nz(isShort[1], false)// Buy only if the buy signal is triggered and we are not already longLONG = not isLong and ta.crossover(fast_ma, bb_basis) and close > bb_basis and math.abs(AO) == 1c_green = fast_ma > bb_basis// Sell only if the sell signal is triggered and we are not already shortSHORT = not isShort and ta.crossunder(fast_ma, bb_basis) and close < bb_basis and math.abs(AO) == 2c_red = fast_ma < bb_basisif LONG isLong := true isShort := false isShortbarcolor(LONG ? color.green : na)if SHORT isLong := false isShort := true isShortbarcolor(c_green ? color.green : na)barcolor(c_red ? color.red : na)// Show Break Alertsplotshape(SHORT, title='Sell', style=shape.labeldown, location=location.abovebar, size=size.normal, text='Sell', textcolor=color.new(color.white, 0), color=color.new(color.red, 0))plotshape(LONG, title='Buy', style=shape.labelup, location=location.belowbar, size=size.normal, text='Buy', textcolor=color.new(color.white, 0), color=color.new(color.green, 0))// === /PLOTTING ===// Send alert to TV alarm sub-systemalertcondition(LONG, title='Buy', message='Buy')alertcondition(LONG, title='Buy', message='Buy')alertcondition(LONG, title='Buy', message='Buy')alertcondition(SHORT, title='Sell', message='Sell')// SuperTrend//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░//@version=5//indicator("Supertrend", overlay=true, timeframe="", timeframe_gaps=true)atrPeriod = input(10, "ATR Length")factor = input.float(3.0, "Factor", step = 0.01)[supertrend, direction] = ta.supertrend(factor, atrPeriod)bodyMiddle = plot((open + close) / 2, display=display.none)upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)////////////////////////////////////////* Put your strategy rules below */////////////////////////////////////////long condition when BB_BUY indicates buy signal and SuperTrend is green//short condition when BB_BUY indicates Sell signal and SuperTrend is red longCondition = LONG and direction < 0 shortCondition = SHORT and direction > 0 //define as 0 if do not want to usecloseLongCondition = 0closeShortCondition = 0// ADX//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░adxEnabled = input.bool(defval = false , title = "Average Directional Index (ADX)", tooltip = "", group ="ADX" ) adxlen = input(14, title="ADX Smoothing", group="ADX")adxdilen = input(14, title="DI Length", group="ADX")adxabove = input(25, title="ADX Threshold", group="ADX")adxdirmov(len) => adxup = ta.change(high) adxdown = -ta.change(low) adxplusDM = na(adxup) ? na : (adxup > adxdown and adxup > 0 ? adxup : 0) adxminusDM = na(adxdown) ? na : (adxdown > adxup and adxdown > 0 ? adxdown : 0) adxtruerange = ta.rma(ta.tr, len) adxplus = fixnan(100 * ta.rma(adxplusDM, len) / adxtruerange) adxminus = fixnan(100 * ta.rma(adxminusDM, len) / adxtruerange) [adxplus, adxminus]adx(adxdilen, adxlen) => [adxplus, adxminus] = adxdirmov(adxdilen) adxsum = adxplus + adxminus adx = 100 * ta.rma(math.abs(adxplus - adxminus) / (adxsum == 0 ? 1 : adxsum), adxlen)adxsig = adxEnabled ? adx(adxdilen, adxlen) : naisADXEnabledAndAboveThreshold = adxEnabled ? (adxsig > adxabove) : true//Backtesting Time Period (Input.time not working as expected as of 03/30/2021. Giving odd start/end dates//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░useStartPeriodTime = input.bool(true, 'Start', group='Date Range', inline='Start Period')startPeriodTime = input.time(timestamp('1 Jan 2019'), '', group='Date Range', inline='Start Period')useEndPeriodTime = input.bool(true, 'End', group='Date Range', inline='End Period')endPeriodTime = input.time(timestamp('31 Dec 2030'), '', group='Date Range', inline='End Period')start = useStartPeriodTime ? startPeriodTime >= time : falseend = useEndPeriodTime ? endPeriodTime <= time : falsecalcPeriod = not start and not end// Trade Direction // ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░tradeDirection = input.string('Long and Short', title='Trade Direction', options=['Long and Short', 'Long Only', 'Short Only'], group='Trade Direction')// Percent as Points// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░per(pcnt) => strategy.position_size != 0 ? math.round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)// Take profit 1// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░tp1 = input.float(title='Take Profit 1 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 1')q1 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 1')// Take profit 2// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░tp2 = input.float(title='Take Profit 2 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 2')q2 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 2')// Take profit 3// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░tp3 = input.float(title='Take Profit 3 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit', inline='Take Profit 3')q3 = input.int(title='% Of Position', defval=100, minval=0, group='Take Profit', inline='Take Profit 3')// Take profit 4// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░tp4 = input.float(title='Take Profit 4 - Target %', defval=100, minval=0.0, step=0.5, group='Take Profit')/// Stop Loss// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░stoplossPercent = input.float(title='Stop Loss (%)', defval=999, minval=0.01, group='Stop Loss') * 0.01slLongClose = close < strategy.position_avg_price * (1 - stoplossPercent)slShortClose = close > strategy.position_avg_price * (1 + stoplossPercent)/// Leverage// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░leverage = input.float(1, 'Leverage', step=.5, group='Leverage')contracts = math.min(math.max(.000001, strategy.equity / close * leverage), 1000000000)/// Trade State Management// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░isInLongPosition = strategy.position_size > 0isInShortPosition = strategy.position_size < 0/// ProfitView Alert Syntax String Generation// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░alertSyntaxPrefix = input.string(defval='CRYPTANEX_99FTX_Strategy-Name-Here', title='Alert Syntax Prefix', group='ProfitView Alert Syntax')alertSyntaxBase = alertSyntaxPrefix + '\n#' + str.tostring(open) + ',' + str.tostring(high) + ',' + str.tostring(low) + ',' + str.tostring(close) + ',' + str.tostring(volume) + ','/// Trade Execution// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░longConditionCalc = (longCondition and isADXEnabledAndAboveThreshold)shortConditionCalc = (shortCondition and isADXEnabledAndAboveThreshold)if calcPeriod if longConditionCalc and tradeDirection != 'Short Only' and isInLongPosition == false strategy.entry('Long', strategy.long, qty=contracts) alert(message=alertSyntaxBase + 'side:long', freq=alert.freq_once_per_bar_close) if shortConditionCalc and tradeDirection != 'Long Only' and isInShortPosition == false strategy.entry('Short', strategy.short, qty=contracts) alert(message=alertSyntaxBase + 'side:short', freq=alert.freq_once_per_bar_close) //Inspired from Multiple %% profit exits example by adolgo https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/ strategy.exit('TP1', qty_percent=q1, profit=per(tp1)) strategy.exit('TP2', qty_percent=q2, profit=per(tp2)) strategy.exit('TP3', qty_percent=q3, profit=per(tp3)) strategy.exit('TP4', profit=per(tp4)) strategy.close('Long', qty_percent=100, comment='SL Long', when=slLongClose) strategy.close('Short', qty_percent=100, comment='SL Short', when=slShortClose) strategy.close_all(when=closeLongCondition or closeShortCondition, comment='Close Postion')/// Dashboard// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░// Inspired by https://www.tradingview.com/script/uWqKX6A2/ - Thanks VertMTshowDashboard = input.bool(group="Dashboard", title="Show Dashboard", defval=true)f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) => _cellText = _title + "\n" + _value table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor, text_size=size.auto)// Draw dashboard tableif showDashboard var bgcolor = color.new(color.black,0) // Keep track of Wins/Losses streaks newWin = (strategy.wintrades > strategy.wintrades[1]) and (strategy.losstrades == strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1]) newLoss = (strategy.wintrades == strategy.wintrades[1]) and (strategy.losstrades > strategy.losstrades[1]) and (strategy.eventrades == strategy.eventrades[1]) varip int winRow = 0 varip int lossRow = 0 varip int maxWinRow = 0 varip int maxLossRow = 0 if newWin lossRow := 0 winRow := winRow + 1 if winRow > maxWinRow maxWinRow := winRow if newLoss winRow := 0 lossRow := lossRow + 1 if lossRow > maxLossRow maxLossRow := lossRow // Prepare stats table var table dashTable = table.new(position.bottom_right, 1, 15, border_width=1) if barstate.islastconfirmedhistory // Update table dollarReturn = strategy.netprofit f_fillCell(dashTable, 0, 0, "Start:", str.format("{0,date,long}", strategy.closedtrades.entry_time(0)) , bgcolor, color.white) // + str.format(" {0,time,HH:mm}", strategy.closedtrades.entry_time(0)) f_fillCell(dashTable, 0, 1, "End:", str.format("{0,date,long}", strategy.opentrades.entry_time(0)) , bgcolor, color.white) // + str.format(" {0,time,HH:mm}", strategy.opentrades.entry_time(0)) _profit = (strategy.netprofit / strategy.initial_capital) * 100 f_fillCell(dashTable, 0, 2, "Net Profit:", str.tostring(_profit, '##.##') + "%", _profit > 0 ? color.green : color.red, color.white) _numOfDaysInStrategy = (strategy.opentrades.entry_time(0) - strategy.closedtrades.entry_time(0)) / (1000 * 3600 * 24) f_fillCell(dashTable, 0, 3, "Percent Per Day", str.tostring(_profit / _numOfDaysInStrategy, '#########################.#####')+"%", _profit > 0 ? color.green : color.red, color.white) _winRate = ( strategy.wintrades / strategy.closedtrades ) * 100 f_fillCell(dashTable, 0, 4, "Percent Profitable:", str.tostring(_winRate, '##.##') + "%", _winRate < 50 ? color.red : _winRate < 75 ? #999900 : color.green, color.white) f_fillCell(dashTable, 0, 5, "Profit Factor:", str.tostring(strategy.grossprofit / strategy.grossloss, '##.###'), strategy.grossprofit > strategy.grossloss ? color.green : color.red, color.white) f_fillCell(dashTable, 0, 6, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white) f_fillCell(dashTable, 0, 8, "Max Wins In A Row:", str.tostring(maxWinRow, '######') , bgcolor, color.white) f_fillCell(dashTable, 0, 9, "Max Losses In A Row:", str.tostring(maxLossRow, '######') , bgcolor, color.white)