Skip to content

PineConnector Script Library

MACD Strategy

pineconnector macd strategy pine script

The famous Moving Average Convergence Divergence (MACD) Strategy is modified to be compatible with PineConnector to enable trading automation. We added a 20-period Exponential Moving Average (EMA20) to the trend-following strategy to filter out counter-trend positions. The MACD strategy can be used for forex trading, stock trading, futures trading, crypto trading and trading of other instruments – also, ideal if you are day trading or swing trading.

Updated 6 October 2026: the alerts now use explicit volume (vol_pct_bal_loss=) with a stop-loss (sl_pips=), replacing the older risk= parameter.

Nevertheless, we recommend that paper trading should be done first, before running on your live trading accounts. 

After setting up this MACD crossover pine script,

  1. an alert will automatically trigger when conditions are met (as per the blue PineConnector arrows)
  2. the PineConnector EA will process the alert message 
  3. the appropriate order or action will be executed automatically on your MT4/5 terminal 

Looking to automate your trading?
PineConnector is a user-friendly bridge that enables retail traders to auto-execute TradingView alerts on MT4 & MT5 without fancy coding. Try Core for 7 days for $7, hosted MT5 included → After the trial, Core is $59/month unless you cancel. 

Steps to use this MACD Strategy:
1
Create a PineScript and attach the code below

//@version=5
indicator("PineConnector x MACD Strategy", overlay=true)

riskPct = input.float(1, "Risk per trade (% of balance)", minval=0.01)
slPips = input.float(20, "Stop-loss distance (pips)", minval=0.1)

fastLength = input(12)
slowlength = input(26)
MACDLength = input(9)
MACD = ta.ema(close, fastLength) - ta.ema(close, slowlength)
aMACD = ta.ema(MACD, MACDLength)
delta = MACD - aMACD

ema20 = ta.ema(close,20)
plot(ema20)

long = ta.crossover(delta, 0) and close > ema20
short = ta.crossunder(delta, 0) and close < ema20

plotshape(long, style=shape.labelup, location=location.belowbar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Buy', textcolor=color.new(color.white, 0))      //plotting up arrow when buy/long conditions met 
plotshape(short, style=shape.labeldown, location=location.abovebar, color=color.new(#046ff9, 0), size=size.large, text='PineConnector \n Sell', textcolor=color.new(color.white, 0))  //plotting down arrow when sell/short conditions met

msgTail = ',vol_pct_bal_loss=' + str.tostring(riskPct) + ',sl_pips=' + str.tostring(slPips)

if long
    alert('changetoyourLICENSEID,buy,' + syminfo.ticker + msgTail, alert.freq_once_per_bar_close)

if short
    alert('changetoyourLICENSEID,sell,' + syminfo.ticker + msgTail, alert.freq_once_per_bar_close)
        
2
Edit the highlighted portion of the code to your preassigned ID found in the PineConnector members area
(e.g. 60123456789)
3
Save the code, and click "Add to Chart"
4

Guide to create alerts: Create alerts

● After changing values from 3, please delete the old alert(s) if any and create a new alert
● Please ensure you select PineConnector x MACD strategy when creating an alert, and select alert() function calls only
● The script sends TradingView's ticker (syminfo.ticker). If your broker uses a different symbol name, replace it with the exact name from the Symbols tab on PineConnector Edge (Symbols guide), or set up Symbol Mapping.

macd strategy pine script


Recommended PineConnector EA settings:
1
Volume: set in the alert
● The alert sends vol_pct_bal_loss=1 with sl_pips=20. With a $1,000 balance, the EA sizes the trade so that a stop-loss hit loses about 1%, or $10. Gaps, slippage and broker volume rounding can make the actual loss differ.
● Change the risk and the stop-loss distance in the indicator's settings. The 20-pip default is an example, not a recommendation; choose a stop that suits the instrument you trade. See the syntax guide for the other volume methods.
2
Close on Reverse: On
● When a sell signal arrives, an open buy position is closed first and then the sell opens, and vice versa.
● On PineConnector Edge, set it in Portal → Edge → EA Settings, under General.

Leave a comment

Back To PineConnector Script Library

Ready when your strategy is

You bring the strategy.We bring the infrastructure.

Connect TradingView to MetaTrader, choose where MT5 runs and put the full PineConnector workflow through its paces from your first month.

Strategy and trading decisions remain yours. The MT5 environment can be ours.

PineConnector Edge

Run the full PineConnector workflow.

$59/mo at launch

Core plan · 1 connection · 1 hosted MT5 environment

Try Core for $7

7 days of Core for $7, then $59/month at the launch price unless you cancel.