Skip to content
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.

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 PineConnector for 7 days – credit card not require 

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

indicator("PineConnector x MACD Strategy", overlay=true)

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)

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

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

if long
    alert('changetoyourLICENSEID,buy,' +syminfo.ticker+ ',risk=1', alert.freq_once_per_bar_close)

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

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

macd strategy pine script

Recommended PineConnector EA settings:
Volume Type: Percentage of account balance
● Assuming risk=1 and $1000 account,
the EA will compute the position size whereby when the trade hits SL, you will lose 1% or $10.
Close on reverse: TRUE
● Then open buy position will be closed first followed by the open of a sell, upon receipt of a sell signal - and vice versa

Leave a comment

Back To PineConnector Script Library