1. Giới thiệu
Trong giao dịch tự động trên MetaTrader 5 (MT5), việc ghi lại lịch sử giao dịch là cực kỳ quan trọng. Thay vì phải export thủ công từ MT5, bạn có thể lập trình EA/Indicator để tự động gửi kết quả giao dịch (trade results) trực tiếp tới Google Sheet.
Điều này mang lại lợi ích:
- Theo dõi từ xa: Xem kết quả giao dịch ở bất kỳ đâu.
- Phân tích dữ liệu: Kết hợp với Google Data Studio hoặc Excel.
- Quản lý nhiều tài khoản: Tập trung dữ liệu về một file.
2. Ý tưởng thực hiện
- Có hai cách phổ biến để gửi dữ liệu từ MT5 sang Google Sheet:
- Dùng Google Apps Script làm API trung gian.
- Tạo Web App trong Google Apps Script.
- Nhận request từ MT5 qua WebRequest().
- Ghi dữ liệu vào Sheet.
- Dùng link public Google Sheet CSV (ít bảo mật, ít dùng cho write).
- Chỉ phù hợp cho việc đọc.
- Không hỗ trợ ghi dữ liệu trực tiếp.
👉 Trong bài này, chúng ta sẽ dùng cách Google Apps Script API để ghi dữ liệu giao dịch từ MT5 sang Google Sheet.
3. Tạo API Google Apps Script
Bước 1: Tạo Google Sheet
Tạo 1 file Google Sheet, đặt tên MT5 Trade Log.
Tạo các cột:
Time | Symbol | Type | Volume | Price | Profit
Bước 2: Tạo Apps Script
Vào Extensions → Apps Script, dán code sau:
function doPost(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = JSON.parse(e.postData.contents);
sheet.appendRow([data.time, data.symbol, data.type, data.volume, data.price, data.profit]);
return ContentService.createTextOutput("Success");
}
Publish → Deploy as Web App → Anyone, even anonymous.
Lấy URL dạng:
https://script.google.com/macros/s/XXXXXXXXXXXX/exec
4. Code MQL5 gửi dữ liệu giao dịch
Hàm gửi dữ liệu WebRequest
string SendToGoogle(string url, string json)
{
char result[];
string headers;
string response;
int res = WebRequest("POST", url, "application/json", NULL, 0, json, result, headers);
if(res == -1)
{
Print("Lỗi WebRequest: ", GetLastError());
return "";
}
response = CharArrayToString(result, 0, -1);
return response;
}
Hàm ghi log giao dịch
void LogTradeToGoogle(string url, ulong ticket)
{
if(!HistorySelectByTicket(ticket)) return;
MqlTradeRecord tr;
if(!HistoryDealGetTicket(ticket)) return;
string symbol = HistoryDealGetString(ticket, DEAL_SYMBOL);
double volume = HistoryDealGetDouble(ticket, DEAL_VOLUME);
double price = HistoryDealGetDouble(ticket, DEAL_PRICE);
double profit = HistoryDealGetDouble(ticket, DEAL_PROFIT);
datetime time = (datetime)HistoryDealGetInteger(ticket, DEAL_TIME);
int type = HistoryDealGetInteger(ticket, DEAL_TYPE);
string json = StringFormat("{\"time\":\"%s\",\"symbol\":\"%s\",\"type\":\"%d\",\"volume\":\"%.2f\",\"price\":\"%.5f\",\"profit\":\"%.2f\"}",
TimeToString(time, TIME_DATE|TIME_SECONDS),
symbol,
type,
volume,
price,
profit);
string resp = SendToGoogle(url, json);
Print("Kết quả gửi: ", resp);
}
Gọi hàm sau khi có giao dịch
Bạn có thể gọi trong OnTradeTransaction:
void OnTradeTransaction(const MqlTradeTransaction &trans,
const MqlTradeRequest &request,
const MqlTradeResult &result)
{
if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
{
string api_url = "https://script.google.com/macros/s/XXXXXXXXXXXX/exec";
LogTradeToGoogle(api_url, trans.deal);
}
}
5. Mở rộng và tối ưu
- Thêm UserID hoặc AccountNumber để quản lý nhiều trader.
- Bảo mật: thêm Key/Token khi gửi request để chống spam.
- Kết hợp phân tích: Dùng Google Data Studio để tạo Dashboard.
- Lưu cả trạng thái lệnh mở chứ không chỉ lệnh đóng.
6. Kết luận
Với hàm trên, bạn đã có thể gửi kết quả giao dịch từ MT5 tới Google Sheet hoàn toàn tự động. Đây là bước quan trọng trong việc xây dựng hệ thống quản lý giao dịch chuyên nghiệp, đặc biệt cho các EA thương mại hoặc quỹ giao dịch.

