| Xây Dựng Hệ Thống Bot Trading Python MT5 Vận Hành 24/7 Trên Cloud

Được viết bởi thanhdt vào ngày 26/07/2026 lúc 06:40 | 6 lượt xem


Bot trading Python MT5 là hệ thống Python điều khiển MetaTrader 5 — không chỉ một script đặt lệnh. Bài mega-post này mô tả 5 module cốt lõi, deploy cloud/VPS và vận hành 24/7 an toàn.

Nền: Trading bot Python MT5 là gì? · aiomql tiếng Việt

5 thành phần bot MT5 hoàn chỉnh

[Data Ingestion] → [Signal Engine] → [Order Manager]
        ↓                ↓                  ↓
    [Logger] ←──────── [Guardian] ────────→ [Alert/Telegram]
Module Nhiệm vụ
Data Ingestion Tick/nến từ MT5, cache, đồng bộ thời gian
Signal Engine Rule hoặc model → BUY/SELL/HOLD
Order Manager Gửi/sửa/hủy lệnh, trailing, partial close
Logger File log + DB (audit khi tranh chấp broker)
Guardian Max DD ngày, kill switch, reconnect

Data Ingestion — ví dụ MetaTrader5

import MetaTrader5 as mt5
import pandas as pd

def fetch_bars(symbol: str, timeframe, count: int = 200) -> pd.DataFrame:
    if not mt5.initialize():
        raise RuntimeError("MT5 init failed")
    rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, count)
    mt5.shutdown()
    df = pd.DataFrame(rates)
    df["time"] = pd.to_datetime(df["time"], unit="s")
    return df

Async/multi-symbol: xem aiomql Python MT5.

Signal Engine — tách logic khỏi execution

  • Rule viết pure function: input OHLCV → output signal.
  • Dễ unit test và backtest tách biệt MT5.
  • Tránh lookahead: signal tại bar t chỉ dùng dữ liệu ≤ t.

Order Manager — an toàn trước tốc độ

  • Validate lot min/max, margin, symbol trade mode.
  • Idempotent: tránh gửi trùng lệnh khi retry.
  • Queue lệnh nếu terminal busy.

Deploy VPS Windows 24/7

  1. VPS Windows gần server broker (latency ổn).
  2. Cài MT5, đăng nhập demo/live, bật Algo Trading.
  3. Python venv + dependencies (MetaTrader5, aiomql, pandas).
  4. Task Scheduler hoặc PM2 (qua wrapper) restart khi crash.
  5. Monitor: Telegram khi bot stop hoặc lỗi kết nối.
# Ví dụ chạy bot (demo)
python bot_main.py --config config.yaml

Telegram alert khẩn cấp

  • Lệnh mở/đóng, lỗi API, Guardian chặn giao dịch.
  • Không gửi API key qua Telegram.
  • Channel riêng cho prod vs dev.

Checklist trước live

  • [ ] Demo ≥ 4 tuần, log đầy đủ
  • [ ] Guardian: max loss ngày + kill switch
  • [ ] Backup config.yaml (không commit secret)
  • [ ] Test reconnect mạng/VPS reboot
  • [ ] Lot nhỏ tuần đầu live

Hub học: Khóa bot trading Python thực chiến.

FAQ

Bot chạy Mac/Linux được không?
MT5 chính thức Windows — dùng VPS Windows.

PM2 với Python trên Windows?
Dùng pm2 + script .cmd hoặc NSSM / Task Scheduler — chọn một, document rõ.

MQL5 EA song song Python?
Có thể; tránh double order — một nguồn signal duy nhất.


Xây dựng IB bằng Bot Auto Trading: https://www.huongnghiepdulieu.com/xay-dung-ib-bang-bot-auto-trading/ · Lịch khai giảng · Hotline 0934 145 100

Đặng Trí Thanh

Đặng Trí Thanh

Giám đốc Công nghệ · DNT Digital · Giảng viên HNDL
807 Bài viết
15.4k Người theo dõi
120k+ Lượt đọc

Đào tạo và triển khai thực chiến Python, MT5 và hệ thống bot auto trading / IB cho học viên và doanh nghiệp.