Skip to content

Automate MACD Crossover Trades From TradingView To MT5

 

You see the MACD line cross its signal, you reach for the order ticket, and by the time the trade is placed the move is already underway. Manual execution adds a delay a crossover strategy cannot afford, and it breaks down the moment a signal fires while you are asleep or away from the screen. The fix is to let TradingView send the signal straight to MetaTrader 5. With PineConnector as the bridge, a MACD cross on your chart becomes a live order on MT5 in about a second, with no manual clicks. This guide shows how to build it, from the Pine Script to the alert to the first test trade.


How The Automation Fits Together

Four pieces connect a MACD cross to a filled order. Your Pine Script detects the crossover and fires an alert(). TradingView sends that alert to the PineConnector webhook. PineConnector validates it and passes the command to the EA in your MT5 terminal, which places the trade.

This guide focuses on the strategy and the alert. If you have not connected MT5 yet, set that up first with our TradingView to MT5 guide, then come back here.


What You Need

  • An MT5 account connected through PineConnector. Our MT5 setup guide covers it.
  • A paid TradingView plan, since webhook alerts are not available on the free tier.
  • Your PineConnector License ID, the 13 to 14 digit number on the Connections page in the Portal.

Step 1: Detect The MACD Crossover In Pine Script

MACD compares two moving averages of price. The MACD line is the gap between a fast and a slow EMA, and the signal line is an EMA of the MACD line. A bullish signal is when the MACD line crosses above the signal line. A bearish signal is the reverse.

In Pine Script, ta.macd returns both lines, and ta.crossover and ta.crossunder detect the crosses.

//@version=5
indicator("MACD Crossover to PineConnector", overlay=true)

fastLen = input.int(12, "Fast Length")
slowLen = input.int(26, "Slow Length")
sigLen  = input.int(9,  "Signal Length")

[macdLine, signalLine, _] = ta.macd(close, fastLen, slowLen, sigLen)

bullCross = ta.crossover(macdLine, signalLine)
bearCross = ta.crossunder(macdLine, signalLine)

Step 2: Send The Trade To PineConnector With alert()

Now wire each cross to a PineConnector command. The alert() function takes a message string and a frequency. The message is the PineConnector command, and syminfo.ticker inserts the symbol of whatever chart the alert runs on.

licenseID = input.string("YOUR_LICENSE_ID", "PineConnector License ID")

if bullCross
    alert(licenseID + ",buy," + syminfo.ticker + ",vol_lots=0.1", alert.freq_once_per_bar_close)

if bearCross
    alert(licenseID + ",sell," + syminfo.ticker + ",vol_lots=0.1", alert.freq_once_per_bar_close)

Three details matter here. Replace YOUR_LICENSE_ID with the ID from your Portal. The vol_lots=0.1 parameter sets a fixed lot size, which keeps the first version simple. And alert.freq_once_per_bar_close fires the signal only after the candle closes, so a cross that appears mid candle and then disappears does not trigger a trade.


Step 3: Decide How Trades Exit

A crossover system is usually a reversal system. When the opposite cross arrives, you want to close the current trade and open the new one. PineConnector handles this in two ways.

The simplest is the Close on Reverse option in the EA settings. Turn it on, and when the EA receives a sell while a buy is open, it closes the buy before opening the sell. With this on, the code above is all you need. The EA options guide shows where to find the setting.

If you would rather control exits in the alert itself, send a close command before the new entry. On a bearish cross, close the long first, then sell:

if bearCross
    alert(licenseID + ",closelong," + syminfo.ticker, alert.freq_once_per_bar_close)
    alert(licenseID + ",sell," + syminfo.ticker + ",vol_lots=0.1", alert.freq_once_per_bar_close)

Step 4: Switch To Risk Based Sizing (Optional)

Fixed lots ignore account size. To risk a set percentage instead, add a stop loss and use vol_pct_bal_loss. The command below risks 1 percent of balance with a 50 pip stop and a 100 pip target:

LicenseID,buy,EURUSD,vol_pct_bal_loss=1,sl_pips=50,tp_pips=100

The vol_pct_bal_loss parameter needs a stop loss to calculate the lot size, so always include sl_pips when you use it. The full parameter list, including trailing stops and partial closes, is in the PineConnector syntax guide.


Step 5: Create The Alert In TradingView

Add the script to your chart, then open the Create Alert dialog. Set the Condition to your script and choose Any alert() function call. This tells TradingView to send every alert the script fires.

In the Notifications tab, paste the webhook URL:

<https://webhook.pineconnector.com>

Leave the Message box as it is, because the alert() calls already carry the command. Save the alert, and the bridge is live.


Step 6: Test On A Demo Account

Send the strategy to a demo MT5 account first. Wait for a cross, or force one by tightening the inputs on a fast timeframe, and confirm the order opens in MT5. Cross check the TradingView Alerts log against the PineConnector Signals log so you know the command was received and read correctly. A short demo run is the difference between catching a typo in your License ID now and finding it later with real money.


Use This For Any Strategy, Not Just MACD

The same wiring works for any indicator or strategy you can define a rule for, whether that is an RSI level, a moving average cross, or a Supertrend flip. Only the entry and exit conditions change. The alert() line that carries the PineConnector command stays the same.

Which route you take depends on whether you can edit the script. If you can open it in the Pine Editor, follow the low-code guide and add PineConnector alert() calls to your conditions, exactly as we did above.

If you are using a script you cannot edit, such as a closed source or community indicator, use the no-code guide. It walks through three quick feasibility tests that tell you whether the script can be automated through strategy order fills, an existing License ID input, or an alertcondition() signal.


Tune It Before You Trust It

MACD crossovers whip back and forth in ranging markets, which produces a string of small losing trades. Two adjustments help. Trade the signal only in the direction of a higher timeframe trend, for example taking longs only when price sits above the 200 period EMA. And test on bar close data across several months before going live, so you see how the system behaves in both trending and quiet conditions.

Automation removes the execution delay. It does not fix a strategy that loses money, so prove the edge on demo before you scale it.


Start your free 14-day trial with us here and send your first automated MACD trade from TradingView to MT5 today.


Leave a comment

Back To PiCo Blog

Your Bridge to
Effortless Trading.

Automate your TradingView strategies on MetaTrader 4/5 — with analytics, tasks, and real-time alerts built in.

No credit card required
Full Advanced plan — Bridge, Analytics, Tasks, Notifications
Go live in under 30 minutes
Only subscribe if you love it — no obligation
Free 14-Day Advanced Trial
Trusted by 66,000+ traders · Built on Microsoft Azure · Empowering traders since 2021