Why would an array that was filled with orders (information) be divided in half ?

 

Hi,

I’m a NewtBee in MQL4 trying to modify an existing program. The section of code that closes orders in profit starts off by dividing the array with the order info in half. I can’t figure out why this would be done. I DID a search of the Forum, but with over 1300 hits….

.

Here is the relevant code from the start of the section that carries out this function:

//|------------------------------------------------------|

//| Start of: 'Close Buys In Profit' Section |

//|------------------------------------------------------|

.

void CloseBuysInProfit()

{

double BuyProfit, LastBuyTime;

RefreshRates();

Orders = ArraySize(BuyProfits) / 2;

for (Order = 0; Order < Orders; Order++)

.

Thanks for any and all assistance (< 8)

 

DougRH4x wrote >>

The section of code that closes orders in profit starts off by dividing it in half. [...]

This sounds like the issue which CB (in particular) keeps reminding people of: when looping through the order list in order to close things, you need to loop down (from OrdersTotal() -1 to zero) rather than up (from 0 to OrdersTotal() - 1).


Have a think about what happens if you don't do this. The Order variable starts off at zero, and you close the order at #0 [i.e. OrderSelect(0, SELECT_BY_POS)]. The Order variable gets incremented to 1 on the next pass round the loop.


But there's now one less order in the MT4 list. The order which was at position #1 moves to #0, the order which was at #2 moves to #1, etc. Therefore, on the second pass round the loop, with Order containing 1, the code skips the order which was originally at position #1 and is now at position #0. In short: only half the orders get processed.

 

Hi JJC,

It's late and i'm going to cogitate it some more when I'm awake, alert and in better shape enough to give it due consideration, but I'm not sure that it couldn't be just as readily done with incremental parameters.

Thanks to both yourself and CB for the info (< 8)

 
It sounds like it was a great design to start if it inherently misses every second trade in such a procedure
 
DougRH4x:
It sounds like it was a great design to start if it inherently misses every second trade in such a procedure

I've just had another look at this topic, and I think - but I'm not certain - that I may have been barking up the wrong tree. It's hard to be sure because you're not quoting very much of the code in question.


It looks as though BuyProfits may be an array of ticket numbers. In which case, the point I was making above is irrelevant. But there's no way of telling why the code does "ArraySize(BuyProfits) / 2" without seeing more of the code.

 

“Have a think about what happens if you don't do this. The Order variable starts off at zero, and you close the order at #0 [i.e. OrderSelect(0, SELECT_BY_POS)]. The Order variable gets incremented to 1 on the next pass round the loop.”

It sounds like it is inherent in this that the position in the array automatically gets incremented by one every time it is acted on? Is that correct? If so, is this conditional? Does it happen every time the array is just accessed and read or only when something like an order is deleted from the default position of ‘zero’. If it is as you say that the array ‘counts down to zero’ and in a sense is a FIFO stack, I can see the wisdom of utilizing zero as the default position then one doesn’t have to worry that they are trying to act on a location in the array (like # 47) that may be empty now. But if it’s default operation is in this manner, it doesn’t make sense to delete item zero and then increment to position #1 automatically any time it is utilized, but if this is the way it automatically works then obviously it has to be taken into account.

To make it even more complicated, if one has an array that is more than a single dimensional array (to me this is not an array at all but just a list of items, by definition an array is usually a matrix of some sort; but as that seems to be the convention in MQL4 then so be it) and is a 2, 3 or 4 dimensional array, which way would it automatically increment then?

BTW I’m not just being argumentative and tilting at windmills here. I'm just trying to get this figured out and get a proper handle on how these things work and what they do and don’t do.

.

“But there's now one less order in the MT4 list. The order which was at position #1 moves to #0, the order which was at #2 moves to #1, etc. Therefore, on the second pass round the loop, with Order containing 1, the code skips the order which was originally at position #1 and is now at position #0. In short: only half the orders get processed.”

It seems to that one would still have the same problem, but only half the orders get processed to start with so only ¼ of them would get closed, etc. Gotta remember I’m still very new to this and there is an assumption that I am making and including in the above statement. I’m not taking into account what the code does following the array divided by 2 statement. Obviously this is incorrect. I still haven’t figure out exactly what the ‘return’ function does. I understand that it can be used for and does ‘return’ data, info, value of variables etc to the program for usage or perhaps output to the user in the chart, journal, log etc. (I hope I at least have this part correct!) but it also says that it ‘returns control of the program to the program that called the current code’ (that the return is (generally) at the end of the specific section of code). As I posted somewhere else, to me this means it calls a completely other external program, sub routine, included file or whatever and runs it until it finished its job and then returned to the next line of code after the line that ‘called’ the external program, etc. But if it goes through a series of conditional manipulation etc and THEN come backs to the start of the array statement and is thus manipulated and incremented by the conditional ‘for’ statement immediately after accessing the array as it is in this code, then I can see it. Also the info in the MQL editor says that the 3rd part of the ‘for’ statement, in this case “Order++’ that increments the counter: Expression3 is calculated after each iteration’ does this take place immediately after the 2nd part of the ‘for’ statement that determines if it is logically ‘true or false' if it is true, or after it has gone through all of the subsequent code in the conditional loops and then increments the ‘Order++’ counter?

I’ve also found places where it says that when it encounters a ‘return’ statement that it carries on past that section of code and onto the next statement in the program. But it seems to me that the ‘return’ statement all by itself ['return;' | or 'return();' | or 'void return();'?] must be telling the control to go back the start of the code/loop that it is the ‘end of’, otherwise how does one form a loop that is longer than just a single block of data executions ‘for, if’ or ‘while’ conditional loops perform?

I had 'return' confused with 'break'. The latter function is clear to me, but I'm still not sure about 'return' and how to make and entire section of code into a loop. I understand about using the Conditional 'For', 'If, Else', and 'While' functions as loops, but not how to utilize more than one of these statements in a whole section or series of code that I want to make into a Loop.


Also I find references to the usage of the ‘square brackets’: “[ ]” in arrays, but no clear definition of what they are specifically used for. Are they used to describe specific locations within the array or ???

 

jjc wrote >>

I've just had another look at this topic, and I think - but I'm not certain - that I may have been barking up the wrong tree. It's hard to be sure because you're not quoting very much of the code in question.

It looks as though BuyProfits may be an array of ticket numbers. In which case, the point I was making above is irrelevant. But there's no way of telling why the code does "ArraySize(BuyProfits) / 2" without seeing more of the code.

Hi JJC,

I’ve uploaded what is mostly the original program but with some additional code both from an article/code base here that CB gave me a hand with.

As I said, it sounds like this particular aspect of the arrays wasn’t well thought out initially and thus the compensation of dividing the array in half right from the start which this program does in numerous places. The part that throws me off is that it isn’t decrimentally moving the counter to compensate what you are talking about but adds to the counter each time (I assume) before it makes another run through it. It may be adding to the counter each time to take into account dividing it in half initially. Though I would think that there might be the possibility of losing half of the data set when it was initially divided in half? I don’t know what the limitations as to their size is, and/or if it automatically resizes itself to compensate for this so that this doesn’t happen or ????. Either way, definitely not the neatest and tidiest arrangement.

I haven’t been able to figure out multi-statement loops yet. I understand the conditional loop statements, but don’t know what, where and how the logic is that either lets it proceed past the end of such a section or go back to the start of the long multi-statement loops again.

This is an adjustable Martingale scalper and trades like crazy and makes good profits but it’s downfall is that it does not close out the losers (early) at all. The early versions launched new trades like crazy (I don’t know why it doesn’t now?) but if left on any account the losers that keep building in # and value inevitably cleans the accounts completely out. It would be a winner and quite profitable if I could figure out how to do it. I’ve been trying off and on for quite some time to get it to do it. I thought I had it working and (thought I) had confirmation on a demo account where it was behaving and was building good equity. So I put it on my Live account and it seemed OK for the day so I left it on overnight. It cleaned my live account right out )< 8( I had it on numerous currency pairs concurrently and it turned out that most of the trades it was trying to place weren’t getting placed because other pairs where placing a trade at the time so other’s couldn’t. The odds that it ran well and profitably for a couple of days on a demo account in this state had to be astronomical! Just my luck )< 8(

I don’t have anywhere near enough knowledge or experience yet to figure it out. It basically has what appears to be a couple of programs all thrown together into one. If they are working in conjunction or against each other I can’t really tell, but it most definitely isn’t closing out the losing trades that it needs to and it’s Martingale aspect doesn’t work properly to compensate for them either so as it stands it is a big loser, but does have good potential inherent in it.

I’d really appreciate it if you could take a look at it and get an adjustable Trailing & Stop Loss working properly in it. You would of course be very welcome to use it yourself if we can get it to behave. I have some later versions where I close out the losing profits in the section right at the start where it is closing out profitable orders, but they aren’t functioning properly so I haven’t sent them. This includes the external double variable I’ve added ‘MaxLoss’ which is self explanatory.

Thanks for any and all assistance and good luck with it.

Regards,

Doug

Files:
 

I can't figure out how to get it to allow more than one upload per posting, so here are the auxiliary files.

Files:
 
Again, I can't figure out how to get it to allow more than one upload per posting, so here are the auxiliary files.
Files:
 

The following code from 'ProcessInitateCB.mq4' for the 'Included' file 'TradeContextCB.mqh'

if (_IsTradeAllowed() < 0) trips over ==> if(TradeIsBusy() < 0)

If placed one right after the other in the program they go into an 'infinite loop' if not spaced out and placed properly within the program.

 
DougRH4x:
Again, I can't figure out how to get it to allow more than one upload per posting, so here are the auxiliary files.

simply zip your files ... not necessary to upload more than one

and if you think you need to post in large letters, it would be nice from you, if you decide for one and only style.

it would make your posts more accessible for "normal" people like me.

(your "postlayouts", besides of the lettersize are awfule as they can be - as a former prepressman i have the right to say that ;-) )

thanks


p.s.

as you already have done a post in very very small lettersize, far away from what you state you can read, i know that you can post in normal lettersize without problems. i have made my conclusions about that ...

Reason: