Useful features from KimIV - page 115

 

Hello Andrey!

  1. There are already three functions for the opening price:
  2. There are also functions for the tickets. There are two of them:
Andrey, these functions have everything you need, except the selection by lot size. I will make in the near future other functions in which the open price and the ticket can be obtained by lot size.

 
KimIV:

Hello Andrey!

  1. There are already three functions for the opening price:
  2. There are also functions for the tickets. There are two of them:
Andrey, these functions have everything you need, except the selection by lot size. I will make in the near future other functions in which the open price and the ticket can be obtained by lot size.
Perhaps, they are redundant for me and I have to get rid of unnecessary conditions, such as: price of the next open position, last open position, closest to the market. And it doesn't always turn out to be correct. Besides, the understanding of all these subtleties will be faster if we compare them correctly. Thank you, I look forward to it.
 

The GetOpenPriceByLot() function.

This function returns the opening price of a position or order by lot size. The selection of positions to be taken into account is specified by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of the specified instrument. The default value"" means any market instrument.NULL value means the current instrument.
  • op- Trade operation, position or order type. Valid values:OP_BUY,OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or-1. The default value of-1 means any trade operation.
  • mn- Identifier of a position or order, MagicNumber. The default value of-1 means any identifier.
  • lo- Lot. The default value0 means any lot.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 29.03.2013                                                     |
//|  Описание : Возвращает цену открытия позиции или ордера по размеру лота.   |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ( ""  - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - торговая операция          ( -1  - любая операция)                 |
//|    mn - MagicNumber                ( -1  - любой магик)                    |
//|    lo - лот                        (  0  - любой лот)                      |
//+----------------------------------------------------------------------------+
double GetOpenPriceByLot(string sy="", int op=-1, int mn=-1, double lo=0) {
  double p=0;
  int    i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (mn<0 || OrderMagicNumber()==mn)) {
        if ((op<0 || OrderType()==op) && OrderType()<6) {
          if (lo==0 || lo==NormalizeLot(OrderLots())) p=OrderOpenPrice();
        }
      }
    }
  }
  return(p);
}
ZS. Attached is a script to testGetOpenPriceByLot()function.
 

The GetTicketByLot() function.

This function returns the ticket of a position or order by lot size. The selection of the positions to be taken into account is specified by external parameters:

  • sy- Name of market instrument. If this parameter is set, the function will only consider positions of the specified symbol. The default value"" means any market instrument.NULL value means the current instrument.
  • op- Trade operation, position or order type. Valid values:OP_BUY,OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or-1. The default value of-1 means any trade operation.
  • mn- Identifier of a position or order, MagicNumber. The default value of-1 means any identifier.
  • lo - Lot. The default value 0 means any lot.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 29.03.2013                                                     |
//|  Описание : Возвращает тикет позиции или ордера по размеру лота.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ( ""  - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - торговая операция          ( -1  - любая операция)                 |
//|    mn - MagicNumber                ( -1  - любой магик)                    |
//|    lo - лот                        (  0  - любой лот)                      |
//+----------------------------------------------------------------------------+
int GetTicketByLot(string sy="", int op=-1, int mn=-1, double lo=0) {
  int i, k=OrdersTotal(), t;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (mn<0 || OrderMagicNumber()==mn)) {
        if ((op<0 || OrderType()==op) && OrderType()<6) {
          if (lo==0 || lo==NormalizeLot(OrderLots())) t=OrderTicket();
        }
      }
    }
  }
  return(t);
}

ZS. Attached is a script to test GetTicketByLot() function.


 

Hello Igor!

Thank you very much for the new features and for being so quick. Everything is very good. The function returns, for example, a ticket order by conditions, which can be set by external variables or you can not use them. I'm a little unclear about this line here:

if ((op<0 || OrderType()==op) && (OrderType()>1 && OrderType()<6))

Here we check for the if statement condition, i.e., if the external variable <0, any order type is available OR if we write, for example, OP_BUY in the function call, the selection will be made from among OP_BUY orders. This condition is clear, but further we have an equivalent condition written through&& which states thatOrderType() must be within the range of pending orders, i.e. from 1 to 5. Our OP_BUY type is not included into this range and therefore the operator's condition must be false and control must pass to the operator following the if-else operator, i.e. it is difficult for me to say where, somewhere behind the curly body arrow. It seems to me that we should specify here the condition for market orders and a separate function of pending orders of the same kind. That is, if I want to make a selection from market orders and do not know its exact type, I take the external variable <0 but address the function where conditions from 0 to 1 are written. The same is true for pending orders. So, I have come to the conclusion that we should use a different function for each category of order types when using this useful condition.

Also, I have a problem with library connection. I've searched and read, but the connection gives an error when compiling the EA I'm writing. I don't have time to deal with it yet, I want to write profitable Expert Advisor faster, so I have copied all three needed libraries and attached them after special function start(), and placed their global variables in global variables of EA and commented out references to them. It is not very convenient, but it works.
 
00007:

I'm a little unclear about this line here:

if ((op<0 || OrderType()==op) && (OrderType()>1 && OrderType()<6))

Thank you! Corrected... This is a consequence of copying, i.e. using a preform of another function intended to work only with limit and stop orders.

00007:

I also have a problem with connecting libraries. I have searched and read, but the connection gives me an error when compiling the Expert Advisor I am writing. I don't have time to figure it out, and I want to write a profitable Expert Advisor faster, so I have copied all three needed libraries and attached them after special function start(), and placed their global variables in global EA variables and commented out references to them. It is not very convenient, but it works.

By the way, I don't use my own libraries in the form of MQH files. I usually copy the functions I need in my EA, i.e. my standard ready-made EA is a single MQ4 file. Everything I need is inside this file. Yes, there is some repetitiveness in the code. But there are no problems with versions of functions.

 

By the way, I also noticed that you have testing scripts for each function and they have all the additional functions you need, which you can simply add to your EA along with the function and it will be all in one file with nothing extra. Functions are simple and useful, but with your permission I will make four of them for myself: two for positions and two for stops. They will be more universal for delicate requirements of my EA. But now I see how easy it is to do. Thank you very much Igor.

Regards Andrei.

 

The TimeOpenFirstPos() function.

This function returns the time of the first open position. Selection of positions to be taken into account is specified by external parameters:

  • sy- Name of market instrument. If you set this parameter, the function will only consider positions of this instrument. The default value"" means any market instrument.NULL value means the current instrument.
  • op- Trade operation, position type. Valid values:OP_BUY,OP_SELL or-1. The default value-1 means any position.
  • mn- Position identifier, MagicNumber. The default value of-1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 25.01.2012                                                     |
//|  Описание : Возвращает время открытия первой открытой позиций.             |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
datetime TimeOpenFirstPos(string sy="", int op=-1, int mn=-1) {
  datetime t=TimeCurrent();
  int      i, k=OrdersTotal();

  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) {
              if (t>OrderOpenTime()) t=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(t);
}
The use of the function is similar to TimeOpenLastPos()
 

Hello

How to add a foeba to your SetRegression()(https://www.mql5.com/ru/forum/107476/page35 ) channel.

 
gince:

Hello

How to add a foeba to your SetRegression()(https://www.mql5.com/ru/forum/107476/page35 ) channel.

Duck, it's easy:

  1. You set the first point of the fibo line (calculate where you want it to come from).
  2. Decide on which bar to get the second point.
  3. Then useEquationDirect() to get the price.
  4. Having coordinates of two points, draw a line through them usingSetTLine() function.

Reason: