Need a help to write one function.

 
facestabs: Im struggling with function which gives true when the latest closed order had been closed by stop loose. Could you help me ?
  1. Help you with what? You haven't stated a problem. Show us your attempt and state the nature of your problem.

  2. Find the last closed order, test it.
    bool hasHitSL = MathAbs( OrderClosePrice() - OrderStopLoss() ) < MathAbs( OrderClosePrice() - OrderTakeProfit() );
  3. Not a good idea to use comments, brokers can change comments, including complete replacement and some do not mark closed orders with a reason.
 
facestabs:
Hello guys!
Im struggling with function which gives true when the latest closed order had been closed by stop loose. Could you help me ? 
Thanks in adnvance!!!

As you neglected to show us your source code, I would surmise that you don't know how to code and that you are looking for others to do the work for you.

If that is the case, then perhaps you can hire someone from the freelance section to assist you.


Freelance

https://www.mql5.com/en/job


If you don't wish to hire someone, then you will need to learn to code.

What you are trying to do can be done using an EA.

For that you can read over the documentation.

Documentation

https://www.mql5.com/en/docs


For working examples of how to code, you can review the code base.

CodeBase

https://www.mql5.com/en/code

MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
  • www.mql5.com
MetaQuotes Language 5 (MQL5) is a high-level language designed for developing technical indicators, trading robots and utility applications, which automate financial trading. MQL5 has been developed by MetaQuotes Software Corp. for their trading platform. The language syntax is very close to C++ enabling programmers to develop applications in...
 

I meant I need to write such a function.

More precisely:

OrderSelect(0,SELECT_BY_POS,MODE_HISTORY);

      if(????????)   <------- (I cant figure out what condition

      {                           checks if it was close by stop loose)

         a=1;

      }

 
facestabs:

I meant I need to write such a function.

More precisely:

OrderSelect(0,SELECT_BY_POS,MODE_HISTORY);

      if(????????)   <------- (I cant figure out what condition

      {                           checks if it was close by stop loose)

         a=1;

      }

First thing....


Second thing...

I told you what you can do. You can learn to code, look over the code base and find something similar for an example to work from, or hire someone to do it properly for you.

You've made no attempt to show your actual work. If you want help, you have to put forth an effort.

If you do show your work, please include a FULL copy of your code and not just a random snippet so that we can properly evaluate what you've done.


Third thing...

You mentioned you needed to create a function.

Here is the documentation page for functions.

https://www.mql5.com/en/docs/basis/function



 
bool LastOrderClosedByStop()
{
   int      ticket      =-1;
   datetime last_time   = 0;
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)&&OrderSymbol()==_Symbol&&OrderCloseTime()>last_time)
      {
         last_time = OrderCloseTime();
         ticket = OrderTicket();
      }
   if(!OrderSelect(ticket,SELECT_BY_TICKET))
   {
      Print("OrderSelectError: ",GetLastError())
      return false;
   }    
   // credit to whroeder -->
   return fabs(OrderClosePrice()-OrderStopLoss()) < fabs(OrderClosePrice()-OrderTakeProfit());
}
Reason: