Trade Request "Position Close", how to work around this?

 

Hello community,

I need to catch the event in the "OnTradeTransaction", that the whole postion is going to close (request). I tried to vary with the Trade Operation Types and the Trade Transaction Types, but I cannot manage to create this "event" -> Position Close.

I´m sure it is pretty simple, but at the moment I got stuck with this problem. Any, hints?


Kindly,

Olle Onkel

 
What have you tried ?
 
angevoyageur:
What have you tried ?

Better explanantion: It is possible "to filter" the event of change in sl/tp with TRADE_ACTION_SLTP.

For instance, I have an open position in EURUSD with a SL. The price is hitting the SL and the position is close. How can the EA filter the "event" that the whole position were closed. I tried some mixtures in the OnTradeTransaction methode like this:

if(request.action == TRADE_ACTION_DEAL ...

...but how further. How can the EA recognize, that the whole position was closed. Not a part of it, the whole.

I hope this is more perspicuous what I mean (?)

 

If you want to identify if was closed by SL or TP, then maybe this helps:

void OnTrade()
  {
   ulong      ld_ticket=0;
   static int old_deals;
//---
   if(HistorySelect(0,TimeCurrent()))
     {
      int i=HistoryDealsTotal();
      if(old_deals==i)
        {
         return;
        }
      old_deals=i;
      ld_ticket=HistoryDealGetTicket(i-1);
      string ld_comment=HistoryDealGetString(ld_ticket,DEAL_COMMENT);
      if(StringFind(ld_comment,"sl",0)!=-1)
        {
         Print("Close by Stop Loss: ",ld_comment);
        }
      if(StringFind(ld_comment,"tp",0)!=-1)
        {
         Print("Close by Take Profit: ",ld_comment);
        }
     }
  }
 
olle.onkel:

Better explanantion: It is possible "to filter" the event of change in sl/tp with TRADE_ACTION_SLTP.

For instance, I have an open position in EURUSD with a SL. The price is hitting the SL and the position is close. How can the EA filter the "event" that the whole position were closed. I tried some mixtures in the OnTradeTransaction methode like this:

...but how further. How can the EA recognize, that the whole position was closed. Not a part of it, the whole.

I hope this is more perspicuous what I mean (?)

What you mean is perfectly clear, what you tried is not.

By the way, see this topic, maybe it can help you.

 

Hello and thanks for your replies,

the content of the topic which angevoyageur posted hit my problem at the closest. But even there no satisfying solution was included.

So, I came to the conclusion, that all my positions will be stored in a struct - array and updated with CPositionInfo on every OnTradeTransaction(). If the comparison of the CPostionInfo and the position-struct-array comes to the result that the position which exists in the array is not part of CPositionInfo, than I have the result "position closed" -> thats my desired "event". 

I thought that there might be a less complicated way like "TRADE_ACTION_POSITION_CLOSE", which would have been to filter easily.


Kindly,

Olle Onkel

 
olle.onkel:

Hello and thanks for your replies,

the content of the topic which angevoyageur posted hit my problem at the closest. But even there no satisfying solution was included.

So, I came to the conclusion, that all my positions will be stored in a struct - array and updated with CPositionInfo on every OnTradeTransaction(). If the comparison of the CPostionInfo and the position-struct-array comes to the result that the position which exists in the array is not part of CPositionInfo, than I have the result "position closed" -> thats my desired "event". 

I thought that there might be a less complicated way like "TRADE_ACTION_POSITION_CLOSE", which would have been to filter easily.


Kindly,

Olle Onkel

Your conclusion is wrong. All what you need is provided in the link I gave you, even the code.

 
angevoyageur:

Your conclusion is wrong. All what you need is provided in the link I gave you, even the code.

Hello angevoyageur,

in this case I´m sorry, I might have overlooked it. I have problems to understand the code sometimes.


Kindly,

Olle Onkel

Reason: