strategy · own pane · open source
MACD crossover strategy (12, 26, 9)
MACD crossover strategy: buy when MACD crosses above its signal line, exit on the bearish cross. Plain-English rules, code and a costed backtest you can rerun. Deepwick Script runs a subset of Pine Script.
What the MACD Crossover strategy does
Trend-following MACD crossover long-only: enter on a bullish MACD/signal cross, exit on the bearish cross. The histogram visualises momentum strength.
- Raise the spread between
FastandSlowfor fewer, larger swings. - Tighter
Signalsmoothing reacts faster but adds whipsaw in chop. - Combine with an ADX > 25 filter to dodge range-bound whipsaws.
Entry and exit rules
Language reference: Deepwick Script.
Default inputs
| Input | Default | Min | Max |
|---|---|---|---|
Fast Length | 12 | 2 | 30 |
Slow Length | 26 | 10 | 100 |
Signal Length | 9 | 2 | 50 |
Stop Loss % | 2 | 0.5 | 10 |
Take Profit % | 6 | 0 | 20 |
How Deepwick fills this strategy
- Market orders fill at the next bar's open.
- Stops and limits follow the bar's path: open → nearer extreme → farther extreme → close.
- Gaps fill at the open.
- Commission is charged on every fill, and slippage applies to market and stop fills, as set in
strategy().
Source code
Deepwick Script, a subset of Pine Script · read-only
//@version=6
strategy("MACD Crossover", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
fast = input.int(12, "Fast Length", minval=2, maxval=30, step=1)
slow = input.int(26, "Slow Length", minval=10, maxval=100, step=1)
sig = input.int(9, "Signal Length", minval=2, maxval=50, step=1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
colorLine = input.color("color.blue", "Color Line")
colorMid = input.color("color.orange", "Color Mid")
colorUp = input.color("color.green", "Color Up")
colorDown = input.color("color.red", "Color Down")
colorNeutral = input.color("color.gray", "Color Neutral")
[m, s, h] = ta.macd(close, fast, slow, sig)
longSignal = strategy.position_size == 0 and ta.crossover(m, s)
exitSignal = strategy.position_size > 0 and ta.crossunder(m, s)
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
strategy.close("Long", comment="MACD cross down")
hline(0, "Zero", color=colorNeutral, linestyle=hline.style_dotted)
p_MACD = plot(m, "MACD", color=colorLine, linewidth=2)
p_Signal = plot(s, "Signal", color=colorMid)
p_Histogram = plot(h, "Histogram", color=h >= 0 ? colorUp : colorDown, style=plot.style_columns)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=colorUp, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=colorDown, text="X")
alertcondition(longSignal, "MACD long entry", "MACD crossed above signal")
alertcondition(exitSignal, "MACD exit", "MACD crossed below signal")
Run it in Deepwick
- Open a chart of any crypto pair or CFD.
- Click “Scripts” and add this strategy.
- Press Run and read the trades in the Strategy Tester.
FAQ
What is the MACD crossover success rate?
There is no single success rate: it changes with the market, timeframe, period and costs. This page shows the win rate of one documented run in the results table; rerun it on your own symbol to get yours.
Should I only take crosses above the zero line?
It is a common filter that keeps you on the side of the trend. Add and macd > 0 to the entry condition and compare both runs with the same costs.
Why does the MACD crossover give late signals?
MACD is built from moving averages of price, so its crosses lag turns by construction. The faster you make it, the more false crosses you get.
No comments yet.
Sign in to comment.