Useful features from KimIV - page 27

 
SamMan писал (а) >>

Exactly. And the smaller the code, the easier it is to understand. I can't stand fonts "smeared" on 2-3 screens. A function should be all on the screen, without flipping. That's why I'm in favour of compactness.


I can't stand them either :) . My function fits in the screen.


And is it OK that you have a lot of optional continuums which, incidentally, again draw the reader's attention away from you?


Well, reducing function complexity is one of basic conditions for writing GOOD code.

If you like it this way, you have 2 options :

1. You are accustomed to a bad code style and you use it yourself, nothing good can come of it, when you will have to write a really fundamental code you will understand my rightness. At least when you switch to MQL5.

2 And I think this is just about you - you use this code, because you are unable to write your own code properly, as a programmer you are not anything and then you have no right to judge and discuss my post to the author of this thread. All the more I was addressing to the author.

I apologise for my post to the author, I wrote in vain, as I won't be using this code anyway, as I have my own libc with everything I need. And because for such a long time there was no clearly negative feedback, it means that the author's code satisfies. Just couldn't stand it :), sorry.

 
TheXpert писал (а) >>

I apologise for my post to the author, I wrote in vain, as I won't be using this code anyway, as I have my own libc with everything I need. And because for such a long time there was no unequivocally negative feedback, it means that the author's code satisfies. Just could not stand it :), sorry.

I apologize for writing, but I couldn't stand it. You will be in vain if you leave the branch because of one's opinion. Especially the author said thank you.

I think it would be nice if one and the same task can be solved by 2 (or more) codes. You can share your codes solving the same task if you think they are better.

Often, seeing how someone else has coded the same thing, but in a different style. You can learn a lot.

P.S. Igor, thank you for your work.

 
Indeed, Andrei, you shouldn't take it like that. Keep it simple! And don't be afraid to push me down. I won't mind if you contribute something to my work or post your own.
 

The ArraySearchDouble() function.

This function searches for an array element using a value of double type and returns the index of the found element or -1. The function finds only one element and stops the search if the result is positive.

  • m - The array in which the element is searched.
  • e - Value of double type, which is to be found in array m.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.09.2005                                                     |
//|  Описание : Выполняет поиск элемента массива по значению                   |
//|             и возвращает индекс найденного элемента или -1.                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    m - массив элементов                                                    |
//|    e - значение элемента                                                   |
//+----------------------------------------------------------------------------+
int ArraySearchDouble(double& m[], double e) {
  for (int i=0; i<ArraySize(m); i++) {
    if (m[i]==e) return(i);
  }
  return(-1);
}
ZS. Attached is a script to test ArraySearchDouble() function.
 

The ArraySearchInt() function.

This function searches for an array element by an int type value and returns the index of the found element or -1. The function finds only one element and stops the search if the result is positive.

  • m - The array of elements of int-type, in which the search is performed.
  • e - Value of int type, which is to be found in array m.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.09.2005                                                     |
//|  Описание : Выполняет поиск элемента массива по значению                   |
//|             и возвращает индекс найденного элемента или -1.                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    m - массив элементов                                                    |
//|    e - значение элемента                                                   |
//+----------------------------------------------------------------------------+
int ArraySearchInt(int& m[], int e) {
  for (int i=0; i<ArraySize(m); i++) {
    if (m[i]==e) return(i);
  }
  return(-1);
}
ZS. Attached is a script to test ArraySearchInt() function.
Files:
 

The ArraySearchString() function.

This function searches an array element for a value of string type and returns the index of the found element or -1. The function finds only one element and stops the search if the result is positive.

  • m - The array of elements of type string, in which the search is performed.
  • e - The value of string type, which is to be found in array m.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 04.03.2008                                                     |
//|  Описание : Выполняет поиск элемента массива по значению                   |
//|             и возвращает индекс найденного элемента или -1.                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    m - массив элементов                                                    |
//|    e - значение элемента                                                   |
//+----------------------------------------------------------------------------+
int ArraySearchString(string& m[], string e) {
  for (int i=0; i<ArraySize(m); i++) {
    if (m[i]==e) return(i);
  }
  return(-1);
}
ZS. Attached is a script to test ArraySearchString() function.
 
KimIV писал (а) >>
Indeed, Andrey, you should not have taken it that way. >> Be simple! And don't be afraid to push me down. I won't mind if you'll make a contribution and correct something of mine or post your own.

OK, I have a slightly different view on things, as I've already written, I consider global universality to be an evil, i.e. the code should be at least a little bit customised for what you are writing, in order to achieve maximum simplicity and convenience. That's why I don't see any sense in publishing my own code here.


I can act as a reviewer of your code, if you don't mind of course, and offer your own version of it or just give comments.



SZZH: Thank you for your support, to be honest I didn't expect that kind of feedback from you.

 
KimIV писал (а) >>

The ArraySearchDouble() function.

This function searches for an array element using a value of double type and returns the index of the found element or -1. The function finds only one element and stops the search if the result is positive.

  • m - The array in which the element is searched.
  • e - The value of type double, which is to be found in array m.
ZS. Attached is a script to test ArraySearchDouble() function.

Double is a tricky thing, and your code may work incorrectly in some situations.

Here's a possible workaround

int ArraySearchDouble(double& array[], double value, double precision = 0.00000000001) 
{ // на то, что я перемещаю скобки не обращайте внимания, мне так читабельней
  for (int i = 0; i < ArraySize(array); i++) 
  {
    if (array[i] > value - precision && array[i] < value + precision) return(i);
  }
  return(-1);
}

And if you want to do the same thing but from the end.

int ArrayRevertSearchDouble(double& array[], double value, double precision = 0.00000000001) 
{ 
  for (int i = ArraySize(array) - 1; i >= 0; i--) 
  {
    if (array[i] > value - precision && array[i] < value + precision) return(i);
  }
  return(-1);
}
 
TheXpert писал (а)
I can act as a reviewer of your code, if you don't mind, of course, and offer my own version of it or just give comments.
Great! It's good for everybody to have a versatile view of things. Me, first of all :-)
 

I was writing an EA the other day, in which I needed to change the lot size depending on the result of a previous trade and the lot size of the same trade. And it turned out that I didn't have a function that would return the lot size of the last closed position. Here's a correction...

The GetLotLastClosePos() function.

This function returns the lot size of the last closed position, or -1. The selection of positions to be taken into account is defined 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 type. Valid values: OP_BUY, OP_SELL or -1. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 16.06.2008                                                     |
//|  Описание : Возвращает размер лота последней закрытой позиции или -1       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetLotLastClosePos(string sy="", int op=-1, int mn=-1) {
  datetime o;
  double   l=-1;
  int      i, k=OrdersHistoryTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (o<OrderCloseTime()) {
                o=OrderCloseTime();
                l=OrderLots();
              }
            }
          }
        }
      }
    }
  }
  return(l);
}
HH. Attached is a script to test the GetLastClosePos() function.
Reason: