Get length of a series()

Posted By: NorbertSz

Get length of a series() - 09/13/22 09:28

Dear all,

Is there a solution for getting the length of a series()?
I have multiple series with different lengths, and a function is iterating through the series. It would be nice if I would not needed to store and pass the length as function parameter, just get that inside the function. For example like the C++ <vector> does:

Code
vars asd = series (0, 100);
if (asd.length() == 100) printf("yep, it's 100");
doSomethingWithEveryElement(asd);

...

void doSomethingWithEveryElement(vars* s){
   for (int i = 0; i < (*s).length(); i++){
      doSomething((*s)[i]);
   }
}
Posted By: jcl

Re: Get length of a series() - 09/13/22 09:35

Try this:

int Length = *(int*)&(asd+1);

Untested, so please post here if it does not work.
Posted By: NorbertSz

Re: Get length of a series() - 09/13/22 10:05

Not working, compile error:
Code
error C2102: '&' requires l-value
Posted By: jcl

Re: Get length of a series() - 09/13/22 11:50

Yes, I've just seen that the lenght cannot be retrieved from the var pointer offset, but needs an extra variable. This will be implemented in the next version.
Posted By: NorbertSz

Re: Get length of a series() - 09/13/22 12:22

Nice, thank you!
Posted By: HamzaAhmed

Re: Get length of a series() - 09/13/22 14:22

Shouldn't Dynamic arrays by Andrew Dolder help your case.

Dynamic arrays
Library by Andrew Dolder for dynamic arrays in lite-C using std::vectors.

Dynamic Arrays
Posted By: AndrewAMD

Re: Get length of a series() - 09/13/22 16:02

Or you can use C++ scripts instead and use std::vector<var> directly!

That said, series() is already optimized, and getting its length would indeed be a very useful feature, especially when backtesting many assets with different start dates.
Posted By: NorbertSz

Re: Get length of a series() - 09/14/22 06:56

Yes, I switched to C++ in the last few weeks: using OOP, destructor, function overloading and vectors were huge improvements in my code. It became much cleaner and easier.

But in this specific case it's about the prices. I have multiple price series() in different TimeFrames, and different lengths. And as I see the series() is perfect for storing the prices, because
  • the automatic shifting on new Bar
  • changing the pointer on Asset change
  • the built-in indicators and functions are also using series

So I want to keep the prices in series() instead of Dynamic Arrays or vectors.
But thank you for the ideas!
Posted By: NorbertSz

Re: Get length of a series() - 09/15/22 08:50

I see in the beta news: SeriesLength. Do you mean this feature?

SeriesLength
int, r/o, containing the number of elements of the date series returned by the last series() call.
https://zorro-project.com/manual/en/series.htm

With this way I still need to store the series length somewhere.

Because what if I want to get the length of a series, but not the last one I called by the command series()? Or do some calculations with multiple series?
For example like this:

Code
double doSomething(var* series){
    for (int i = 0; i<(*series).length(); i++){
        //...calc something
    }
}

double doSomethingElse(var* series, var* series2){
    for (int i = 0; i<(*series).length(); i++){
        for (int j = 0; j<(*series2).length(); j++){
            //...calc something with (*series)[i] and (*series2)[j]
        }
    }
}
...
function run(){

    ...

    priceM1 = series(priceClose(0), 100);
    TimeFrame = 5;
    priceM5 = series(priceClose(0), 250);

    double result = 0;

    double someThing1 = doSomething(&priceM1);
    if (someThing1 >= 0.5){
        double someThing2 = doSomething(&priceM5);
        if (someThing2 >= 0.5){
                result = doSomethingElse(&priceM1, &priceM5);
        }
    }

    ...

}


It would be nice if I could ask exactly from the series itself. For example like this:

Code
vars a = series(0, 100);
int length = a.length();


Or like this:

Code
vars a = series(0, 100);
int length = getSeriesLength(a);
Posted By: AndrewAMD

Re: Get length of a series() - 09/15/22 13:19

series() is already context-based, and so is SeriesLength.

A C++ script writer can write one function that returns a struct/tuple of vars and int by wrapping series() and SeriesLength.
Posted By: NorbertSz

Re: Get length of a series() - 09/15/22 16:24

Yes I can wrap it. Or I can store and get the length of the series with a pointer map, like this:

Code
#include <map>
#include <zorro.h>

using namespace std;

int getSeriesLength(var* s);
map<int, int> seriesLength;

DLLFUNC void run(){
	BarPeriod = 60;
	LookBack = 100;
	asset("EUR/USD");
	
	vars price60 = series(price(0), 20);
	seriesLength.insert(pair<int, int>((int)price60, 20));
	TimeFrame = 4;
	vars price240 = series(price(0), 5);
	seriesLength.insert(pair<int, int>((int)price240, 5));
	
	if (!is(LOOKBACK)){
		printf("\n%d %d", getSeriesLength(price60), getSeriesLength(price240));
		quit(0);
	}
}

int getSeriesLength(var* s){
	return seriesLength.at((int)s);
}


I'm just saying it would be nice if series could store its own length natively, therefore we don't need to handle that information with these tricks.
© 2024 lite-C Forums