Useful features from KimIV - page 17

 

The function itself:


//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 10.05.2008                                                     |
//|  Описание : Возвращает корреляцию двух рядов.                              |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    x - массив значений первого ряда                                        |
//|    y - массив значений второго ряда                                        |
//+----------------------------------------------------------------------------+
double Correlation(double& x[], double& y[]) {
  double co=0, sa=0, sb=0, sc=0, xs=0, ys=0;
  int    i, k=MathMin(ArraySize(x), ArraySize(y));

  if (k>0) {
    for (i=0; i<k; i++) {
      xs+=x[i]; ys+=y[i];
    }
    xs/=k; ys/=k;
    for (i=0; i<k; i++) {
      sa+=(x[i]-xs)*(y[i]-ys);
      sb+=(x[i]-xs)*(x[i]-xs);
      sc+=(y[i]-ys)*(y[i]-ys);
    }
    sb=MathSqrt(sb*sc);
    if (sb!=0) co=sa/sb;
  }
  return(co);
}
ZS. In the trailer is a script for testing the Correlation() function.
Files:
 

GetIndexLastPos() function.

This function returns the index of the last open position or -1. A more accurate selection of positions to be checked is defined by external parameters:

  • sy - Name of market instrument. If we set this parameter, the function will consider only positions of the specified instrument. The default value "" means any market instrument. NULL 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                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает индекс последней открытой позиции или -1            |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetIndexLastPos(string sy="", int op=-1, int mn=-1) {
  datetime o;
  int      i, k=OrdersTotal(), r=-1;

  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 (o<OrderOpenTime()) {
                o=OrderOpenTime();
                r=i;
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
ZS. Attached is a script to test GetIndexLastPos() function.
Files:
 

The GetLotLastPos() function.

This function returns the lot size of the last open position or -1. A more accurate selection of positions to be checked is defined by external parameters:

  • sy - Name of market instrument. If you set this parameter, the function will consider only positions of the specified instrument. The default value "" means any market instrument. 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                                                     |
//|  Описание : Возвращает размер лота последней открытой позиции или -1       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetLotLastPos(string sy="", int op=-1, int mn=-1) {
  datetime o;
  double   l=-1;
  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 (o<OrderOpenTime()) {
                o=OrderOpenTime();
                l=OrderLots();
              }
            }
          }
        }
      }
    }
  }
  return(l);
}
ZS. Attached is a script to test GetLotLastPos() function.
Files:
 
KimIV:

GetIndexLastPos() function.

This function returns the index of the last position opened or -1. A more accurate selection of positions to be checked is specified by external parameters:

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

What is the index ? Is it a bar number of the last position opened? Or something else?

 
rid писал (а):
What is the index? Is it the bar number of the last open position? Or something else?

This is the sequence number of the position in the list of open positions and set orders. The numbering starts from zero and is the same for positions and orders.

 

Dear Igor, please help me. The problem is that I cannot find anywhere the code how to attach MA to CCI or Stoch. The buy or sell signal should be generated when this МА crosses the CCI level, e.g. +100/-100.

Maybe there is an Expert Advisor that uses the code I need?

 

GetMaxLotFromOpenPos() function.

This function returns the maximum lot size from the number of open positions. A more accurate selection of positions to be checked is specified by external parameters:

  • sy - Name of market instrument. If this parameter is set, the function will consider only positions of the specified symbol. The default value "" means any market instrument. NULL 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                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает максимальный размер лота из открытых позиций        |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetMaxLotFromOpenPos(string sy="", int op=-1, int mn=-1) {
  double l=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=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (l<OrderLots()) l=OrderLots();
            }
          }
        }
      }
    }
  }
  return(l);
}
ZS. In the trailer there is a script to test GetMaxLotFromOpenPos() function.
 

GetMinLotFromOpenPos() function.

This function returns the minimum lot size from the number of open positions. A more accurate selection of positions to be checked is specified by external parameters:

  • sy - Name of market instrument. If this parameter is set, the function will consider only positions of the specified symbol. The default value "" means any market instrument. NULL 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                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает минимальный размер лота из открытых позиций         |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetMinLotFromOpenPos(string sy="", int op=-1, int mn=-1) {
  double l=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=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (l==0 || l>OrderLots()) l=OrderLots();
            }
          }
        }
      }
    }
  }
  return(l);
}
HH. Attached is a script to test GetMinLotFromOpenPos() function.
 
GYL:

Dear Igor, please help me. The problem is that I cannot find anywhere the code how to attach MA to CCI or Stoch. The buy or sell signal should be generated when this МА crosses the CCI level, e.g. +100/-100.

Perhaps there is an Expert Advisor that uses the code I need?

This question is obviously not relevant. I will give you an example of such a code fragment using stochastic and MA examples in order not to distract Igor.

double Stochastic_array[50];int j=0; while (j<50) {
Stochastic_array[j]= iStochastic(NULL, 0, Stochastic_period, 3,3,
                                     MODE_SMA,0,MODE_MAIN, j); j++; }
ArraySetAsSeries(Stochastic_array,true);
double MA_0 =iMAOnArray(Stochastic_array,0,MA_period,1,MODE_SMA,0) ;
double Stochastic_0=iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN, 0);
double Stochastic_1=iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN, 1);

MA. I will keep the post for 24 hours - then I will delete it!

 
rid:
The question is obviously off-topic. In order not to distract Igor by trifles I will give you an example of such a piece of code, using stochastic and

MA. I will keep this post for 24 hours - then I will delete it!

Thank you very much, you have helped me a lot.
Reason: