[SOLVED] Accessing non-buffer arrays as timeseries

 

Im trying to get access to the dynamic array's elements as timeseries but I have no Idea what is wrong with my method, it just doesn't work and no amount of searching helped me.

After I store values into the array for each bar that EA process, I want to read the last added element using index 0 . so I tried this:

// declaring global variable
string catalog[];

void OnTick() {
        ArraySetAsSeries(catalog, true);
        int last = ArraySize(catalog);
        ArrayResize(catalog, last + 1);
        catalog[last] = "some incremented string value";

now I'm expecting catalog[0] gives me the latest added value but it gives me the first one. I tried different variations with ArraySetAsSeries() placed after value assignment and it

didn't work either. Im really puzzled and would appreciate any suggestions.

 
hematinik:

Im trying to get access to the dynamic array's elements as timeseries but I have no Idea what is wrong with my method, it just doesn't work and no amount of searching helped me.

After I store values into the array for each bar that EA process, I want to read the last added element using index 0 . so I tried this:

now I'm expecting catalog[0] gives me the latest added value but it gives me the first one. I tried different variations with ArraySetAsSeries() placed after value assignment and it

didn't work either. Im really puzzled and would appreciate any suggestions.

You are using ArraySetAsSeries which turns around the order of the array.

You use the variable last as index, but last will point to the end of the array, because it is set to the length of the array, then you increase the array by one element. Now the value in last is the last/highest valid index of the array.

Since you turned the array around, having the "oldest" element in the last space, you will always receive the first inserted value.

Use 0 to get the most recent value from your arrray.
 
Dominik Egert #:
You are using ArraySetAsSeries which turns around the order of the array.

You use the variable last as index, but last will point to the end of the array, because it is set to the length of the array, then you increase the array by one element. Now the value in last is the last/highest valid index of the array.

Since you turned the array around, having the "oldest" element in the last space, you will always receive the first inserted value.

Use 0 to get the most recent value from your arrray.

Oh right.. thank you very much Sir. I have a vague memory of reading something along the line that "ArraySetAsSeries() only changes the getting method and not the setting method"

too much reading scrambled my memory I guess.

 

Just in case someone else might find this useful, here's how I managed to work the problem:

// declaring global variable
string catalog[];

void OnTick() {
        
        // first we set the array as non-series
        ArraySetAsSeries(catalog, false);
        
        int last = ArraySize(catalog);
        ArrayResize(catalog, last + 1);
        catalog[last] = "some incremented string value";
        
        // now we can set the array as series
        ArraySetAsSeries(catalog, true);

        // this gives us the last added element
        Comment(catalog[0]);
}
 
hematinik #:

Just in case someone else might find this useful, here's how I managed to work the problem:

You don't need to change the direction of the array with ArraySetAsSeries, you can just resize by one and assign directly to index 0...
Reason: