Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 519

 
PolarSeaman:

How do I know that in the history of this particular position some of it is closed?

I opened a position with 1.0 lot, closed it with 0.5, and with 10$ loss. Later I closed another 0.25 with 5$ profit. I will close it all at 50$ profit. In fact, right now I have $50 profit.

How do I close -$10 and +$5 in the calculation of the current profit? To close at $55 profit

See the comment of the position (in mql4) - the closed part has a ticket of the remaining part (to#1234556789), the remaining part has a ticket of the closed (from#123456788) - I do not remember where there are gaps - you will see for yourself

 
Artyom Trishkin:

Look at the comment of the position (in mql4) - the closed part has a ticket of the remainder (to#1234556789), the remainder has a ticket of the closed one (from#123456788) - I do not remember where the spaces are - you will see for yourself

I compare the comment of the closed position with the ticket of the open one, it returns "0.0"

double prof_cl_pos(string sy="0", int op=-1, int mn=-1) {
  datetime ta;
  int      i, k=OrdersHistoryTotal();
  double profit_=0;
  string comment="";

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
             // comment=OrderComment();
              if (ticket_op_pos(Symbol(), -1,mn)==OrderComment())
              profit_=OrderProfit();
            }
          }
        }
      }
    }
  }
  return(profit_);
}
  int ticket_op_pos(string sy="", int op=-1, int mn=-1) {
  datetime o;
  double   l=-1;
  string comment="";
  int     ticket, 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) {
                ticket=OrderTicket();
              }
            }
          }
        }
      }
    }

  return(ticket);
}
 
PolarSeaman:

Comparing a comment of a closed position with a ticket of an open position, returns "0.0".

From the comment the ticket should be highlighted, i.e. extra characters like "from #" and "to #" should be removed. It's not done here.

 
Ihor Herasko:

From the comment you need to highlight the ticket, i.e. remove extra characters like "from #" and "to #". This is not done here.

Exactly! How do you cut out "to #"?
 
PolarSeaman:
Exactly! How do you cut "to #"?
find the occurrence of character/substring # and (optionally) make sure it's "from" or "to" before it

take the leftover part and convert it to long

in-5e, there's a rudimentary regexp - it'll be easier

 
Maxim Kuznetsov:
find occurrence of character/substring #
Find with what?
 
PolarSeaman:
How do I find it?

Is the site search not working? Don't you have documentation at the top of the page?

Документация по MQL5: Строковые функции / StringSubstr
Документация по MQL5: Строковые функции / StringSubstr
  • www.mql5.com
[in]  Длина извлекаемой подстроки. Если значение параметра равно -1 либо параметр не задан, то будет извлекаться подстрока, начиная с указанной позиции и до конца строки.
 
Artyom Trishkin:

Is the site search not working? Don't you have documentation at the top of the page?

Thanks, I did this.

              comment=OrderComment();
               substr = StringSubstr(comment, 4, 9);//пропускаю "to #" беру цифры
              if (ticket_op_pos(Symbol(), -1,mn)==substr)

but the search history function started to return the current profit on the open position

double prof_cl_pos(string sy="0", int op=-1, int mn=-1) {
  datetime ta;
  int      i, k=OrdersHistoryTotal();
  double profit_=0;
  string comment="";
  string substr="";

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              comment=OrderComment();
               substr = StringSubstr(comment, 4, 9);
              if (ticket_op_pos(Symbol(), -1,mn)==substr)
              profit_=OrderProfit();
            }
          }
        }
      }
    }
  }
  return(profit_);
}
 

> Thanks, that's how I did it.

Don't do that again :-) you can't rely on the length or start of a fragment when dealing with undefined strings

There's a nice StringFind() function - search for "#" or "from #".

ps/ you should also use a single case, either upper or lowercase. Unfortunately there is no protocol for tagging comments, so it's possible that some server will say "FROM #" (in capital letters);
in particular some put [sl] and some (sl) at the end of the comment at stop-loss. And the size of the comment is limited by the way. And so on and so forth.
 

What is the harm of such a construction?

datetime tm1 = D'09.04.2018';
long     tm2 = TimeCurrent();

 if(tm1 > tm2) Print("Работает");

Or is it better to makedatetime a long type?