Useful features from KimIV - page 16

 
KimIV:
khorosh:
there is an error in bool idPriceLevel() function included in e-TFL_v2 Expert Advisor: gdUpPrice==0 is repeated twice in the last condition, you probably meant to write gdDnPrice==0 in the second case.

Thank you! I have uploaded the corrected version of e-TFL_v2 to the website.


khorosh:
For some reason this EA works in my Strategy Tester in visual mode but it does not open positions on a demo account, although it reports buy and sell levels in the comments. Maybe you can tell me what is the matter.

I do not know... I have just finished testing this Expert Advisor on a NorthFinance demo. Yesterday I called my friend from Moscow. He complained about the same Expert Advisor. He says that it opens position by the line and then more and more until you stop it and opens many positions. Therefore, there were two objectives for testing:

1. Concerning your question. Check of general functionality.

2. On my friend's question. I opened only one position from one line.

To check it, I put e-TFL_v2 Expert Advisor on 5-minute timeframes EURUSD, GBPUSD, USDCHF and USDJPY. Using trend lines I drew channels on the last 20-30 bars. As a result, the Expert Advisor worked as it should. EUR has been bought from the bottom line, while JPY has been sold from the top one. For pound and chyf everything was correct too. So you should check it on your own. The Expert Advisor works.

KimIV:
khorosh:
The idPriceLevel() function in e-TFL_v2 has an error: in the last condition gdUpPrice==0 is repeated twice. Probably, in the second case you meant to write gdDnPrice==0.

Thank you! I have uploaded the corrected version of e-TFL_v2 to the website.


khorosh:
For some reason this EA works in my Strategy Tester in visual mode but it does not open positions on a demo account, although it reports buy and sell levels in the comments. Maybe you can tell me what is the matter.

I do not know... I have just finished testing this Expert Advisor on a NorthFinance demo. Yesterday my friend from Moscow called me. He complained about the same Expert Advisor. He says that it opens position by the line and then more and more until you stop it and opens many positions. Therefore, there were two objectives for testing:

1. Concerning your question. Check of general functionality.

2. On my friend's question. I opened only one position from one line.

To check it, I put e-TFL_v2 Expert Advisor on 5-minute timeframes EURUSD, GBPUSD, USDCHF and USDJPY. Using trend lines I drew channels on the last 20-30 bars. As a result, the Expert Advisor worked as it should. EUR has been bought from the bottom line, while JPY has been sold from the top one. For pound and chyf everything was correct too. So you should check it on your own. The Expert Advisor works.

Maybe, I don't quite understand the EA logic, but it seems to me that if one line is used, the condition where the error occurred will be fulfilled and idPriceLevel() will set False. Explain, if I'm wrong.

 
khorosh:

if a single line is used, the condition where the error occurred will be met and idPriceLevel() will be set to False, and this will not open the position.

Yes, that is correct. The EA requires two lines for its normal operation: one line is above the price and the second line is below the current price. I have corrected the description on the website. Thank you!

 
KimIV:
khorosh:

if a single line is used, the condition where the error occurred will be met and idPriceLevel() will be set to False, and this will not open the position.

Yes, that is correct. The EA requires two lines for its normal operation: one line is above the price and the second line is below the current price. I have corrected the description on the website. Thank you!

That is why my position did not open, as I worked with one line. Everything is clear now. Thank you for your unselfish efforts. Your functions are very clear and clearly described. Perhaps most of our EAs, which are being developed now, will now use your functions.

 

Function ExistOPNearMarket().

This function returns a flag that an order or position exists near the market (at a specified distance in pips from the market). A more accurate selection of orders or positions to be checked is specified by external parameters:

  • sy - Name of instrument. If this parameter is set, the function will only check orders or positions of the specified instrument. The "" or NULL means the current symbol.
  • op - Trade operation, order or position type. Valid values: OP_BUY, OP_SELL, OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP, OP_SELLSTOP or -1. The default value of -1 means any trade operation.
  • mn - Identifier of the order or position (MagicNumber). Default value -1 means any identifier.
  • ds - Distance from market in pips. The default value is 1000000.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает флаг существования позиции или ордера около рынка   |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента        ("" или NULL - текущий символ)     |
//|    op - торговая операция               (    -1      - любая операция)     |
//|    mn - MagicNumber                     (    -1      - любой магик)        |
//|    ds - расстояние в пунктах от рынка   (  1000000   - по умолчанию)       |
//+----------------------------------------------------------------------------+
bool ExistOPNearMarket(string sy="", int op=-1, int mn=-1, int ds=1000000) {
  int i, k=OrdersTotal(), ot;

  if (sy=="" || sy=="0") sy=Symbol();
  double p=MarketInfo(sy, MODE_POINT);
  if (p==0) if (StringFind(sy, "JPY")<0) p=0.0001; else p=0.01;
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      ot=OrderType();
      if ((OrderSymbol()==sy) && (op<0 || ot==op)) {
        if (mn<0 || OrderMagicNumber()==mn) {
          if (ot==OP_BUY || ot==OP_BUYLIMIT || ot==OP_BUYSTOP) {
            if (MathAbs(MarketInfo(sy, MODE_ASK)-OrderOpenPrice())<ds*p) return(True);
          }
          if (ot==OP_SELL || ot==OP_SELLLIMIT || ot==OP_SELLSTOP) {
            if (MathAbs(OrderOpenPrice()-MarketInfo(sy, MODE_BID))<ds*p) return(True);
          }
        }
      }
    }
  }
  return(False);
}
SZZY. attached script for experiments with ExistOPNearMarket() function.
 

Function ExistPosByPrice().

This function returns a flag for the existence of a position at a given open price. A more accurate selection of positions to be checked is defined by external parameters:

  • sy - Name of the market instrument. If you specify this parameter, the function will only check positions of a 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.
  • pp - Position open price. Default value 0 means any price.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает флаг существования позиций по цене открытия         |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - торговая операция          (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//|    pp - цена                       ( 0   - любая цена)                     |
//+----------------------------------------------------------------------------+
bool ExistPosByPrice(string sy="", int op=-1, int mn=-1, double pp=0) {
  double px, py;
  int    d, 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=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) {
            d=MarketInfo(OrderSymbol(), MODE_DIGITS);
            px=NormalizeDouble(pp, d);
            py=NormalizeDouble(OrderOpenPrice(), d);
            if (pp<=0 || px==py) return(True);
          }
        }
      }
    }
  }
  return(False);
}
ZS. In the trailer there is a script to test the ExistPosByPrice() function.
Files:
 

The GetAmountLotFromOpenPos() function.

This function returns the sum of lots of open positions. A more accurate 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 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 GetAmountLotFromOpenPos(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) {
              l+=OrderLots();
            }
          }
        }
      }
    }
  }
  return(l);
}
ZS. Attached is a script to test GetAmountLotFromOpenPos() function.
 

GetIndexByTicket() function.

This function returns the index of the order or position by ticket. The more accurate selection of orders or positions to be searched is specified by external parameters:

  • ti - Ticket order, position. The value of this parameter cannot be empty or zero.
  • sy - Name of market instrument. If you set this parameter, the function will only consider positions of the specified instrument. Default value - "" means any market instrument. NULL means the current instrument.
  • op - Trade operation, order or position type. Valid values: OP_BUY, OP_SELL, OP_BUYLIMIT, OP_SELLLIMIT, OP_BUYSTOP, OP_SELLSTOP or -1. The default value of -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                                                     |
//|  Описание : Возвращает индекс ордера или позиции по тикету.                |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    ti - тикет ордера, позиции                                              |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetIndexByTicket(int ti, string sy="", int op=-1, int mn=-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=="") && (op<0 || OrderType()==op)) {
        if ((mn<0 || OrderMagicNumber()==mn) && OrderTicket()==ti) return(i);
      }
    }
  }
  return(-1);
}
PS. Attached is a script to test GetIndexByTicket() function.
 

Hello

I would like to know if you have a function for determining the correlation of two series passed to it as parameters?

 
scorpionk:

Hello

I would like to know if you have a function to determine the correlation of the two series passed to it as parameters?

no... But I have an interest in this topic. If you provide the formulas to calculate the correlation, I will write the function.


ZS. I won't look for the formulas myself. I won't go to links with materials either. That is, I have no desire to understand the subject. There is only a desire to write a function using ready-made formulas.

 
KimIV:
scorpionk:

Hello

I would like to know if you have a function to determine the correlation of the two series passed to it as parameters?

no... But I have an interest in this topic. If you provide the formulas to calculate the correlation, I will write the function.


ZS. I won't look for the formulas myself. I won't go to links with materials either. That is, I have no desire to understand the subject. There is only desire to write function on the ready formulas.


Correlation formula