My first MQL5 array doesn't work
-
I created my first easy array for MT5 but I get more than 20 errors. Could someone please tell me where's the problem?
-
@l-andorrà Have always stayed away from arrays, maybe roar can help, I have noticed this though when you create the variable you likely need this; array[], as for the number you use I am not sure, maybe you would need to set ort change array size before it will wrk.
-
@jstap This is my last frontier to explore! I find arrays can be very useful but I don't know hoe to create them in fxDreema. Any help would be much appreciated.
-
@l-andorrà I have never tried, well not recently, put this is a variable array[1], try to put data in it and display in a comment, if it works you can try adding 1 & 2 and display. if this doesn't work though you will have to see what custom code is needed. What always confused me with arrays is getting the value, getting max or min is simple but each value I'm not sure.
-
@l-andorrà the array works just fine in my backtest

-
@l-andorrà said in My first MQL5 array doesn't work:
This is my last frontier to explore!
Haha, ok

What you did looks correct.
Technically you can also do this in MQL:
int array[] = {10, 20, 30};But for some reason in fxdreema if you define a variable with name
array[]and value{10, 20, 30}, the array is added in the EA, but the value is not applied. I don't know why. I know I worked on this few times and its possible that this is intentional. -
Ok, I made it now to work with initial values like
{10, 20, 30}.If no value is specified, and when the type of the array is numeric (int, double), the value will automatically appear as 0, which looks wrong when we work with arrays, but this 0 is not actually used. Even if you write any other numeric value, it will not be used. Only value containing
{will be used as initial value. -
@l-andorrà
Did you get these errors on fxdreema trying to export the project as .ex5 ? -
@fxDreema Your changes apparently worked...partially. Now no errors are identified when compiling but when I run the EA on the chart no info is visible on the chart until I remove the EA!!

Then I can see the results as per roar image. Can't the comment(ugly) block be executed on the on init for MT5, maybe?

-
@l-andorrà, I did nothing to fix any compiling errors. The example you posted in the first post compiles sucessfully, it has no problems, not even warnings. I tested it and my results are the same as @roar. I don't know what you did to get these compilation errors, and I don't know what they are. Maybe you can tell us what your errors are.
About the Init event... I noticed few times that when I try to run some EA in MT5 on the weekend, it says Market Closed or something. I didn't investigate this problem, I only noticed it. Do you have such errors?
By the way, the
{10, 20, 30}thing doesn't work for Constants, because they are inputs. If we try to do this in MQL5:input double myArray[] = {3, 3, 3};it gives us error:
array is not allowed as input. -
I think this is a graphics and chart refreshing issue, array itself works

If you print out the array elements (using Print() function) instead of commenting on the chart, it always works.
Also, if you make a ChartRedraw() after the comment block, the commenting works too.
https://fxdreema.com/shared/71T8DgAze
Mql5 is such a diva, always demanding some weird special tricks lol
-
Excuse me. Can you tell me what is the purpose of using Array? What can it do?
I am not good at Raw Code. I want to learn more(from MQL, youtube, google, etc,.). Thank you. -
@roar said in My first MQL5 array doesn't work:
Mql5 is such a diva, always demanding some weird special tricks lol
Absolutely. MQL5 is very annoying language. And I'm a JavaScript developer.
@Pheaktra-Capo Arrays contain multiple values, as many as you want. And you can iterate these values - access them one after another. So they are often used in
forloops. A simple array is like one row (or column) of data in a table. In MQL there are multi-dimentional arrays that are more like whole tables of data... but you don't want to try these. -
@Pheaktra-Capo
If you need your EA to calculate a lot of data related to a common ground, e.g. the data of a series of candles, you need to use arrays. Otherwise you lose track. You then only need one variable e.g. myarray[10][5] to replace 50 individual variables. A huge advantage!
And actually, working with arrays is very easy if you take some time and invest some practice. -
It works now. Thank you all for your help. BTW. how can I remove the 'unsolved' tag on the thread?

-
I finally found how to mark a thread as 'solved'. Why didn't anyone told me? :D.
-
@l-andorrà I see how did you do it?
-
@fxDreema thank you very much. I will search and learn more about array.
-
@RGoo Sound very good. This might help ton of my variable. Thank you very much.
-
@jstap You can find it on my other thread with questions on arrays.