Hi Viffer,
arrays are just a set of values under common name. You can have:
A=10, B=5, C=8
or
A1=10, A2=5, A3=8
or
A[1]=10, A[2]=5, A[3]=8
the last one will be an array A, it will have 3 elements and its index will go from 1 to 3.
Now in MQL4 all arrays start at 0, so it actually should be:
A[0]=10, A[1]=5, A[2]=8
the array A will contain 3 elements again, however its index will start at 0 and go to 2. Thats how MQL4 (and some other languages) represent arrays.
The default arrays in MQL4 that keep the values of the the bars on a chart - High[], Low, Open, Close - all start at 0 and go to Bars-1. 'Bars' contains the length of the array and because it start at 0, therefore its last index will be Bars-1 (see the example above again if needed).
Now about your example bars:
- you need to have 2 things - an array and its current length i.e. how many trades you have already put into it.
- so you define an array TRADES[100] for example - meaning it can keep UP to 100 trades (you can change this number according to your needs as well as you can resize the array if needed)
- and then you define int TRADE_COUNT=0. This will be your counter, your index, i.e. how many trades you are keeping into the array. Every time you finish with a trade you increase this with 1 i.e. TRADE_COUNT++;
- so in your example, you keep your first trade at TRADES[TRADE_COUNT]=20 (lets say 20 bars) which for the first time will be TRADES[0]=20. After you count the last bar you increase TRADE_COUNT with 1 i.e. at this time TRADE_COUNT=1;
- for your next trade you calculate and keep its value again at TRADES[TRADE_COUNT] however this time it will be TRADES[1]=5 (for example 5 bars).
- and so on, every time you increase TRADE_COUNT...
Thats why your values never get overwritten, instead you have an array that keeps all the values.
And the beauty of it is that the code is always the same because you always work with TRADES[TRADE_COUNT]. You just increase TRADE_COUNT with 1 every time so in fact it will always be different variable...
At the end your array will contain the info you need:
TRADES[0]=10, TRADES[1]=5, TRADES[2]=8 and so on...
Hope the above helps!
arrays are just a set of values under common name. You can have:
A=10, B=5, C=8
or
A1=10, A2=5, A3=8
or
A[1]=10, A[2]=5, A[3]=8
the last one will be an array A, it will have 3 elements and its index will go from 1 to 3.
Now in MQL4 all arrays start at 0, so it actually should be:
A[0]=10, A[1]=5, A[2]=8
the array A will contain 3 elements again, however its index will start at 0 and go to 2. Thats how MQL4 (and some other languages) represent arrays.
The default arrays in MQL4 that keep the values of the the bars on a chart - High[], Low, Open, Close - all start at 0 and go to Bars-1. 'Bars' contains the length of the array and because it start at 0, therefore its last index will be Bars-1 (see the example above again if needed).
Now about your example bars:
- you need to have 2 things - an array and its current length i.e. how many trades you have already put into it.
- so you define an array TRADES[100] for example - meaning it can keep UP to 100 trades (you can change this number according to your needs as well as you can resize the array if needed)
- and then you define int TRADE_COUNT=0. This will be your counter, your index, i.e. how many trades you are keeping into the array. Every time you finish with a trade you increase this with 1 i.e. TRADE_COUNT++;
- so in your example, you keep your first trade at TRADES[TRADE_COUNT]=20 (lets say 20 bars) which for the first time will be TRADES[0]=20. After you count the last bar you increase TRADE_COUNT with 1 i.e. at this time TRADE_COUNT=1;
- for your next trade you calculate and keep its value again at TRADES[TRADE_COUNT] however this time it will be TRADES[1]=5 (for example 5 bars).
- and so on, every time you increase TRADE_COUNT...
Thats why your values never get overwritten, instead you have an array that keeps all the values.
And the beauty of it is that the code is always the same because you always work with TRADES[TRADE_COUNT]. You just increase TRADE_COUNT with 1 every time so in fact it will always be different variable...
At the end your array will contain the info you need:
TRADES[0]=10, TRADES[1]=5, TRADES[2]=8 and so on...
Hope the above helps!
thank you
Automated
--
grid trading EA in action, +558 pips in 24 hours:
http://www.gridtradingcourse.com/videos/grid_trading_eurusd_1/Grid_Trading_EURUSD.html
Suppose you wanted to calculate the average number of bars that a trade stayed open for say the last 20 trades. The trade opens, I start counting bars until it closes, I now have an variable defining the length (in bars) of the last trade. When the next trade opens, the variable is subsequently overwritten, but obviously, I need the array to hold the first value and keep adding as new values are calculated. Holding these values escapes me.... I suppose (and what I understand for the documentation) I could set 20 variables (say A-T) and cascade the results down the list of variables as new data comes in (ie, A=10, next event, A=12,B=10 etc, all the way to T) and then initialise the array "int myarray[20]={A,B,C...T}". This doesn't seem very efficient though... and what if I now want the last 50? a load more coding?
Logically, it would seem better to just hold a list of all results of my bar counting (or a defined number that can be easily changed) and use the averaging period in iMAOnArray to select the 20 (or however many) results. Achieving that, escapes me though. I would really appreciate if someone could explain to me how you would get these variables into the array to enable calculations... or show me some example code. I've read the Arrays tutorial and to be honest, that has just left me confused.
Thanks for any advice
V