Loading…

Telegram Command Executor

A phone with access to the right Telegram bot can flatten a live account's entire book in one message. That makes this a genuinely dangerous piece of remote control, so every layer here exists to make an unauthorised or malformed message harmless rather than to make the happy path convenient. The chat-ID whitelist is checked first, before the message text is even parsed - a message from any chat not in InpAllowedChatIds is rejected outright with no reply, so an unknown sender learns nothing about the bot's behaviour. A message from a whitelisted chat must still carry a separate shared secret inside the text itself ("/flatten mySecret123") - knowing the bot's Telegram chat is not enough on its own, a second independent factor is required, and leaving InpCommandSecret blank makes the EA refuse every single command rather than default to open. Only four exact commands are recognised at all; anything else is rejected with no fallback interpretation. An hourly circuit breaker caps how many commands can be accepted regardless of how many arrive, and every accepted or rejected message - including the reason - is appended to a CSV audit log, so an attacker's attempts or a broken sender stay visible even when nothing executed. /flatten is the only command with a trading path: it closes every open position matching InpMagicFilter using a ticket-collect-then-close loop, so a partial failure partway through is still reported per ticket rather than silently abandoned. /pause and /resume set a shared global variable (TCE_Paused) that other cooperating EAs on the same terminal can voluntarily check before opening new trades - this EA has no power to force another EA to stop, only to signal that it should. Notes and limitations getUpdates is polled on a fixed interval (InpPollIntervalSec) rather than Telegram's long-poll mode, since MQL4's WebRequest is synchronous and a long timeout would block the terminal - commands are seen with up to that interval's delay, not instantly. The JSON response is parsed with a small hand-rolled key scanner suited to Telegram's fixed getUpdates shape, not a general-purpose JSON parser - a response with unusual structure (which the real Telegram API does not produce) would not parse correctly. UTF-8 text outside the plain ASCII range is not percent-encoded in outgoing replies and falls back to "?" per character. The processed-update offset is persisted to a local sandbox file so a restart does not reprocess or silently skip messages, but this file is per-terminal - moving the EA to a different terminal starts the offset over.

Expert advisor · MT4 · v1.0 · Free

About Telegram Command Executor

Telegram Command Executor is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 9 settings, such as Bot token, Allowed chat ids and Command secret. MQL source code included.

Files in the download: TelegramCommandExecutor.ex4, TelegramCommandExecutor.mq4, TelegramCommandExecutor.txt.

Settings

SettingDefaultWhat it does
Bot tokenTelegram bot token from @BotFather
Allowed chat idsComma-separated whitelist of Telegram chat IDs allowed to send commands
Command secretShared secret every command must include - leave blank to refuse everything (fails safe)
Poll interval sec5Seconds between getUpdates polls
Magic filter0/flatten closes only this magic's positions - 0 closes every position on the account
Max commands per hour10Circuit breaker - accepted commands allowed per rolling hour
Dry runfalsetrue = validate and log only, /flatten never actually closes anything
Max retries5
Retry wait ms500

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: Telegram · Alerts · MT4 Expert Advisor · MQL4 Source Code

Expert advisors (EA) for MT4 & MT5 · PolyMaria