array vs 4 variables on global

 

real scenario... I have coded ea for grid trading with main closing strategies using various averaging math, which requires saving of at least 3 prices for both buys and sells, including first trade, last trade, 2nd closest to last trade, etc etc. I currently am using 3 variables on the global environment to save these prices.

Question for the pro's and forum gods... Is there any advantage or reason that I might change these to a single array with 3 or more prices in the 1 array? ie buys[first_price, last_price, 2ndlast_price];

It seems obvious to me that it could make the code look cleaner, but is there any other reason that I might consider doing this change?

 
Revo Trades:

real scenario... I have coded ea for grid trading with main closing strategies using various averaging math, which requires saving of at least 3 prices for both buys and sells, including first trade, last trade, 2nd closest to last trade, etc etc. I currently am using 3 variables on the global environment to save these prices.

Question for the pro's and forum gods... Is there any advantage or reason that I might change these to a single array with 3 or more prices in the 1 array? ie buys[first_price, last_price, 2ndlast_price];

It seems obvious to me that it could make the code look cleaner, but is there any other reason that I might consider doing this change?

Is the current code slow?
Is the current code hard to read?

If you answer no to both questions, don't refactor it.

As for performance, I don't think in this case will matter since you are dealing with a small amount of data.

 
Alexandre Borela #:


i am starting to think it is a pain sometimes scrolling down past the reinitialisation lines haha, otherwise, the code is still straight forward and logical order, even if i do say so myself haha, including headings separating these initialsing lines etc. Thanks for your questions.

EDIT. there has been a couple times during high volatility in the markets that trades had more slippage than i would expect, but that could be just that: just sudden spike in prices. It didnt happen enuf times for me to worry about or to investigate more, but it could be either/or the code or volatile prices. In any case, it didnt happen enuf times for me to think it was obviously a code issue.
 
Revo Trades #:

i am starting to think it is a pain sometimes scrolling down past the reinitialisation lines haha, otherwise, the code is still straight forward and logical order, even if i do say so myself haha, including headings separating these initialsing lines etc. Thanks for your questions.

EDIT. there has been a couple times during high volatility in the markets that trades had more slippage than i would expect, but that could be just that: just sudden spike in prices. It didnt happen enuf times for me to worry about or to investigate more, but it could be either/or the code or volatile prices. In any case, it didnt happen enuf times for me to think it was obviously a code issue.

I do refactor my code if navigating through it starts to become a pain too lol I completely understand the feeling.

 
Maybe i am just overthinking it again. Maybe i should just add these reinitialising lines to a void function at the bottom of the code where it can be forgotten about completely haha
 
Revo Trades:

real scenario... 

It seems obvious to me that it could make the code look cleaner, but is there any other reason that I might consider doing this change?

It’s not going to make any difference performance wise. Personally I would use a structure for easy clarity in the  code or an array with enum references.   Both of these will help to self document your code and avoid bugs
 
Paul Anscombe #:
It’s not going to make any difference performance wise. Personally I would use a structure for easy clarity in the  code or an array with enum references.   Both of these will help to self document your code and avoid bugs

you mean like in a previous post of yours here?

https://www.mql5.com/en/forum/376085#comment_24197387

 
Revo Trades #:

you mean like in a previous post of yours here?

https://www.mql5.com/en/forum/376085#comment_24197387

yes that will do it fine just use a double array.

if you choose not to use an enum  then keep the index the same as chart bars for easier understanding, so 0=latest 1=the previous and so on.

 
Paul Anscombe #:

yes that will do it fine just use a double array.

if you choose not to use an enum  then keep the index the same as chart bars for easier understanding, so 0=latest 1=the previous and so on.

my original idea, before i created this thread was to do the array without the enums, but i like the idea of yours with the enums. i have created clear names for the variables, but I think i like your idea with the enums, so elegant! If i dont use my ea for a year, the enums will make it so easy to get "the hang of the code again". lol thanks a whole bunch.

Reason: