Useful features from KimIV - page 89

 
Is it possible to add in your Expert Advisor e-CloseByPercentProfit.mq4 to set TP and SL on the pair in the chart which it is installed?
 

Good afternoon, all.

Question on the function


//+------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                        |
//|  Описание : Возвращает количество позиций.      |
//+------------------------------------------------------------------+

int NumberOfPositions(string sy="", int op=-1, int mn=-1){
  int i, k=OrdersTotal(), kp=0;  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) kp++;
          }}}}}  return( kp);}  
          

I have a multi-currency counterpart. The pairs are set in external parameters. With its own magician (for each pair).

Among the entry conditions for each instrument there is this:
if ( NumberOfPositions(Symbol_1, -1,Magic_1)<1){//если нет открытых
//позиций по 1-му инструменту, 
OR (for the second pair)
if ( NumberOfPositions(Symbol_2, -1,Magic_2)<1){//если нет открытых
//позиций по 2-му инструменту, проверяем ОСТАЛЬНЫЕ условия

What happened is that I charged both symbols yesterday - EURUSD, and this morning I found that only one trade opened, although there seemed to be signals in both cases.

And then it occurred to me. That the function

NumberOfPositions(string sy="", int op=-1, int mn=-1)
First it checks if there is an open position for a symbol. And then everything else.

In other words, if I have an open position with the EA for the instrument EIRUSD, the second position will not open even with another magician according to a different algorithm.

Please, tell me if it is true or not?

Or am I wrong in my reasoning? Or the function evaluates all criteria oneby one ...


 

I often use this function, it returns everything according to the input parameters. The items that the function goes through are sorted sequentially according to the parameters. See for yourself.

int NumberOfPositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal(), kp=0;

  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) kp++; //внутри магика, и только здесь позиция учтена 
          }
        }
      }
    }
  }
  return( kp);
}
 

So it means that my EA will not be able to open a second EURUSD position, even with another magician, as long as the first position is open ?

Apparently so. Because even in the tester it doesn't seem to see any overlapping positions.

//--------------------------------------------------------

 
Rita >> :

So, it means that my EA will not be able to open the second EURUSD position even if it has another magician, while the first position is open?

Exactly the opposite :)) It means that the function counts your symbols and magic numbers separately. That is, if there is an open position on EURUSD with Magic_1, then NumberOfPositions("EURUSD", -1, Magic_2)==0; unless there is another position on EURUSD with Magic_2.

In general, the function counts open positions according to (sy && op && mn), i.e. positions will be counted exactly with these parameters. If there is no match of at least one parameter, the order will not be taken into account by this function, and it will not be displayed.

Print the function values in Comment and you will be able to see in real time the number of orders and whether the function is working correctly. Most likely the reason for the algorithm malfunctioning is in the EA code.

 
Okay. I'll check the code now.
 
hope писал(а) >>

Adding a binding to the symbol in the function call does not help. Why does it count the profit for each position separately (picture above)?

Nah, I'd rather offer you my drawing...

...and a script...

 
KimIV писал(а) >>

Nah, I'd rather offer you my drawing...

...and a script...

I've realised I'm a fool... You can't get udders by adding beef and milk.

 
KimIV писал(а) >>
Angela, the compiler didn't expect to find my function definition in this very place. This means that somewhere above the code you have something missing. It may be a semicolon or a closing curly brace. Anyway, check the code carefully.

I figured it out, I just inserted your function inside int start(), when I put it outside int start(), everything compiled. The problem is different, in the indicator in visualisation mode in the tester this function doesn't get data from the EA to the indicator.

 
e-News-Lucky$.mq4

Lucky$ & KimIV

http://www.kimiv.ru

Hello Igor!

I use your Expert Advisor, it is very good, I have tried to attach indicator to it through iCustom yesterday but iCustom did not see it. I wonder what build-in functions like iHigh, iLow see it without any problem, but it refuses to place pending orders via indicator.

I put it like this for iHigh and iLow of the day bar. This function works as expected, places pending orders, but does not want to place pending orders with indicator(((.... i would like to show the code fragment on how to place pending orders by the indicator in this EA? The indicator may be anything but a call to iCustom function .

If you have some time, thank you.

//глобальные переменные

double Hi_Bar;
double Lou_Bar;

//+------------------------------------------------------------------+
//| Установка ордеров                                                |
//+------------------------------------------------------------------+
void SetOrders() {
  Hi_Bar  = iHigh(Symbol(), PERIOD_D1, 0);
  Lou_Bar =  iLow(Symbol(), PERIOD_D1, 0);
  double ldStop=0, ldTake=0;
  int    spr=MarketInfo(Symbol(), MODE_SPREAD);
  double pAsk= Hi_Bar+( DistanceSet+ spr)*Point;
  double pBid= Lou_Bar- DistanceSet*Point;


Reason: