How to sort current values of multiple series

Posted By: boatman

How to sort current values of multiple series - 03/19/15 10:53

I have several separate series that I am using to measure volatility. I'd like to sort the current values of each series in ascending order and pick out the five highest values for use in another function.

What is the best way to accomplish this in Lite-C?

I've tried passing the current value of each series to an element in an array (myarray[] = {series1[0], series2[0], ... , series12[0]} and then using the qsort function:

Code:
#include <stdio.h>
function run()
{
var myarray[] = {2, 6, 1, 9, 52, 3};
qsort(myarray, 6, 8, compare);	
}



This returns an error that seems to originate in stdio.h:

Error in 'stdio.h' line 228:
'API' undeclared identifier
< API(_fsopen,msvcrt)
>.

Can anyone suggest what I'm doing wrong, or a better approach to this problem?

Thanks in advance
Posted By: GPEngine

Re: How to sort current values of multiple series - 03/19/15 14:25

You are correct that you should not sort the series in place
however my guess is that you have not discovered http://zorro-trader.com/manual/en/sortdata.htm
Posted By: boatman

Re: How to sort current values of multiple series - 03/19/15 22:19

Hi GP

I had looked at the sortData function, but since its argument is a series rather than single values from multiple series, I assumed it wasn't applicable for my case. Are you implying that I should create a series out of the values I want to sort and then pass that to the sortData function? I will give that a try.

Cheers
Posted By: GPEngine

Re: How to sort current values of multiple series - 03/20/15 05:58

When including something
#include <stdio.h>
you must also always include
#include <default.c>
FIRST

default.c is only implicitly included if there are no other includes. yep.

So try
#include <default.c>
#include <stdio.h>

function run() {
...
}
Posted By: boatman

Re: How to sort current values of multiple series - 03/20/15 06:40

That's great, thanks for the tip. Now I just need to work out how to use the qsort function. Any ideas how to include the 'compare' argument (the code below is the sort code from stdio.h)?

void qsort(
void *base, // array to be sorted
int num, // number of elements
int width, // element size
void* compare //int (__cdecl *compare)(void* element1,void* element2)

Also, I am having a go at the sortData function you mentioned above. I'm pretty sure I can achieve what I want to achieve, but no doubt my code will be highly inefficient. I'll post it once I'm done and would really appreciate any critiques.

Cheers
Posted By: GPEngine

Re: How to sort current values of multiple series - 03/20/15 14:57

Well, if it's efficiency you're concerned about... rub hands together
Since you only need the top k values where k << N , you don't need to sort the whole list. O(N log N). You can use
  • Partial selection sort O(Nk)
  • max-heap of k elements. O(N + k log N)
As for the compare function did you try something like qsort translated to lite-c?
Posted By: GPEngine

Re: How to sort current values of multiple series - 03/20/15 15:51

jcl,
add lines like these to every header file in your include/.
Code:
#ifndef API
INCLUDE_DEFAULT_C_FIRST  // intentional compile failure
#endif


That way Boatman would have at least seen a clue in the cryptic compiler error
Code:
sort compiling..
Error in 'stdio.h' line 231: 
'INCLUDE_DEFAULT_C_FIRST' undeclared identifier

and maybe not needed to waste a day in the forums.
Posted By: jcl

Re: How to sort current values of multiple series - 03/20/15 17:24

This error message would probably not be seen as less cryptic. But we'll include <default.c> in all headers - that should solve the problem.
Posted By: GPEngine

Re: How to sort current values of multiple series - 03/21/15 05:16

That solution is so simple I figured there must have been a technical reason for not doing it. It took more effort than that for you to write the remark about it on http://www.zorro-trader.com/manual/en/include.htm

iwyu, bro.
Posted By: Smon

Re: How to sort current values of multiple series - 05/05/17 18:34

I'm trying to use sortData with no success.

What am I doing wrong?

Code:
function main()
{
	var Data[6] = {1,7,10,0,12,19};

	
	sortData (var* Data,6);      //syntax error!
	
	
	for (int i=0, i<6, i++)
	{
		printf("n%d, "Data[i]);
	}
	
	
}




I'm working on a support and resistance indicator. The array will contain price values later on. This is why I declared the array as "var".
Posted By: Smon

Re: How to sort current values of multiple series - 05/07/17 05:55

Code:
function main()
{
	var Data[6] = {1,7,10,0,12,19};

	sortData(Data,6);

	int i;
	for(i=0; i<6; i++)
	{
		printf("n%.0f", Data[i]);
	}

}

© 2024 lite-C Forums