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 · MT5 · v1.0 · Free
About Telegram Command Executor
Telegram Command Executor is an expert advisor (trading robot) for MetaTrader 5. It can size trades from your balance and risk percent. It has 9 settings, such as Bot token, Allowed chat ids and Command secret. MQL source code included.
Files in the download: TelegramCommandExecutor.ex5, TelegramCommandExecutor.mq5, TelegramCommandExecutor.txt.
Trading robots can lose money. Check every setting and test on a demo account before using real funds.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bot token | Telegram bot token from @BotFather | |
| Allowed chat ids | Comma-separated whitelist of Telegram chat IDs allowed to send commands | |
| Command secret | Shared secret every command must include - leave blank to refuse everything (fails safe) | |
| Poll interval sec | 5 | Seconds between getUpdates polls |
| Magic filter | 0 | /flatten closes only this magic's positions - 0 closes every position on the account |
| Max commands per hour | 10 | Circuit breaker - accepted commands allowed per rolling hour |
| Dry run | false | true = validate and log only, /flatten never actually closes anything |
| Max retries | 5 | |
| Retry wait ms | 500 |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Experts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the expert advisor from the Navigator onto a chart, check the settings, allow Algo/Auto Trading and press OK.
- Test it first on a demo account or in the Strategy Tester.
Tags: Telegram · Risk & Lot Size · MT5 Expert Advisor · MQL5 Source Code