Useful features from KimIV - page 25

 

The TimeOpenLastPos() function.

This function returns the time of the last open position. Selection of positions to be taken into account is defined 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. A value of NULL means the current instrument.
  • op - Trade operation, position type. Valid values: OP_BUY, OP_SELL or -1. Default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает время открытия последней открытой позиций.          |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
datetime TimeOpenLastPos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  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);
}
ZS. Attached is a script to test TimeOpenLastPos() function.
Files:
 
Prival:
Is there a bubble sorting function in the backroom

Not available, but it took me 10 minutes to make

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 10.06.2008                                                     |
//|  Описание : Выполняет пузырьковую сортировку элементов массива.            |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    a - массив элементов                                                    |
//+----------------------------------------------------------------------------+
void BubbleSort(double& a[]) {
  double t;
  int    i, j, n=ArraySize(a);

  for (i=n-1; i>0; i--) {
    for (j=0; j<i; j++) {
      if (a[j]>a[j+1]) {
        t=a[j];
        a[j]=a[j+1];
        a[j+1]=t;
      }
    }
  }
}
ZS. Attached is a script to test the BubbleSort() function.
Files:
 

The function BarsBetweenLastFractals().

This function returns the number of bars between the last two fractals or -1. The market instrument and timeframe from which the fractals are to be taken are specified by the parameters:

  • sy - Name of the market instrument. Default value is "" or NULL for the current market instrument.
  • tf - Time frame (number of minutes per bar). Default value 0 means current timeframe.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 10.06.2008                                                     |
//|  Описание : Возвращает количество баров между двумя последними фракталами. |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента        ("" или NULL - текущий символ)     |
//|    tf - таймфрейм                       (    0       - текущий ТФ)         |
//+----------------------------------------------------------------------------+
int BarsBetweenLastFractals(string sy="", int tf=0) {
  double fu=0, fd=0;
  int    i, nu=0, nd=0;

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=2; i<100; i++) {
    fu=iFractals(sy, tf, MODE_UPPER, i);
    if (fu!=0) {
      if (nu==0) nu=i;
    }
    fd=iFractals(sy, tf, MODE_LOWER, i);
    if (fd!=0) {
      if (nd==0) nd=i;
    }
    if (nu>0 && nd>0) return(MathAbs(nu-nd));
  }
  Print("BarsBetweenLastFractals(): Фракталы не найдены");
  return(-1);
}
ZS. Attached is a script to test BarsBetweenLastFractals() function.
 

SecondsAfterCloseLastPos() function.

This function returns the number of seconds after the last position was closed. The selection of positions to be taken into account is specified by the parameters:

  • sy - Name of market instrument. If this parameter is set, the function will only take into account 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. The default value of -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает количество секунд после закрытия последней позиций. |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
datetime SecondsAfterCloseLastPos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  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 (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(TimeCurrent()-t);
}
P.S. Attached is a script to test the SecondsAfterCloseLastPos() function.
 

Hello

Some advice.

I have an EA which puts orders to both sides at a certain time

Please explain on your fingers how to make the second order be automatically deleted when the first one triggers.

Or tweak it

Thank you

 
aceventura:

Hello

Some advice.

I have an EA which puts orders to both sides at a certain time

Please explain on your fingers how to make the second order be automatically deleted when the first one triggers.

Or tweak it

Thank you

KimIV

Igor. Do you have enough fingers?

 
aceventura:

Hello

Some advice.

I have an EA which puts orders to both sides at a certain time

Please explain on your fingers how to make the second order be automatically deleted when the first one triggers.

Or tweak it

Thank you

First take the function number of orders in this branch and if it returns "== 1", "< 2" or "== odd number", then call the function from this branch "close orders".

 
Vinin:

KimIV

Igor. Do you have enough fingers?

Probably not... I only have two: index finger and grip... hee

 
SergNF:

First take the number of orders function in this branch, and then if it returns "== 1", "< 2" or "== odd number", then call the "close orders" function from this branch.

The function of the orders quantity will not work, because the EA is set differently for every pair. And it sets orders on different pairs at the same time. Then if four orders are placed for two pairs and one triggered, three of them are deleted and the opposite order for each is deleted.