Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

888 BOT #backtest

A Pine Script v4 strategy built on SMA (volume, 61), EMA (_src - _src[1], _per) and Change (low). It has 9 order rules, 4 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 0% of equity and pays 0.04 commission. 48 settings; defaults include SOURCE hlc3, JMA LENGTH 30 and JMA PHASE 40.

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 888 BOT #backtest repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 12 of 12 trades.
How did 888 BOT #backtest do on BTCUSDT 1D with default settings?
+2.95%
From 26 Jan 2024 to 21 Oct 2026, over 12 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can 888 BOT #backtest 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 888 BOT #backtest use a stop loss?
Yes
strategy.exit sets a stop on line 457, 475.
Does 888 BOT #backtest run in Zeal Terminal?
Yes
It compiles in 181 ms and runs 1,000 bars in 446 ms, in the browser.

BTCUSDT · 1D · default settings

  • JMA
  • RF
  • RF High Target
  • RF Low Target
  • SAR
  • Upper Bollinger Band
  • Lower Bollinger Band
  • Long TP Level
  • Short TP Level
  • Winning trade
  • Losing trade
  • Equity
  • Buy and hold
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (48)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$10,296$20,253Equity from $10,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots. 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.
Net profit
+2.95%
$295.50
Buy and hold
+102.5%
same period
Closed trades
12
11 won · 1 lost
Profitable
91.7%
of closed trades
Profit factor
5.73
gross profit ÷ gross loss
Max drawdown
0.87%
−$88.80

One run with the author's default settings, $10,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.

  • Likely bug

    sma(volume, sma_length) ignores the settings line 253

    Line 253 hard-codes volume where line 234 uses “ SOURCE”. Changing those settings moves one band but not this one.

    sma(volume,sma_length)sma(src, sma_length)
  • Likely bug

    sma(volume, sma_length) ignores the settings line 261

    Line 261 hard-codes volume where line 234 uses “ SOURCE”. Changing those settings moves one band but not this one.

    sma(volume,sma_length)sma(src, sma_length)
  • Likely bug

    sma(volume, sma_length) ignores the settings line 442

    Line 442 hard-codes volume where line 234 uses “ SOURCE”. Changing those settings moves one band but not this one.

    sma(volume, sma_length)sma(src, sma_length)
  • 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 447, 449, 453, 455, 473, 491, 493. The upgraded version below moves each call inside an if block.

  • Note

    Uses iff(), which v5 removed line 218

    iff() on line 218, 219, 461, 462, 463, 465, 468, 469, 470, 471, 479, 480, 481, 483, 486, 487, 488, 489. v5 writes it as the ternary c ? a : b.

  • Note

    Uses transp =, which v5 removed line 159

    transp = on line 159, 160, 371, 407. 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 pow → math.pow, ema → ta.ema, abs → math.abs, change → ta.change, rma → ta.rma, max → math.max, sma → ta.sma, sar → ta.sar, sum → math.sum, sqrt → math.sqrt, stdev → ta.stdev, macd → ta.macd, atr → ta.atr, min → math.min, round → math.round.
  • 7 strategy calls use the when = argument (line 447, 449, 453, 455, 473, 491, 493). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 10: input(type = input.source) → input.source()
  • Line 12: input(type = input.bool) → input.bool()
  • Line 13: input(type = input.integer) → input.int()
  • Line 14: input(type = input.integer) → input.int()
  • Line 15: input(type = input.float) → input.float()
132 more edits
  • Line 17: input(type = input.bool) → input.bool()
  • Line 18: input(type = input.integer) → input.int()
  • Line 19: input(type = input.float) → input.float()
  • Line 21: input(type = input.bool) → input.bool()
  • Line 23: input(type = input.integer) → input.int()
  • Line 24: input(type = input.float) → input.float()
  • Line 26: input(type = input.bool) → input.bool()
  • Line 27: input(type = input.float) → input.float()
  • Line 28: input(type = input.float) → input.float()
  • Line 29: input(type = input.float) → input.float()
  • Line 31: input(type = input.bool) → input.bool()
  • Line 32: input(type = input.integer) → input.int()
  • Line 33: input(type = input.integer) → input.int()
  • Line 35: input(type = input.bool) → input.bool()
  • Line 37: input(type = input.integer) → input.int()
  • Line 38: input(type = input.integer) → input.int()
  • Line 39: input(type = input.integer) → input.int()
  • Line 40: input(type = input.integer) → input.int()
  • Line 41: input(type = input.integer) → input.int()
  • Line 43: input(type = input.bool) → input.bool()
  • Line 44: input(type = input.float) → input.float()
  • Line 45: input(type = input.integer) → input.int()
  • Line 47: input(type = input.float) → input.float()
  • Line 48: input(type = input.float) → input.float()
  • Line 50: input(type = input.bool) → input.bool()
  • Line 52: input(type = input.float) → input.float()
  • Line 54: input(type = input.integer) → input.int()
  • Line 55: input(type = input.float) → input.float()
  • Line 57: input(type = input.float) → input.float()
  • Line 59: input(type = input.bool) → input.bool()
  • Line 61: input(type = input.bool) → input.bool()
  • Line 62: input(type = input.integer) → input.int()
  • Line 63: input(type = input.float) → input.float()
  • Line 64: input(type = input.float) → input.float()
  • Line 65: input(type = input.bool) → input.bool()
  • Line 67: input(type = input.bool) → input.bool()
  • Line 68: input(type = input.integer) → input.int()
  • Line 71: input(type = input.float) → input.float()
  • Line 72: input(type = input.integer) → input.int()
  • Line 119: pow → math.pow
  • Line 126: pow → math.pow
  • Line 126: pow → math.pow
  • Line 143: ema → ta.ema
  • Line 143: abs → math.abs
  • Line 144: ema → ta.ema
  • Line 159: transp = 50 → color.new(filtcolor, 50)
  • Line 160: transp = 50 → color.new(filtcolor, 50)
  • Line 167: change → ta.change
  • Line 168: change → ta.change
  • Line 171: rma → ta.rma
  • Line 171: tr → ta.tr
  • Line 172: rma → ta.rma
  • Line 173: rma → ta.rma
  • Line 175: rma → ta.rma
  • Line 175: abs → math.abs
  • Line 182: max → math.max
  • Line 182: max → math.max
  • Line 182: abs → math.abs
  • Line 182: abs → math.abs
  • Line 183: max → math.max
  • Line 184: max → math.max
  • Line 190: abs → math.abs
  • Line 191: sma → ta.sma
  • Line 206: sar → ta.sar
  • Line 218: iff(c, a, b) → (c ? a : b)
  • Line 218: abs → math.abs
  • Line 219: iff(c, a, b) → (c ? a : b)
  • Line 219: abs → math.abs
  • Line 230: sum → math.sum
  • Line 230: sum → math.sum
  • Line 231: sqrt → math.sqrt
  • Line 231: sma → ta.sma
  • Line 231: pow → math.pow
  • Line 234: sma → ta.sma
  • Line 235: sma → ta.sma
  • Line 237: stdev → ta.stdev
  • Line 238: sma → ta.sma
  • Line 241: macd → ta.macd
  • Line 253: sma → ta.sma
  • Line 261: sma → ta.sma
  • Line 325: atr → ta.atr
  • Line 326: atr → ta.atr
  • Line 329: max → math.max
  • Line 330: min → math.min
  • Line 333: min → math.min
  • Line 333: max → math.max
  • Line 333: max → math.max
  • Line 334: min → math.min
  • Line 334: max → math.max
  • Line 334: max → math.max
  • Line 365: sma → ta.sma
  • Line 366: stdev → ta.stdev
  • Line 371: transp = 95 → color.new(in_longCondition ? #009688 : #f06292, 95)
  • Line 407: transp = 95 → color.new(W_color, 95)
  • Line 442: min → math.min
  • Line 442: max → math.max
  • Line 442: round → math.round
  • Line 442: sma → ta.sma
  • Line 458: abs → math.abs
  • Line 461: iff(c, a, b) → (c ? a : b)
  • Line 461: abs → math.abs
  • Line 462: iff(c, a, b) → (c ? a : b)
  • Line 462: abs → math.abs
  • Line 463: iff(c, a, b) → (c ? a : b)
  • Line 463: abs → math.abs
  • Line 464: abs → math.abs
  • Line 464: abs → math.abs
  • Line 465: iff(c, a, b) → (c ? a : b)
  • Line 465: abs → math.abs
  • Line 466: abs → math.abs
  • Line 466: abs → math.abs
  • Line 468: iff(c, a, b) → (c ? a : b)
  • Line 469: iff(c, a, b) → (c ? a : b)
  • Line 470: iff(c, a, b) → (c ? a : b)
  • Line 471: iff(c, a, b) → (c ? a : b)
  • Line 476: abs → math.abs
  • Line 479: iff(c, a, b) → (c ? a : b)
  • Line 479: abs → math.abs
  • Line 480: iff(c, a, b) → (c ? a : b)
  • Line 480: abs → math.abs
  • Line 481: iff(c, a, b) → (c ? a : b)
  • Line 481: abs → math.abs
  • Line 482: abs → math.abs
  • Line 482: abs → math.abs
  • Line 483: iff(c, a, b) → (c ? a : b)
  • Line 483: abs → math.abs
  • Line 484: abs → math.abs
  • Line 484: abs → math.abs
  • Line 486: iff(c, a, b) → (c ? a : b)
  • Line 487: iff(c, a, b) → (c ? a : b)
  • Line 488: iff(c, a, b) → (c ? a : b)
  • Line 489: iff(c, a, b) → (c ? a : b)
⋯// © Xaviz-//@version=4+//@version=5strategy(title = "888 BOT #backtest", shorttitle = "888💹", overlay = true, initial_capital = 10000, pyramiding = 10, currency = "USD",⋯// ————— Source input-src                 = input(hlc3,       title = "  SOURCE",                             type = input.source)+src                 = input.source(hlc3,       title = "  SOURCE")// ————— JMA inputs-Act_JMA             = input(true,       title = "JURIK MOVING AVERAGE",                 type = input.bool)+Act_JMA             = input.bool(true,       title = "JURIK MOVING AVERAGE")-JMA_length          = input(30,         title = "  JMA LENGTH",                         type = input.integer,   minval = 0)+JMA_length          = input.int(30,         title = "  JMA LENGTH",   minval = 0)-phase               = input(40,         title = "  JMA PHASE",                          type = input.integer,   minval = 0)+phase               = input.int(40,         title = "  JMA PHASE",   minval = 0)-power               = input(2.5,        title = "  JMA POWER",                          type = input.float,     minval = 0,     step = 0.5)+power               = input.float(2.5,        title = "  JMA POWER",     minval = 0,     step = 0.5)// ————— Range Filter inputs-Act_RF              = input(true,       title = "RANGE FILTER",                         type = input.bool)+Act_RF              = input.bool(true,       title = "RANGE FILTER")-per                 = input(20,         title = "  SAMPLING PERIOD",                    type = input.integer,   minval = 1)+per                 = input.int(20,         title = "  SAMPLING PERIOD",   minval = 1)-mult                = input(1.7,        title = "  RANGE MULTIPLIER",                   type = input.float,     minval = 0.1,   step = 0.1)+mult                = input.float(1.7,        title = "  RANGE MULTIPLIER",     minval = 0.1,   step = 0.1)// ————— ADX inputs-Act_ADX             = input(true,       title = "AVERAGE DIRECTIONAL INDEX",            type = input.bool)+Act_ADX             = input.bool(true,       title = "AVERAGE DIRECTIONAL INDEX")ADX_options         = input("CLASSIC",  title = "  ADX OPTION",                                                                                 options = ["CLASSIC", "MASANAKAMURA"])-ADX_len             = input(22,         title = "  ADX LENGTH",                         type = input.integer,   minval = 1)+ADX_len             = input.int(22,         title = "  ADX LENGTH",   minval = 1)-th                  = input(20,         title = "  ADX THRESHOLD",                      type = input.float,     minval = 0,     step = 0.5)+th                  = input.float(20,         title = "  ADX THRESHOLD",     minval = 0,     step = 0.5)// ————— SAR inputs-Act_SAR             = input(true,       title = "PARABOLIC SAR",                        type = input.bool)+Act_SAR             = input.bool(true,       title = "PARABOLIC SAR")-Sst                 = input (0.25,      title = "  SAR STAR",                           type = input.float,     minval = 0.01,  step = 0.01)+Sst                 = input.float (0.25,      title = "  SAR STAR",     minval = 0.01,  step = 0.01)-Sinc                = input (0.25,      title = "  SAR INC",                            type = input.float,     minval = 0.01,  step = 0.01)+Sinc                = input.float (0.25,      title = "  SAR INC",     minval = 0.01,  step = 0.01)-Smax                = input (0.13,      title = "  SAR MAX",                            type = input.float,     minval = 0.01,  step = 0.01)+Smax                = input.float (0.13,      title = "  SAR MAX",     minval = 0.01,  step = 0.01)// ————— RSI with volume inputs-Act_RSI             = input(true,       title = "RSI VOLUME WEIGHTED",                  type = input.bool)+Act_RSI             = input.bool(true,       title = "RSI VOLUME WEIGHTED")-RSI_len             = input(34,         title = "  RSI LENGHT",                         type = input.integer,   minval = 1)+RSI_len             = input.int(34,         title = "  RSI LENGHT",   minval = 1)-RSI_obos            = input(45,         title = "  RSI CENTER LINE",                    type = input.integer,   minval = 1)+RSI_obos            = input.int(45,         title = "  RSI CENTER LINE",   minval = 1)// ————— MACD / MAC-Z inputs-Act_MACD            = input(true,       title = "MA CONVERGENCE/DIVERGENCE",            type = input.bool)+Act_MACD            = input.bool(true,       title = "MA CONVERGENCE/DIVERGENCE")MACD_options        = input("MAC-Z",    title = "  MACD OPTION",                                                                                options = ["MACD", "MAC-Z"])-fastLength          = input(45,         title = "  MACD FAST MA LENGTH",                type = input.integer,   minval = 1)+fastLength          = input.int(45,         title = "  MACD FAST MA LENGTH",   minval = 1)-slowLength          = input(47,         title = "  MACD SLOW MA LENGTH",                type = input.integer,   minval = 1)+slowLength          = input.int(47,         title = "  MACD SLOW MA LENGTH",   minval = 1)-signalLength        = input(13,         title = "  MACD SIGNAL LENGTH",                 type = input.integer,   minval = 1)+signalLength        = input.int(13,         title = "  MACD SIGNAL LENGTH",   minval = 1)-lengthz             = input(9,          title = "  Z-VWAP LENGTH",                      type = input.integer,   minval = 1)+lengthz             = input.int(9,          title = "  Z-VWAP LENGTH",   minval = 1)-lengthStdev         = input(14,         title = "  STDEV LENGTH",                       type = input.integer,   minval = 1)+lengthStdev         = input.int(14,         title = "  STDEV LENGTH",   minval = 1)// ————— Volume inputs for entries condition and for calculate quantities later-Act_Vol             = input(true,       title = "VOLUME CONDITION",                     type = input.bool)+Act_Vol             = input.bool(true,       title = "VOLUME CONDITION")-volume_f            = input(1.4,        title = "  VOLUME FACTOR",                      type = input.float,     minval = 0,     step = 0.1)+volume_f            = input.float(1.4,        title = "  VOLUME FACTOR",     minval = 0,     step = 0.1)-sma_length          = input(61,         title = "  SMA VOLUME LENGTH",                  type = input.integer,   minval = 1)+sma_length          = input.int(61,         title = "  SMA VOLUME LENGTH",   minval = 1)// ————— First take profit input-tp_long0            = input(1.7,        title = "  TAKE PROFIT LONG %",                 type = input.float,     minval = 0,     step = 0.1) +tp_long0            = input.float(1.7,        title = "  TAKE PROFIT LONG %",     minval = 0,     step = 0.1) -tp_short0           = input(1.8,        title = "  TAKE PROFIT SHORT %",                type = input.float,     minval = 0,     step = 0.1) +tp_short0           = input.float(1.8,        title = "  TAKE PROFIT SHORT %",     minval = 0,     step = 0.1) // ————— Stop Loss input-Act_sl              = input(true,       title = "ACTIVATE STOP LOSS 🧻",                type = input.bool)+Act_sl              = input.bool(true,       title = "ACTIVATE STOP LOSS 🧻")SL_options          = input("NORMAL",   title = "  STOP LOSS OPTION",                                                                           options = ["NORMAL", "ATR", "BOTH"])-sl0                 = input(3.7,        title = "  STOP LOSS %",                        type = input.float,     minval = 0,     step = 0.1)+sl0                 = input.float(3.7,        title = "  STOP LOSS %",     minval = 0,     step = 0.1)// ————— ATR Inputs-atrPeriod           = input(13,         title = "  ATR SL PERIOD",                      type = input.integer,   minval = 0)+atrPeriod           = input.int(13,         title = "  ATR SL PERIOD",   minval = 0)-multiplierPeriod    = input(7.0,        title = "  ATR SL MULTIPLIER",                  type = input.float,     minval = 0,     step = 0.1)+multiplierPeriod    = input.float(7.0,        title = "  ATR SL MULTIPLIER",     minval = 0,     step = 0.1)// ————— Risk input-Risk                = input(3.5,        title = "  % RISK ALLOWED",                     type = input.float,     minval = 0,     step = 0.5)+Risk                = input.float(3.5,        title = "  % RISK ALLOWED",     minval = 0,     step = 0.5)// ————— Confirmed Stop loss-Act_Conf_SL         = input(false,      title = "STOP LOSS CONFIRMED",                  type = input.bool)+Act_Conf_SL         = input.bool(false,      title = "STOP LOSS CONFIRMED")// ————— Bollinger Bands inputs-Act_BB              = input(true,       title = "ACTIVATE BOLLINGER BANDS RE-ENTRY 🚀", type = input.bool)+Act_BB              = input.bool(true,       title = "ACTIVATE BOLLINGER BANDS RE-ENTRY 🚀")-BB_length           = input(20,         title = "  BB LENGTH",                          type = input.integer,   minval = 1)+BB_length           = input.int(20,         title = "  BB LENGTH",   minval = 1)-BB_mult             = input(1.9,        title = "  BB MULTIPLIER",                      type = input.float,     minval = 0.001, step = 0.1)+BB_mult             = input.float(1.9,        title = "  BB MULTIPLIER",     minval = 0.001, step = 0.1)-bbBetterPrice       = input(0.5,        title = "  % MINIMUM BETTER PRICE",             type = input.float,     minval = 0.1,   step = 0.1)+bbBetterPrice       = input.float(0.5,        title = "  % MINIMUM BETTER PRICE",     minval = 0.1,   step = 0.1)-Act_divide          = input(false,      title = "ACTIVATE DIVIDE TP",                   type = input.bool)+Act_divide          = input.bool(false,      title = "ACTIVATE DIVIDE TP")// ————— Backtest input-Act_BT              = input(true,       title = "BACKTEST 💹",                          type = input.bool)+Act_BT              = input.bool(true,       title = "BACKTEST 💹")-backtest_time       = input(180,        title = "  BACKTEST DAYS",                      type = input.integer,   minval = 1)*24*60*60*1000+backtest_time       = input.int(180,        title = "  BACKTEST DAYS",   minval = 1)*24*60*60*1000entry_Type          = input("% EQUITY", title = "  ENTRY TYPE",                                                                                 options = ["CONTRACTS","CASH","% EQUITY"])et_Factor           = (entry_Type == "CONTRACTS") ? 1 : (entry_Type == "% EQUITY") ? (100/(strategy.equity/close)) : close-quanTity            = input(8.0,        title = "  QUANTITY (LEVERAGE 1X)",             type = input.float,     minval = 0,     step = 0.5) / et_Factor+quanTity            = input.float(8.0,        title = "  QUANTITY (LEVERAGE 1X)",     minval = 0,     step = 0.5) / et_Factor-Max_Lev             = input(8,          title = "  MAXIMUM LEVERAGE",                   type = input.integer,   minval = 1,     maxval = 8)   +Max_Lev             = input.int(8,          title = "  MAXIMUM LEVERAGE",   minval = 1,     maxval = 8)   // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //⋯    beta            = 0.45 * (_JMA_length - 1) / (0.45 * (_JMA_length - 1) + 2)-    alpha           = pow(beta, _power)+    alpha           = math.pow(beta, _power)    jma             = 0.0⋯    e2              = 0.0-    e2              := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])+    e2              := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])    jma             := e2 + nz(jma[1])⋯    wper            = (_per*2) - 1-    avrng           = ema(abs(_src - _src[1]), _per)+    avrng           = ta.ema(math.abs(_src - _src[1]), _per)-    _smoothrng      = ema(avrng, wper) * _mult+    _smoothrng      = ta.ema(avrng, wper) * _mult    _filt           = _src⋯filtplot            = plot(Act_RF ? filt  : na, color = filtcolor,  linewidth = 1,                  title = "RF")-hbandplot           = plot(Act_RF ? hband : na, color = filtcolor,                  transp = 50,    title = "RF High Target")+hbandplot           = plot(Act_RF ? hband : na, color = color.new(filtcolor, 50),    title = "RF High Target")-lbandplot           = plot(Act_RF ? lband : na, color = filtcolor,                  transp = 50,    title = "RF Low Target")+lbandplot           = plot(Act_RF ? lband : na, color = color.new(filtcolor, 50),    title = "RF Low Target")fill(hbandplot, lbandplot,                      color = filtcolor,                                  title = "RF Target Range")⋯calcADX(_len) =>-    up              = change(high)+    up              = ta.change(high)-    down            = -change(low)+    down            = -ta.change(low)    plusDM          = na(up)   ? na : (up > down and up > 0   ? up   : 0)    minusDM         = na(down) ? na : (down > up and down > 0 ? down : 0)-    truerange       = rma(tr, _len)+    truerange       = ta.rma(ta.tr, _len)-    _plus           = fixnan(100 * rma(plusDM, _len)  / truerange)+    _plus           = fixnan(100 * ta.rma(plusDM, _len)  / truerange)-    _minus          = fixnan(100 * rma(minusDM, _len) / truerange)+    _minus          = fixnan(100 * ta.rma(minusDM, _len) / truerange)    sum             = _plus + _minus-    _adx            = 100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)+    _adx            = 100 * ta.rma(math.abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)    [_plus,_minus,_adx]⋯    SmoothedDirectionalMovementMinus    = 0.0-    TrueRange                           = max(max(high - low, abs(high - nz(close[1]))), abs(low - nz(close[1])))+    TrueRange                           = math.max(math.max(high - low, math.abs(high - nz(close[1]))), math.abs(low - nz(close[1])))-    DirectionalMovementPlus             = high - nz(high[1]) > nz(low[1]) - low ? max(high - nz(high[1]), 0) : 0+    DirectionalMovementPlus             = high - nz(high[1]) > nz(low[1]) - low ? math.max(high - nz(high[1]), 0) : 0-    DirectionalMovementMinus            = nz(low[1]) - low > high - nz(high[1]) ? max(nz(low[1]) - low, 0)   : 0+    DirectionalMovementMinus            = nz(low[1]) - low > high - nz(high[1]) ? math.max(nz(low[1]) - low, 0)   : 0    SmoothedTrueRange                   := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1]) /_len) + TrueRange⋯    DIM                                 = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100-    DX                                  = abs(DIP-DIM) / (DIP+DIM)*100+    DX                                  = math.abs(DIP-DIM) / (DIP+DIM)*100-    adx                                 = sma(DX, _len)+    adx                                 = ta.sma(DX, _len)    [DIP,DIM,adx]⋯// ————— SAR calculation from TV-SAR = sar(Sst, Sinc, Smax)+SAR = ta.sar(Sst, Sinc, Smax)// ————— SAR Plotting⋯RSI_Volume(fv, _length) =>  -    up              = iff(fv > fv[1], abs(fv - fv[1]) * volume, 0)+    up              = (fv > fv[1] ? math.abs(fv - fv[1]) * volume : 0)-    dn              = iff(fv < fv[1], abs(fv - fv[1]) * volume, 0)+    dn              = (fv < fv[1] ? math.abs(fv - fv[1]) * volume : 0)    upt             = WiMA(up,_length)⋯calc_zvwap(pds) =>-    mean            = sum(volume * close, pds) / sum(volume, pds)+    mean            = math.sum(volume * close, pds) / math.sum(volume, pds)-    vwapsd          = sqrt(sma(pow(close - mean, 2), pds))+    vwapsd          = math.sqrt(ta.sma(math.pow(close - mean, 2), pds))    (close - mean ) / vwapsdzscore              = calc_zvwap(lengthz)-fastMA              = sma(src, fastLength)+fastMA              = ta.sma(src, fastLength)-slowMA              = sma(src, slowLength)+slowMA              = ta.sma(src, slowLength)macd                = fastMA - slowMA-macz                = zscore + macd / stdev(src, lengthStdev)+macz                = zscore + macd / ta.stdev(src, lengthStdev)-signal              = sma(macz, signalLength)+signal              = ta.sma(macz, signalLength)histmacz            = macz - signal// ————— MACD calculation-[_,_,histmacd]      = macd(src,  fastLength, slowLength, signalLength)+[_,_,histmacd]      = ta.macd(src,  fastLength, slowLength, signalLength)hist                = MACD_options == "MACD" ? histmacd : histmacz⋯MACD_longCond       := (Act_MACD    ? (hist > 0)                                    : RSI_longCond)-VOL_longCond        := (Act_Vol     ? (volume > sma(volume,sma_length) * volume_f)  : MACD_longCond)+VOL_longCond        := (Act_Vol     ? (volume > ta.sma(volume,sma_length) * volume_f)  : MACD_longCond)// ————— All indicators with short conditions and enable/disable option⋯MACD_shortCond      := (Act_MACD    ? (hist < 0)                                    : RSI_shortCond)-VOL_shortCond       := (Act_Vol     ? (volume > sma(volume,sma_length) * volume_f)  : MACD_shortCond)+VOL_shortCond       := (Act_Vol     ? (volume > ta.sma(volume,sma_length) * volume_f)  : MACD_shortCond)// ————— Defining long/short condition from indicators + volume⋯// ————— Stop Loss ATR calculation-ATR_SL_Long         = low  - atr(atrPeriod) * multiplierPeriod+ATR_SL_Long         = low  - ta.atr(atrPeriod) * multiplierPeriod-ATR_SL_Short        = high + atr(atrPeriod) * multiplierPeriod+ATR_SL_Short        = high + ta.atr(atrPeriod) * multiplierPeriodlongStopPrev        = nz(ATR_SL_Long[1], ATR_SL_Long)shortStopPrev       = nz(ATR_SL_Short[1], ATR_SL_Short)-ATR_SL_Long         := close[1] > longStopPrev  ? max(ATR_SL_Long, longStopPrev)   : ATR_SL_Long+ATR_SL_Long         := close[1] > longStopPrev  ? math.max(ATR_SL_Long, longStopPrev)   : ATR_SL_Long-ATR_SL_Short        := close[1] < shortStopPrev ? min(ATR_SL_Short, shortStopPrev) : ATR_SL_Short+ATR_SL_Short        := close[1] < shortStopPrev ? math.min(ATR_SL_Short, shortStopPrev) : ATR_SL_Short// ————— Calculating Sl according Risk and Initial Capitalsl = in_longCondition ?      -     min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * max(1, last_dynamic_Leverage_long)  * max(1, nLongs)))) : +     math.min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * math.max(1, last_dynamic_Leverage_long)  * math.max(1, nLongs)))) : -     min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * max(1, last_dynamic_Leverage_short) * max(1, nShorts))))+     math.min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * math.max(1, last_dynamic_Leverage_short) * math.max(1, nShorts))))      ⋯// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-BB_basis            = sma(src, BB_length)+BB_basis            = ta.sma(src, BB_length)-BB_dev              = BB_mult  * stdev(src, BB_length)+BB_dev              = BB_mult  * ta.stdev(src, BB_length)BB_upper            = BB_basis + BB_dev⋯l_BB                = plot(Act_BB ? BB_lower : na,  title = "Lower Bollinger Band",      color = #f06292, linewidth = 2)-fill(u_BB, l_BB,                                    title = "Bollinger Band Background", color = in_longCondition ? #009688 : #f06292, transp = 95)+fill(u_BB, l_BB,                                    title = "Bollinger Band Background", color = color.new(in_longCondition ? #009688 : #f06292, 95))// ————— Initial Bollinger Bands conditions⋯W_color             = (dayofweek == dayofweek.sunday or dayofweek == dayofweek.saturday) ? color.white : na-bgcolor(W_color,          title = "Weekend",    transp = 95)+bgcolor(color.new(W_color, 95),          title = "Weekend")// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //⋯// ————— Volume Factor for determine quantities-Volume_Factor_Leverage      = min(Max_Lev, max(1, round(volume / sma(volume, sma_length))))+Volume_Factor_Leverage      = math.min(Max_Lev, math.max(1, math.round(volume / ta.sma(volume, sma_length))))last_dynamic_Leverage_long  := _longCondition ? Volume_Factor_Leverage  : nz(last_dynamic_Leverage_long[1])⋯strategy.exit("Tpl", "long", -   profit   = (abs((last_open_longCondition  * (1 + tp_long)) - last_open_longCondition) / syminfo.mintick), +   profit   = (math.abs((last_open_longCondition  * (1 + tp_long)) - last_open_longCondition) / syminfo.mintick),    limit    = nLongs >= 1 ? strategy.position_avg_price * (1 + tp_long) : na,   loss     = Act_Conf_SL == false ? -             (iff(Act_sl and (SL_options == "NORMAL"), (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick), -             iff(Act_sl  and (SL_options == "ATR"), (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick), -             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) < +             ((Act_sl and (SL_options == "NORMAL") ? (math.abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "ATR") ? (math.abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "BOTH") and ((math.abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) < -             (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)), (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick),-             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) > +             (math.abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)) ? (math.abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "BOTH") and ((math.abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) > -             (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)), (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick), na))))) : na,+             (math.abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)) ? (math.abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick) : na))))) : na,   stop     = Act_Conf_SL == false and nLongs >= 1 ? -             (iff(Act_sl and (SL_options == "NORMAL"), ((1-(sl/100))*strategy.position_avg_price),-             iff(Act_sl  and (SL_options == "ATR"), ATR_SL_Long, -             iff(Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) > ATR_SL_Long), ((1-(sl/100))*strategy.position_avg_price), -             iff(Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) < ATR_SL_Long), ATR_SL_Long, na))))) : na)+             ((Act_sl and (SL_options == "NORMAL") ? ((1-(sl/100))*strategy.position_avg_price) : (Act_sl  and (SL_options == "ATR") ? ATR_SL_Long : (Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) > ATR_SL_Long) ? ((1-(sl/100))*strategy.position_avg_price) : (Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) < ATR_SL_Long) ? ATR_SL_Long : na))))) : na)// Canceling long exit orders to avoid simultaneity with re-entry⋯strategy.exit("Tps", "short",-   profit   = (abs((last_open_shortCondition * (1 - tp_short)) - last_open_shortCondition) / syminfo.mintick), +   profit   = (math.abs((last_open_shortCondition * (1 - tp_short)) - last_open_shortCondition) / syminfo.mintick),    limit    = nShorts >= 1 ? strategy.position_avg_price*(1-(tp_short)) : na,   loss     = Act_Conf_SL == false ? -             (iff(Act_sl and (SL_options == "NORMAL"), (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick), -             iff(Act_sl  and (SL_options == "ATR"), (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick), -             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) < +             ((Act_sl and (SL_options == "NORMAL") ? (math.abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "ATR") ? (math.abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "BOTH") and ((math.abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) < -             (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)), (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick),-             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) > +             (math.abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)) ? (math.abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) : (Act_sl  and (SL_options == "BOTH") and ((math.abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) > -             (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)), (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick), na))))) : na,+             (math.abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)) ? (math.abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick) : na))))) : na,   stop     = Act_Conf_SL == false and nShorts >= 1 ? -             (iff(Act_sl and (SL_options == "NORMAL"), ((1+(sl/100))*strategy.position_avg_price),-             iff(Act_sl  and (SL_options == "ATR"), ATR_SL_Short, -             iff(Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) < ATR_SL_Short), ((1+(sl/100))*strategy.position_avg_price),-             iff(Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) > ATR_SL_Short), ATR_SL_Short, na))))) : na)+             ((Act_sl and (SL_options == "NORMAL") ? ((1+(sl/100))*strategy.position_avg_price) : (Act_sl  and (SL_options == "ATR") ? ATR_SL_Short : (Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) < ATR_SL_Short) ? ((1+(sl/100))*strategy.position_avg_price) : (Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) > ATR_SL_Short) ? ATR_SL_Short : na))))) : na)// Canceling short exit orders to avoid simultaneity with re-entry⋯

Clean v5

VerifiedDownload the v5 script

Same 12 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 22: input() → input.string()
  • Line 36: input() → input.string()
  • Line 51: input() → input.string()
  • Line 69: input() → input.string()
  • Line 447: when = Act_BT and testPeriod → if block
  • Line 449: when = Act_BT and testPeriod → if block
5 more edits
  • Line 453: when = Act_BT and testPeriod → if block
  • Line 455: when = Act_BT and testPeriod → if block
  • Line 467: when = Final_long_BB → if block
  • Line 479: when = Final_short_BB → if block
  • Line 481: when = (Final_Long_sl and Act_Conf_SL) or (Final_Short_sl and Act_Conf_SL) → if block
⋯Act_ADX             = input.bool(true,       title = "AVERAGE DIRECTIONAL INDEX")-ADX_options         = input("CLASSIC",  title = "  ADX OPTION",                                                                                 options = ["CLASSIC", "MASANAKAMURA"])+ADX_options         = input.string("CLASSIC",  title = "  ADX OPTION",                                                                                 options = ["CLASSIC", "MASANAKAMURA"])ADX_len             = input.int(22,         title = "  ADX LENGTH",   minval = 1)⋯Act_MACD            = input.bool(true,       title = "MA CONVERGENCE/DIVERGENCE")-MACD_options        = input("MAC-Z",    title = "  MACD OPTION",                                                                                options = ["MACD", "MAC-Z"])+MACD_options        = input.string("MAC-Z",    title = "  MACD OPTION",                                                                                options = ["MACD", "MAC-Z"])fastLength          = input.int(45,         title = "  MACD FAST MA LENGTH",   minval = 1)⋯Act_sl              = input.bool(true,       title = "ACTIVATE STOP LOSS 🧻")-SL_options          = input("NORMAL",   title = "  STOP LOSS OPTION",                                                                           options = ["NORMAL", "ATR", "BOTH"])+SL_options          = input.string("NORMAL",   title = "  STOP LOSS OPTION",                                                                           options = ["NORMAL", "ATR", "BOTH"])sl0                 = input.float(3.7,        title = "  STOP LOSS %",     minval = 0,     step = 0.1)⋯backtest_time       = input.int(180,        title = "  BACKTEST DAYS",   minval = 1)*24*60*60*1000-entry_Type          = input("% EQUITY", title = "  ENTRY TYPE",                                                                                 options = ["CONTRACTS","CASH","% EQUITY"])+entry_Type          = input.string("% EQUITY", title = "  ENTRY TYPE",                                                                                 options = ["CONTRACTS","CASH","% EQUITY"])et_Factor           = (entry_Type == "CONTRACTS") ? 1 : (entry_Type == "% EQUITY") ? (100/(strategy.equity/close)) : close⋯if (_longCondition)-    strategy.entry("long", strategy.long, qty = Volume_Factor_Leverage * quanTity, when = Act_BT and testPeriod)+    if Act_BT and testPeriod+        strategy.entry("long", strategy.long, qty = Volume_Factor_Leverage * quanTity)if (Final_long_BB)-    strategy.entry("long", strategy.long, qty = last_dynamic_Leverage_long * quanTity, when = Act_BT and testPeriod)+    if Act_BT and testPeriod+        strategy.entry("long", strategy.long, qty = last_dynamic_Leverage_long * quanTity)   ⋯if (_shortCondition) -    strategy.entry("short", strategy.short, qty = Volume_Factor_Leverage * quanTity, when = Act_BT and testPeriod)+    if Act_BT and testPeriod+        strategy.entry("short", strategy.short, qty = Volume_Factor_Leverage * quanTity)if (Final_short_BB) -    strategy.entry("short", strategy.short, qty = last_dynamic_Leverage_short * quanTity, when = Act_BT and testPeriod)+    if Act_BT and testPeriod+        strategy.entry("short", strategy.short, qty = last_dynamic_Leverage_short * quanTity)// ————— Closing positions with first long TP⋯// Canceling long exit orders to avoid simultaneity with re-entry-strategy.cancel("Tpl", when = Final_long_BB)+if Final_long_BB+    strategy.cancel("Tpl")// ————— Closing positions with first short TP⋯// Canceling short exit orders to avoid simultaneity with re-entry-strategy.cancel("Tps", when = Final_short_BB)+if Final_short_BB+    strategy.cancel("Tps")// ————— Closing all positions with Xlong/Xshort-strategy.close_all(when = (Final_Long_sl and Act_Conf_SL) or (Final_Short_sl and Act_Conf_SL))+if (Final_Long_sl and Act_Conf_SL) or (Final_Short_sl and Act_Conf_SL)+    strategy.close_all()// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //⋯

Does it run in Pine Script v6?

No

The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L77:C0: a 'bool' cannot be 'na' in Pine Script v6 — declare it as true or false. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// © Xaviz-//@version=5+//@version=6strategy(title = "888 BOT #backtest", shorttitle = "888💹", overlay = true, initial_capital = 10000, pyramiding = 10, currency = "USD",⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
SOURCEhlc3sourceJMA_Rising JMA_Falling smoothrng filt upward downward RSI_V fastMA slowMA macz histmacd BB_basis BB_dev JMA _
JURIK MOVING AVERAGEtrueboolJMA_longCond JMA_shortCond JMA
JMA LENGTH30int (written input.integer) · min 0JMA_Rising JMA_Falling JMA
JMA PHASE40int (written input.integer) · min 0JMA_Rising JMA_Falling JMA
JMA POWER2.5float · min 0 · step 0.5JMA_Rising JMA_Falling JMA
RANGE FILTERtrueboolRF_longCond RF_shortCond RF RF High Target RF Low Target
SAMPLING PERIOD20int (written input.integer) · min 1smoothrng filt upward downward
RANGE MULTIPLIER1.7float · min 0.1 · step 0.1smoothrng filt upward downward
AVERAGE DIRECTIONAL INDEXtrueboolADX_longCond ADX_shortCond ADX
ADX OPTIONCLASSICstring · options ["CLASSIC", "MASANAKAMURA"]DIPlus DIMinus ADX
ADX LENGTH22int (written input.integer) · min 1DIPlusC DIMinusC ADXC DIPlusM DIMinusM ADXM
ADX THRESHOLD20float · min 0 · step 0.5ADX_longCond ADX_shortCond ADX_color BB_long BB_short
PARABOLIC SARtrueboolSAR_longCond SAR_shortCond SAR
SAR STAR0.25float · min 0.01 · step 0.01SAR
SAR INC0.25float · min 0.01 · step 0.01SAR
SAR MAX0.13float · min 0.01 · step 0.01SAR
RSI VOLUME WEIGHTEDtrueboolRSI_longCond RSI_shortCond
RSI LENGHT34int (written input.integer) · min 1RSI_V
RSI CENTER LINE45int (written input.integer) · min 1RSI_longCond RSI_shortCond
MA CONVERGENCE/DIVERGENCEtrueboolMACD_longCond MACD_shortCond
MACD OPTIONMAC-Zstring · options ["MACD", "MAC-Z"]hist
MACD FAST MA LENGTH45int (written input.integer) · min 1fastMA histmacd _
MACD SLOW MA LENGTH47int (written input.integer) · min 1slowMA histmacd _
MACD SIGNAL LENGTH13int (written input.integer) · min 1signal histmacd _
Z-VWAP LENGTH9int (written input.integer) · min 1zscore
STDEV LENGTH14int (written input.integer) · min 1macz
VOLUME CONDITIONtrueboolVOL_longCond VOL_shortCond
VOLUME FACTOR1.4float · min 0 · step 0.1VOL_longCond VOL_shortCond
SMA VOLUME LENGTH61int (written input.integer) · min 1VOL_longCond VOL_shortCond Volume_Factor_Leverage
TAKE PROFIT LONG %1.7float · min 0 · step 0.1tp_long
TAKE PROFIT SHORT %1.8float · min 0 · step 0.1tp_short
ACTIVATE STOP LOSS 🧻trueboolNormal_long_sl ATR_long_sl Both_long_sl Normal_short_sl ATR_short_sl Both_short_sl ATR SL exit Tpl exit Tps
STOP LOSS OPTIONNORMALstring · options ["NORMAL", "ATR", "BOTH"]Normal_long_sl ATR_long_sl Both_long_sl Normal_short_sl ATR_short_sl Both_short_sl ATR SL exit Tpl exit Tps
STOP LOSS %3.7float · min 0 · step 0.1sl
ATR SL PERIOD13int (written input.integer) · min 0ATR_SL_Long ATR_SL_Short
ATR SL MULTIPLIER7.0float · min 0 · step 0.1ATR_SL_Long ATR_SL_Short
% RISK ALLOWED3.5float · min 0 · step 0.5sl
STOP LOSS CONFIRMEDfalseboolNormal_long_sl ATR_long_sl Both_long_sl Normal_short_sl ATR_short_sl Both_short_sl exit Tpl exit Tps close_all
ACTIVATE BOLLINGER BANDS RE-ENTRY 🚀trueboolUpper Bollinger Band Lower Bollinger Band BB_long BB_short
BB LENGTH20int (written input.integer) · min 1BB_basis BB_dev
BB MULTIPLIER1.9float · min 0.001 · step 0.1BB_dev
% MINIMUM BETTER PRICE0.5float · min 0.1 · step 0.1BB_long BB_short
ACTIVATE DIVIDE TPfalsebooltp_long tp_short
BACKTEST 💹trueboolentry long entry short
BACKTEST DAYS180int (written input.integer) · min 1testPeriod
ENTRY TYPE% EQUITYstring · options ["CONTRACTS","CASH","% EQUITY"]et_Factor
QUANTITY (LEVERAGE 1X)8.0float · min 0 · step 0.5sl entry long entry short
MAXIMUM LEVERAGE8int (written input.integer) · min 1 · max 8Volume_Factor_Leverage

What it calculates

  • SMA (sma, ta.sma in v5)volume, SMA VOLUME LENGTH (61)VOL_longCond · line 253
  • SMA (sma, ta.sma in v5)volume, SMA VOLUME LENGTH (61)VOL_shortCond · line 261
  • EMA (ema, ta.ema in v5)abs(_src - _src[1]), _peravrng · line 143
  • EMA (ema, ta.ema in v5)avrng, wper_smoothrng · line 144
  • Change (change, ta.change in v5)lowdown · line 168
  • RMA (rma, ta.rma in v5)tr, _lentruerange · line 171
  • RMA (rma, ta.rma in v5)plusDM, _len_plus · line 172
  • RMA (rma, ta.rma in v5)minusDM, _len_minus · line 173
  • RMA (rma, ta.rma in v5)abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len_adx · line 175
  • SMA (sma, ta.sma in v5)DX, _lenadx · line 191
  • SAR (sar, ta.sar in v5) SAR STAR (0.25), SAR INC (0.25), SAR MAX (0.13)SAR · line 206
  • SMA (sma, ta.sma in v5)pow(close - mean, 2), pdsvwapsd · line 231
  • SMA (sma, ta.sma in v5) SOURCE (hlc3), MACD FAST MA LENGTH (45)fastMA · line 234
  • SMA (sma, ta.sma in v5) SOURCE (hlc3), MACD SLOW MA LENGTH (47)slowMA · line 235
  • Stdev (stdev, ta.stdev in v5) SOURCE (hlc3), STDEV LENGTH (14)macz · line 237
  • SMA (sma, ta.sma in v5)macz, MACD SIGNAL LENGTH (13)signal · line 238
  • MACD (macd, ta.macd in v5) SOURCE (hlc3), MACD FAST MA LENGTH (45), MACD SLOW MA LENGTH (47), MACD SIGNAL LENGTH (13)histmacd · line 241
  • ATR (atr, ta.atr in v5) ATR SL PERIOD (13)ATR_SL_Long · line 325
  • ATR (atr, ta.atr in v5) ATR SL PERIOD (13)ATR_SL_Short · line 326
  • SMA (sma, ta.sma in v5) SOURCE (hlc3), BB LENGTH (20)BB_basis · line 365
  • Stdev (stdev, ta.stdev in v5) SOURCE (hlc3), BB LENGTH (20)BB_dev · line 366
  • SMA (sma, ta.sma in v5)volume, SMA VOLUME LENGTH (61)Volume_Factor_Leverage · line 442

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 191 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.

Order rules

OrderFires whenBy defaultLine
Open long longAct_BT and testPeriodOn447
Open long longAct_BT and testPeriodOn449
Open short shortAct_BT and testPeriodOn453
Open short shortAct_BT and testPeriodOn455
Exit Tpl stop and limitevery barOn457
cancel TplFinal_long_BBBB_long and last_Final_longCondition > nz(last_long_BB[1]) and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1]) and not Final_Long_slOn473
Exit Tps stop and limitevery barOn475
cancel TpsFinal_short_BBBB_short and last_Final_shortCondition > nz(last_short_BB[1]) and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]) and not Final_Short_slOn491
Close (Final_Long_sl and Act_Conf_SL) or (Final_Short_sl and Act_Conf_SL)On493

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.

12/12trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+3.0%+102.5%1292%5.730.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.4%+5.5%2934%0.922.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.6%+9.8%875%1.510.9%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.4%+8.4%3139%0.902.9%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.2%+16.7%667%0.831.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+3.0%+21.5%3471%1.571.9%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−2.0%+148.3%520%0.062.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−3.1%+17.1%2857%0.633.4%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 barsTradesWonP&L
Rising market250%−$35.85
Sideways market10100%$331.35
Falling market0—$0.00

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

0.6%

The 95th percentile of the worst drawdown when the same 12 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.6% to 0.6%; the tick is what actually happened, 0.6%. Order luck alone moves it this much.

0%1%
All 12 trades
SideEntryExitBarsP&L
short4 Jun 2026 64,142.804 Jun 2026 62,978.900$27.76
short5 Jun 2026 63,886.005 Jun 2026 62,731.400$41.59
short6 Jun 2026 61,056.506 Jun 2026 59,906.500$43.60
short25 Jun 2026 61,078.0025 Jun 2026 59,979.000$27.83
short26 Jun 2026 59,794.6026 Jun 2026 58,695.200$28.55
short27 Jun 2026 60,097.3028 Jun 2026 59,015.501$28.00
long20 Aug 2026 69,334.8020 Aug 2026 71,198.700$42.54
long21 Aug 2026 73,027.0021 Aug 2026 74,205.700$25.12
long22 Aug 2026 78,338.0024 Aug 2026 79,669.802$39.89
long25 Aug 2026 78,992.8025 Aug 2026 80,324.500$26.47
long22 Sep 2026 86,620.0024 Sep 2026 83,415.102−$62.46
long3 Oct 2026 84,518.004 Oct 2026 85,954.801$26.61

Similar scripts

Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.

ScriptSharesTypeNetTradesMax drawdown
888 BOT #backtest this page—v4 strategy+3.0%120.9%
Strategy - Trend Chaser - PSe by ElvinKennedyHernandezLatayanatr macd rma ema sma stdevv5 strategyNo trades on this market
Contrarian Scalping Counter Trend Bb Envelope Adx and Stochastic by exlux99sma stdev ema rmav5 strategy+1.9%153.2%
Trend Following with Bollinger Bands by Robrecht99sma stdev ema atrv5 strategy+2.5%71.7%
Best TradingView Strategy - For NASDAQ and DOW30 and other Index by The_Bigger_Bullsma stdev rma emav5 strategy+2.2%173.4%
[KL] BOLL + MACD Strategy v2 (published) by DojiEmojisma stdev ema atrv4 strategy−32.1%2854.0%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 181 msWith a workaround 1,000 bars in 446 msNot measured
PineTSNo v5 and v6 only—Not measured

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/// © Xaviz//@version=4strategy(title = "888 BOT #backtest", shorttitle = "888💹", overlay = true, initial_capital = 10000, pyramiding = 10, currency = "USD",   default_qty_type = strategy.percent_of_equity, default_qty_value = 0, commission_value = 0.04)// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Inputs// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Source inputsrc                 = input(hlc3,       title = "  SOURCE",                             type = input.source)// ————— JMA inputsAct_JMA             = input(true,       title = "JURIK MOVING AVERAGE",                 type = input.bool)JMA_length          = input(30,         title = "  JMA LENGTH",                         type = input.integer,   minval = 0)phase               = input(40,         title = "  JMA PHASE",                          type = input.integer,   minval = 0)power               = input(2.5,        title = "  JMA POWER",                          type = input.float,     minval = 0,     step = 0.5)// ————— Range Filter inputsAct_RF              = input(true,       title = "RANGE FILTER",                         type = input.bool)per                 = input(20,         title = "  SAMPLING PERIOD",                    type = input.integer,   minval = 1)mult                = input(1.7,        title = "  RANGE MULTIPLIER",                   type = input.float,     minval = 0.1,   step = 0.1)// ————— ADX inputsAct_ADX             = input(true,       title = "AVERAGE DIRECTIONAL INDEX",            type = input.bool)ADX_options         = input("CLASSIC",  title = "  ADX OPTION",                                                                                 options = ["CLASSIC", "MASANAKAMURA"])ADX_len             = input(22,         title = "  ADX LENGTH",                         type = input.integer,   minval = 1)th                  = input(20,         title = "  ADX THRESHOLD",                      type = input.float,     minval = 0,     step = 0.5)// ————— SAR inputsAct_SAR             = input(true,       title = "PARABOLIC SAR",                        type = input.bool)Sst                 = input (0.25,      title = "  SAR STAR",                           type = input.float,     minval = 0.01,  step = 0.01)Sinc                = input (0.25,      title = "  SAR INC",                            type = input.float,     minval = 0.01,  step = 0.01)Smax                = input (0.13,      title = "  SAR MAX",                            type = input.float,     minval = 0.01,  step = 0.01)// ————— RSI with volume inputsAct_RSI             = input(true,       title = "RSI VOLUME WEIGHTED",                  type = input.bool)RSI_len             = input(34,         title = "  RSI LENGHT",                         type = input.integer,   minval = 1)RSI_obos            = input(45,         title = "  RSI CENTER LINE",                    type = input.integer,   minval = 1)// ————— MACD / MAC-Z inputsAct_MACD            = input(true,       title = "MA CONVERGENCE/DIVERGENCE",            type = input.bool)MACD_options        = input("MAC-Z",    title = "  MACD OPTION",                                                                                options = ["MACD", "MAC-Z"])fastLength          = input(45,         title = "  MACD FAST MA LENGTH",                type = input.integer,   minval = 1)slowLength          = input(47,         title = "  MACD SLOW MA LENGTH",                type = input.integer,   minval = 1)signalLength        = input(13,         title = "  MACD SIGNAL LENGTH",                 type = input.integer,   minval = 1)lengthz             = input(9,          title = "  Z-VWAP LENGTH",                      type = input.integer,   minval = 1)lengthStdev         = input(14,         title = "  STDEV LENGTH",                       type = input.integer,   minval = 1)// ————— Volume inputs for entries condition and for calculate quantities laterAct_Vol             = input(true,       title = "VOLUME CONDITION",                     type = input.bool)volume_f            = input(1.4,        title = "  VOLUME FACTOR",                      type = input.float,     minval = 0,     step = 0.1)sma_length          = input(61,         title = "  SMA VOLUME LENGTH",                  type = input.integer,   minval = 1)// ————— First take profit inputtp_long0            = input(1.7,        title = "  TAKE PROFIT LONG %",                 type = input.float,     minval = 0,     step = 0.1) tp_short0           = input(1.8,        title = "  TAKE PROFIT SHORT %",                type = input.float,     minval = 0,     step = 0.1) // ————— Stop Loss inputAct_sl              = input(true,       title = "ACTIVATE STOP LOSS 🧻",                type = input.bool)SL_options          = input("NORMAL",   title = "  STOP LOSS OPTION",                                                                           options = ["NORMAL", "ATR", "BOTH"])sl0                 = input(3.7,        title = "  STOP LOSS %",                        type = input.float,     minval = 0,     step = 0.1)// ————— ATR InputsatrPeriod           = input(13,         title = "  ATR SL PERIOD",                      type = input.integer,   minval = 0)multiplierPeriod    = input(7.0,        title = "  ATR SL MULTIPLIER",                  type = input.float,     minval = 0,     step = 0.1)// ————— Risk inputRisk                = input(3.5,        title = "  % RISK ALLOWED",                     type = input.float,     minval = 0,     step = 0.5)// ————— Confirmed Stop lossAct_Conf_SL         = input(false,      title = "STOP LOSS CONFIRMED",                  type = input.bool)// ————— Bollinger Bands inputsAct_BB              = input(true,       title = "ACTIVATE BOLLINGER BANDS RE-ENTRY 🚀", type = input.bool)BB_length           = input(20,         title = "  BB LENGTH",                          type = input.integer,   minval = 1)BB_mult             = input(1.9,        title = "  BB MULTIPLIER",                      type = input.float,     minval = 0.001, step = 0.1)bbBetterPrice       = input(0.5,        title = "  % MINIMUM BETTER PRICE",             type = input.float,     minval = 0.1,   step = 0.1)Act_divide          = input(false,      title = "ACTIVATE DIVIDE TP",                   type = input.bool)// ————— Backtest inputAct_BT              = input(true,       title = "BACKTEST 💹",                          type = input.bool)backtest_time       = input(180,        title = "  BACKTEST DAYS",                      type = input.integer,   minval = 1)*24*60*60*1000entry_Type          = input("% EQUITY", title = "  ENTRY TYPE",                                                                                 options = ["CONTRACTS","CASH","% EQUITY"])et_Factor           = (entry_Type == "CONTRACTS") ? 1 : (entry_Type == "% EQUITY") ? (100/(strategy.equity/close)) : closequanTity            = input(8.0,        title = "  QUANTITY (LEVERAGE 1X)",             type = input.float,     minval = 0,     step = 0.5) / et_FactorMax_Lev             = input(8,          title = "  MAXIMUM LEVERAGE",                   type = input.integer,   minval = 1,     maxval = 8)   // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Variables// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Long/Shortvar bool    longCond                    = na,   var bool    shortCond                   = navar int     CondIni_long                = 0,    var int     CondIni_short               = 0var bool    _Final_longCondition        = na,   var bool    _Final_shortCondition       = navar float   last_open_longCondition     = na,   var float   last_open_shortCondition    = navar float   last_dynamic_Leverage_long  = na,   var float   last_dynamic_Leverage_short = navar int     last_longCondition          = na,   var int     last_shortCondition         = navar int     last_Final_longCondition    = na,   var int     last_Final_shortCondition   = navar int     nLongs                      = na,   var int     nShorts                     = na// ————— Take profitvar bool    long_tp                     = na,   var bool    short_tp                    = navar int     last_long_tp                = na,   var int     last_short_tp               = navar bool    Final_Long_tp               = na,   var bool    Final_Short_tp              = na// ————— Stop Lossvar int     CondIni_long_sl             = 0,    var int     CondIni_short_sl            = 0var bool    Final_Long_sl0              = na,   var bool    Final_Short_sl0             = navar bool    Final_Long_sl               = na,   var bool    Final_Short_sl              = navar int     last_long_sl                = na,   var int     last_short_sl               = na// ————— Indicatorsvar bool    JMA_longCond                = na,   var bool    JMA_shortCond               = navar bool    RF_longCond                 = na,   var bool    RF_shortCond                = navar bool    ADX_longCond                = na,   var bool    ADX_shortCond               = navar bool    SAR_longCond                = na,   var bool    SAR_shortCond               = navar bool    RSI_longCond                = na,   var bool    RSI_shortCond               = navar bool    MACD_longCond               = na,   var bool    MACD_shortCond              = navar bool    VOL_longCond                = na,   var bool    VOL_shortCond               = navar bool    JMA_XlongCond               = na,   var bool    JMA_XshortCond              = navar bool    RF_XlongCond                = na,   var bool    RF_XshortCond               = navar bool    ADX_XlongCond               = na,   var bool    ADX_XshortCond              = navar bool    SAR_XlongCond               = na,   var bool    SAR_XshortCond              = navar int     CondIni_long_BB             = 0,    var int     CondIni_short_BB            = 0var bool    Final_long_BB               = na,   var bool    Final_short_BB              = navar int     last_long_BB                = na,   var int     last_short_BB               = na// ————— Average Pricevar float   sum_long                    = 0.0,  var float   sum_short                   = 0.0var float   Position_Price              = 0.0// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Jurik Moving Average// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— JMA calculationJMA(_JMA_length, _phase, _power, _src) =>    phaseRatio      = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5    beta            = 0.45 * (_JMA_length - 1) / (0.45 * (_JMA_length - 1) + 2)    alpha           = pow(beta, _power)    jma             = 0.0    e0              = 0.0    e0              := (1 - alpha) * _src + alpha * nz(e0[1])    e1              = 0.0    e1              := (_src - e0) * (1 - beta) + beta * nz(e1[1])    e2              = 0.0    e2              := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])    jma             := e2 + nz(jma[1])    // ————— Defining JMA trendJMA_Rising          = JMA(JMA_length, phase, power, src) > JMA(JMA_length, phase, power, src)[1]JMA_Falling         = JMA(JMA_length, phase, power, src) < JMA(JMA_length, phase, power, src)[1]// ————— JMA PlottingJMA_color           = JMA_Rising ? color.lime : JMA_Falling ? #e91e63 : color.orangeplot(Act_JMA ? JMA(JMA_length, phase, power, src) : na, color=JMA_color, linewidth = 2, title= "JMA")// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Range Filter// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Range Filter calculationRange_filter(_src, _per, _mult) =>    float _upward   = 0.0    float _downward = 0.0    wper            = (_per*2) - 1    avrng           = ema(abs(_src - _src[1]), _per)    _smoothrng      = ema(avrng, wper) * _mult    _filt           = _src    _filt           := _src  > nz(_filt[1]) ? ((_src-_smoothrng) < nz(_filt[1]) ? nz(_filt[1]) : (_src-_smoothrng)) : ((_src+_smoothrng) > nz(_filt[1]) ? nz(_filt[1]) : (_src+_smoothrng))    _upward         := _filt > _filt[1]     ? nz(_upward[1])   + 1 : _filt < _filt[1] ? 0 : nz(_upward[1])    _downward       := _filt < _filt[1]     ? nz(_downward[1]) + 1 : _filt > _filt[1] ? 0 : nz(_downward[1])    [_smoothrng,_filt,_upward,_downward]    // ————— Defining variables for include in future conditions[smoothrng, filt, upward, downward] = Range_filter(src, per, mult)// ————— Defining high and low bandshband               = filt + smoothrnglband               = filt - smoothrng// ————— Range Filter Plottingfiltcolor           = upward > 0 ? color.lime : downward > 0 ? color.red : color.orangefiltplot            = plot(Act_RF ? filt  : na, color = filtcolor,  linewidth = 1,                  title = "RF")hbandplot           = plot(Act_RF ? hband : na, color = filtcolor,                  transp = 50,    title = "RF High Target")lbandplot           = plot(Act_RF ? lband : na, color = filtcolor,                  transp = 50,    title = "RF Low Target")fill(hbandplot, lbandplot,                      color = filtcolor,                                  title = "RF Target Range")// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— ADX// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Classic ADX calculatingcalcADX(_len) =>    up              = change(high)    down            = -change(low)    plusDM          = na(up)   ? na : (up > down and up > 0   ? up   : 0)    minusDM         = na(down) ? na : (down > up and down > 0 ? down : 0)    truerange       = rma(tr, _len)    _plus           = fixnan(100 * rma(plusDM, _len)  / truerange)    _minus          = fixnan(100 * rma(minusDM, _len) / truerange)    sum             = _plus + _minus    _adx            = 100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)    [_plus,_minus,_adx]// ————— Masanakamura ADX calculatingcalcADX_Masanakamura(_len) =>    SmoothedTrueRange                   = 0.0    SmoothedDirectionalMovementPlus     = 0.0    SmoothedDirectionalMovementMinus    = 0.0    TrueRange                           = max(max(high - low, abs(high - nz(close[1]))), abs(low - nz(close[1])))    DirectionalMovementPlus             = high - nz(high[1]) > nz(low[1]) - low ? max(high - nz(high[1]), 0) : 0    DirectionalMovementMinus            = nz(low[1]) - low > high - nz(high[1]) ? max(nz(low[1]) - low, 0)   : 0    SmoothedTrueRange                   := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1]) /_len) + TrueRange    SmoothedDirectionalMovementPlus     := nz(SmoothedDirectionalMovementPlus[1])  - (nz(SmoothedDirectionalMovementPlus[1])  / _len) + DirectionalMovementPlus    SmoothedDirectionalMovementMinus    := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1]) / _len) + DirectionalMovementMinus    DIP                                 = SmoothedDirectionalMovementPlus  / SmoothedTrueRange * 100    DIM                                 = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100    DX                                  = abs(DIP-DIM) / (DIP+DIM)*100    adx                                 = sma(DX, _len)    [DIP,DIM,adx] // ————— Defining variables for include in future conditions   [DIPlusC,DIMinusC,ADXC] = calcADX(ADX_len) [DIPlusM,DIMinusM,ADXM] = calcADX_Masanakamura(ADX_len)DIPlus                  = ADX_options == "CLASSIC" ? DIPlusC    : DIPlusMDIMinus                 = ADX_options == "CLASSIC" ? DIMinusC   : DIMinusMADX                     = ADX_options == "CLASSIC" ? ADXC       : ADXM// ————— Plotting ADX bar colorsADX_color = DIPlus > DIMinus and ADX > th ? color.green : DIPlus < DIMinus and ADX > th ? color.red : color.orangebarcolor(color = Act_ADX ? ADX_color : na, title = "ADX")// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— SAR// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— SAR calculation from TVSAR = sar(Sst, Sinc, Smax)// ————— SAR Plottingplot(Act_SAR ? SAR : na, color = ADX_color, style = plot.style_circles, title = "SAR") // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— RSI with Volume// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— RSI with volume calculationWiMA(_src, W_length) =>     var float MA_s  = 0.0    MA_s            :=(_src + nz(MA_s[1] * (W_length-1)))/W_length    MA_sRSI_Volume(fv, _length) =>      up              = iff(fv > fv[1], abs(fv - fv[1]) * volume, 0)    dn              = iff(fv < fv[1], abs(fv - fv[1]) * volume, 0)    upt             = WiMA(up,_length)    dnt             = WiMA(dn,_length)    100 * (upt / (upt + dnt))// ————— Defining variable for include in conditionsRSI_V               = RSI_Volume(src, RSI_len)// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— MACD// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— MAC-Z calculationcalc_zvwap(pds) =>    mean            = sum(volume * close, pds) / sum(volume, pds)    vwapsd          = sqrt(sma(pow(close - mean, 2), pds))    (close - mean ) / vwapsdzscore              = calc_zvwap(lengthz)fastMA              = sma(src, fastLength)slowMA              = sma(src, slowLength)macd                = fastMA - slowMAmacz                = zscore + macd / stdev(src, lengthStdev)signal              = sma(macz, signalLength)histmacz            = macz - signal// ————— MACD calculation[_,_,histmacd]      = macd(src,  fastLength, slowLength, signalLength)hist                = MACD_options == "MACD" ? histmacd : histmacz// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Strategy// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— All indicators with long conditions and enable/disable optionJMA_longCond        := (Act_JMA     ? (JMA_Rising)                                  : VOL_longCond) RF_longCond         := (Act_RF      ? (high > hband and upward > 0)                 : JMA_longCond)ADX_longCond        := (Act_ADX     ? (DIPlus > DIMinus and ADX > th)               : RF_longCond)SAR_longCond        := (Act_SAR     ? (SAR < close)                                 : ADX_longCond)RSI_longCond        := (Act_RSI     ? (RSI_V > RSI_obos)                            : SAR_longCond)MACD_longCond       := (Act_MACD    ? (hist > 0)                                    : RSI_longCond)VOL_longCond        := (Act_Vol     ? (volume > sma(volume,sma_length) * volume_f)  : MACD_longCond)// ————— All indicators with short conditions and enable/disable optionJMA_shortCond       := (Act_JMA     ? (JMA_Falling)                                 : VOL_shortCond) RF_shortCond        := (Act_RF      ? (low < lband and downward > 0)                : JMA_shortCond)ADX_shortCond       := (Act_ADX     ? (DIPlus < DIMinus and ADX > th)               : RF_shortCond)SAR_shortCond       := (Act_SAR     ? (SAR > close)                                 : ADX_shortCond)RSI_shortCond       := (Act_RSI     ? (RSI_V < RSI_obos)                            : SAR_shortCond)MACD_shortCond      := (Act_MACD    ? (hist < 0)                                    : RSI_shortCond)VOL_shortCond       := (Act_Vol     ? (volume > sma(volume,sma_length) * volume_f)  : MACD_shortCond)// ————— Defining long/short condition from indicators + volumelongCond            := JMA_longCond  and RF_longCond  and ADX_longCond  and SAR_longCond  and RSI_longCond  and MACD_longCond  and VOL_longCondshortCond           := JMA_shortCond and RF_shortCond and ADX_shortCond and SAR_shortCond and RSI_shortCond and MACD_shortCond and VOL_shortCond// ————— Avoiding confirmed long/short simultaneityCondIni_long        := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_long[1])CondIni_short       := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_short[1])// ————— Confirmed long/short conditionslongCondition       = (longCond[1]  and nz(CondIni_long[1])  == -1)shortCondition      = (shortCond[1] and nz(CondIni_short[1]) == 1)// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Position Price// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Last opened long/short price on unconfirmed/confirmed conditionslast_open_longCondition     := longCondition  or Final_long_BB[1]  ? close[1] : nz(last_open_longCondition[1])last_open_shortCondition    := shortCondition or Final_short_BB[1] ? close[1] : nz(last_open_shortCondition[1])// ————— Check if your last position was a confirmed long or a shortlast_longCondition          := longCondition  or Final_long_BB[1]  ? time : nz(last_longCondition[1])last_shortCondition         := shortCondition or Final_short_BB[1] ? time : nz(last_shortCondition[1])in_longCondition            = last_longCondition  > last_shortConditionin_shortCondition           = last_shortCondition > last_longCondition// ————— Check if your last position was a confirmed final long or short without BBlast_Final_longCondition    := longCondition  ? time : nz(last_Final_longCondition[1])last_Final_shortCondition   := shortCondition ? time : nz(last_Final_shortCondition[1])// ————— Counting long & short iterationsnLongs                      := nz(nLongs[1])nShorts                     := nz(nShorts[1])// ————— Longs Counterif longCondition or Final_long_BB    nLongs                  := nLongs + 1    nShorts                 := 0    sum_long                := nz(last_open_longCondition) + nz(sum_long[1])    sum_short               := 0.0    // ————— Shorts Counterif shortCondition or Final_short_BB    nLongs                  := 0    nShorts                 := nShorts + 1    sum_short               := nz(last_open_shortCondition) + nz(sum_short[1])    sum_long                := 0.0// ————— Calculating and Plotting the price averagePosition_Price              := nz(Position_Price[1])Position_Price              := longCondition or Final_long_BB ? sum_long/nLongs : shortCondition or Final_short_BB ? sum_short/nShorts : naplot((nLongs > 1) or (nShorts > 1) ? Position_Price : na, title = "Average Price", color = in_longCondition ? color.aqua : color.orange, linewidth = 2, style = plot.style_cross)    // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Take Profit// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Take Profit divided by n entriestp_long             = (Act_divide and (nLongs  > 1) ? tp_long0  / nLongs  : tp_long0)  / 100tp_short            = (Act_divide and (nShorts > 1) ? tp_short0 / nShorts : tp_short0) / 100// ————— First TP Conditionslong_tp             := high > (fixnan(Position_Price) * (1 + tp_long))  and in_longConditionshort_tp            := low  < (fixnan(Position_Price) * (1 - tp_short)) and in_shortCondition// ————— Get the time of the last tp closelast_long_tp        := long_tp  ? time : nz(last_long_tp[1])last_short_tp       := short_tp ? time : nz(last_short_tp[1])// ————— Final Take profit condition (never after the stop loss)Final_Long_tp       := (long_tp  and last_longCondition  > nz(last_long_tp[1])  and last_longCondition  > nz(last_long_sl[1]))Final_Short_tp      := (short_tp and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]))// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ———————————————————— Stop Loss// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Stop Loss ATR calculationATR_SL_Long         = low  - atr(atrPeriod) * multiplierPeriodATR_SL_Short        = high + atr(atrPeriod) * multiplierPeriodlongStopPrev        = nz(ATR_SL_Long[1], ATR_SL_Long)shortStopPrev       = nz(ATR_SL_Short[1], ATR_SL_Short)ATR_SL_Long         := close[1] > longStopPrev  ? max(ATR_SL_Long, longStopPrev)   : ATR_SL_LongATR_SL_Short        := close[1] < shortStopPrev ? min(ATR_SL_Short, shortStopPrev) : ATR_SL_Short// ————— Calculating Sl according Risk and Initial Capitalsl = in_longCondition ?           min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * max(1, last_dynamic_Leverage_long)  * max(1, nLongs)))) :      min(sl0, (((Risk / (100 / (strategy.equity / close)))*100) / (quanTity * max(1, last_dynamic_Leverage_short) * max(1, nShorts))))      // ————— Stop Loss long conditionsNormal_long_sl      = Act_Conf_SL ?  ((SL_options == "NORMAL") ? ((Act_sl and in_longCondition  and close <= ((1 - (sl / 100)) * (fixnan(Position_Price))))) : na) :                                     ((SL_options == "NORMAL") ? ((Act_sl and in_longCondition  and low   <= ((1 - (sl / 100)) * (fixnan(Position_Price))))) : na)ATR_long_sl         = Act_Conf_SL ?  ((SL_options == "ATR") ?    ((Act_sl and in_longCondition  and close <= (ATR_SL_Long))) : na) :                                     ((SL_options == "ATR") ?    ((Act_sl and in_longCondition  and low   <= (ATR_SL_Long))) : na)Both_long_sl        = Act_Conf_SL ?  ((SL_options == "BOTH") ?   ((Act_sl and in_longCondition  and close <= ((1 - (sl / 100)) * (fixnan(Position_Price)))) or                                                                  ((Act_sl and in_longCondition  and close <= (ATR_SL_Long)))) : na) :                                     ((SL_options == "BOTH") ?   ((Act_sl and in_longCondition  and low   <= ((1 - (sl / 100)) * (fixnan(Position_Price)))) or                                                                  ((Act_sl and in_longCondition  and low   <= (ATR_SL_Long)))) : na)// ————— Stop Loss short conditionsNormal_short_sl     = Act_Conf_SL ?  ((SL_options == "NORMAL") ? ((Act_sl and in_shortCondition and close >= ((1 + (sl / 100)) * (fixnan(Position_Price))))) : na) :                                     ((SL_options == "NORMAL") ? ((Act_sl and in_shortCondition and high  >= ((1 + (sl / 100)) * (fixnan(Position_Price))))) : na)ATR_short_sl        = Act_Conf_SL ?  ((SL_options == "ATR") ?    ((Act_sl and in_shortCondition and close >= (ATR_SL_Short))) : na) :                                     ((SL_options == "ATR") ?    ((Act_sl and in_shortCondition and high  >= (ATR_SL_Short))) : na)Both_short_sl       = Act_Conf_SL ?  ((SL_options == "BOTH") ?   ((Act_sl and in_shortCondition and close >= ((1 + (sl/100)) * (fixnan(Position_Price)))) or                                                                  ((Act_sl and in_shortCondition and close >= (ATR_SL_Short)))) : na) :                                     ((SL_options == "BOTH") ?   ((Act_sl and in_shortCondition and high  >= ((1 + (sl/100)) * (fixnan(Position_Price)))) or                                                                  ((Act_sl and in_shortCondition and high  >= (ATR_SL_Short)))) : na)// ————— Get the time of the last sl closelast_long_sl        := Normal_long_sl  or ATR_long_sl  or Both_long_sl  ? time : nz(last_long_sl[1])last_short_sl       := Normal_short_sl or ATR_short_sl or Both_short_sl ? time : nz(last_short_sl[1])// ————— Final Stop Loss conditionFinal_Long_sl       := (Normal_long_sl  or ATR_long_sl  or Both_long_sl)  and last_longCondition  > nz(last_long_sl[1])  and last_longCondition  > nz(last_long_tp[1])  and not Final_Long_tpFinal_Short_sl      := (Normal_short_sl or ATR_short_sl or Both_short_sl) and last_shortCondition > nz(last_short_sl[1]) and last_shortCondition > nz(last_short_tp[1]) and not Final_Short_tp//Plottin ATR SLplot(Act_sl and (SL_options != "NORMAL") ? in_longCondition ? ATR_SL_Long[1] : ATR_SL_Short[1] : na, title = "ATR SL", color = color.purple)// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————————————————————  Bollinger Bands Re-entry// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //BB_basis            = sma(src, BB_length)BB_dev              = BB_mult  * stdev(src, BB_length)BB_upper            = BB_basis + BB_devBB_lower            = BB_basis - BB_devu_BB                = plot(Act_BB ? BB_upper : na,  title = "Upper Bollinger Band",      color = #009688, linewidth = 2)l_BB                = plot(Act_BB ? BB_lower : na,  title = "Lower Bollinger Band",      color = #f06292, linewidth = 2)fill(u_BB, l_BB,                                    title = "Bollinger Band Background", color = in_longCondition ? #009688 : #f06292, transp = 95)// ————— Initial Bollinger Bands conditionsBB_long             = Act_BB and in_longCondition  and not (DIPlus < DIMinus and ADX > th) and (close <= BB_lower) and (close < last_open_longCondition  * (1 - (bbBetterPrice / 100)))BB_short            = Act_BB and in_shortCondition and not (DIPlus > DIMinus and ADX > th) and (close >= BB_upper) and (close > last_open_shortCondition * (1 + (bbBetterPrice / 100)))// ————— Get the time of the last BB closelast_long_BB        := BB_long  ? time : nz(last_long_BB[1])last_short_BB       := BB_short ? time : nz(last_short_BB[1])// ————— Final Bollinger Bands condition for longFinal_long_BB       := BB_long and last_Final_longCondition > nz(last_long_BB[1]) and                        last_longCondition > nz(last_long_tp[1]) and                        last_longCondition > nz(last_long_sl[1]) and not Final_Long_sl// ————— Final Bollinger Bands condition for short                 Final_short_BB      := BB_short and last_Final_shortCondition > nz(last_short_BB[1]) and                        last_shortCondition > nz(last_short_tp[1]) and                        last_shortCondition > nz(last_short_sl[1]) and not Final_Short_sl// ————— Final confirmed Re-entries on long & short conditionsFinal_Long_BB = Final_long_BB[1]Final_Short_BB = Final_short_BB[1]// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————————————————————  Signal Plotting// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— TP Long LevelstplLevel            = (in_longCondition and                       (last_longCondition > nz(last_long_tp[1])) and                       (last_longCondition > nz(last_long_sl[1])) and not Final_Long_sl[1]) ?                       (nLongs > 1) ?                       (fixnan(Position_Price) * (1 + tp_long)) : (last_open_longCondition * (1 + tp_long)) : naplot(tplLevel,            title = "Long TP Level",       style = plot.style_circles,                                   color = color.lime,  linewidth = 2)tpsLevel            = (in_shortCondition and                       (last_shortCondition > nz(last_short_tp[1])) and                       (last_shortCondition > nz(last_short_sl[1])) and not Final_Short_sl[1]) ?                       (nShorts > 1) ?                       (fixnan(Position_Price) * (1 - tp_short)) : (last_open_shortCondition * (1 - tp_short)) : naplot(tpsLevel,            title = "Short TP Level",      style = plot.style_circles,                                   color = color.red,   linewidth = 2)// ————— WeekendW_color             = (dayofweek == dayofweek.sunday or dayofweek == dayofweek.saturday) ? color.white : nabgcolor(W_color,          title = "Weekend",    transp = 95)// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————————————————————  Re-entry Conditions// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Re-entry on long after tp, sl or Xlongif Final_Long_tp or Final_Long_sl    CondIni_long    := -1    sum_long        := 0.0    nLongs          := na    // ————— Re-entry on short after tp, sl or Xshortif Final_Short_tp or Final_Short_sl    CondIni_short   := 1    sum_short       := 0.0    nShorts         := na    // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————————————————————  Backtest// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————— Defining new final unconfirmed long conditions_longCondition      = (longCond and not in_longCondition) or                       (longCond and Final_Long_tp) or                       (longCond and Final_Long_sl) or                      (longCond and not longCondition and (last_long_tp >= nz(last_longCondition))) or                       (longCond and not longCondition and (last_long_sl >= nz(last_longCondition)))   // ————— Defining new final unconfirmed short conditions_shortCondition     = (shortCond and not in_shortCondition) or                       (shortCond and Final_Short_tp) or                       (shortCond and Final_Short_sl) or                       (shortCond and not shortCondition and (last_short_tp >= nz(last_shortCondition))) or                       (shortCond and not shortCondition and (last_short_sl >= nz(last_shortCondition)))// ————— Test period declarationtestPeriod = time >= timenow - backtest_time// ————— Volume Factor for determine quantitiesVolume_Factor_Leverage      = min(Max_Lev, max(1, round(volume / sma(volume, sma_length))))last_dynamic_Leverage_long  := _longCondition ? Volume_Factor_Leverage  : nz(last_dynamic_Leverage_long[1])last_dynamic_Leverage_short := _shortCondition ? Volume_Factor_Leverage : nz(last_dynamic_Leverage_short[1])// ————— Entering long positionsif (_longCondition)    strategy.entry("long", strategy.long, qty = Volume_Factor_Leverage * quanTity, when = Act_BT and testPeriod)if (Final_long_BB)    strategy.entry("long", strategy.long, qty = last_dynamic_Leverage_long * quanTity, when = Act_BT and testPeriod)   // ————— Entering short positionsif (_shortCondition)     strategy.entry("short", strategy.short, qty = Volume_Factor_Leverage * quanTity, when = Act_BT and testPeriod)if (Final_short_BB)     strategy.entry("short", strategy.short, qty = last_dynamic_Leverage_short * quanTity, when = Act_BT and testPeriod)// ————— Closing positions with first long TPstrategy.exit("Tpl", "long",    profit   = (abs((last_open_longCondition  * (1 + tp_long)) - last_open_longCondition) / syminfo.mintick),    limit    = nLongs >= 1 ? strategy.position_avg_price * (1 + tp_long) : na,   loss     = Act_Conf_SL == false ?              (iff(Act_sl and (SL_options == "NORMAL"), (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick),              iff(Act_sl  and (SL_options == "ATR"), (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick),              iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) <              (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)), (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick),             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) >              (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick)), (abs(ATR_SL_Long-last_open_longCondition)/syminfo.mintick), na))))) : na,   stop     = Act_Conf_SL == false and nLongs >= 1 ?              (iff(Act_sl and (SL_options == "NORMAL"), ((1-(sl/100))*strategy.position_avg_price),             iff(Act_sl  and (SL_options == "ATR"), ATR_SL_Long,              iff(Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) > ATR_SL_Long), ((1-(sl/100))*strategy.position_avg_price),              iff(Act_sl  and (SL_options == "BOTH") and (((1-(sl/100))*strategy.position_avg_price) < ATR_SL_Long), ATR_SL_Long, na))))) : na)// Canceling long exit orders to avoid simultaneity with re-entrystrategy.cancel("Tpl", when = Final_long_BB)// ————— Closing positions with first short TPstrategy.exit("Tps", "short",   profit   = (abs((last_open_shortCondition * (1 - tp_short)) - last_open_shortCondition) / syminfo.mintick),    limit    = nShorts >= 1 ? strategy.position_avg_price*(1-(tp_short)) : na,   loss     = Act_Conf_SL == false ?              (iff(Act_sl and (SL_options == "NORMAL"), (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick),              iff(Act_sl  and (SL_options == "ATR"), (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick),              iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) <              (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)), (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick),             iff(Act_sl  and (SL_options == "BOTH") and ((abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) >              (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick)), (abs(ATR_SL_Short-last_open_shortCondition)/syminfo.mintick), na))))) : na,   stop     = Act_Conf_SL == false and nShorts >= 1 ?              (iff(Act_sl and (SL_options == "NORMAL"), ((1+(sl/100))*strategy.position_avg_price),             iff(Act_sl  and (SL_options == "ATR"), ATR_SL_Short,              iff(Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) < ATR_SL_Short), ((1+(sl/100))*strategy.position_avg_price),             iff(Act_sl  and (SL_options == "BOTH") and (((1+(sl/100))*strategy.position_avg_price) > ATR_SL_Short), ATR_SL_Short, na))))) : na)// Canceling short exit orders to avoid simultaneity with re-entrystrategy.cancel("Tps", when = Final_short_BB)// ————— Closing all positions with Xlong/Xshortstrategy.close_all(when = (Final_Long_sl and Act_Conf_SL) or (Final_Short_sl and Act_Conf_SL))// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- //// ————————————————————  by Xaviz