Pine Script v4 · Strategy
Momentum Strategy (BTC-USDT; 1h) - MACD (with source code)
A Pine Script v4 strategy built on 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 commits 100% of equity and pays 0.075% commission. 26 settings; defaults include Fast Length 12, Slow Length 26 and Source close.
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 Strategy (BTC-USDT; 1h) - MACD (with source code) 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 Momentum Strategy (BTC-USDT; 1h) - MACD (with source code) do on BTCUSDT 1D with default settings?
- −14.96%
- From 26 Jan 2024 to 21 Oct 2026, over 41 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Momentum Strategy (BTC-USDT; 1h) - MACD (with source code) 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 Momentum Strategy (BTC-USDT; 1h) - MACD (with source code) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Momentum Strategy (BTC-USDT; 1h) - MACD (with source code) run in Zeal Terminal?
- Yes
- It compiles in 53 ms and runs 1,000 bars in 91 ms, in the browser.
BTCUSDT · 1D · default settings
- MACD
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −14.96%
- −$14.96
- Buy and hold
- +102.5%
- same period
- Closed trades
- 41
- 21 won · 20 lost
- Profitable
- 51.2%
- of closed trades
- Profit factor
- 0.87
- gross profit ÷ gross loss
- Max drawdown
- 40.13%
- −$43.93
One run with the author's default settings, $100 starting capital, commission as the script sets it. A short position was still open at the last bar. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses transp =, which v5 removed line 58
transp = on line 58, 59. 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 ema → ta.ema, sma → ta.sma, wma → ta.wma, rma → ta.rma, hma → ta.hma, pow → math.pow, linreg → ta.linreg, change → ta.change, crossover → ta.crossover, crossunder → ta.crossunder, rsi → ta.rsi.
v4 → v5
VerifiedSame 41 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 7: input(type = input.integer) → input.int()
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.source) → input.source()
- Line 10: input(type = input.integer) → input.int()
- Line 15: type is reserved in v5: renamed type_
89 more edits
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: ema → ta.ema
- Line 16: sma → ta.sma
- Line 16: wma → ta.wma
- Line 16: rma → ta.rma
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: ema → ta.ema
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: sma → ta.sma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: wma → ta.wma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: rma → ta.rma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: hma → ta.hma
- Line 16: ema → ta.ema
- Line 35: pow → math.pow
- Line 41: linreg → ta.linreg
- Line 47: change → ta.change
- Line 48: change → ta.change
- Line 51: crossover → ta.crossover
- Line 52: crossunder → ta.crossunder
- Line 55: change → ta.change
- Line 56: change → ta.change
- Line 58: change → ta.change
- Line 58: change → ta.change
- Line 58: transp = 0 → color.new(change(macd)>0 and on_minmax?color.white:change(macd)<0 and on_minmax?color.purple:color.blue, 0)
- Line 59: transp = 0 → color.new(col_signal, 0)
- Line 64: rsi → ta.rsi
- Line 67: change → ta.change
- Line 67: change → ta.change
- Line 68: change → ta.change
- Line 68: change → ta.change
- Line 78: change → ta.change
- Line 78: change → ta.change
- Line 79: change → ta.change
- Line 79: change → ta.change
- Line 87: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 91: strategy.entry(…, false) → strategy.entry(…, strategy.short)
⋯// © Drun30-//@version=4+//@version=5strategy("MACD+RSI", initial_capital=100, overlay=false,default_qty_type=strategy.percent_of_equity,default_qty_value=100,commission_type="percent",commission_value=0.075)⋯// Getting inputs-fast_length = input(title="Fast Length", type=input.integer, defval=12,group="MACD")+fast_length = input.int(title="Fast Length", defval=12,group="MACD")-slow_length = input(title="Slow Length", type=input.integer, defval=26,group="MACD")+slow_length = input.int(title="Slow Length", defval=26,group="MACD")-src = input(title="Source", type=input.source, defval=close,group="MACD")+src = input.source(title="Source", defval=close,group="MACD")-signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9,group="MACD")+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9,group="MACD")sma_source1 = input(title="Simple MA FAST (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")⋯// Calculating-ma(source,length,type)=>+ma(source,length,type_)=>- type=="FEMA"?4*ema(source,length)-ema(ema(ema(ema(source,length),length),length),length):type=="FSMA"?4*sma(source,length)-sma(sma(sma(sma(source,length),length),length),length):type=="FWMA"?4*wma(source,length)-wma(wma(wma(wma(source,length),length),length),length):type=="FRMA"?4*rma(source,length)-rma(rma(rma(rma(source,length),length),length),length):type=="TEMA"?3*ema(source,length)-ema(ema(ema(source,length),length),length):type=="TSMA"?3*sma(source,length)-sma(sma(sma(source,length),length),length):type=="TWMA"?3*wma(source,length)-wma(wma(wma(source,length),length),length):type=="TRMA"?3*rma(source,length)-rma(rma(rma(source,length),length),length):type=="EMA"?ema(source,length):type=="SMA"?sma(source,length):type=="WMA"?wma(source,length):type=="RMA"?rma(source,length):type=="DEMA"?2*ema(source,length)-ema(ema(source,length),length):type=="DSMA"?2*sma(source,length)-sma(sma(source,length),length):type=="DWMA"?2*wma(source,length)-wma(wma(source,length),length):type=="DRMA"?2*rma(source,length)-rma(rma(source,length),length):type=="HMA"?hma(source,length):type=="DHMA"?2*hma(source,length)-hma(hma(source,length),length):type=="THMA"?3*hma(source,length)-hma(hma(hma(source,length),length),length):type=="FHMA"?4*hma(source,length)-hma(hma(hma(hma(source,length),length),length),length):ema(source,length)+ type_=="FEMA"?4*ta.ema(source,length)-ta.ema(ta.ema(ta.ema(ta.ema(source,length),length),length),length):type_=="FSMA"?4*ta.sma(source,length)-ta.sma(ta.sma(ta.sma(ta.sma(source,length),length),length),length):type_=="FWMA"?4*ta.wma(source,length)-ta.wma(ta.wma(ta.wma(ta.wma(source,length),length),length),length):type_=="FRMA"?4*ta.rma(source,length)-ta.rma(ta.rma(ta.rma(ta.rma(source,length),length),length),length):type_=="TEMA"?3*ta.ema(source,length)-ta.ema(ta.ema(ta.ema(source,length),length),length):type_=="TSMA"?3*ta.sma(source,length)-ta.sma(ta.sma(ta.sma(source,length),length),length):type_=="TWMA"?3*ta.wma(source,length)-ta.wma(ta.wma(ta.wma(source,length),length),length):type_=="TRMA"?3*ta.rma(source,length)-ta.rma(ta.rma(ta.rma(source,length),length),length):type_=="EMA"?ta.ema(source,length):type_=="SMA"?ta.sma(source,length):type_=="WMA"?ta.wma(source,length):type_=="RMA"?ta.rma(source,length):type_=="DEMA"?2*ta.ema(source,length)-ta.ema(ta.ema(source,length),length):type_=="DSMA"?2*ta.sma(source,length)-ta.sma(ta.sma(source,length),length):type_=="DWMA"?2*ta.wma(source,length)-ta.wma(ta.wma(source,length),length):type_=="DRMA"?2*ta.rma(source,length)-ta.rma(ta.rma(source,length),length):type_=="HMA"?ta.hma(source,length):type_=="DHMA"?2*ta.hma(source,length)-ta.hma(ta.hma(source,length),length):type_=="THMA"?3*ta.hma(source,length)-ta.hma(ta.hma(ta.hma(source,length),length),length):type_=="FHMA"?4*ta.hma(source,length)-ta.hma(ta.hma(ta.hma(ta.hma(source,length),length),length),length):ta.ema(source,length)fast_ma = ma(src,fast_length,sma_source1) ⋯ if not na(macd[1])- macd:=pow((macd*(recent_stress)),level)+(1-recent_stress*macd[1])+ macd:=math.pow((macd*(recent_stress)),level)+(1-recent_stress*macd[1])use_ma= input(true,title="Use moving average (MACD)?",group="Moving Average")⋯if use_linreg- macd:=linreg(macd,input(10,title="Length",group="Linear Regression"),input(1,title="Offset",group="Linear Regression"))+ macd:=ta.linreg(macd,input(10,title="Length",group="Linear Regression"),input(1,title="Offset",group="Linear Regression"))//macd == linea blu (differenza tra media mobile veloce e media mobile lenta)⋯on_minmax = input(true,title="Use min/max macd",group="Condition entry/exit")-aperturaLong = change(macd)>0//crossover(macd,signal)+aperturaLong = ta.change(macd)>0//crossover(macd,signal)-aperturashort=not (change(macd)>0)//crossunder(macd,signal)+aperturashort=not (ta.change(macd)>0)//crossunder(macd,signal)if on_cross on_minmax:=false- aperturaLong := crossover(macd,signal)+ aperturaLong := ta.crossover(macd,signal)- aperturashort := crossunder(macd,signal)+ aperturashort := ta.crossunder(macd,signal)if on_minmax on_cross:=false- aperturaLong := change(macd)>0//crossover(macd,signal)+ aperturaLong := ta.change(macd)>0//crossover(macd,signal)- aperturashort:=change(macd)<0//crossunder(macd,signal)+ aperturashort:=ta.change(macd)<0//crossunder(macd,signal)//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )-plot(macd, title="MACD", color=change(macd)>0 and on_minmax?color.white:change(macd)<0 and on_minmax?color.purple:color.blue, transp=0)+plot(macd, title="MACD", color=color.new(ta.change(macd)>0 and on_minmax?color.white:ta.change(macd)<0 and on_minmax?color.purple:color.blue, 0))-plot(on_cross?signal:na, title="Signal", color=col_signal, transp=0)+plot(on_cross?signal:na, title="Signal", color=color.new(col_signal, 0))rsiFilter = input(false,title="Use RSI filter?",group="RSI")⋯srcr=input(close,title="RSI source",group="RSI")-rsi=rsi(srcr,len)+rsi=ta.rsi(srcr,len)ovb=input(90,title="Overbought height",group="RSI") ovs=input(44,title="Oversold height",group="RSI")-okLong=rsi<ovb and change(macd)>0 and change(macd)[1]<=0+okLong=rsi<ovb and ta.change(macd)>0 and ta.change(macd)[1]<=0-okShort=rsi>ovs and change(macd)<0 and change(macd)[1]>=0+okShort=rsi>ovs and ta.change(macd)<0 and ta.change(macd)[1]>=0if not rsiFilter⋯if rsiTP- aperturaLong := change(macd)>0 and change(macd)[1]<=0 and rsi<ovb//crossover(macd,signal)+ aperturaLong := ta.change(macd)>0 and ta.change(macd)[1]<=0 and rsi<ovb//crossover(macd,signal)- aperturashort:=change(macd)<0 and change(macd)[1]>=0 and rsi>ovs//crossunder(macd,signal)+ aperturashort:=ta.change(macd)<0 and ta.change(macd)[1]>=0 and rsi>ovs//crossunder(macd,signal)if not rsiTP⋯if usiLong and strategy.position_size<=0 and okLong and aperturaLong- strategy.entry("LONGHISSIMO",true)+ strategy.entry("LONGHISSIMO",strategy.long)if chiusuraLong ⋯if usiShort and strategy.position_size>=0 and okShort and aperturashort- strategy.entry("SHORTISSIMO",false)+ strategy.entry("SHORTISSIMO",strategy.short)Same 41 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 11: input() → input.string()
- Line 12: input() → input.string()
- Line 13: input() → input.string()
- Line 29: input() → input.bool()
- Line 30: input() → input.float()
- Line 31: input() → input.int()
16 more edits
- Line 36: input() → input.bool()
- Line 38: input() → input.int()
- Line 38: input() → input.string()
- Line 39: input() → input.bool()
- Line 41: input() → input.int()
- Line 41: input() → input.int()
- Line 45: input() → input.bool()
- Line 46: input() → input.bool()
- Line 60: input() → input.bool()
- Line 61: input() → input.bool()
- Line 62: input() → input.int()
- Line 63: input() → input.source()
- Line 65: input() → input.int()
- Line 66: input() → input.int()
- Line 73: input() → input.bool()
- Line 74: input() → input.bool()
⋯signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9,group="MACD")-sma_source1 = input(title="Simple MA FAST (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")+sma_source1 = input.string(title="Simple MA FAST (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")-sma_source2 = input(title="Simple MA SLOW (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")+sma_source2 = input.string(title="Simple MA SLOW (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")-sma_signal = input(title="Simple MA(Signal Line)",defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")+sma_signal = input.string(title="Simple MA(Signal Line)",defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")// Calculating⋯col_signal = #ff6a00-use_stress=input(false,title="Use stress on recent bars",group="Stress")+use_stress=input.bool(false,title="Use stress on recent bars",group="Stress")-recent_stress=input(0.01,title="Stress on recent bars",group="Stress",step=0.01,minval=0.01,maxval=0.99)+recent_stress=input.float(0.01,title="Stress on recent bars",group="Stress",step=0.01,minval=0.01,maxval=0.99)-level=input(1,title="Level of stress",group="Stress")+level=input.int(1,title="Level of stress",group="Stress")if use_stress ⋯ macd:=math.pow((macd*(recent_stress)),level)+(1-recent_stress*macd[1])-use_ma= input(true,title="Use moving average (MACD)?",group="Moving Average")+use_ma= input.bool(true,title="Use moving average (MACD)?",group="Moving Average")if use_ma- macd:=ma(macd,input(36,title="Length",group="Moving Average"),input(title="Type MA",defval="THMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="Moving Average"))+ macd:=ma(macd,input.int(36,title="Length",group="Moving Average"),input.string(title="Type MA",defval="THMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="Moving Average"))-use_linreg= input(true,title="Use linear regression (MACD)?",group="Linear Regression")+use_linreg= input.bool(true,title="Use linear regression (MACD)?",group="Linear Regression")if use_linreg- macd:=ta.linreg(macd,input(10,title="Length",group="Linear Regression"),input(1,title="Offset",group="Linear Regression"))+ macd:=ta.linreg(macd,input.int(10,title="Length",group="Linear Regression"),input.int(1,title="Offset",group="Linear Regression"))//macd == linea blu (differenza tra media mobile veloce e media mobile lenta)⋯//hist == istogramma (differenza tra macd e media mobile)-on_cross = input(false,title="Use cross macd and signal",group="Condition entry/exit")+on_cross = input.bool(false,title="Use cross macd and signal",group="Condition entry/exit")-on_minmax = input(true,title="Use min/max macd",group="Condition entry/exit")+on_minmax = input.bool(true,title="Use min/max macd",group="Condition entry/exit")aperturaLong = ta.change(macd)>0//crossover(macd,signal)⋯plot(on_cross?signal:na, title="Signal", color=color.new(col_signal, 0))-rsiFilter = input(false,title="Use RSI filter?",group="RSI")+rsiFilter = input.bool(false,title="Use RSI filter?",group="RSI")-rsiTP = input(true,title="Use RSI Take Profit?",group="RSI")+rsiTP = input.bool(true,title="Use RSI Take Profit?",group="RSI")-len=input(14,title="RSI period",group="RSI")+len=input.int(14,title="RSI period",group="RSI")-srcr=input(close,title="RSI source",group="RSI")+srcr=input.source(close,title="RSI source",group="RSI")rsi=ta.rsi(srcr,len)-ovb=input(90,title="Overbought height",group="RSI") +ovb=input.int(90,title="Overbought height",group="RSI") -ovs=input(44,title="Oversold height",group="RSI")+ovs=input.int(44,title="Oversold height",group="RSI")okLong=rsi<ovb and ta.change(macd)>0 and ta.change(macd)[1]<=0⋯ -usiLong=input(true,title="Use long?")+usiLong=input.bool(true,title="Use long?")-usiShort=input(true,title="Use short?")+usiShort=input.bool(true,title="Use short?")chiusuraShort=rsi<ovs or (aperturaLong)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 41 before, 28 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © Drun30-//@version=5+//@version=6strategy("MACD+RSI", initial_capital=100, overlay=false,default_qty_type=strategy.percent_of_equity,default_qty_value=100,commission_type="percent",commission_value=0.075)⋯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 |
|---|---|---|---|
| MACD | |||
| Fast Length | 12 | int (written input.integer) | fast_ma |
| Slow Length | 26 | int (written input.integer) | slow_ma |
| Source | close | source | fast_ma slow_ma |
| Signal Smoothing | 9 | int (written input.integer) · min 1 · max 50 | signal |
| Simple MA FAST (Oscillator) | EMA | string · options ["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"] | fast_ma |
| Simple MA SLOW (Oscillator) | EMA | string · options ["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"] | slow_ma |
| Simple MA(Signal Line) | EMA | string · options ["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"] | signal |
| Stress | |||
| Use stress on recent bars | false | bool | line 32 |
| Stress on recent bars | 0.01 | float · min 0.01 · max 0.99 · step 0.01 | macd |
| Level of stress | 1 | int | macd |
| Moving Average | |||
| Use moving average (MACD)? | true | bool | line 37 |
| Length | 36 | int | signal hist aperturaLong aperturashort okLong okShort line 34 MACD |
| Type MA | THMA | string · options ["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"] | signal hist aperturaLong aperturashort okLong okShort line 34 MACD |
| Linear Regression | |||
| Use linear regression (MACD)? | true | bool | line 40 |
| Length | 10 | int | signal hist aperturaLong aperturashort okLong okShort line 34 MACD |
| Offset | 1 | int | signal hist aperturaLong aperturashort okLong okShort line 34 MACD |
| Condition entry/exit | |||
| Use cross macd and signal | false | bool | line 49 Signal |
| Use min/max macd | true | bool | line 53 MACD |
| RSI | |||
| Use RSI filter? | false | bool | line 69 |
| Use RSI Take Profit? | true | bool | line 77 line 80 |
| RSI period | 14 | int | rsi |
| RSI source | close | source | rsi |
| Overbought height | 90 | int | aperturaLong okLong chiusuraLong |
| Oversold height | 44 | int | aperturashort okShort chiusuraShort |
| Use long? | true | bool | entry LONGHISSIMO line 86 |
| Use short? | true | bool | entry SHORTISSIMO line 90 |
What it calculates
- RSI (
rsi,ta.rsiin v5)RSI source (close), RSI period (14)rsi · line 64
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Close SHORTISSIMO | chiusuraShort | On | 85 |
| Open LONGHISSIMO long | usiLong and strategy.position_size<=0 and okLong and aperturaLong | On | 87 |
| Close LONGHISSIMO | chiusuraLong | On | 89 |
| Open SHORTISSIMO short | usiShort and strategy.position_size>=0 and okShort and aperturashort | On | 91 |
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 varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −15.0% | +102.5% | 41 | 51% | 0.87 | 40.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +4.9% | +5.5% | 37 | 43% | 1.13 | 15.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −23.8% | +9.8% | 42 | 38% | 0.85 | 57.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +20.8% | +8.4% | 40 | 58% | 1.48 | 15.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −68.8% | +16.7% | 35 | 46% | 0.52 | 80.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +54.4% | +21.5% | 41 | 56% | 1.75 | 18.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −44.5% | +148.3% | 44 | 55% | 0.66 | 64.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +9.1% | +17.1% | 39 | 49% | 1.26 | 12.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 15 | 53% | −$17.16 |
| Sideways market | 14 | 36% | −$45.73 |
| Falling market | 12 | 67% | $48.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
58.0%
The 95th percentile of the worst drawdown when the same 41 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 33.4% to 58.0%; the tick is what actually happened, 35.5%. Order luck alone moves it this much.
All 41 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 5 Jul 2024 57,050.00 | 3 Aug 2024 61,498.30 | 29 | $7.64 |
| long | 20 Aug 2024 59,438.50 | 5 Sep 2024 57,970.90 | 16 | −$2.82 |
| long | 19 Sep 2024 61,760.00 | 5 Oct 2024 62,086.00 | 16 | $0.40 |
| short | 5 Oct 2024 62,086.00 | 11 Oct 2024 60,326.40 | 6 | $2.83 |
| long | 20 Oct 2024 68,378.00 | 6 Nov 2024 69,372.00 | 17 | $1.41 |
| short | 6 Nov 2024 69,372.00 | 12 Nov 2024 88,648.00 | 6 | −$30.62 |
| long | 12 Nov 2024 88,648.00 | 29 Nov 2024 95,644.00 | 17 | $6.11 |
| short | 29 Nov 2024 95,644.00 | 18 Dec 2024 106,134.00 | 19 | −$9.46 |
| long | 18 Dec 2024 106,134.00 | 26 Dec 2024 99,429.60 | 8 | −$4.88 |
| short | 26 Dec 2024 99,429.60 | 28 Dec 2024 94,299.00 | 2 | $3.54 |
| long | 9 Jan 2025 95,060.60 | 1 Feb 2025 102,430.00 | 23 | $5.63 |
| short | 1 Feb 2025 102,430.00 | 3 Feb 2025 97,700.60 | 2 | $3.57 |
| long | 20 Feb 2025 96,644.40 | 2 Mar 2025 86,064.50 | 10 | −$9.24 |
| long | 19 Mar 2025 82,715.00 | 6 Apr 2025 83,538.00 | 18 | $0.63 |
| short | 6 Apr 2025 83,538.00 | 7 Apr 2025 78,430.00 | 1 | $4.46 |
| long | 19 Apr 2025 84,474.70 | 9 May 2025 103,262.00 | 20 | $17.48 |
| short | 9 May 2025 103,262.00 | 27 May 2025 109,435.00 | 18 | −$5.93 |
| long | 27 May 2025 109,435.00 | 29 May 2025 107,782.00 | 2 | −$1.51 |
| short | 29 May 2025 107,782.00 | 6 Jun 2025 101,509.00 | 8 | $5.07 |
| long | 16 Jun 2025 105,594.00 | 25 Jun 2025 106,083.00 | 9 | $0.29 |
| short | 25 Jun 2025 106,083.00 | 29 Jun 2025 107,297.00 | 4 | −$1.23 |
| long | 29 Jun 2025 107,297.00 | 25 Jul 2025 118,341.00 | 26 | $9.47 |
| short | 25 Jul 2025 118,341.00 | 2 Aug 2025 113,298.00 | 8 | $4.23 |
| long | 14 Aug 2025 123,306.00 | 26 Aug 2025 110,112.00 | 12 | −$11.61 |
| long | 10 Sep 2025 111,546.00 | 27 Sep 2025 109,643.00 | 17 | −$1.77 |
| long | 7 Oct 2025 124,659.00 | 17 Oct 2025 108,194.00 | 10 | −$12.51 |
| long | 31 Oct 2025 108,323.00 | 11 Nov 2025 106,011.00 | 11 | −$1.85 |
| short | 11 Nov 2025 106,011.00 | 12 Nov 2025 103,059.00 | 1 | $2.09 |
| long | 1 Dec 2025 90,360.00 | 19 Dec 2025 85,516.40 | 18 | −$4.48 |
| long | 4 Jan 2026 90,628.00 | 23 Jan 2026 89,559.70 | 19 | −$1.02 |
| long | 17 Feb 2026 68,892.40 | 14 Mar 2026 70,930.00 | 25 | $2.13 |
| short | 14 Mar 2026 70,930.00 | 19 Mar 2026 71,246.60 | 5 | −$0.47 |
| long | 19 Mar 2026 71,246.60 | 23 Mar 2026 67,859.00 | 4 | −$3.81 |
| long | 10 Apr 2026 71,788.00 | 30 Apr 2026 75,780.00 | 20 | $3.99 |
| short | 30 Apr 2026 75,780.00 | 23 May 2026 75,539.50 | 23 | $0.13 |
| long | 17 Jun 2026 65,675.00 | 3 Jul 2026 61,560.00 | 16 | −$4.99 |
| long | 11 Jul 2026 64,161.70 | 29 Jul 2026 63,915.00 | 18 | −$0.39 |
| short | 29 Jul 2026 63,915.00 | 2 Aug 2026 62,823.70 | 4 | $1.13 |
| long | 13 Aug 2026 63,480.00 | 6 Sep 2026 79,831.80 | 24 | $18.85 |
| short | 6 Sep 2026 79,831.80 | 24 Sep 2026 84,397.60 | 18 | −$5.44 |
| long | 24 Sep 2026 84,397.60 | 10 Oct 2026 82,635.60 | 16 | −$1.95 |
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 Strategy (BTC-USDT; 1h) - MACD (with source code) this page | — | v4 strategy | −15.0% | 41 | 40.1% |
| Triple RSI strategy by Trade_by_DB | rsi | v5 strategy | +3.2% | 2 | 3.3% |
| Hobbiecode - RSI + Close previous day by hobbiecode | rsi | v5 strategy | +1.2% | 63 | 4.2% |
| dirt cheap yet elegant RSI Strategy by SparkyFlary | rsi | v4 strategy | −11.4% | 8 | 20.5% |
| RSI Classic Strategy (by Coinrule) by Coinrule | rsi | v4 strategy | +7.5% | 6 | 7.2% |
| RSITrendStrategy by email_analysts | rsi | v4 strategy | +69.2% | 22 | 38.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 53 ms | With a workaround 1,000 bars in 91 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 87, 91) so the engine trades the way the script says. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © Drun30//@version=4strategy("MACD+RSI", initial_capital=100, overlay=false,default_qty_type=strategy.percent_of_equity,default_qty_value=100,commission_type="percent",commission_value=0.075)//@version=4// Getting inputsfast_length = input(title="Fast Length", type=input.integer, defval=12,group="MACD")slow_length = input(title="Slow Length", type=input.integer, defval=26,group="MACD")src = input(title="Source", type=input.source, defval=close,group="MACD")signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9,group="MACD")sma_source1 = input(title="Simple MA FAST (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")sma_source2 = input(title="Simple MA SLOW (Oscillator)", defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")sma_signal = input(title="Simple MA(Signal Line)",defval="EMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="MACD")// Calculatingma(source,length,type)=> type=="FEMA"?4*ema(source,length)-ema(ema(ema(ema(source,length),length),length),length):type=="FSMA"?4*sma(source,length)-sma(sma(sma(sma(source,length),length),length),length):type=="FWMA"?4*wma(source,length)-wma(wma(wma(wma(source,length),length),length),length):type=="FRMA"?4*rma(source,length)-rma(rma(rma(rma(source,length),length),length),length):type=="TEMA"?3*ema(source,length)-ema(ema(ema(source,length),length),length):type=="TSMA"?3*sma(source,length)-sma(sma(sma(source,length),length),length):type=="TWMA"?3*wma(source,length)-wma(wma(wma(source,length),length),length):type=="TRMA"?3*rma(source,length)-rma(rma(rma(source,length),length),length):type=="EMA"?ema(source,length):type=="SMA"?sma(source,length):type=="WMA"?wma(source,length):type=="RMA"?rma(source,length):type=="DEMA"?2*ema(source,length)-ema(ema(source,length),length):type=="DSMA"?2*sma(source,length)-sma(sma(source,length),length):type=="DWMA"?2*wma(source,length)-wma(wma(source,length),length):type=="DRMA"?2*rma(source,length)-rma(rma(source,length),length):type=="HMA"?hma(source,length):type=="DHMA"?2*hma(source,length)-hma(hma(source,length),length):type=="THMA"?3*hma(source,length)-hma(hma(hma(source,length),length),length):type=="FHMA"?4*hma(source,length)-hma(hma(hma(hma(source,length),length),length),length):ema(source,length)fast_ma = ma(src,fast_length,sma_source1) slow_ma = ma(src,slow_length,sma_source2)macd = fast_ma - slow_ma //Differenza tra la media mobile veloce e quella lenta signal = ma(macd,signal_length,sma_signal) //usa o la SMA oppure la EMA sulla differenza tra la media mobile veloce e lentahist = macd - signal //Differenza tra la differenza precedente e la media mobile della differenza// Plot colorscol_grow_above = #26A69Acol_grow_below = #FFCDD2col_fall_above = #B2DFDBcol_fall_below = #EF5350col_macd = #0094ffcol_signal = #ff6a00use_stress=input(false,title="Use stress on recent bars",group="Stress")recent_stress=input(0.01,title="Stress on recent bars",group="Stress",step=0.01,minval=0.01,maxval=0.99)level=input(1,title="Level of stress",group="Stress")if use_stress macd:=macd*(1/(1-recent_stress)) if not na(macd[1]) macd:=pow((macd*(recent_stress)),level)+(1-recent_stress*macd[1])use_ma= input(true,title="Use moving average (MACD)?",group="Moving Average")if use_ma macd:=ma(macd,input(36,title="Length",group="Moving Average"),input(title="Type MA",defval="THMA", options=["HMA","DHMA","THMA","FHMA","WMA","DWMA","TWMA","FWMA","SMA","DSMA","TSMA","FSMA","EMA","DEMA","TEMA","FEMA","RMA","DRMA","TRMA","FRMA"],group="Moving Average"))use_linreg= input(true,title="Use linear regression (MACD)?",group="Linear Regression")if use_linreg macd:=linreg(macd,input(10,title="Length",group="Linear Regression"),input(1,title="Offset",group="Linear Regression"))//macd == linea blu (differenza tra media mobile veloce e media mobile lenta)//signal == linea arancione (media mobile dell'macd)//hist == istogramma (differenza tra macd e media mobile)on_cross = input(false,title="Use cross macd and signal",group="Condition entry/exit")on_minmax = input(true,title="Use min/max macd",group="Condition entry/exit")aperturaLong = change(macd)>0//crossover(macd,signal)aperturashort=not (change(macd)>0)//crossunder(macd,signal)if on_cross on_minmax:=false aperturaLong := crossover(macd,signal) aperturashort := crossunder(macd,signal)if on_minmax on_cross:=false aperturaLong := change(macd)>0//crossover(macd,signal) aperturashort:=change(macd)<0//crossunder(macd,signal)//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )plot(macd, title="MACD", color=change(macd)>0 and on_minmax?color.white:change(macd)<0 and on_minmax?color.purple:color.blue, transp=0)plot(on_cross?signal:na, title="Signal", color=col_signal, transp=0)rsiFilter = input(false,title="Use RSI filter?",group="RSI")rsiTP = input(true,title="Use RSI Take Profit?",group="RSI")len=input(14,title="RSI period",group="RSI")srcr=input(close,title="RSI source",group="RSI")rsi=rsi(srcr,len)ovb=input(90,title="Overbought height",group="RSI") ovs=input(44,title="Oversold height",group="RSI")okLong=rsi<ovb and change(macd)>0 and change(macd)[1]<=0okShort=rsi>ovs and change(macd)<0 and change(macd)[1]>=0if not rsiFilter okLong:=true okShort:=true usiLong=input(true,title="Use long?")usiShort=input(true,title="Use short?")chiusuraShort=rsi<ovs or (aperturaLong)chiusuraLong=rsi>ovb or (aperturashort)if rsiTP aperturaLong := change(macd)>0 and change(macd)[1]<=0 and rsi<ovb//crossover(macd,signal) aperturashort:=change(macd)<0 and change(macd)[1]>=0 and rsi>ovs//crossunder(macd,signal)if not rsiTP chiusuraShort:=okLong and aperturaLong chiusuraLong:=okShort and aperturashort if chiusuraShort strategy.close("SHORTISSIMO")if usiLong and strategy.position_size<=0 and okLong and aperturaLong strategy.entry("LONGHISSIMO",true)if chiusuraLong strategy.close("LONGHISSIMO")if usiShort and strategy.position_size>=0 and okShort and aperturashort strategy.entry("SHORTISSIMO",false)