handle onPositionClose

 

Is there a handle ?

onPositionClose(){

  // do something

}


Handle which will check, when broker closing the position by SL or TP, the trigger will launch onPositionClose function and give me the position info

 
waza123:

Is there a handle ?

onPositionClose(){

  // do something

}

Handle which will check, when broker closing the position by SL or TP, the trigger will launch onPositionClose function and give me the position info

In case you want to identify a position closure, you may use, for instance

if(!PositionSelect(_Symbol))
  {
   Print("Position was closed.");
  }

inside OnTrade() the event handler.

Regards,
Malacarne 

 
Malacarne:

In case you want to identify a position closure, you may use, for instance

inside OnTrade() the event handler.

Regards,
Malacarne 

this is hack, and does not give me info that position for that symbol was opened.
 
waza123:
this is hack, and does not give me info that position for that symbol was opened.
You asked about a position being closed, not opened...
 
You should use OnTradeTransaction event. The question was discussed and sample code provided here - https://www.mql5.com/en/forum/11996
Detect stop trigger in OnTradeTransaction backtest
Detect stop trigger in OnTradeTransaction backtest
  • www.mql5.com
Unfortunately, i get only DEAL_ENTRY_IN provided by MqlTradeTransaction passed to OnTradeTransaction. - - Category: technical indicators
 

On tradeTransaction is the way to go ; 


void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
{ 
 if(trans.symbol!=symbol) return;  
 if(trans.type != TRADE_TRANSACTION_DEAL_ADD) return;
 if(!PositionSelect(symbol))
// position closed
{}

}


it is definitively a PositionClosed event trap.

Reason: