You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求修改request.comment值并实现止损触发类型通知功能

Hey there! Let's work through this problem together. The core idea is to tag each order with a unique identifier for its stop-loss type in the comment field when placing the trade, then detect which stop triggered the close once the order exits, and send a targeted notification about it. Here's how to implement this step by step:

Implement Stop-Loss Type Notification & Custom Order Comments

Step 1: Customize Order Comments with Stop-Loss Tags

First, when placing your order, modify the request.comment to include a clear suffix that indicates the type of stop-loss you're using. For example, if you have three common SL types (fixed, trailing, dynamic), you can append a unique tag to your base comment.

Here's the adjusted order placement code:

// Define your stop-loss types to match your strategy
enum SL_TYPE {
    SL_FIXED,
    SL_TRAILING,
    SL_DYNAMIC
};

// Set this variable based on your actual SL logic (e.g., switch based on strategy rules)
SL_TYPE current_sl_type = SL_FIXED;

// Build the final comment with SL type tag
string base_comment = pComment;
string sl_tag = "";
switch(current_sl_type) {
    case SL_FIXED:
        sl_tag = "_SL_FIXED";
        break;
    case SL_TRAILING:
        sl_tag = "_SL_TRAILING";
        break;
    case SL_DYNAMIC:
        sl_tag = "_SL_DYNAMIC";
        break;
}
request.comment = base_comment + sl_tag;

// Finalize order parameters
request.action = TRADE_ACTION_DEAL;
request.symbol = pSymbol;
request.type = pType;
request.sl = pStop;
request.tp = pProfit;
request.volume = pVolume;

// Set correct price based on order direction
if(pType == ORDER_TYPE_BUY) {
    request.price = SymbolInfoDouble(pSymbol, SYMBOL_ASK);
} else if(pType == ORDER_TYPE_SELL) {
    request.price = SymbolInfoDouble(pSymbol, SYMBOL_BID);
}

ulong order_ticket;
if(!OrderSend(request, order_ticket)) {
    Print("Order placement failed! Error code: ", GetLastError());
} else {
    Print("Order placed successfully. Ticket: ", order_ticket);
}

Step 2: Detect Stop-Loss Triggers & Send Notifications

In MT5, use the OnTrade() event handler to monitor trade activity. When an order is closed by a stop-loss, we'll check its comment tag to identify which SL type triggered the close, then send a notification.

Add this event handler to your expert advisor:

void OnTrade() {
    MqlTradeEvent trade_event;
    if(!TradeGetEvent(trade_event)) {
        Print("Failed to retrieve trade event. Error: ", GetLastError());
        return;
    }

    // Only process closed orders
    if(trade_event.action != TRADE_ACTION_DEAL || trade_event.type != ORDER_TYPE_CLOSE) {
        return;
    }

    // Fetch the closed order's details
    MqlOrder closed_order;
    if(!OrderSelect(trade_event.order, SELECT_BY_TICKET, MODE_HISTORY) || !OrderGetOrder(trade_event.order, closed_order)) {
        Print("Failed to load closed order details. Error: ", GetLastError());
        return;
    }

    // Check if the order was closed by stop-loss
    if(closed_order.close_reason == ORDER_CLOSE_REASON_SL) {
        string sl_type_label = "Unknown Stop-Loss Type";
        
        // Parse the comment to identify the SL type
        if(StringFind(closed_order.comment, "_SL_FIXED") != -1) {
            sl_type_label = "Fixed Stop-Loss";
        } else if(StringFind(closed_order.comment, "_SL_TRAILING") != -1) {
            sl_type_label = "Trailing Stop-Loss";
        } else if(StringFind(closed_order.comment, "_SL_DYNAMIC") != -1) {
            sl_type_label = "Dynamic Stop-Loss";
        }

        // Compose and send the notification
        string notification_msg = StringFormat(
            "Order #%d closed by %s!\nSymbol: %s\nVolume: %.2f",
            closed_order.ticket, sl_type_label, closed_order.symbol, closed_order.volume
        );
        SendNotification(notification_msg);
        Print(notification_msg); // Log for debugging
    }
}

Quick Notes for MT4 Users

If you're working in MT4 (which doesn't have the OnTrade() event), you'll need to add logic in the OnTick() function to scan the order history for recently closed orders, then check their close reason and comment tag using similar parsing logic.

内容的提问来源于stack exchange,提问作者Pingui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:05:40