how to swap elements in an array around, especially initializing an empty array with given length?

 

Hey, my basic goal is to calculate stuff around linear regression.
I get the how and the formulas for it, that's not the problem.


the thing is:

I am looking at the closing prices with commands like Close[i], index being i.

closing prices are basically stored in an array,

however they are put in their in anti-chronological order.
meaning index 1 (or 0?) is the newest price, index 2, the next oldest, etc.


so they are saved right to left in a way.


however, for the regression line that I want , I obviously want it to be used and calculated with values in chronoligcal order, so the slop tells me in which direction the graph moves when going towards newer prices.


anyways, currently prices are stored in the array from the newest to the oldest one.
I howefer want it the other way around, so that first index place is oldest price and last index element is newest price.


so basically need to swap array around.


I sure could do this operation on and in the array itself.

but cause I will otherwise get confused along the way, I wanna be simplistic.
I know how many prices there are (somewhere in the program it's hardcoded to be 5 bars, so a array of length 5).


and I would basically like to do something like that:

(n=5 is given)

int[] newarray=new int[5];

for (int i=1; i<=n; i++){
   newarray[i]=oldarray[n-i+1];
}


however, fter reading about all these array initialization methods and the act that arrays can only be hardcoded as

new array={1,2,3,4,5}

 (something like that at least)

I am confused if or how I can do stuff like I want .

basically first , to a variable, assigning an empty array of length n (or 5).
and later inside an for loop actually assigning values to each index place.


is that possible?





unrelated but also confusing/ a bit annoying:
the running variable for the for loop. I usually define and initialize it inside the for command like above.
however that didnt really work when I tried it.


so I had to do some initialization outside(!) the loop, like

int i=1;

and then run the for loop like this:

for(i;i<=n;i++){
...
}


which I find very annoying. I am used that, in literally every languag ein existence, the running variable for a for loop to be definied initialized, etc. directly inside the brackets after the for.


and not partially outside as a separate command.

not that it is a big issue, I jsut hate it cause i gotta start to care about what these i, j, k, etc. are that are defined at the scripts start.
just to learn that they were literally only used as some cheap running variables in a for loop.


is ther some way to do for looping like I firstly worte, with the

for (int i=0;i<=n;i++) {...}


kind of syntax?

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
When a graphical object is created using the ObjectCreate() function, it's necessary to specify the type of object being created, which can be one of the values of the ENUM_OBJECT enumeration. Further specifications of object properties are possible using functions for working with graphical objects.
 
densch: anyways, currently prices are stored in the array from the newest to the oldest one.

I howefer want it the other way around, so that first index place is oldest price and last index element is newest price.

  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Perhaps you should read the manual. ArraySetAsSeries - Array Functions - MQL4 Reference

  3. As far a linear regression goes, it makes no difference; just negate the slope after computing from as series array.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Perhaps you should read the manual. ArraySetAsSeries - Array Functions - MQL4 Reference

  3. As far a linear regression goes, it makes no difference; just negate the slope after computing from as series array.

Okay, changed it.

I will look into this set/get; etc. as series stuff even though they are kind of complicated.

I mean, I kind of want to ac on the first 5 bars of the Close array (or whatever it technically is)

Not sure if I can just plug that somehow into that ArraySetAsSeries function.

gotta learn abit about this seriesdynamic array weird stuff.


I only know my good old standard arrays of defined length and only 1 dimension (I know how 2 dimensional ones work too).
so I gotta read up on that weirds tuff there with timeseries and whatever .


I wanna calculate lots of other stuff too with the (order changed) array, so I'll need it either way

 

Also, one slightly unrelated question:
 as I am trying out different stuff currently, to get a feel how some of the mor euncommon quirks of mql4 work,
I wrote this code:

void OnTick()
  {
    double matrix[5];//n=5
    
    for(int i=1;i<=5;i++)
    {
      matrix[i-1]=Close[i]; //matrix 0-4, close 1-5
      Print("The ",i,"th close price is ",matrix[i]);
      
    }
    
   
  }
  

nothing special, literally just creates an array of length 5 at each tick and , with a for loop, puts the close prices in the array.
(I dont wanna start with the current bar, but use the last 5 completed ones)

While simultaniously printing the currently assigned value in the log.


after that the log looks something like this:

2020.05.13 21:20:17.360 2020.05.08 23:59:22  Lineare Regression Versuch 2 EURGBP,H1: The 5th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:22  Lineare Regression Versuch 2 EURGBP,H1: The 4th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:22  Lineare Regression Versuch 2 EURGBP,H1: The 3th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:22  Lineare Regression Versuch 2 EURGBP,H1: The 2th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:22  Lineare Regression Versuch 2 EURGBP,H1: The 1th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:20  Lineare Regression Versuch 2 EURGBP,H1: The 5th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:20  Lineare Regression Versuch 2 EURGBP,H1: The 4th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:20  Lineare Regression Versuch 2 EURGBP,H1: The 3th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:20  Lineare Regression Versuch 2 EURGBP,H1: The 2th close price is 0
2020.05.13 21:20:17.360 2020.05.08 23:59:20  Lineare Regression Versuch 2 EURGBP,H1: The 1th close price is 0


more or less what it should do, at each tick, the price of the last 5 closed bars is printed.

my only issue: it always, always prints 0 as price!
which is obviously not true, as I see the price in the chart moving in the range of 0.87.


and I am not sure why it doesnt cshow the price.

it's not like I somewhere in my code defined the price as an integer, did I?
cause that's my guess: that for whatever reason, it actually prints the next lower integer (which is =), basically displaying floor(actual price).


Might you have an idea why this happens? :-)

 
densch: Might you have an idea why this happens? :-)

The code you posted is not the code that generates that log. There are no mind readers here and our crystal balls are cracked. We can't see your broken code.

 
William Roeder:

The code you posted is not the code that generates that log. There are no mind readers here and our crystal balls are cracked. We can't see your broken code.

well, except it is.

I ain't no magician, either I am simply stating the facts.

But I seem to see different code than you or whatever, so attached are pics of everything.


still no idea how THAT code produces that log. or why.

Also, I  dont even own a crystal ball, would pay you 10 bucks for your broken one :-)

Files:
MT_1.png  143 kb
MT_2.png  161 kb
MT_3.png  120 kb
 
densch:

Also, one slightly unrelated question:
 as I am trying out different stuff currently, to get a feel how some of the mor euncommon quirks of mql4 work,
I wrote this code:

nothing special, literally just creates an array of length 5 at each tick and , with a for loop, puts the close prices in the array.
(I dont wanna start with the current bar, but use the last 5 completed ones)

While simultaniously printing the currently assigned value in the log.


after that the log looks something like this:


more or less what it should do, at each tick, the price of the last 5 closed bars is printed.

my only issue: it always, always prints 0 as price!
which is obviously not true, as I see the price in the chart moving in the range of 0.87.


and I am not sure why it doesnt cshow the price.

it's not like I somewhere in my code defined the price as an integer, did I?
cause that's my guess: that for whatever reason, it actually prints the next lower integer (which is =), basically displaying floor(actual price).


Might you have an idea why this happens? :-)

Because you set value at index i-1 but your print i, which is not yet set.

 

Funnily enough, the proper prices are actually printed when I write something even more simple like

void OnTick()
  {
  
      Print("The current close price is ",Close[1]);
      
    
  }


then it write them with all the after dot digits (and not jsut as an integer)
 
oh, I see. now stuff starts working again after changing index here and there.

This might be the last time I annoy you guys but I have a thing:
for good or worse I finally got the prices now down.
however, comparing the stuff in the log versus getting the info of a bar when I hover over it with the mouse cursor, I am not sure if the prices arent a bit off by +- pip.
So to test this, I would like to get the actualy time of the corresponding bar.
I wrote something like this:

void OnTick()
  {
  
      Print("The current close price is ",Close[1],"and time ",TimeToStr(Time[Bars-1]));
      
    
  }
But I must be royally doing something wrong again cause the time displayed is always the same for all the bars, so the times arent actually displayed in relation to a specific bar.
cause by now I have realized that some arrays are one direction or some are another (where index 0 is the left most or the right most thing) I also tried Time[0] and Time[1].
Although a different date and time, it still gives me the same date for every bar, so written date not dependent on current bar either.

I am probaly again not getting at all how the Time[] array works,
can someone tell me, if I put Time[] in the OnTick function, what index I would need to use to get the current bars opening time/the second last bars close tie 8which should be the same, right?)
 
densch:
oh, I see. now stuff starts working again after changing index here and there.

This might be the last time I annoy you guys but I have a thing:
for good or worse I finally got the prices now down.
however, comparing the stuff in the log versus getting the info of a bar when I hover over it with the mouse cursor, I am not sure if the prices arent a bit off by +- pip.
So to test this, I would like to get the actualy time of the corresponding bar.
I wrote something like this:

But I must be royally doing something wrong again cause the time displayed is always the same for all the bars, so the times arent actually displayed in relation to a specific bar.
cause by now I have realized that some arrays are one direction or some are another (where index 0 is the left most or the right most thing) I also tried Time[0] and Time[1].
Although a different date and time, it still gives me the same date for every bar, so written date not dependent on current bar either.

I am probaly again not getting at all how the Time[] array works,
can someone tell me, if I put Time[] in the OnTick function, what index I would need to use to get the current bars opening time/the second last bars close tie 8which should be the same, right?)

You obviously don't understand what you are doing. You need to read the documentation and practice.

This code is executed inside OnTick(), so of course a fix index will always return the same value, until there is a new bar.

 
densch:
B now, I kind of hate MQL4 for being XXX.

it just does not work

Watch your language.

That's a classical example of Dunning–Kruger effect.

Reason: