# Array or not to array

232
2016.04.15 17:04

Hi Guys.... thanx for any input

Could someone please point me in the right direction here.... I am trying to create a function (well 2 - Buy and Sell positions) that will provide me with the following information:

For all open Buy positions that are in a Loss:

1. The Order ID of the "furthest" Buy position from the Bid price (ie: open price most points away from current price) - this must be sorted from largest to smallest - so that I can loop thru the open trades in that order ie: I would like to test the furthest positions criteria (Lotsize and Loss amount) against other variables and if true then close the position else move to the next furthest trade and test etc. etc.

2. The corresponding Lot Size

3. The corresponding Loss amount

....and visa versa for all the open Sell positions....

I am not familiar with Arrays but am willing to get stuck into them if they are going to be appropriate to what I am trying to do.

Sooo.... in your opinion... if this is even possible:

Should this be done:

1. Using Arrays, or

2. Loops

Any input would be welcome..... Thanx....

Thanx....

68
2016.04.15 20:48

You are going to need both arrays and loops. And rather than arrays, I would use an object or structure to hold the three pieces of information that you want, but if you are not comfortable with those two concepts, then stick to arrays. You can in theory use a double array to hold your order id, lot size and loss amount. And you will need an array of arrays, since you want those three items for each of the orders opened.

So your function will basically loop through the opened orders, and for each of them, figure out if it is in a loss, if yes then store id, size and loss amount in an array and add it to the array of arrays. Then at the end you want to order the array of arrays based on the loss amount of each order.

Finally, your function will just return this double dimensional array with the ordered set of orders containing the arrays with the three items you put in there.

Regards

2349
2016.04.15 20:54

In this case, loops only will make the code execution slow. It might not be that noticeable when you are using the EA in real time, but when running optimisations in the Strategy Tester, that can really slow things down.

Yes, an array of a structure will probably be the best solution. To make it efficient, don't populate the arrays on every tick. Rather populate array during the initialisation of the EA to cater for previously opened orders.

After that, add to the array during order placement and during tick events, just do the array management in order to reduce CPU usage.

232
2016.04.15 21:24

Thanx guys....

Ok... so it's 2 for arrays... and we'll give looping a 1/2......

Unfortunately I have been avoiding using Arrays and I need to spend some time getting to know how they work....

The crazy thing is that I'm really at the end game of my EA and I'm hoping not to have to go down another rabbit hole....

I have a workaround that is quite interesting and which I would like to try...

But just to recap: If I got to learn about Arrays.... Would it be possible for me to use them to filter the worst/second worst/3rd worse etc. trades in one direction (Buy or Sell)... and then use the corresponding/associated data (Lotsize and Loss) in a formula that would determine whether that position is closed or not..... ie. I guess my question is....

Is data in an Array dynamic and accessible....? And....

...is it worth going to the effort to learn about Arrays? (my code is over 8 000 lines long and I have yet to use an array - perhaps that is why it is so long...)

Thanx

Moderator
9625
2016.04.15 21:29
FMIC:

In this case, loops only will make the code execution slow. It might not be that noticeable when you are using the EA in real time, but when running optimisations in the Strategy Tester, that can really slow things down.

Yes, an array of a structure will probably be the best solution. To make it efficient, don't populate the arrays on every tick. Rather populate array during the initialisation of the EA to cater for previously opened orders.

After that, add to the array during order placement and during tick events, just do the array management in order to reduce CPU usage.

` `
`With an array of a structure, it will be necessary to write your own function to sort the array. ArraySort will not operate on struct arrays. `
232
2016.04.15 21:33
GumRai:

Thanx GumRai....

Arrays 3... Looping 1/2....

2349
2016.04.15 21:38
GumRai:
`With an array of a structure, it will be necessary to write your own function to sort the array. ArraySort will not operate on struct arrays.`
True, but since the OP did not provide any skeleton code, I gave the advice that I believe will fulfill the requirements the best based on my own experience. Using, pure arrays can seem to be the quickest solution due to the use of the ArraySort, but he will soon reach a point where pure arrays will become rather messy and limiting. So, i suggested an array of structure because it will be the next best thing before having to suggest the use of Classes.
Moderator
9625
2016.04.15 21:41
Mike.T:

...is it worth going to the effort to learn about Arrays? (my code is over 8 000 lines long and I have yet to use an array - perhaps that is why it is so long...)

`Yes, definitely worth learning about arrays, I can’t imagine writing code without them. `
`I hardly ever loop through OrdersTotal() nowadays except for in a recovery function when the EA is initialized.`
`If your 8000 lines of code execute correctly, then there is no need to change it, but it is worth learning for future projects`
232
2016.04.15 22:48

Thanx guys....

You've both answered my  initial question perfectly.... and without much info....

I  appreciate the input....

GumRai - thanx - I forgot to mention that the actual trading code out of the 8000 lines is only about 1500... the rest is all custom indicators and a GUI that I built which I think everyone should have.... one click buttons on your chart to close all Short/long positions etc.... start break-even/trailing stops... Allow Trading or Stop trading.... etc. etc. etc.

FMIC... your advice was well taken....thanx... I will PM you tomorrow if I can.....  I get a 404 error if I try to access my profile and I cannot seem to PM you.... so there is something wrong....

2349
2016.04.15 23:06
Mike.T:

FMIC... your advice was well taken....thanx... I will PM you tomorrow if I can.....  I get a 404 error if I try to access my profile and I cannot seem to PM you.... so there is something wrong....

Or just remove the "." from it.

232
2016.04.15 23:12
FMIC: