recognize If SL was triggered

 

hi community, 

I cannot open position with SL direclty. [if I entry the value as 4th position - system doesnt accept] 

  if (!trade.Sell(lots, NULL, bid, 0, 0, "comment"))
     Print("error on sell open position");

so I add BuyStop afterwards:

   if (!trade.BuyStop(orderLots, buyStopPrice, _Symbol, 0, 0, ORDER_TIME_GTC, 0, "buy stop loss triggered"))
       Print("error buy stop loss");

Everything works ok, but now my EA needs to have knowledge If position was closed by stop loss.

void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
{
    if (HistoryDealSelect(trans.deal) == true)
    {   
        ENUM_DEAL_REASON deal_reason = (ENUM_DEAL_REASON)HistoryDealGetInteger(trans.deal, DEAL_REASON); 
    Print(EnumToString(deal_reason));
    }
}


and If price hits my BuyStop position the deal_reason returns  DEAL_REASON_EXPERT and no DEAL_REASON_SL  how I wish:) 

1. Why system does not accept to set SL at the beginning - broker's fault? Did I forget sth ? 

2. How can I differentiate If SL was triggered? 


for the reponses - thanks a lot!

 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.