[WARNING CLOSED!] Any newbie question, so as not to clutter up the forum. Professionals, don't go by. Can't go anywhere without you. - page 275

 

The question is.

How to get a number without minus, if it turns out that Open[1] < Close[1] and when you subtract Open[1]-Close[1] I get the value with a minus, how to make it was always a positive value, I need to calculate the distance between Open and Close maybe there is another way?

Thank you.

 
Martingeil >> :

The question is.

How to get a number without minus, if it turns out that Open[1] < Close[1] and when you subtract Open[1]-Close[1] I get the value with a minus, how to make it was always a positive value, I need to calculate the distance between Open and Close maybe there is another way?

>> Thank you.

https://docs.mql4.com/ru/math/MathAbs

 

;) >> Thank you!

 

Good day!

When writing an EA for MT4, there was a need to determine programmatically (i.e. in the code of the EA itself) if at least one order of this EA was opened or not. Can you advise whether there is such a function? Perhaps, if there is, it would look something like this

Bool (has an EA order with this magic number been opened)

{answer= True/False}

?

Experts - help please - we are already wracking our brains

P.S.- tried the option with order numbers - didn't work

 
waitra >> :

Good day!

When writing an EA for MT4, there was a need to determine programmatically (i.e. in the code of the EA itself) whether at least one order of this EA was opened or not. Do you know if there is such a function?

Will this function work?

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru/                  |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает количество позиций.                                 |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal(), kp=0;

  if ( sy=="0") sy=Symbol();
  for ( i=0; i< k; i++)                                    {
    if (OrderSelect( i, SELECT_BY_POS, MODE_TRADES))      {
      if (OrderSymbol()== sy || sy=="")                   {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if ( op<0 || OrderType()== op)                   {
            if ( mn<0 || OrderMagicNumber()== mn) kp++;
          }}}}}
  return( kp);
}

Example usage: We put this function at the very end of our code and check it when we open a position:



if ( NumberOfPositions(NULL,OP_BUY, Magic)< 1) {
// если нет окрытых OP_BUY позиций с заданным магиком, покупаем
ticket=OrderSend(Symbol(),0, Lots,Ask, Slippage,Bid- SL*Point,
                      Ask+ TP*Point, NULL, Magic ,0,Blue);
                                              }

If we don't look for positions but for pending orders, we can use this function

Function NumberOfOrders().
This function returns the number of orders . You can limit the list of orders to be checked using the function parameters:
sy - Name of the instrument. If we set this parameter, the function will only check the orders of the specified instrument. NULL means current instrument, and "" (default) means any instrument.
op - Type of pending order. Valid values: OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or -1. The default value of -1 means any order.
mn - Order identifier (MagicNumber). The default value of -1 means any MagicNumber.

https://www.mql5.com/ru/forum/107476/page12

 

Please explain what the files provide:

#include <stdlib.mqh>
#include <stderror.mqh>

If possible, in some simple way.

Thank you.

 
Serbov писал(а) >>

Please explain what the files provide:

If possible, in some simple way.

Thank you.

they are so-called "library files". these files store frequently used functions. they are used to avoid loading the source. if the library file is "declared", the functions from the declared file can be used as if they were included in the source.

 
Serbov >> :

Please explain what the files give us:

If possible, please keep it simple.

Thank you.

When using (calling) a function from these files (already initially built into mt4) we, in case of incorrect operation of the EA, can see in the log the number of the error we made in the code .

For example, we call the GetLastError() function :

ticket= OrderSend(Symbol(),OP_BUY, lots,Ask,3, SL, TP,"Name_Expert", Magic,0, clOpenBuy );
   if( ticket < 0)
 {Print("Ошибка открытия ордера BUY #", GetLastError());  } 

In this case, if we made an error in the code or set incorrect external parameters, we will see the number of this error in the log.

For example, 130.

And the decoding of the number can be found at -https://book.mql4.com/ru/appendix/errors

And we do not need to go through the whole code, because we already know where the mistake is made!

 
rid писал(а) >>

Will this function work?


Example usage: we put the function itself at the very end of the code (outside the START function) and then check it when opening a position:



If we don't look for positions but for pending orders, we can use

Function NumberOfOrders().
This function returns the number of orders . You can limit the list of orders to be checked using the function parameters:
sy - Name of instrument. If you specify this parameter, the function will only check the orders of the specified instrument. NULL means current instrument, and "" (by default) means any instrument.
op - Type of pending order. Valid values: OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or -1. The default value of -1 indicates any order.
mn - Order identifier (MagicNumber). Default value -1 means any MagicNumber.

https://www.mql5.com/ru/forum/107476/page12

Thank you very much for the tip, we tried it but it did not work, please check if there is a mistake somewhere in the code:

bool Open_Buy()

{

   bool res=false;    


if( условие_1)        { 

       if ( NumberOfPositions(NULL, Magic)< 1) // если нет открытых позиций с заданным магиком

          { res=true; }}

else  

   {

    if( условие_1) 

       { 

       if ( NumberOfPositions(NULL, Magic)> 1) // если советник уже открывал позиции

       {

          if ( условие_2)              { 

             res=true; }

           } 

}}

return( res);

}

If we want to check if there is a Sell or Buy condition and we have not open or closed an order, in this case, only the first condition will work. And if the EA has already opened orders in the past, then, when an order is opened, the first + second conditions should trigger.

I looked through the link provided by you, maybe the ExistOrders(); is more suitable?

(but I honestly could not place it, since I apparently lack the skill for the time being).

 

You have not called the function correctly.

Your function is called like this: if (NumberOfPositions(NULL,Magic)> 1)

It should look like this:

if( условие_1)        { 

       if ( NumberOfPositions(NULL,-1, Magic)< 1) 
// если нет открытых позиций с заданным магиком

          { res=true; }}

Feel the difference (you omitted "-1") ! and RECTIFY YOUR CODE.

To control it, you can print a comment on the chart. For example, like this (at the beginning of the START function).

Comment ("Количество текущих открытых советником позций = ",
                                  NumberOfPositions(NULL,-1,Magic)  );
Magic_2=Magic*3;
As for your second condition, I would ("without thinking too hard") set a different magician and separate function Open_Buy_2() to open positions by the second condition !

The code of the second condition would look like this

 if ( условие_2)                          { 
  if ( NumberOfPositions(NULL,-1, Magic)== 1)   {
    if ( NumberOfPositions(NULL,-1, Magic_2)< 1) { 
..... ..... 
Reason: