Download MetaTrader 5

Find out if a trade was stopped out

To add comments, please log in or register
Learning MQL5? Start with documentation!
Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2012.01.05 22:07 
What is a simple method to figure out that the last trade done was a loss?
johhny6
130
johhny6 2012.01.07 09:51  
ingvar_e:
What is a simple method to figure out that the last trade done was a loss?

Look at the history, it shows you if the trade was a loss or profit.


it also tells you in the comments for that trade, if it was because of a stop-loss or take-profit (sl: xxx or tp: xxx)

Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2012.01.07 11:44  
johhny6:

Look at the history, it shows you if the trade was a loss or profit.


it also tells you in the comments for that trade, if it was because of a stop-loss or take-profit (sl: xxx or tp: xxx)

I tried that. I checked history after the trade had finished, the code was in progress to start a new trade. I had saved the ticket for the

completed trade. I found the trade. I could read a lot of info about the trade but profit was 0 even if the trade had gone to stoploss. So I took another route. 

I work with TP and SL so I just keep tab that these leels are reached. Not perfect but will work for now (developing and running strategy tester). If someone has a  piece of code I will try it 

Code below always show zero profit even though the trade has finished

int TradeResult(ulong ticket,string symbol,string id)
 {   
    Logger("TradeResult In:","Ticket",IntegerToString(ticket),"symbol:",symbol,"TradeId:",id);
    if (!HistorySelect(0,TimeCurrent()))
     return 0;
       
    mydeal.Ticket(ticket);
    ulong morder = mydeal.Ticket();
    if(morder)
     {
      string msymbol = mydeal.Symbol();
      double mprofit = mydeal.Profit();
      double mprice = mydeal.Price();
      string mid = mydeal.Comment();
      int mtradetype = mydeal.Type();
      string mtradedescr = mydeal.TypeDescription();
      Logger("TradeResult Out","Ticket",IntegerToString(morder),"symbol:",msymbol,"Comment:",mid,mtradedescr,DoubleToString(mprofit,2),DoubleToString(mprice,5));
      if(mprofit>0)
       {
         Logger("TradeResult","TradeId:",mid,"Profit");
         return 1;
       }
      if(mprofit<0)
       {
         Logger("TradeResult","TradeId:",mid,"Loss");
         return -1;
       }   
       
     }
    else
      Logger("TradeResult","Ticket not found","Ticket",IntegerToString(ticket)); 
    return 0;
 }



Stephen Njuki
5179
Stephen Njuki 2012.01.07 12:40  
ingvar_e:

 

Hi

 

What does this mean?

    mydeal.Ticket(ticket);
    ulong morder = mydeal.Ticket();
    if(morder)
     {
//---

 

 I think you should select the most recent order by its position's index and I think this is equal to the total history orders minus one.

 

 

Ingvar Engelbrecht
4504
Ingvar Engelbrecht 2012.01.08 00:52  
ssn:

 

Hi

 

What does this mean?

 

 I think you should select the most recent order by its position's index and I think this is equal to the total history orders minus one.


I just save the  ticket for the previous trade in "Ticket" and use that to retrieve the data for it. It works. Other data that I receive this way

are for the previous trade so its correct. It is just that  mydeal.Profit()  is always zero and that is incorrect. I have both SL and TP profit set

and the trade has ended.

 

 


To add comments, please log in or register