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

 
MakarFX #:

If you don't like "-1", you can do this.

No, you can't - the program will crash if you access your array that way.

At best, if you access your internal arrays, you will miss the data you need, or simply not reach it in the loop.

 
Nikolay Ivanov #:

Basically it's simple, your unique sums up all the mismatched orders between different magicians...

For example there are 3 orders

The first iteration of magic=1 unique=0, at the end of the iteration unique will be=2.

The second iteration magik=2unique=2, at the end of the iterationunique will be=3

Since 3>=number of all orders, the while loop will break... And magic =3 was never checked... So magic =2 again and so on with all...

The first order is given magic =1 by default. Then the loop goes into action:

Start of iteration magic = 1 unique = 0, at the end (when there is already one order with magic = 1) magic = 1 is still 1 and unique = 0
Second iteration magic = 2 unique = 0, at the end magic = 2 and unique = 1 (no match) and the retorn is given to the 2nd order magic = 2.
The third should be the same...

I wanted to print the whole process, but something went wrong:

int GetMagic()
{
  int magic = 0;
  int unique = 0;
  if(OrdersTotal()){
    do{
        magic++;
        for(int i = OrdersTotal() - 1; i >= 0 ; i --){
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            if(magic != OrderMagicNumber())
              unique ++;
          }
        }
        Print (" MAGIC ", magic, " | UNIQUE ", unique)
      }while(unique < OrdersTotal());
  } else return (magic + 1);

  return (magic);
}

-----------------------------------

'endless cycle.mq4'     endless cycle.mq4       1       1
'}' - semicolon expected        endless cycle.mq4       49      7
'else' - 'while' expected       endless cycle.mq4       50      5
'else' - illegal 'else' without matching 'if'   endless cycle.mq4       50      5
'OnTick' - function declarations are allowed on global, namespace or class scope only   endless cycle.mq4       60      6
'OnTick' - function already defined and has body        endless cycle.mq4       60      6
   see previous declaration of function 'OnTick'        endless cycle.mq4       60      6
'}' - unexpected end of program endless cycle.mq4       63      1
6 errors, 0 warnings            7       1
 
Nerd Trader #:

The first order is given magic = 1 by default. Then a loop is started:

Start of iteration magic = 1 unique = 0, at the end (when there is already one order with magic = 1 ) magic is still 1 and unique = 0
Second iteration magic = 2 unique = 0, at the end, magic = 2 and unique = 1 (no match) and the retorn is assigned to the 2nd order magic = 2.
The third should be the same...

I wanted to print the whole process, but something went wrong:

int GetMagic()
{
  int magic = 0;
  int unique = 0;
  if(OrdersTotal()){
    do{
        magic++;
        for(int i = OrdersTotal() - 1; i >= 0 ; i --){
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            if(magic != OrderMagicNumber())
              unique ++;
          }
        }
        Print (" MAGIC ", magic, " | UNIQUE ", unique); // <---
      }while(unique < OrdersTotal());
  } else return (magic + 1);

  return (magic);
}


 
Artyom Trishkin #:

Aim formore- ALL programming languages.

It's simple - in programming languages, counting starts at zero. The first cell of an array will have an index of 0. Therefore, you have to do a reverse loop BEFORE zero INCLUDING it. I.e. >=0

OrdersTotal() outputs, for example, 10. And you start a loop from 10, while the array's last index is 9 (remember, we're starting from zero?). And what will happen when you access a non-existing array cell? That's right - the program will crash on a critical error, because you have gone into an unallocated area of memory, beyond the array's limits.

These are solid crutches for sure. Read, learn and everything will come to you.

Thank you. That's not what I meant. Anything could be forgiven for mql if it were on an open licence. Otherwise, I'm picking on you. Well, it seems to me, for example, that we could have put a flag in NormalizeDouble function and used place trimming or arithmetic rounding. Or, for example, we could expand the functions of position handling, for example, return the number of open positions only for sell or buy. Also, a more detailed help could be written. But this is only the opinion of an amateur. Also, please forgive me if the previous statement seemed harsh.
 

There are tons of free and good courses on C/C++.

Mql is close to them and you can freely learn. Fortunately, deep knowledge is not required. But at least the syntax of the language and the basic basics of algorithms.

Otherwise we get nonsense in the topic, instead of consultations on the platform and algorithms, primary schools level dialogues - where to put ; what is a loop and why indexes are 0.

For example, there is a site intuit.ru - there are courses on C and algorithms, take your time, a couple of weeks at most. At the same time you can get a paper :-)

 
Maxim Kuznetsov the syntax of the language and the basic basics of algorithms.

Otherwise we get nonsense in the topic, instead of consultations on the platform and algorithms, primary schools level dialogues - where to put ; what is a loop and why indexes are 0.

For example, there is a site intuit.ru - there are courses on C and algorithms, take your time, a couple of weeks at most. You may get a paper at the same time:-)

Like this?


 
Artyom Trishkin #:

OrdersTotal() gives for example 10. And you start the loop from 10. But the last index of the array is 9 (remember, we're counting from zero?). And what will happen when you access a non-existing array cell? That's right - the program will crash on a critical error, because you have gone into an unallocated area of memory, beyond the array's limits.

I stand corrected... There won't be a critical error, just the OrderSelect boolean will return false... and that's all... But of course, it's better to avoid... For if you use incorrect code features, it's difficult to predict possible errors...


Nerd Trader#:

The first order is given magic = 1 by default. Then a loop is started:

Start iteration magic = 1 unique = 0, at the end (when there is already one order with magic = 1 ) magic = 1 and unique = 0
Second iteration magic = 2 unique = 0, at the end magic = 2 and unique = 1 (no match) and the retorn is assigned to the 2nd order magic = 2.
The third should be the same...

I just see that the filling of unique goes very fast and reaches the limit, and magic barely grows... Generally, it was wrong to use the same variable for both loop control and for the logic inside the loop where this variable inflates...

Print to find a solution in this context, but I would suggest just reformulating the problem... For example find the maximum magik in orders by the loop and return it with +1, that would be the right simple solution.

Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
Любые вопросы новичков по MQL4 и MQL5, помощь и обсуждение по алгоритмам и кодам
  • 2021.10.19
  • www.mql5.com
В этой ветке я хочу начать свою помощь тем, кто действительно хочет разобраться и научиться программированию на новом MQL4 и желает легко перейти н...
 

You can do it this way, the code is called as needed, so its weight is not a problem

For the test I used Buy/Sell

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
  Print(GetMagic());
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int GetMagic()
{
//Если должен быть открыт стопордер и если есть уже открытые или отложенные
//ордера, то возможно некоторые с маджиком, это надо проверить и сгенерировать
//для нашего ордера уникальный маджик
  int arr[];
  int n=-1;
  int OT=OrdersTotal();
  for(int i=0; i<OT; i++) {
    if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) {
      if(OrderType() == OP_SELL || OrderType() == OP_BUY) {
        if(ArraySearchInt(arr, OrderMagicNumber())==-1) {
          n++;
          ArrayResize(arr,n+1);
          arr[n]=OrderMagicNumber();
        }
      }
    }
  }
  ArraySort(arr);
  OT=ArraySize(arr);
  for(int i=0; i<OT; i++)
    if(arr[i]!=i) return i;
  return OT;
}
//-- удаляет копии с массива
int ArraySearchInt(int& m[], int e)
{
  for(int i=0; i<ArraySize(m); i++) {
    if(m[i]==e) return(i);
  }
  return(-1);
}
 

Forum on trading, automated trading systems & strategy testing

Any questions from newbies on MQL4 and MQL5, tips and discussion on algorithms and codes

Vitaly Muzichenko, 2021.10.19 23:08

int GetMagic()
{
  int magic = 0;
  int unique = 0;
  if(OrdersTotal()){
    do{
        magic++;
        for(int i = OrdersTotal() - 1; i >= 0 ; i --){
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            if(magic != OrderMagicNumber())
              unique ++;
          }
        }
        Print (" MAGIC ", magic, " | UNIQUE ", unique); // <---
      }while(unique < OrdersTotal());
  } else return (magic + 1);

  return (magic);
}


Semyon Semyonych.
Nikolay Ivanov #:

I will correct it a little. There will be no critical error, just the boolean OrderSelect will return false ... and that's all... But of course it's better to avoid... For if the code features are not used correctly, it's hard to predict the possible pitfalls...


I just see unique filling up at a very fast pace and reaching its limit, while magic barely grows... Generally it was wrong to use the same variable for both loop control and for the logic inside the loop, where this variable inflates...

Print to find a solution in this context, but I would suggest just reformulating the problem... For example find the maximum magik in orders by the loop and return it with +1, that would be the right simple solution.

It's a last resort, in case this option doesn't work out. In the meantime, this is what the print came up with.

int GetMagic()
{
  int magic = 0;
  int unique = 0;
  if(OrdersTotal()){
    do{
        magic++;
        for(int i = OrdersTotal() - 1; i >= 0 ; i --){
          if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
            if(magic != OrderMagicNumber()){
              unique ++;
              Print (" MAGIC ", magic, " | UNIQUE ", unique);
            }
          }
        }
      }while(unique < OrdersTotal());
  } else return (magic + 1);

  return (magic);
}
------------------------------------------------

2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #10  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 9
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 8
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 7
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 6
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 5
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #9  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 8
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 7
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 6
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 5
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #8  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 7
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 6
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 5
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #7  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 6
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 5
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #6  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 5
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #5  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 4
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #4  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 3
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #3  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 2
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 1 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #2  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5:  MAGIC 2 | UNIQUE 1
2021.10.20 02:02:10.841 2021.01.04 00:01:00  endless cycle EURUSD,M5: open #1  buy 0.01 EURUSD at 1.22405 ok
2021.10.20 02:02:10.827 2021.01.04 00:00:00  endless cycle test started

 
Vitaly Muzichenko #:

You can do it this way, the code is called as needed, so its weight is not a problem

For the test I used Buy/Sell

I'll look at it tomorrow, I don't have the energy to get into it now.