Accessing string array via loop

Posted By: dusktrader

Accessing string array via loop - 02/14/14 16:19

Please forgive my very rusty C. I'm trying to learn the best way to accomplish these things.

I've managed to get this code working but I would like some feedback if it can be done more elegantly, please. My objective was to manage a listing of assets in the run() function only, and then let helper functions also be able to access that list. (That way I only have to keep track of 1 listing of assets)

So I'm creating an array and then passing that array's pointer to my function which can then access it as well.

I'd like to know if there is a better way to define the string array than what I have here. Also, is there a way to use the syntax "while(asset(loop( ..." instead of the for loop?

THANKS
Code:
function myfunction(char **assets)
{
	printf("\n\nin function now...");

	int i;
	for(i=0; i<=sizeof(assets); i++)
	{
		printf("\nin loop: assets[%i]= %s",i,assets[i]);
		asset(assets[i]);
		printf("\ncurrent asset= %s",Asset);
	}
}

function main()
{
	const char *assets[5];
	assets[0] = "EURUSD";
	assets[1] = "GBPUSD";
	assets[2] = "USDCAD";
	assets[3] = "NZDUSD";
	assets[4] = "GBPCHF";

	myfunction(assets);
}



PS: I should also point out that the above code does not seem to work in a real strategy, when I try to substitute while(asset(loop( with the for-loop as above. It just gives a generic "crash" message when I try to Train or Test. So I'd really like to figure out how to use while(asset(loop( and iterate through the array of assets. All of the examples I can find in the manual seem to use individual components like this: while(asset(loop("EURUSD","GBPUSD")))
Posted By: Radar

Re: Accessing string array via loop - 02/14/14 20:05

Hey DuskTrader,

From what I understand of lite-c, sizeof(array) gives the size of the pointer rather than the array, so you need to pass the number of elements to your function. Try...

Code:
function myfunction(char **assets, int ElementCount)
{
	printf("\n\nin function now...");

	int i;
	for(i=0; i<=ElementCount; i++)
	{
		printf("\nin loop: assets[%i]= %s",i,assets[i]);
		asset(assets[i]);
		printf("\ncurrent asset= %s",Asset);
	}
}

function main()
{
	const char *assets[5];
	assets[0] = "EURUSD";
	assets[1] = "GBPUSD";
	assets[2] = "USDCAD";
	assets[3] = "NZDUSD";
	assets[4] = "GBPCHF";

	myfunction(assets, 5);
}



Hope that hits the spot.
Posted By: dusktrader

Re: Accessing string array via loop - 02/14/14 21:21

Thanks for that Radar. It does work now, in theory. But when I try to use it on a live strategy I am now getting this while Train'ing:
Quote:
dt-e9-htc-15min-b compiling.................
Train: dt-e9-htc-15min-b 2008..2014
Error 060 - out of memory: TR 93.1 MB (73.2 MB total)
Cancelled


I think the while(asset(loop( logic must be more memory-friendly than a for loop. Here is the code I'm trying to use in the run() function:
Code:
int assetCount = 2;
const char *assets[2];
assets[0] = "EURUSD";
assets[1] = "NZDUSD";

int i;
for(i=0; i<assetCount; i++)
{
	asset(assets[i]);
	...

Posted By: Radar

Re: Accessing string array via loop - 02/15/14 00:44

Hey DuskTrader,

I've come across the error number (060) a few times, but for a different reason (memory fragmented).

You could try restarting Zorro, to see if memory fragmentation is causing the problem, but if that doesn't work, try trimming things down to the barest minimum to test if the for loop works. It won't really help with your full system, though frown

I did a quick search of the Zorro help file, but found no mention of the maximum memory that Zorro can use frown I reckon JCL would know how to avoid the problem, though.
Posted By: dusktrader

Re: Accessing string array via loop - 02/20/14 00:55

I found something tonight that seems to be a solution for my issue. But it seems too easy... where's the 'gotcha'? Am I doing something wrong here??

I don't use the preprocessor commands very often, and actually didnt realize I could even use it this way. It seems that define is like creating a macro, even for C syntax.

Code:
#define ASSETLOOP while(asset(loop("NZDJPY","USDJPY","EURAUD","EURCAD","AUDUSD","AUDCHF","AUDJPY")))


The above code works... if I create my loop once at the top of my script, then whenever I need the loop I just use the macro ASSETLOOP in the place where the syntax would normally go.
Posted By: jcl

Re: Accessing string array via loop - 02/20/14 08:20

Do not call "loop" more than twice in your script. "loop" is only for changing the asset (first call) and the algo (second call). If you have more than 2 loop calls, something is wrong in the script.

Due to the special handling of the loop function in training and testing, it can not be arbitrarily called, but only be used in the way described in the manual. Of course, the normal C "while", "for", and "do..until" loops can be used without restriction.

The loop function can handle a string array, but only by passing all elements, such as loop(assets[0],assets[1],... etc.). Unused strings at the end must be 0. We could implement a more comfortable string list handling in a future version.
© 2024 lite-C Forums