# How i understand Close trade by TP

21374

Hi Guys

Is there any MQL code that i understand last trade is close by TP. I wrote below code but it is not work.Because sometime ClosePrice=TakePrice is not True. I attach picture that u understand that ClosePrice=TakePrice is not always true. How can i solve this problem.

```double GetLastTrade()
{
int ord; lord=0;
string c;
//----
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
if(OrderSymbol()==Symbol())
{
if (OrderType()==OP_SELL) lord=-1;
c=StringSubstr(OrderComment(),0,StringFind(OrderComment(),"_",0));
lpos=StrToInteger(c);

lop = NormalizeDouble(OrderOpenPrice(), Digits);
lcp = NormalizeDouble(OrderClosePrice(), Digits);
lsl = NormalizeDouble(OrderStopLoss(), Digits);
ltp = NormalizeDouble(OrderTakeProfit(), Digits);
llot = OrderLots();
lProfit = OrderProfit();

if (ltp==lcp)  //OrderProfit()>0
return(OrderLots());
else
return(-OrderLots());
}
}
return(0);
}```

Moderator
18024

capilta:

Hi Guys

Is there any MQL code that i understand last trade is close by TP.

No, there isn't. You have to do it by deduction.
15595

capilta: but it is not work.Because sometime ClosePrice=TakePrice is not True.
1. Of course it doesn't. The == operand. - MQL4 forum
2. You have to do it by deduction.
```bool isCloseByTP = MathAbs( OrderClosePrice() - OrderTakeProfit() ) // Assumes no
< MathAbs( OrderClosePrice() - OrderStopLoss() );  // OrderClose```