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

 
OK, men of science, then explain why magic increment stops at 2 while unique does not. In the source code we open 10 orders and each of them should be assigned a unique magic through inique increment and their uniqueness should be checked by comparing (!=) in nested loops: on one increment magic there is a pass with comparison of all orders mages, if there is no match, then unique will be equal to the number of those mixtures and if it is equal or greater than number of orders then magic is unique and it will be assigned to the order.

I don't know if this is too complicated, but I think it is easier to understand if we look at the code.
Files:
 
pribludilsa #:
Thing is, form is convenient in that by giving it a number of items, it can fill some array. Everything will crash if 0 is fed, because the ArrayResize is tied to number of positions. On this reason I have an additional condition if number of positions greater than 0, then this loop. I mess up with it all the time too, now I've checked it again. The advice is correct, because otherwise the forte won't fill the zero element of the array, which is what the forte is for.

Yes, I have constant problems because of this. I hope this clarification will reduce the number of hours spent on fixing bugs many times over.

 
Nerd Trader #:
Ok, men of science, explain then why magic increment stops at 2 while unique does not. In the source code we open 10 orders and each of them should be assigned a unique magic through inique increment, their uniqueness is checked by comparing (!=) in nested loops: on one increment magic there is a pass with comparison of all orders magic, if no match, then unique will be equal to number of those unmatched and if it is equal to number of orders then magic is unique and it will be assigned to an order.

I don't know if it's too complicated, but I think it's easier to understand if we look at the code.

If you need the loop to create 10 orders with different mages, that's what you should do.

    for(int i = 0; i < 10; i ++)
    { 
    int order_send = OrderSend(Symbol(), OP_BUY, 0.01, Bid, 10,
    0, 0, "", i, 0, CLR_NONE);
    if(order_send == -1) ResetLastError();
    }

In this case i has the role of not only controlling the loop, but also the magician for the order.


But in your example, there are a lot of errors that have created a kind of a puzzle. Few people will thoroughly investigate what is the reason for it...

Well, at the very least, the order loop is not correct... And the magic variable is local and resets all the time... (doesn't accumulate)

 
Nikolay Ivanov #:

If you need the loop to create 10 orders with different mages, that's what you should do.

In this case i has the role of not only controlling the loop, but also the magician for the order.


But in your example, there are a lot of errors that have created a kind of a puzzle. Few people will thoroughly investigate what is the reason for it...

Well, at the very least, the order loop is not correct... And the magic variable is local and resets all the time... (does not accumulate)

This code is for the test and the EA will not create orders in such a loop; there is some logic that should not be included into the testing. Why is the order cycle wrong? magic should be cleared, there is no reason for it to accumulate. And there is nothing to understand here, there are only two nested loops with a couple of lines of code inside.

 
Nerd Trader #:

Yes, I have constant problems because of this. I hope this clarity will reduce the number of hours spent fixing bugs.

It took me a while to figure it out too. The irony is that I started figuring it out at the same time as other users posted about the same thing here. I just had the right copy-paste, then sorted it out and wrote it wrong, because I only checked the loop's input and forgot the output. Check the code with the printer by forcing the check values.
 
Nerd Trader #:

This is code for testing, the EA will not create orders in such a loop, there is a logic for this which we should not add to testing. Why is the order loop wrong? magic should be reset, there is no reason for it to accumulate.

There is no -1.

for(int i = OrdersTotal()-1; i >= 0 ; i --)

If magic is designed that way, then it turns out there is an error in the logic (in the overall design) and we need to solve a puzzle to find it...

 
Nikolay Ivanov #:

no -1

If magic is designed that way, then there is an error in logic (in the overall design), you have to solve the puzzle to find it...

Unless it's this. But -1 doesn't solve anything there either. Why is there an error? The magic will be given when magic is equal to or greater than the number of orders. If magic should be zeroed, otherwise if we close several orders with magic 5 and 6, the new ones will be higher than the magic of the last order, i.e. 11-12 and so on. And this way when an order is zeroed out, it is compared and iterated from zero, which results in new orders with majicks of closed ones, i.e. 5 and 6...

 
pribludilsa #:
It took me a long time to figure it out, too. The irony is that I started figuring it out at the same time as other users' posts about the same thing here. I just had the right copy-paste, then sorted it out and wrote it wrong, because I only checked the loop's input and forgot the output. Check the code with a printer, forcing the check values.
It's so annoying that mql4 doesn't have a decent debugger. Even on real data on source above, debugger doesn't enter into loop. I have to make do with print, but it's not serious.
 

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 be broken... And magic =3 was never checked... So magic =2 again and so on with all...

 
pribludilsa #:
Oh, right, thanks. But it turns out it's a crutch. Just like the whole mql thing.

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.