Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,014 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Accessing string array via loop #437347
02/14/14 16:19
02/14/14 16:19
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
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")))

Re: Accessing string array via loop [Re: dusktrader] #437364
02/14/14 20:05
02/14/14 20:05
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline
Newbie
Radar  Offline
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
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.


Have fun!
Radar =8^)
Re: Accessing string array via loop [Re: Radar] #437368
02/14/14 21:21
02/14/14 21:21
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
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]);
	...


Re: Accessing string array via loop [Re: dusktrader] #437379
02/15/14 00:44
02/15/14 00:44
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline
Newbie
Radar  Offline
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
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.


Have fun!
Radar =8^)
Re: Accessing string array via loop [Re: Radar] #437585
02/20/14 00:55
02/20/14 00:55
Joined: Jul 2013
Posts: 522
D
dusktrader Offline OP
User
dusktrader  Offline OP
User
D

Joined: Jul 2013
Posts: 522
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.

Re: Accessing string array via loop [Re: dusktrader] #437597
02/20/14 08:20
02/20/14 08:20
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
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.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1