Download MetaTrader 5
To add comments, please log in or register
Download MetaTrader 5 and start trading today!
ThreadHandle
105
ThreadHandle 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.
whroeder1
13627
whroeder1 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
ThreadHandle
105
ThreadHandle 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.
whroeder1
13627
whroeder1 2015.11.01 16:04  
Keelan:
Whats the MKT_COUNT variable?
if op is 0 wont you get divide by 0 error?
  1. Buy=0, Sell=1, Count=2; works for pending orders also.
  2. Where in my post do you see a divide by op?
ThreadHandle
105
ThreadHandle 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.
/
To add comments, please log in or register