if stopped out - Coding Help

 

Hi


I am trying to find whether the last order closed is by Stop-loss or not.

My fixed stop-loss is 100 pips, so I call order history and check the OrderClosePrice() > (100-10)

int Hist=OrdersHistoryTotal();
OrderSelect(Hist, SELECT_BY_POS,MODE_HISTORY);
if(OrderCloseTime()!=0)
{
  if(OrderType() == OP_BUY && OrderClosePrice() < OrderStopLoss()+0.001 && OrderOpenPrice()-OrderClosePrice() > 80)
  {
    CloseNextSell();
    Comment("Ticket ",OrderTicket()," Buy Order Closed By Stoploss" );
  }
  if(OrderType() == OP_SELL && OrderClosePrice() > OrderStopLoss()-0.001 && OrderClosePrice()-OrderOpenPrice() > 80)
  {
    CloseNextBuy();
    Comment("Ticket ",OrderTicket()," Sell Order Closed By Stoploss");
  }
}

Please correct me.

 
int Hist=OrdersHistoryTotal();// OrdersHistoryTotal()-1

if(OrderCloseTime()!=0) // how can be OrderCloseTime() not equal to something in History

OrderOpenPrice()-OrderClosePrice() > 80 // my guess is OrderOpenPrice()-OrderClosePrice() > 80*Point
 
qjol:

datetime OrderCloseTime( )
Returns close time for the currently selected order. If order close time is not 0 then the order selected and has been closed and retrieved from the account history. Open and pending orders close time is equal to 0.
Note: The order must be previously selected by the OrderSelect() function.
Sample:
  if(OrderSelect(10,SELECT_BY_POS,MODE_HISTORY)==true)
    {
     datetime ctm=OrderOpenTime();
     if(ctm>0) Print("Open time for the order 10 ", ctm);
     ctm=OrderCloseTime();
     if(ctm>0) Print("Close time for the order 10 ", ctm);
    }
  else
    Print("OrderSelect failed error code is",GetLastError());
 
qjol:


OrderOpenPrice()-OrderClosePrice() > 80 // my guess is OrderOpenPrice()-OrderClosePrice() > 80*Point
Yes it is. thanks
 

but u r selecting from the history

if(OrderSelect(10,SELECT_BY_POS,MODE_HISTORY)==true)

so sure it's not equal to 0 what u r checking again ?

 
qjol:

but u r selecting from the history

so sure it's not equal to 0 what u r checking again ?


sorry I am just learning, I will remove if its irrelevant. other than that is it okay?
 
u tell me i don't know i didn't checked your script i only gave u some points to think about
 
richo:
I am trying to find whether the last order closed is by Stop-loss or not
    for(int iPos= OrdersHistoryTotal() - 1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         <= OP_SELL // Avoid cr/bal https://www.mql5.com/en/forum/126192
    ){
        if( MathAbs(OrderClosePrice() - OrderTakeProfit()) 
          > MathAbs(OrderClosePrice() - OrderStopLoss()  ){ // Closed by SL
        }
        else{                                               // Closed by TP
        }
        break; // Last closed only
    }
 
WHRoeder:
x

Thank you so much. very helpful.
Reason: