# Historical Data method of close

Publish the program in the Market and sell it to millions of traders!
105
2015.11.01 12:04
I'm trying to find a way to specify how the last order was closed. I have used the OrdersHistoryTotal() then OrderSelect(index, SELECT_BY_POS , MODE_HISTORY) and finally OrderType() but this simply refers to the ENUM for OP_BUY, OP_SELL etc. Is there anyway to specifically find out if an order closed by s/l , t/p or closed manually? Thanks.
14206
2015.11.01 15:32
Not compiled, not tested
```enum ClosedBy { BY_SL, BY_TP, BY_CLOSE };
ClosedBy OrderClosedBy(void){
double minGapStop = MarketInfo(OrderSymbol(), MODE_STOPLEVEL) * _Point;
double dir        = op_direction(OrderType() );
double ocp        = OrderClosePrice();
double osl        = OrderStopLoss();
double otp        = OrderTakeProfit();
if(osl != 0.0 && (ocp - osl)*dir <  minGapStop) return BY_SL;
if(otp != 0.0 && (otp - ocp)*dir < -minGapStop) return BY_TP;
return BY_CLOSE; // including stop out
}
double   op_direction(int op){
return OP_BUY == (op % MKT_COUNT) ? +1.0 : -1.0;
}
```
Not compiled, not tested
105
2015.11.01 15:55
Thanks, Seems pretty straight forward. Whats the MKT_COUNT variable? if op is 0 wont you get divide by 0 error? I'll just use a switch i think. Thanks again.
14206
2015.11.01 16:04
 Keelan:Whats the MKT_COUNT variable? if op is 0 wont you get divide by 0 error? Buy=0, Sell=1, Count=2; works for pending orders also. Where in my post do you see a divide by op?
105
2015.11.01 16:32
It works btw and nvm 0 / 2 will give the -1 but the MKT_COUNT isn't standard. Thanks a lot for the help. They should have functions like this built in. by the by, do you know why you cant use long double? i thought it was standard.
/