how to create a function to execute only on closing any position. whether ontrade or ontradetransaction . ?

 
I have created some functions to run ONLY when I CLOSE any POSITION and to achieve this i am using this function.
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
  {
  Print("type is:", trans.type);
  if(trans.type == TRADE_TRANSACTION_ORDER_DELETE)
    {
    Print("inside type closed :",trans.deal_type);
    string content = dealInfo();
    sendRequest(content);
     }
   else
     {
      return;
     }
  }

But this function is running on all events whether i am placing a trade or modifying  deleting .
please tell me how to create a logic that executes only when any position is closed.

 
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
{
        if(trans.type != 6) return;                                        		// only interested in DEAL_ADD events.
        if(trans.symbol!=_Symbol) return;                                    		// only interested in this symbol.
        if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY) != DEAL_ENTRY_OUT) return;   	// only interested in position closures
        
        // do what you do here...

}
 

Here's my code that will run when there's a closed position,

void CStrategy::OnTradeTransactionEvent(const MqlTradeTransaction& trans,
                                        const MqlTradeRequest&     request,
                                        const MqlTradeResult&      result) {
   
   // when closing buy position
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_SELL 
         && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)  {
      
      // do your stuff here
   }

   // when closing sell position
   if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_BUY
         && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT) {
      
      // do your stuff here
   }
}
 
Luandre Ezra #:

Here's my code that will run when there's a closed position,

struct CStrategy
  {
   void              OnTradeTransactionEvent(const MqlTradeTransaction& trans,
                                const MqlTradeRequest&     request,
                                const MqlTradeResult&      result)
     {

      // when closing buy position
      if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_SELL && trans.deal_type == DEAL_TYPE_BUY
         && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
        {
         Print("when closing both any one")
         // do your stuff here
        }

      // when closing sell position
      if(trans.type == TRADE_TRANSACTION_DEAL_ADD && trans.deal_type == DEAL_TYPE_BUY
         && HistoryDealSelect(trans.deal) && HistoryDealGetInteger(trans.deal, DEAL_ENTRY) == DEAL_ENTRY_OUT)
        {
         Print("when closing only buy ")
         // do your stuff here
        }
     }
  }

used this code but no respone on closing any of position

 
Code2219 or 2319 #:
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result)
  {
   if(trans.type != 6)
      return;                                          // only interested in DEAL_ADD events.
   if(trans.symbol!=_Symbol)
      return;                                      // only interested in this symbol.
   if(HistoryDealGetInteger(trans.deal,DEAL_ENTRY) != DEAL_ENTRY_OUT)
      return;    // only interested in position closures

   //string content = dealInfo();

  // if(content != NULL)
    // {
    Print("sent data on trade");
      //sendRequest(content);
    // }

  }


used your code but not working  I am not getting any call to this function on closing any position.

 
Anshul Chouhan:
I have created some functions to run ONLY when I CLOSE any POSITION and to achieve this i am using this function.

But this function is running on all events whether i am placing a trade or modifying  deleting .
please tell me how to create a logic that executes only when any position is closed.

this might help.

https://www.mql5.com/en/docs/event_handlers/ontradetransaction

in the sample cod/example is reference to        case 10036: return("TRADE_RETCODE_POSITION_CLOSED");     break;

Documentation on MQL5: Event Handling / OnTradeTransaction
Documentation on MQL5: Event Handling / OnTradeTransaction
  • www.mql5.com
OnTradeTransaction - Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

https://www.mql5.com/ru/code/35561

I have modified this by @Vladimir Karputov

i merely changed line #84 from TRADE_TRANSACTION_DEAL_ADD


I am unable to try it until next week. If you prove it working, please give report.

Alert Position Open
Alert Position Open
  • www.mql5.com
Помощник - оповещает выбранным звуком об открытии позиции
 

found this article too. 

https://www.mql5.com/en/articles/1111

It deals with all types of deals, so should be able to remove pending orders and modify types from the code.

MQL5 Cookbook: Processing of the TradeTransaction Event
MQL5 Cookbook: Processing of the TradeTransaction Event
  • www.mql5.com
This article considers capabilities of the MQL5 language from the point of view of the event-driven programming. The greatest advantage of this approach is that the program can receive information about phased implementation of a trade operation. The article also contains an example of receiving and processing information about ongoing trade operation using the TradeTransaction event handler. In my opinion, such an approach can be used for copying deals from one terminal to another.