对处理程序编程以进行止损

时间:2019-02-14 17:41:21

标签: mql5

我想每次自动触发止损(或获利)时执行一系列命令。

我不确定该怎么做,因为我还没有找到这些事件的处理程序。

1 个答案:

答案 0 :(得分:2)

void OnTradeTransaction(const MqlTradeTransaction& trans,const MqlTradeRequest& request,const MqlTradeResult& result)
   {
    if(trans.type!=TRADE_TRANSACTION_DEAL_ADD)return;
    if(!HistoryDealSelect(trans.deal))return;
    if(HistoryDealGetInteger(trans.deal,DEAL_MAGIC)!=InpMagicNumber)return;
    if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY)!=DEAL_ENTRY_OUT)return;
    if(HistoryDealGetString(trans.deal,DEAL_SYMBOL)!=_Symbol)return;
    long reason=HistoryDealGetInteger(trans.deal,DEAL_REASON);
    if(reason==DEAL_REASON_SL)
       {
        Print(__LINE__,__FILE__," we are afraid SL is hit);
       }
    else if(reason==DEAL_REASON_TP)
       {
        Print(__LINE__,__FILE__," TP is HIT!);
       }
   }