DEV Community

Bil Baz
Bil Baz

Posted on Edited on Originally published at bazsocial.com AI-assisted

How to send TradingView alerts to MetaTrader 5 with a webhook

Short answer. TradingView cannot talk to MetaTrader 5 directly. You need three parts: a TradingView alert that sends a webhook, a small HTTPS server that receives and stores it, and an Expert Advisor in MT5 that fetches new orders from that server with WebRequest and places them. TradingView requires two-factor authentication for webhooks, sends only to ports 80 or 443, and gives your server 3 seconds to answer.

How the pieces fit

  1. TradingView alert. Your indicator or strategy fires an alert; its message is the order (for example JSON with symbol, side, stop and target), and the alert's webhook URL points to your server.
  2. Receiver. An HTTPS endpoint on your own server or VPS checks the request, stores the order with a unique id and answers at once (under 3 seconds).
  3. Expert Advisor. Every second or two, the EA in MT5 asks the receiver for orders newer than the last id it saw, checks them against your risk limits, places them and reports back.

The EA pulls orders instead of the server pushing them because MT5 cannot accept incoming connections; WebRequest only makes outgoing calls.

Step 1: the alert message

{"token": "<random-webhook-token>", "id": "{{timenow}}-{{ticker}}", "symbol": "{{ticker}}", "side": "buy", "price": {{close}}, "sl": 2341.5, "tp": 2362.0}
Enter fullscreen mode Exit fullscreen mode
  • A valid JSON message is sent as application/json; anything else arrives as plain text.
  • The token is a random string used only for this webhook. Never put a broker or TradingView password in the message.
  • {{ticker}}, {{close}} and {{timenow}} are TradingView placeholders that are filled in when the alert fires.

Step 2: the receiver

  • Accept only HTTPS on port 443, and optionally only TradingView's published sending addresses (listed on its webhook help page).
  • Reject any request whose token does not match.
  • Store each order once: TradingView or the network can deliver the same alert twice, so the id must be unique.
  • Reply 200 OK immediately and do the slow work afterwards.

Step 3: the Expert Advisor

  • In MT5, open Tools > Options > Expert Advisors and add your receiver's address to "Allow WebRequest for listed URL". Without it, WebRequest fails.
  • Only Expert Advisors and scripts can call WebRequest; indicators cannot. The call blocks until the server answers, so keep the timeout short (about 2 seconds) and call it from OnTimer.
  • WebRequest does not run in the Strategy Tester, so test on a demo account.
void OnTimer()
{
   char body[], reply[];
   string replyHeaders;
   string url = "https://your-server.example/orders?after=" + IntegerToString(lastId);
   int status = WebRequest("GET", url, "Authorization: Bearer " + Token + "\r\n", 2000, body, reply, replyHeaders);
   if(status != 200) return;              // try again on the next timer tick
   // parse the orders, check risk limits, send with CTrade, then remember the newest id
}
Enter fullscreen mode Exit fullscreen mode

Safety checks that matter more than speed

  • Symbol mapping. TradingView's XAUUSD may be GOLD, XAUUSD.m or XAUUSDm at your broker.
  • Risk limits in the EA, not only in the alert: maximum lot, maximum open trades, a daily loss stop and the broker's minimum stop distance.
  • Stale orders. Skip an order that arrives long after the alert fired; the price will have moved.
  • A VPS near your broker's server, so MT5 stays connected when your PC sleeps.
  • Demo first. Run it on a demo account for at least two weeks before real money.

Ready-made options

You can build this yourself or use a ready connector. BAZ Bridge (my product) is one: an EA plus a hosted receiver that executes BAZ Sniper alerts on your own MT5 account within your limits. Other connectors exist; compare how each handles duplicates, symbol mapping and risk limits before you choose.


I'm a Dubai-based MQL5 developer and gold trader and the founder of BAZ Trading, which sells BAZ Bridge. Trading carries a high risk of loss; automation can lose money quickly. This is education, not investment advice. Originally published on bazsocial.com.

Sources: TradingView webhook alerts ยท MQL5 WebRequest

Top comments (0)