Pine Script v3 · Strategy
LegacyMomentum Trading By Mahfuz Azim
A Pine Script v3 strategy built on SMA (src, len), EMA (src, len) and RSI (close, 14). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is 1000 contracts and pays 0.1% commission. 25 settings; defaults include Relative TimeFrame Multiplier for Second MA Ribbon (0=none, max=100) 4, Fast MA Type: EMA and Fast - Length 16.
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 Momentum Trading By Mahfuz Azim repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 5 of 5 trades.
- How did Momentum Trading By Mahfuz Azim do on BTCUSDT 1D with default settings?
- +76,395.00%
- From 26 Jan 2024 to 21 Oct 2026, over 18 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Momentum Trading By Mahfuz Azim be converted to Pine Script v5?
- No
- The automatic step from v3 to v4 changes the results, so no v4 download is offered; the edits are shown below.
- Does Momentum Trading By Mahfuz Azim use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Momentum Trading By Mahfuz Azim run in Zeal Terminal?
- Yes
- It compiles in 62 ms and runs 1,000 bars in 424 ms, in the browser.
BTCUSDT · 1D · default settings
- MA Fast
- MA Medium Fast
- MA Slow
- MA Fast
- MA Medium Fast
- MA Slow
- QQE X Over Channel
- QQE X Under Channel
- QQE Cross Over
- QQE Cross Under
- Cross Alert Completed
- Close Order
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +76,395.00%
- $7,639,500.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 18
- 7 won · 11 lost
- Profitable
- 38.9%
- of closed trades
- Profit factor
- 1.15
- gross profit ÷ gross loss
- Max drawdown
- 11,732.80%
- −$33,629,000.00
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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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 373, 374, 375, 376. The upgraded version below moves each call inside an if block.
Upgrade and export
Written in Pine Script v3. The upgrade to v5 takes 2 steps, 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 exp → math.exp, cos → math.cos, sma → ta.sma, ema → ta.ema, wma → ta.wma, vwma → ta.vwma, round → math.round, sqrt → math.sqrt, rsi → ta.rsi, abs → math.abs, max → math.max, min → math.min, cross → ta.cross, rising → ta.rising, falling → ta.falling, change → ta.change.
- Colours are bare names (red, green, blue, aqua, olive, black, fuchsia); v4 spells them color.red, color.green, color.blue, color.aqua, color.olive, color.black, color.fuchsia.
- 4 strategy calls use the when = argument (line 373, 374, 375, 376). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v3 → v4
DiffersTrades differ: 18 before, 17 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=3 → //@version=4
- Line 123: string → input.string
- Line 127: string → input.string
- Line 131: string → input.string
- Line 138: float → input.float
- Line 342: red → color.red
13 more edits
- Line 342: green → color.green
- Line 348: blue → color.blue
- Line 348: aqua → color.aqua
- Line 354: olive → color.olive
- Line 355: red → color.red
- Line 357: blue → color.blue
- Line 358: black → color.black
- Line 360: aqua → color.aqua
- Line 361: fuchsia → color.fuchsia
- Line 390: red → color.red
- Line 390: green → color.green
- Line 391: red → color.red
- Line 391: green → color.green
-//@version=3+//@version=4//⋯showAvgs = input(true,title="Show Moving Average Lines")-type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)+type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)len1 = input(defval=16, title="Fast - Length", minval=1)⋯// Medium Fast MA - type, source, length-type2 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)+type2 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)len2 = input(defval=21, title="Medium - Length", minval=1)⋯// Slow MA - type, source, length-type3 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)+type3 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)len3 = input(defval=26, title="Slow Length", minval=1)⋯SF = input(8,title='RSI Smoothing Factor')-QQEfactor = input(5.0,type=float,title='Fast QQE Factor')+QQEfactor = input(5.0,type=input.float,title='Fast QQE Factor')threshhold = input(10, title="RSI Threshhold")⋯// Ma's-tcolor = direction<0?red:green+tcolor = direction<0?color.red:color.greenma1=plot(showAvgs?ma_fast:na, title="MA Fast", color=tcolor, linewidth=1, transp=0)⋯// Ma's-altTcolor=altDirection<0?blue:aqua+altTcolor=altDirection<0?color.blue:color.aquama4=plot(showAvgs and mult>1?ma_fast_alt:na, title="MA Fast", color=altTcolor, linewidth=1, transp=0)⋯// QQE exit from Thresh Hold Channel-plotshape(sQQEc and QQEclong==1 and not isLong, title="QQE X Over Channel", style=shape.triangleup, location=location.belowbar, text="XC", color=olive, transp=20, size=size.tiny)+plotshape(sQQEc and QQEclong==1 and not isLong, title="QQE X Over Channel", style=shape.triangleup, location=location.belowbar, text="XC", color=color.olive, transp=20, size=size.tiny)-plotshape(sQQEc and QQEcshort==1 and not isShort, title="QQE X Under Channel", style=shape.triangledown, location=location.abovebar, text="XC", color=red, transp=20, size=size.tiny)+plotshape(sQQEc and QQEcshort==1 and not isShort, title="QQE X Under Channel", style=shape.triangledown, location=location.abovebar, text="XC", color=color.red, transp=20, size=size.tiny)// QQE crosses-plotshape(sQQEx and QQExlong==1 and QQEclong!=1 and not isLong, title="QQE Cross Over", style=shape.triangleup, location=location.belowbar, text="XQ", color=blue, transp=20, size=size.tiny)+plotshape(sQQEx and QQExlong==1 and QQEclong!=1 and not isLong, title="QQE Cross Over", style=shape.triangleup, location=location.belowbar, text="XQ", color=color.blue, transp=20, size=size.tiny)-plotshape(sQQEx and QQExshort==1 and QQEcshort!=1 and not isShort, title="QQE Cross Under", style=shape.triangledown, location=location.abovebar, text="XQ", color=black, transp=20, size=size.tiny)+plotshape(sQQEx and QQExshort==1 and QQEcshort!=1 and not isShort, title="QQE Cross Under", style=shape.triangledown, location=location.abovebar, text="XQ", color=color.black, transp=20, size=size.tiny)// Signal crosses zero line-plotshape(sQQEz and QQEzlong==1 and QQEclong!=1 and not isLong and QQExlong!=1, title="QQE Zero Cross Over", style=shape.triangleup, location=location.belowbar, text="XZ", color=aqua, transp=20, size=size.tiny)+plotshape(sQQEz and QQEzlong==1 and QQEclong!=1 and not isLong and QQExlong!=1, title="QQE Zero Cross Over", style=shape.triangleup, location=location.belowbar, text="XZ", color=color.aqua, transp=20, size=size.tiny)-plotshape(sQQEz and QQEzshort==1 and QQEcshort!=1 and not isShort and QQExshort!=1, title="QQE Zero Cross Under", style=shape.triangledown, location=location.abovebar, text="XZ", color=fuchsia, transp=20, size=size.tiny)+plotshape(sQQEz and QQEzshort==1 and QQEcshort!=1 and not isShort and QQExshort!=1, title="QQE Zero Cross Under", style=shape.triangledown, location=location.abovebar, text="XZ", color=color.fuchsia, transp=20, size=size.tiny)//⋯// show only when alert condition is met and bar closed.-plotshape(isLong or isShort,title= "Cross Alert Completed", location=location.bottom, color=isShort?red:green, transp=0, style=shape.circle,size=size.auto,offset=0)+plotshape(isLong or isShort,title= "Cross Alert Completed", location=location.bottom, color=isShort?color.red:color.green, transp=0, style=shape.circle,size=size.auto,offset=0)-plotshape(isCloseShort[1] or isCloseLong[1],title= "Close Order", location=location.top, color=isCloseShort[1]?red:green, transp=0, style=shape.square,size=size.auto,offset=-1)+plotshape(isCloseShort[1] or isCloseLong[1],title= "Close Order", location=location.top, color=isCloseShort[1]?color.red:color.green, transp=0, style=shape.square,size=size.auto,offset=-1)// - ALERTING END⋯v4 → v5
VerifiedSame 17 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 123: input(type = input.string) → input.string()
- Line 127: input(type = input.string) → input.string()
- Line 131: input(type = input.string) → input.string()
- Line 138: input(type = input.float) → input.float()
- Line 187: exp → math.exp
57 more edits
- Line 188: cos → math.cos
- Line 198: sma → ta.sma
- Line 201: ema → ta.ema
- Line 202: ema → ta.ema
- Line 207: ema → ta.ema
- Line 208: ema → ta.ema
- Line 211: ema → ta.ema
- Line 212: ema → ta.ema
- Line 212: ema → ta.ema
- Line 212: ema → ta.ema
- Line 228: type is reserved in v5: renamed type_
- Line 229: ema → ta.ema
- Line 230: wma → ta.wma
- Line 231: vwma → ta.vwma
- Line 236: wma → ta.wma
- Line 236: wma → ta.wma
- Line 236: wma → ta.wma
- Line 236: round → math.round
- Line 236: sqrt → math.sqrt
- Line 239: sma → ta.sma
- Line 239: sma → ta.sma
- Line 240: sma → ta.sma
- Line 253: rsi → ta.rsi
- Line 254: ema → ta.ema
- Line 255: abs → math.abs
- Line 256: ema → ta.ema
- Line 257: ema → ta.ema
- Line 264: max → math.max
- Line 265: min → math.min
- Line 266: cross → ta.cross
- Line 266: cross → ta.cross
- Line 278: rising → ta.rising
- Line 278: falling → ta.falling
- Line 279: rising → ta.rising
- Line 279: falling → ta.falling
- Line 335: change → ta.change
- Line 336: change → ta.change
- Line 337: change → ta.change
- Line 338: change → ta.change
- Line 343: transp = 0 → color.new(tcolor, 0)
- Line 344: transp = 0 → color.new(tcolor, 0)
- Line 345: transp = 0 → color.new(tcolor, 0)
- Line 346: transp = 90 → color.new(tcolor, 90)
- Line 349: transp = 0 → color.new(altTcolor, 0)
- Line 350: transp = 0 → color.new(altTcolor, 0)
- Line 351: transp = 0 → color.new(altTcolor, 0)
- Line 352: transp = 90 → color.new(altTcolor, 90)
- Line 354: transp = 20 → color.new(color.olive, 20)
- Line 355: transp = 20 → color.new(color.red, 20)
- Line 357: transp = 20 → color.new(color.blue, 20)
- Line 358: transp = 20 → color.new(color.black, 20)
- Line 360: transp = 20 → color.new(color.aqua, 20)
- Line 361: transp = 20 → color.new(color.fuchsia, 20)
- Line 373: strategy.entry(…, 1) → strategy.entry(…, strategy.long)
- Line 375: strategy.entry(…, 0) → strategy.entry(…, strategy.short)
- Line 390: transp = 0 → color.new(isShort?color.red:color.green, 0)
- Line 391: transp = 0 → color.new(isCloseShort[1]?color.red:color.green, 0)
-//@version=4+//@version=5//⋯showAvgs = input(true,title="Show Moving Average Lines")-type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)+type1 = input.string(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])len1 = input(defval=16, title="Fast - Length", minval=1)⋯// Medium Fast MA - type, source, length-type2 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)+type2 = input.string(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])len2 = input(defval=21, title="Medium - Length", minval=1)⋯// Slow MA - type, source, length-type3 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=input.string)+type3 = input.string(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])len3 = input(defval=26, title="Slow Length", minval=1)⋯SF = input(8,title='RSI Smoothing Factor')-QQEfactor = input(5.0,type=input.float,title='Fast QQE Factor')+QQEfactor = input.float(5.0,title='Fast QQE Factor')threshhold = input(10, title="RSI Threshhold")⋯variant_supersmoother(src,len) =>- a1 = exp(-1.414*3.14159 / len)+ a1 = math.exp(-1.414*3.14159 / len)- b1 = 2*a1*cos(1.414*3.14159 / len)+ b1 = 2*a1*math.cos(1.414*3.14159 / len) c2 = b1⋯ v5 = 0.0- v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len+ v5 := na(v5[1]) ? ta.sma(src, len) : (v5[1] * (len - 1) + src) / len v5variant_zerolagema(src,len) =>- ema1 = ema(src, len)+ ema1 = ta.ema(src, len)- ema2 = ema(ema1, len)+ ema2 = ta.ema(ema1, len) v10 = ema1+(ema1-ema2)⋯variant_doubleema(src,len) =>- v2 = ema(src, len)+ v2 = ta.ema(src, len)- v6 = 2 * v2 - ema(v2, len)+ v6 = 2 * v2 - ta.ema(v2, len) v6variant_tripleema(src,len) =>- v2 = ema(src, len)+ v2 = ta.ema(src, len)- v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential+ v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len) // Triple Exponential v7⋯// return variant, defaults to SMA -variant(type, src, len, g) =>+variant(type_, src, len, g) =>- type=="EMA" ? ema(src,len) : + type_=="EMA" ? ta.ema(src,len) : - type=="WMA" ? wma(src,len): + type_=="WMA" ? ta.wma(src,len): - type=="VWMA" ? vwma(src,len) : + type_=="VWMA" ? ta.vwma(src,len) : - type=="SMMA" ? variant_smoothed(src,len) : + type_=="SMMA" ? variant_smoothed(src,len) : - type=="DEMA" ? variant_doubleema(src,len): + type_=="DEMA" ? variant_doubleema(src,len): - type=="TEMA" ? variant_tripleema(src,len): + type_=="TEMA" ? variant_tripleema(src,len): - type=="LAGMA" ? variant_lag(src,g) :+ type_=="LAGMA" ? variant_lag(src,g) :- type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) :+ type_=="HullMA"? ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) :- type=="SSMA" ? variant_supersmoother(src,len) : + type_=="SSMA" ? variant_supersmoother(src,len) : - type=="ZEMA" ? variant_zerolagema(src,len) : + type_=="ZEMA" ? variant_zerolagema(src,len) : - type=="TMA" ? sma(sma(src,len),len) : + type_=="TMA" ? ta.sma(ta.sma(src,len),len) : - sma(src,len)+ ta.sma(src,len)// - /variant ⋯//-Rsi = rsi(RSIsrc,RSILen)+Rsi = ta.rsi(RSIsrc,RSILen)-RSIndex = ema(Rsi, SF)+RSIndex = ta.ema(Rsi, SF)-AtrRsi = abs(RSIndex[1] - RSIndex)+AtrRsi = math.abs(RSIndex[1] - RSIndex)-MaAtrRsi = ema(AtrRsi, Wilders_Period)+MaAtrRsi = ta.ema(AtrRsi, Wilders_Period)-DeltaFastAtrRsi = ema(MaAtrRsi,Wilders_Period) * QQEfactor+DeltaFastAtrRsi = ta.ema(MaAtrRsi,Wilders_Period) * QQEfactor//⋯trend = 0-longband:=RSIndex[1] > longband[1] and RSIndex > longband[1] ? max(longband[1],newlongband) : newlongband+longband:=RSIndex[1] > longband[1] and RSIndex > longband[1] ? math.max(longband[1],newlongband) : newlongband-shortband:=RSIndex[1] < shortband[1] and RSIndex < shortband[1] ? min(shortband[1],newshortband) : newshortband+shortband:=RSIndex[1] < shortband[1] and RSIndex < shortband[1] ? math.min(shortband[1],newshortband) : newshortband-trend:=cross(RSIndex, shortband[1])? 1 : cross(longband[1], RSIndex) ? -1 : nz(trend[1],1)+trend:=ta.cross(RSIndex, shortband[1])? 1 : ta.cross(longband[1], RSIndex) ? -1 : nz(trend[1],1)FastAtrRsiTL = trend==1 ? longband : shortband⋯// Get Direction From Medium Moving Average-direction = rising(ma_medium,3) ? 1 : falling(ma_medium,3) ? -1 : 0+direction = ta.rising(ma_medium,3) ? 1 : ta.falling(ma_medium,3) ? -1 : 0-altDirection = rising(ma_medium_alt,3) ? 1 : falling(ma_medium_alt,3) ? -1 : 0+altDirection = ta.rising(ma_medium_alt,3) ? 1 : ta.falling(ma_medium_alt,3) ? -1 : 0//⋯tradestate := tradestate==0 ? (buy==1 ? 1 : sell==1 ? 2 : 0) : (tradestate==1 and closeLong==1) or (tradestate==2 and closeShort==1)? 0 : tradestate -isLong = change(tradestate) and tradestate==1+isLong = ta.change(tradestate) and tradestate==1-isShort = change(tradestate) and tradestate==2+isShort = ta.change(tradestate) and tradestate==2-isCloseLong = change(tradestate) and tradestate==0 and nz(tradestate[1])==1+isCloseLong = ta.change(tradestate) and tradestate==0 and nz(tradestate[1])==1-isCloseShort = change(tradestate) and tradestate==0 and nz(tradestate[1])==2+isCloseShort = ta.change(tradestate) and tradestate==0 and nz(tradestate[1])==2// - SERIES VARIABLES END⋯tcolor = direction<0?color.red:color.green-ma1=plot(showAvgs?ma_fast:na, title="MA Fast", color=tcolor, linewidth=1, transp=0)+ma1=plot(showAvgs?ma_fast:na, title="MA Fast", color=color.new(tcolor, 0), linewidth=1)-ma2=plot(showAvgs?ma_medium:na, title="MA Medium Fast", color=tcolor, linewidth=2, transp=0)+ma2=plot(showAvgs?ma_medium:na, title="MA Medium Fast", color=color.new(tcolor, 0), linewidth=2)-ma3=plot(showAvgs?ma_slow:na, title="MA Slow", color=tcolor, linewidth=1, transp=0)+ma3=plot(showAvgs?ma_slow:na, title="MA Slow", color=color.new(tcolor, 0), linewidth=1)-fill(ma1,ma3,color=tcolor,transp=90)+fill(ma1,ma3,color=color.new(tcolor, 90))// Ma'saltTcolor=altDirection<0?color.blue:color.aqua-ma4=plot(showAvgs and mult>1?ma_fast_alt:na, title="MA Fast", color=altTcolor, linewidth=1, transp=0)+ma4=plot(showAvgs and mult>1?ma_fast_alt:na, title="MA Fast", color=color.new(altTcolor, 0), linewidth=1)-ma5=plot(showAvgs and mult>1?ma_medium_alt:na, title="MA Medium Fast", color=altTcolor, linewidth=2, transp=0)+ma5=plot(showAvgs and mult>1?ma_medium_alt:na, title="MA Medium Fast", color=color.new(altTcolor, 0), linewidth=2)-ma6=plot(showAvgs and mult>1?ma_slow_alt:na, title="MA Slow", color=altTcolor, linewidth=1, transp=0)+ma6=plot(showAvgs and mult>1?ma_slow_alt:na, title="MA Slow", color=color.new(altTcolor, 0), linewidth=1)-fill(ma4,ma6,color=altTcolor,transp=90)+fill(ma4,ma6,color=color.new(altTcolor, 90))// QQE exit from Thresh Hold Channel-plotshape(sQQEc and QQEclong==1 and not isLong, title="QQE X Over Channel", style=shape.triangleup, location=location.belowbar, text="XC", color=color.olive, transp=20, size=size.tiny)+plotshape(sQQEc and QQEclong==1 and not isLong, title="QQE X Over Channel", style=shape.triangleup, location=location.belowbar, text="XC", color=color.new(color.olive, 20), size=size.tiny)-plotshape(sQQEc and QQEcshort==1 and not isShort, title="QQE X Under Channel", style=shape.triangledown, location=location.abovebar, text="XC", color=color.red, transp=20, size=size.tiny)+plotshape(sQQEc and QQEcshort==1 and not isShort, title="QQE X Under Channel", style=shape.triangledown, location=location.abovebar, text="XC", color=color.new(color.red, 20), size=size.tiny)// QQE crosses-plotshape(sQQEx and QQExlong==1 and QQEclong!=1 and not isLong, title="QQE Cross Over", style=shape.triangleup, location=location.belowbar, text="XQ", color=color.blue, transp=20, size=size.tiny)+plotshape(sQQEx and QQExlong==1 and QQEclong!=1 and not isLong, title="QQE Cross Over", style=shape.triangleup, location=location.belowbar, text="XQ", color=color.new(color.blue, 20), size=size.tiny)-plotshape(sQQEx and QQExshort==1 and QQEcshort!=1 and not isShort, title="QQE Cross Under", style=shape.triangledown, location=location.abovebar, text="XQ", color=color.black, transp=20, size=size.tiny)+plotshape(sQQEx and QQExshort==1 and QQEcshort!=1 and not isShort, title="QQE Cross Under", style=shape.triangledown, location=location.abovebar, text="XQ", color=color.new(color.black, 20), size=size.tiny)// Signal crosses zero line-plotshape(sQQEz and QQEzlong==1 and QQEclong!=1 and not isLong and QQExlong!=1, title="QQE Zero Cross Over", style=shape.triangleup, location=location.belowbar, text="XZ", color=color.aqua, transp=20, size=size.tiny)+plotshape(sQQEz and QQEzlong==1 and QQEclong!=1 and not isLong and QQExlong!=1, title="QQE Zero Cross Over", style=shape.triangleup, location=location.belowbar, text="XZ", color=color.new(color.aqua, 20), size=size.tiny)-plotshape(sQQEz and QQEzshort==1 and QQEcshort!=1 and not isShort and QQExshort!=1, title="QQE Zero Cross Under", style=shape.triangledown, location=location.abovebar, text="XZ", color=color.fuchsia, transp=20, size=size.tiny)+plotshape(sQQEz and QQEzshort==1 and QQEcshort!=1 and not isShort and QQExshort!=1, title="QQE Zero Cross Under", style=shape.triangledown, location=location.abovebar, text="XZ", color=color.new(color.fuchsia, 20), size=size.tiny)//⋯if testPeriod- strategy.entry("Long", 1, when=isLong)+ strategy.entry("Long", strategy.long, when=isLong) strategy.close("Long", when=isCloseLong )- strategy.entry("Short", 0, when=isShort)+ strategy.entry("Short", strategy.short, when=isShort) strategy.close("Short", when=isCloseShort )⋯// show only when alert condition is met and bar closed.-plotshape(isLong or isShort,title= "Cross Alert Completed", location=location.bottom, color=isShort?color.red:color.green, transp=0, style=shape.circle,size=size.auto,offset=0)+plotshape(isLong or isShort,title= "Cross Alert Completed", location=location.bottom, color=color.new(isShort?color.red:color.green, 0), style=shape.circle,size=size.auto,offset=0)-plotshape(isCloseShort[1] or isCloseLong[1],title= "Close Order", location=location.top, color=isCloseShort[1]?color.red:color.green, transp=0, style=shape.square,size=size.auto,offset=-1)+plotshape(isCloseShort[1] or isCloseLong[1],title= "Close Order", location=location.top, color=color.new(isCloseShort[1]?color.red:color.green, 0), style=shape.square,size=size.auto,offset=-1)// - ALERTING END⋯Clean v5
VerifiedSame 17 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 118: input() → input.int()
- Line 122: input() → input.bool()
- Line 124: input() → input.int()
- Line 128: input() → input.int()
- Line 132: input() → input.int()
- Line 136: input() → input.int()
19 more edits
- Line 137: input() → input.int()
- Line 139: input() → input.int()
- Line 141: input() → input.bool()
- Line 142: input() → input.bool()
- Line 143: input() → input.bool()
- Line 145: input() → input.string()
- Line 146: input() → input.string()
- Line 148: input() → input.bool()
- Line 149: input() → input.bool()
- Line 150: input() → input.bool()
- Line 151: input() → input.bool()
- Line 152: input() → input.source()
- Line 163: input() → input.int()
- Line 164: input() → input.int()
- Line 165: input() → input.int()
- Line 373: when = isLong → if block
- Line 374: when = isCloseLong → if block
- Line 375: when = isShort → if block
- Line 376: when = isCloseShort → if block
⋯// Use Alternate Anchor TF for MAs -anchor = input(4,minval=0,maxval=100,title="Relative TimeFrame Multiplier for Second MA Ribbon (0=none, max=100)")+anchor = input.int(4,minval=0,maxval=100,title="Relative TimeFrame Multiplier for Second MA Ribbon (0=none, max=100)")//⋯// Fast MA - type, source, length-showAvgs = input(true,title="Show Moving Average Lines")+showAvgs = input.bool(true,title="Show Moving Average Lines")type1 = input.string(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])-len1 = input(defval=16, title="Fast - Length", minval=1)+len1 = input.int(defval=16, title="Fast - Length", minval=1)gamma1 = 0.33⋯type2 = input.string(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])-len2 = input(defval=21, title="Medium - Length", minval=1)+len2 = input.int(defval=21, title="Medium - Length", minval=1)gamma2 = 0.55⋯type3 = input.string(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"])-len3 = input(defval=26, title="Slow Length", minval=1)+len3 = input.int(defval=26, title="Slow Length", minval=1)gamma3 = 0.77⋯// QQE rsi Length, Smoothing, fast ATR factor, source-RSILen = input(14,title='RSI Length')+RSILen = input.int(14,title='RSI Length')-SF = input(8,title='RSI Smoothing Factor')+SF = input.int(8,title='RSI Smoothing Factor')QQEfactor = input.float(5.0,title='Fast QQE Factor')-threshhold = input(10, title="RSI Threshhold")+threshhold = input.int(10, title="RSI Threshhold")//-sQQEx = input(true,title="Show QQE Signal crosses")+sQQEx = input.bool(true,title="Show QQE Signal crosses")-sQQEz = input(false,title="Show QQE Zero crosses")+sQQEz = input.bool(false,title="Show QQE Zero crosses")-sQQEc = input(true,title="Show QQE Thresh Hold Channel Exits")+sQQEc = input.bool(true,title="Show QQE Thresh Hold Channel Exits")//-tradeSignal = input("XC", title="Select which QQE signal to Buy/Sell", options=["XC","XQ","XZ"])+tradeSignal = input.string("XC", title="Select which QQE signal to Buy/Sell", options=["XC","XQ","XZ"])-closeSignal = input("XQ", title="Select which QQE signal to Close Order", options=["XC","XQ","XZ"])+closeSignal = input.string("XQ", title="Select which QQE signal to Close Order", options=["XC","XQ","XZ"])//-xfilter = input(true, title="Filter XQ Buy/Sell Orders by Threshold" )+xfilter = input.bool(true, title="Filter XQ Buy/Sell Orders by Threshold" )-filter = input(false,title="Use Moving Average Filter")+filter = input.bool(false,title="Use Moving Average Filter")-dfilter = input(true, title="Use Trend Directional Filter" )+dfilter = input.bool(true, title="Use Trend Directional Filter" )-ufirst = input(false, title="Only Alert First Buy/Sell in a new Trend")+ufirst = input.bool(false, title="Only Alert First Buy/Sell in a new Trend")-RSIsrc = input(close,title="Source")+RSIsrc = input.source(close,title="Source")src = RSIsrc // MA source⋯//*** START of COMMENT OUT [Alerts]-testStartYear = input(2018, "Backtest Start Year",minval=1980)+testStartYear = input.int(2018, "Backtest Start Year",minval=1980)-testStartMonth = input(6, "Backtest Start Month",minval=1,maxval=12)+testStartMonth = input.int(6, "Backtest Start Month",minval=1,maxval=12)-testStartDay = input(12, "Backtest Start Day",minval=1,maxval=31)+testStartDay = input.int(12, "Backtest Start Day",minval=1,maxval=31)testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)⋯if testPeriod- strategy.entry("Long", strategy.long, when=isLong)+ if isLong+ strategy.entry("Long", strategy.long)- strategy.close("Long", when=isCloseLong )+ if isCloseLong+ strategy.close("Long" )- strategy.entry("Short", strategy.short, when=isShort)+ if isShort+ strategy.entry("Short", strategy.short)- strategy.close("Short", when=isCloseShort )+ if isCloseShort+ strategy.close("Short" )//end if⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L328:C107: cannot use 'int' as a condition — Pine Script v6 does not cast 'int' or 'float' to 'bool' implicitly. Wrap it in bool(). The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
-//@version=5+//@version=6//⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Relative TimeFrame Multiplier for Second MA Ribbon (0=none, max=100) | 4 | int · min 0 · max 100 | mult |
| Show Moving Average Lines | true | bool | MA Fast MA Medium Fast MA Slow MA Fast MA Medium Fast MA Slow |
| Fast MA Type: | EMA | string · options ["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"] | ma_fast ma_fast_alt |
| Fast - Length | 16 | int · min 1 | ma_fast ma_fast_alt |
| Medium MA Type: | EMA | string · options ["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"] | ma_medium ma_medium_alt |
| Medium - Length | 21 | int · min 1 | ma_medium ma_medium_alt |
| Slow MA Type: | EMA | string · options ["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"] | ma_slow ma_slow_alt |
| Slow Length | 26 | int · min 1 | ma_slow ma_slow_alt |
| RSI Length | 14 | int | Wilders_Period Rsi |
| RSI Smoothing Factor | 8 | int | RSIndex |
| Fast QQE Factor | 5.0 | float | DeltaFastAtrRsi |
| RSI Threshhold | 10 | int | QQEclong QQEcshort QQExfilter |
| Show QQE Signal crosses | true | bool | QQE Cross Over QQE Cross Under |
| Show QQE Zero crosses | false | bool | QQE Zero Cross Over QQE Zero Cross Under |
| Show QQE Thresh Hold Channel Exits | true | bool | QQE X Over Channel QQE X Under Channel |
| Select which QQE signal to Buy/Sell | XC | string · options ["XC","XQ","XZ"] | buy_ sell_ closeLong closeShort |
| Select which QQE signal to Close Order | XQ | string · options ["XC","XQ","XZ"] | closeLong closeShort |
| Filter XQ Buy/Sell Orders by Threshold | true | bool | QQExfilter |
| Use Moving Average Filter | false | bool | QQEflong QQEfshort |
| Use Trend Directional Filter | true | bool | QQEflong QQEfshort |
| Only Alert First Buy/Sell in a new Trend | false | bool | buy sell |
| Source | close | source | src srcclose Rsi |
| Backtest Start Year | 2018 | int · min 1980 | testPeriodStart |
| Backtest Start Month | 6 | int · min 1 · max 12 | testPeriodStart |
| Backtest Start Day | 12 | int · min 1 · max 31 | testPeriodStart |
What it calculates
- SMA (
sma,ta.smain v5)src, lenv5 · line 198 - EMA (
ema,ta.emain v5)src, lenema1 · line 201 - EMA (
ema,ta.emain v5)ema1, lenema2 · line 202 - EMA (
ema,ta.emain v5)src, lenv2 · line 211 - EMA (
ema,ta.emain v5)v2, lenv6 · line 208 - EMA (
ema,ta.emain v5)v2, lenv7 · line 212 - RSI (
rsi,ta.rsiin v5)Source (close), RSI Length (14)Rsi · line 253 - EMA (
ema,ta.emain v5)Rsi, RSI Smoothing Factor (8)RSIndex · line 254 - EMA (
ema,ta.emain v5)AtrRsi, Wilders_PeriodMaAtrRsi · line 256 - EMA (
ema,ta.emain v5)MaAtrRsi, Wilders_PeriodDeltaFastAtrRsi · line 257 - Cross (
cross,ta.crossin v5)RSIndex, shortband[1]trend · line 266 - Rising / Falling (
rising,ta.risingin v5)ma_medium, 3direction · line 278 - Rising / Falling (
rising,ta.risingin v5)ma_medium_alt, 3altDirection · line 279
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 102 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | isLongchange(tradestate) and tradestate==1 | On | 373 |
| Close Long | isCloseLongchange(tradestate) and tradestate==0 and nz(tradestate[1])==1 | On | 374 |
| Open Short short | isShortchange(tradestate) and tradestate==2 | On | 375 |
| Close Short | isCloseShortchange(tradestate) and tradestate==0 and nz(tradestate[1])==2 | On | 376 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo calc_on_every_tick. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +76,395.0% | +102.5% | 18 | 39% | 1.15 | 11,732.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +42,782.4% | +5.5% | 23 | 22% | 1.21 | 62,470.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +25,145.7% | +9.8% | 17 | 59% | 3.13 | 244.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +3,945.9% | +8.4% | 18 | 33% | 1.73 | 1,688.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −923.5% | +16.7% | 25 | 32% | 0.63 | 306.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −49.2% | +21.5% | 21 | 24% | 0.87 | 112.9% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1,927.5% | +148.3% | 19 | 53% | 1.43 | 149.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −1,062.0% | +17.1% | 21 | 19% | 0.53 | 1,153.5% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 3 | 67% | $10,316,900.00 |
| Sideways market | 11 | 27% | −$3,515,450.00 |
| Falling market | 4 | 50% | $838,021.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
1,271,800.0%
The 95th percentile of the worst drawdown when the same 18 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 119.6% to 1,271,800.0%; the tick is what actually happened, 192.4%. Order luck alone moves it this much.
All 18 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 18 Feb 2024 51,642.60 | 15 Apr 2024 65,661.90 | 57 | $13,901,900.00 |
| short | 25 Jun 2024 60,293.30 | 15 Jul 2024 60,797.90 | 20 | −$625,701.00 |
| long | 24 Jul 2024 65,936.00 | 3 Aug 2024 61,498.30 | 10 | −$4,565,090.00 |
| short | 8 Aug 2024 55,133.80 | 22 Aug 2024 61,156.00 | 14 | −$6,138,560.00 |
| short | 10 Sep 2024 57,042.00 | 14 Sep 2024 60,498.00 | 4 | −$3,573,520.00 |
| long | 26 Sep 2024 63,152.00 | 3 Oct 2024 60,649.30 | 7 | −$2,626,540.00 |
| long | 19 Oct 2024 68,428.00 | 5 Nov 2024 67,850.00 | 17 | −$714,258.00 |
| short | 28 Feb 2025 84,708.60 | 4 Mar 2025 86,221.20 | 4 | −$1,683,520.00 |
| short | 11 Mar 2025 78,595.90 | 13 Apr 2025 85,276.90 | 33 | −$6,844,920.00 |
| long | 26 Apr 2025 94,638.70 | 27 May 2025 109,435.00 | 31 | $14,592,000.00 |
| long | 12 Jul 2025 117,528.00 | 27 Jul 2025 117,920.00 | 15 | $156,882.00 |
| long | 6 Oct 2025 123,482.00 | 12 Oct 2025 110,644.00 | 6 | −$13,072,000.00 |
| short | 8 Nov 2025 103,339.00 | 28 Nov 2025 91,333.90 | 20 | $11,810,500.00 |
| long | 17 Jan 2026 95,550.90 | 21 Jan 2026 88,427.70 | 4 | −$7,307,260.00 |
| short | 2 Feb 2026 76,968.20 | 16 Feb 2026 68,832.60 | 14 | $7,989,830.00 |
| long | 19 Apr 2026 75,691.80 | 16 May 2026 79,113.20 | 27 | $3,266,640.00 |
| short | 2 Jun 2026 71,408.90 | 14 Jun 2026 64,458.00 | 12 | $6,815,020.00 |
| long | 23 Sep 2026 86,208.60 | 10 Oct 2026 82,635.60 | 17 | −$3,741,850.00 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Momentum Trading By Mahfuz Azim this page | — | v3 strategy | +76,395.0% | 18 | 11,732.8% |
| EMA Stoch Strategy For ProfitView by treigen | rsi sma ema cross | v5 strategy | −69.8% | 5 | 80.6% |
| RSI Rising Crypto Trending Strategy by exlux99 | sma ema rsi rising | v4 strategy | No trades on this market | ||
| Moving Average Crossover Strategy by Decam9 | sma ema rsi cross | v5 strategy | +0.0% | 75 | 4.3% |
| Strategy Myth-Busting #13 - MultiEMA+BXTrender - [SP-MYN] by myncrypto | ema rsi rising falling | v5 strategy | −0.3% | 2 | 0.3% |
| MACD, RSI, & RVOL Strategy by BobBarker42069 | rsi ema sma | v4 strategy | +32.6% | 1 | 1.5% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 62 ms | With a workaround 1,000 bars in 424 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as 1, 0 as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 373, 375) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
//@version=3////*** START of COMMENT OUT [Alerts]strategy(title="Momentum Trading By Mahfuz Azim", shorttitle="Momentum Trading v1.6 By Mahfuz Azim", overlay=true, pyramiding=0, default_qty_value=1000, commission_value=0.1, commission_type=strategy.commission.percent, initial_capital=10000)//*** END of COMMENT OUT [Alerts]//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<////>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>////*** START of COMMENT OUT [BackTest]//study(title="[Alerts]QQE Cross v6.0 by Mahfuz Azim", shorttitle="[AL]QQEX v6.0", overlay=true,max_bars_back=2000)//*** END of COMMENT OUT [BackTest]//// Author: Mahfuz Azim// Date: 21-April-2021// Version: v 0.1.5 , Major Release July-2021//// Description:// A following indicator is Trend following that uses fast QQE crosses with Moving Averages// for trend direction filtering. QQE or Qualitative Quantitative Estimation is based // on the relative strength index (RSI), but uses a smoothing technique as an additional // transformation. Three crosses can be selected (all selected by default): // - Smooth RSI signal crossing ZERO (XZ)// - Smooth RSI signal crossing Fast QQE line (XQ), this is like an early warning swing signal.// - Smooth RSI signal exiting the RSI Threshhold Channel (XC), this is like a confirmed swing signal.// An optimumal Smooth RSI threshold level is between 5% and 10% (default=10), it helps reduce// the false swings.// These signals can be selected to Open Short/Long and/or Close a trade, default is XC open// trade and XQ (or opposite open) to Close trade.//// The (LONG/SHORT) alerts can be optionally filtered by the Moving Average Ribbons:// - For LONG alert the Close must be above the fast MA Ribbon and // fast MA Ribbon must be above the slow MA Ribbon.// - For SHORT alert the Close must be below the fast MA Ribbon and// fast MA Ribbon must be below the slow MA Ribbon.// and/or directional filter:// - For LONG alert the Close must be above the medium MA and the// directional of both MA ribbons must be Bullish.// - For SELL alert the Close must be below the medium MA and the// directional of both MA ribbons must be Bearish.//// This indicator is designed to be used as a Signal to Signal trading BOT // in automatic or semi-automatic way (start and stop when conditions are suitable).// - For LONG and SHORT alerts I recommend you use "Once per Bar" alarm option// - For CLOSE alerts I recommend you use "Once per Bar Close" alarm option// (* The script has been designed so that long/short signals come at start of candles *)// (* and close signals come at the end of candles *)//// Mofidifications:// 0.1.5 - Major Release Version// - Added second MA ribbon to help filter signals to the trend direction.// - Modified Alert filtering to include second MA Ribbon// - Change default settings to reflect Signal to Signal BOT parameters.// - Removed older redunant alerts.//// 0.1.4 - Development series//// 0.1.3 - Fix bug with painting Buy/Sell arrows when non-repaint shunt mode selected.// - Added option to alert just the first Buy/Sell alert after a trend swing// - Added Long and Short Alarms. When combined with the "first Buy/Sell" in trend option,// It is now possible to use this indicator to interface with AutoView // or ProfitView. I suggest using the "QQEX XZ Alert" alarm to exit Long or Short// trade. Use only "Once per bar Close" option for Alarms. This is not a full// fledged trading BOT though with TP/SL settings.//// - Changed QQE defaults to be a bit smoother (8, 5, 3) instead of (6, 3, 2.618).//// 0.1.21 - Added implied GPL copyright notice.// - Changed defaults to use HullMAs instead of EMAs.// 0.1.2 - No repaint on BUY/SELL alert, however, now trades should be taken when the BUY/SELL// Alert is displayed. The alarm is still generated on the previous candle so you can// still get a pre-warning, this enables you time to analyse the pending alert.// - Added option to test success of alerted trades, highlight successful and failed trade bars// and show simple stats: success rate and number of trades (out of 5000), this will help// tune the settings for timeframe and currency PAIR.// 0.1.1 - Added code to use the medium moving average (EMA20) rising/falling for additional// trend direction filter.// - Remove Moving Average cross over signals and other options not used in this indicator.// - Added code to distinguish between the crosses, now only show Thresh Hold crosses as BUY/SELL// alerts.// - Modidied default settings to more well known MA's and slightly different QQE settings, these// work well at lower timeframes.// - Added circle plots at bottom of chart to show when actual BUY/SELL alerts occur.// 0.1 - original//// References:// Some Code borrowed from:// - "Scalp Jockey - MTF MA Cross Visual Strategizer by JayRogers"// - "QQE MT4 by glaz"// Inspiration from:// - http://www.forexstrategiesresources.com/binary-options-strategies-ii/189-aurora-binary-trading/// - http://www.forexstrategiesresources.com/metatrader-4-trading-systems-v/652-qqe-smoothed-trading/// - http://dewinforex.com/forex-indicators/qqe-indicator-not-quite-grail-but-accurately-defines-trend-and-flat.html// - "Binary option trading by two previous bars" by radixvinni////// -----------------------------------------------------------------------------// //// Copyright 2021 Mahfuz Azim//// This program is free software: you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation, either version 3 of the License, or// any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// // The GNU General Public License can be found here// <http://www.gnu.org/licenses/>.//// -----------------------------------------------------------------------------//// Use Alternate Anchor TF for MAs anchor = input(4,minval=0,maxval=100,title="Relative TimeFrame Multiplier for Second MA Ribbon (0=none, max=100)")//// - INPUTS START// Fast MA - type, source, lengthshowAvgs = input(true,title="Show Moving Average Lines")type1 = input(defval="EMA", title="Fast MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)len1 = input(defval=16, title="Fast - Length", minval=1)gamma1 = 0.33// Medium Fast MA - type, source, lengthtype2 = input(defval="EMA", title="Medium MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)len2 = input(defval=21, title="Medium - Length", minval=1)gamma2 = 0.55// Slow MA - type, source, lengthtype3 = input(defval="EMA", title="Slow MA Type: ", options=["SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "ZEMA", "TMA", "SSMA"], type=string)len3 = input(defval=26, title="Slow Length", minval=1)gamma3 = 0.77//// QQE rsi Length, Smoothing, fast ATR factor, sourceRSILen = input(14,title='RSI Length')SF = input(8,title='RSI Smoothing Factor')QQEfactor = input(5.0,type=float,title='Fast QQE Factor')threshhold = input(10, title="RSI Threshhold")//sQQEx = input(true,title="Show QQE Signal crosses")sQQEz = input(false,title="Show QQE Zero crosses")sQQEc = input(true,title="Show QQE Thresh Hold Channel Exits")//tradeSignal = input("XC", title="Select which QQE signal to Buy/Sell", options=["XC","XQ","XZ"])closeSignal = input("XQ", title="Select which QQE signal to Close Order", options=["XC","XQ","XZ"])//xfilter = input(true, title="Filter XQ Buy/Sell Orders by Threshold" )filter = input(false,title="Use Moving Average Filter")dfilter = input(true, title="Use Trend Directional Filter" )ufirst = input(false, title="Only Alert First Buy/Sell in a new Trend")RSIsrc = input(close,title="Source")src = RSIsrc // MA sourcesrcclose= RSIsrc/////////////////////////////////////////////////* Backtesting Period Selector | Component *///////////////////////////////////////////////////* https://www.tradingview.com/script/eCC1cvxQ-Backtesting-Period-Selector-Component *////* https://www.tradingview.com/u/pbergden/ *////* Modifications made by Mahfuz Azim*////>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>////*** START of COMMENT OUT [Alerts]testStartYear = input(2018, "Backtest Start Year",minval=1980)testStartMonth = input(6, "Backtest Start Month",minval=1,maxval=12)testStartDay = input(12, "Backtest Start Day",minval=1,maxval=31)testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)testStopYear = 9999 //input(9999, "Backtest Stop Year",minval=1980)testStopMonth = 12 // input(12, "Backtest Stop Month",minval=1,maxval=12)testStopDay = 31 //input(31, "Backtest Stop Day",minval=1,maxval=31)testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)testPeriod = time >= testPeriodStart and time <= testPeriodStop ? true : false//*** END of COMMENT OUT [Alerts]//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<//// - INPUTS ENDgold = #FFD700AQUA = #00FFFFFFBLUE = #0000FFFFRED = #FF0000FFLIME = #00FF00FFGRAY = #808080FF// - FUNCTIONS// - variant(type, src, len, gamma)// Returns MA input selection variant, default to SMA if blank or typo.// SuperSmoother filter// © 2013 John F. Ehlersvariant_supersmoother(src,len) => a1 = exp(-1.414*3.14159 / len) b1 = 2*a1*cos(1.414*3.14159 / len) c2 = b1 c3 = (-a1)*a1 c1 = 1 - c2 - c3 v9 = 0.0 v9 := c1*(src + nz(src[1])) / 2 + c2*nz(v9[1]) + c3*nz(v9[2]) v9 variant_smoothed(src,len) => v5 = 0.0 v5 := na(v5[1]) ? sma(src, len) : (v5[1] * (len - 1) + src) / len v5variant_zerolagema(src,len) => ema1 = ema(src, len) ema2 = ema(ema1, len) v10 = ema1+(ema1-ema2) v10 variant_doubleema(src,len) => v2 = ema(src, len) v6 = 2 * v2 - ema(v2, len) v6variant_tripleema(src,len) => v2 = ema(src, len) v7 = 3 * (v2 - ema(v2, len)) + ema(ema(v2, len), len) // Triple Exponential v7 //calc Laguerrevariant_lag(p,g) => L0 = 0.0 L1 = 0.0 L2 = 0.0 L3 = 0.0 L0 := (1 - g)*p+g*nz(L0[1]) L1 := -g*L0+nz(L0[1])+g*nz(L1[1]) L2 := -g*L1+nz(L1[1])+g*nz(L2[1]) L3 := -g*L2+nz(L2[1])+g*nz(L3[1]) f = (L0 + 2*L1 + 2*L2 + L3)/6 f// return variant, defaults to SMA variant(type, src, len, g) => type=="EMA" ? ema(src,len) : type=="WMA" ? wma(src,len): type=="VWMA" ? vwma(src,len) : type=="SMMA" ? variant_smoothed(src,len) : type=="DEMA" ? variant_doubleema(src,len): type=="TEMA" ? variant_tripleema(src,len): type=="LAGMA" ? variant_lag(src,g) : type=="HullMA"? wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) : type=="SSMA" ? variant_supersmoother(src,len) : type=="ZEMA" ? variant_zerolagema(src,len) : type=="TMA" ? sma(sma(src,len),len) : sma(src,len)// - /variant // If have anchor specified, calculate the base multiplier, base on time in mins//mult = isintraday ? anchor==0 or interval<=0 or interval>=anchor or anchor>1440? 1 : round(anchor/interval) : 1//mult := not isintraday? 1 : mult // Only available Daily or less// Anchor is a relative multiplier based on current TF.mult = anchor>0 ? anchor : 1 // - FUNCTIONS END // - Fast ATR QQE//Wilders_Period = RSILen * 2 - 1//Rsi = rsi(RSIsrc,RSILen)RSIndex = ema(Rsi, SF)AtrRsi = abs(RSIndex[1] - RSIndex)MaAtrRsi = ema(AtrRsi, Wilders_Period)DeltaFastAtrRsi = ema(MaAtrRsi,Wilders_Period) * QQEfactor//newshortband= RSIndex + DeltaFastAtrRsinewlongband= RSIndex - DeltaFastAtrRsilongband = 0.0shortband=0.0trend = 0longband:=RSIndex[1] > longband[1] and RSIndex > longband[1] ? max(longband[1],newlongband) : newlongbandshortband:=RSIndex[1] < shortband[1] and RSIndex < shortband[1] ? min(shortband[1],newshortband) : newshortbandtrend:=cross(RSIndex, shortband[1])? 1 : cross(longband[1], RSIndex) ? -1 : nz(trend[1],1)FastAtrRsiTL = trend==1 ? longband : shortband// - SERIES VARIABLES// MA'sma_fast = variant(type1, srcclose, len1, gamma1)ma_medium = variant(type2, srcclose, len2, gamma2)ma_slow = variant(type3, srcclose, len3, gamma3)// MA'sma_fast_alt = variant(type1, srcclose, len1*mult, gamma1)ma_medium_alt = variant(type2, srcclose, len2*mult, gamma2)ma_slow_alt = variant(type3, srcclose, len3*mult, gamma3)// Get Direction From Medium Moving Averagedirection = rising(ma_medium,3) ? 1 : falling(ma_medium,3) ? -1 : 0altDirection = rising(ma_medium_alt,3) ? 1 : falling(ma_medium_alt,3) ? -1 : 0//// Find all the QQE CrossesQQExlong = 0, QQExlong := nz(QQExlong[1])QQExshort = 0, QQExshort := nz(QQExshort[1])QQExlong := FastAtrRsiTL< RSIndex ? QQExlong+1 : 0QQExshort := FastAtrRsiTL> RSIndex ? QQExshort+1 : 0// Zero crossQQEzlong = 0, QQEzlong := nz(QQEzlong[1])QQEzshort = 0, QQEzshort := nz(QQEzshort[1])QQEzlong := RSIndex>=50 ? QQEzlong+1 : 0QQEzshort := RSIndex<50 ? QQEzshort+1 : 0// // Thresh Hold channel Crosses give the BUY/SELL alerts.QQEclong = 0, QQEclong := nz(QQEclong[1])QQEcshort = 0, QQEcshort := nz(QQEcshort[1])QQEclong := RSIndex>(50+threshhold) ? QQEclong+1 : 0QQEcshort := RSIndex<(50-threshhold) ? QQEcshort+1 : 0//// Check Filtering.QQEflong = mult == 1 ? (not filter or (srcclose>ma_medium and ma_medium>ma_slow and ma_fast>ma_medium)) and (not dfilter or (direction>0 )) : (not filter or (ma_medium>ma_medium_alt and srcclose>ma_fast and ma_fast>ma_medium)) and (not dfilter or (direction>0 and altDirection>0 and srcclose>ma_medium))QQEfshort = mult == 1 ? (not filter or (srcclose<ma_medium and ma_medium<ma_slow and ma_fast<ma_medium)) and (not dfilter or (direction<0 )) : (not filter or (ma_medium<ma_medium_alt and srcclose<ma_fast and ma_fast<ma_medium)) and (not dfilter or (direction<0 and altDirection<0 and srcclose<ma_medium)) QQExfilter = (not xfilter or RSIndex>(50+threshhold) or RSIndex<(50-threshhold))//// Get final BUY / SELL alert determinationbuy_ = 0, buy_ := nz(buy_[1])sell_ = 0, sell_ := nz(sell_[1])// Make sure Buy/Sell are non-repaint and occur after close signal.buy_ := tradeSignal=="XC"? (QQEclong[1]==1 and QQEflong[1] ? buy_+1 : 0) : tradeSignal=="XQ"? (QQExlong[1]==1 and QQEflong[1] and QQExfilter[1]? buy_+1 : 0) : tradeSignal=="XZ"? (QQEzlong[1]==1 and QQEflong[1] ? buy_+1 : 0) : 0sell_ := tradeSignal=="XC"? (QQEcshort[1]==1 and QQEfshort[1] ? sell_+1 : 0) : tradeSignal=="XQ"? (QQExshort[1]==1 and QQEfshort[1] and QQExfilter[1]? sell_+1 : 0) : tradeSignal=="XZ"? (QQEzshort[1]==1 and QQEfshort[1] ? sell_+1 : 0) : 0//// Find the first Buy/Sell in trend swing.Buy = 0, Buy := nz(Buy[1])Sell = 0, Sell := nz(Sell[1])Buy := sell_>0 ? 0 : buy_==1 or Buy>0 ? Buy+1 : BuySell := buy_>0 ? 0 : sell_==1 or Sell>0 ? Sell+1 : Sell// Select First or all buy/sell alerts.buy = ufirst ? Buy : buy_sell = ufirst ? Sell : sell_closeLong = 0, closeLong := nz(closeLong[1])closeShort = 0, closeShort := nz(closeShort[1])closeLong := closeSignal=="XC" ? (QQEcshort==1 ? closeLong+1 : 0) : closeSignal=="XQ" ? tradeSignal=="XQ" ? (QQExshort==1 ? closeLong+1 : 0) : ((QQExshort==1 or QQEzshort or QQEcshort) ? closeLong+1 : 0) : closeSignal=="XZ" ? (QQEzshort==1 ? closeLong+1 : 0) : 0closeShort := closeSignal=="XC" ? (QQEclong==1 ? closeShort+1 : 0) : closeSignal=="XQ" ? tradeSignal=="XQ" ? (QQExlong==1 ? closeShort+1 : 0) : ((QQExlong==1 or QQEzlong or QQEclong==1) ? closeShort+1 : 0) : closeSignal=="XZ" ? (QQEzlong==1 ? closeShort+1 : 0) : 0tradestate = 0, tradestate := nz(tradestate[1])tradestate := tradestate==0 ? (buy==1 ? 1 : sell==1 ? 2 : 0) : (tradestate==1 and closeLong==1) or (tradestate==2 and closeShort==1)? 0 : tradestate isLong = change(tradestate) and tradestate==1isShort = change(tradestate) and tradestate==2isCloseLong = change(tradestate) and tradestate==0 and nz(tradestate[1])==1isCloseShort = change(tradestate) and tradestate==0 and nz(tradestate[1])==2// - SERIES VARIABLES END// - PLOTTING// Ma'stcolor = direction<0?red:greenma1=plot(showAvgs?ma_fast:na, title="MA Fast", color=tcolor, linewidth=1, transp=0)ma2=plot(showAvgs?ma_medium:na, title="MA Medium Fast", color=tcolor, linewidth=2, transp=0)ma3=plot(showAvgs?ma_slow:na, title="MA Slow", color=tcolor, linewidth=1, transp=0)fill(ma1,ma3,color=tcolor,transp=90)// Ma'saltTcolor=altDirection<0?blue:aquama4=plot(showAvgs and mult>1?ma_fast_alt:na, title="MA Fast", color=altTcolor, linewidth=1, transp=0)ma5=plot(showAvgs and mult>1?ma_medium_alt:na, title="MA Medium Fast", color=altTcolor, linewidth=2, transp=0)ma6=plot(showAvgs and mult>1?ma_slow_alt:na, title="MA Slow", color=altTcolor, linewidth=1, transp=0)fill(ma4,ma6,color=altTcolor,transp=90)// QQE exit from Thresh Hold Channelplotshape(sQQEc and QQEclong==1 and not isLong, title="QQE X Over Channel", style=shape.triangleup, location=location.belowbar, text="XC", color=olive, transp=20, size=size.tiny)plotshape(sQQEc and QQEcshort==1 and not isShort, title="QQE X Under Channel", style=shape.triangledown, location=location.abovebar, text="XC", color=red, transp=20, size=size.tiny)// QQE crossesplotshape(sQQEx and QQExlong==1 and QQEclong!=1 and not isLong, title="QQE Cross Over", style=shape.triangleup, location=location.belowbar, text="XQ", color=blue, transp=20, size=size.tiny)plotshape(sQQEx and QQExshort==1 and QQEcshort!=1 and not isShort, title="QQE Cross Under", style=shape.triangledown, location=location.abovebar, text="XQ", color=black, transp=20, size=size.tiny)// Signal crosses zero lineplotshape(sQQEz and QQEzlong==1 and QQEclong!=1 and not isLong and QQExlong!=1, title="QQE Zero Cross Over", style=shape.triangleup, location=location.belowbar, text="XZ", color=aqua, transp=20, size=size.tiny)plotshape(sQQEz and QQEzshort==1 and QQEcshort!=1 and not isShort and QQExshort!=1, title="QQE Zero Cross Under", style=shape.triangledown, location=location.abovebar, text="XZ", color=fuchsia, transp=20, size=size.tiny)////*** START of COMMENT OUT [BackTest]//plotshape(isLong, title="QQEX Long", style=shape.arrowup, location=location.belowbar, text="Open\nLONG", color=lime, textcolor=green, transp=0, size=size.small)//plotshape(isShort, title="QQEX Short", style=shape.arrowdown, location=location.abovebar, text="Open\nSHORT", color=red, textcolor=maroon, transp=0, size=size.small)//plotshape(isCloseLong, title="QQEX Close Long", style=shape.arrowdown, location=location.abovebar, text="Close\nLONG", color=gray, textcolor=gray, transp=0, size=size.small)//plotshape(isCloseShort, title="QQEX Close Short", style=shape.arrowup, location=location.belowbar, text="Close\nSHORT", color=gray, textcolor=gray, transp=0, size=size.small)//*** END of COMMENT OUT [BackTest]// - PLOTTING END// - ALERTING//*** START of COMMENT OUT [Alerts]if testPeriod strategy.entry("Long", 1, when=isLong) strategy.close("Long", when=isCloseLong ) strategy.entry("Short", 0, when=isShort) strategy.close("Short", when=isCloseShort )//end if//*** END of COMMENT OUT [Alerts]//*** START of COMMENT OUT [BackTest]//// Signal to Signal BOT Alerts.////alertcondition(isLong, title="QQEX Long", message="QQEX LONG") // use "Once per Bar" option//alertcondition(isShort, title="QQEX Short", message="QQEX SHORT") // use "Once per Bar" option//alertcondition(isCloseLong, title="QQEX Close Long", message="QQEX CLOSE LONG") // use "Once per Bar Close" option//alertcondition(isCloseShort, title="QQEX Close Short", message="QQEX CLOSE SHORT") // use "Once per Bar Close" option////*** END of COMMENT OUT [BackTest]// show only when alert condition is met and bar closed.plotshape(isLong or isShort,title= "Cross Alert Completed", location=location.bottom, color=isShort?red:green, transp=0, style=shape.circle,size=size.auto,offset=0)plotshape(isCloseShort[1] or isCloseLong[1],title= "Close Order", location=location.top, color=isCloseShort[1]?red:green, transp=0, style=shape.square,size=size.auto,offset=-1)// - ALERTING END//EOF