Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Imhotep), 567 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Strings from Zorro to R #483727
07/17/21 18:12
07/17/21 18:12
Joined: Dec 2014
Posts: 204
Germany
Smon Offline OP
Member
Smon  Offline OP
Member

Joined: Dec 2014
Posts: 204
Germany
What is this good for?

I manage data from different assets in R and wanted to reference this data by asset name.
So when I want something from R, I must pass a numerical vector but also the information which asset I want to work with.

I could have done it like this:

1. Step: Pass the asset string with Rx()
2. Step: Pass the numerical data with Rv()

As the R-Bridge is painfully slow, I wanted to reduce this into one single call. This is my solution:

Code
#ifndef <r.h>
	#include <r.h>
#endif

int maxstr2int = 0; //

//works just like stridx() but pushes the strings to the R session as well:
int str2int(string mystring)
{
	mystring = strx(mystring,"/",""); //I think getting rid of "/" is always a good idea
	int index = stridx(mystring)+1; //+1 because the first element in a vector in R has index 1, while in C it has index 0
	
	if(maxstr2int == 0) Rx("Zstrings <- c()"); //create Zstrings Vector in R (it doesn't exist yet if maxstr2int == 0)
	
	if(index > maxstr2int)
	{
		maxstr2int = index;
		string Rstring = strf("Zstrings[%d] <- '%s'", index, mystring);
		Rx(Rstring);
	}
	return index-1; //By subtracting 1, we get the same return value as with stridx() 
}


function run() 
{
	if(!Rstart("", 3))
	{
		print(TO_ANY, "\nError - could not start R session!");
		quit();
	}
	
	while(asset(loop("AUD/JPY","EUR/GBP","GBP/CHF","USD/CHF"))) //just some assets as strings. This will work with any sting (up to 1000 characters)
	{
		int index = str2int(Asset);
		//Access the Strings in R:
		string Rstring = strf("print(paste('String in R:', Zstrings[%d]))", str2int(Asset)+1); //Error when directly passed to Rx()
		printf("\nString in C: %s - Below should be the same:", strxid(str2int(Asset)));
		Rx(Rstring, 3);
		wait(5000); //Output way out of sync, if we continue too fast. You should really use DebugView when you code hybrid scripts with R
	}
}

Last edited by Smon; 07/17/21 19:03.
Re: Strings from Zorro to R [Re: Smon] #483863
08/06/21 13:45
08/06/21 13:45
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline
Member
Grant  Offline
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
I did something similar using the sprintf function. My main motivation wasn't the code speed, but the fact that I run multi-currency models, hence the PairID variable.


Code
TmpC[256];

sprintf(TmpC, "PredictionsLong <- predict(ModelLong%i%s", PairID, ", ModelCase[1:1,])")
Rx(TmpC, 0);

Re: Strings from Zorro to R [Re: Smon] #485307
02/21/22 19:08
02/21/22 19:08
Joined: Dec 2021
Posts: 7
B
budfox Offline
Newbie
budfox  Offline
Newbie
B

Joined: Dec 2021
Posts: 7
Hey Smon,

thank you for the helpful post!

I'm currently trying to read strings from R into Zorro. I tried using Rd and Rv but I'm just not getting it.

I was wondering if you could share a simple example or give me a hint? I included my current status below...

Code

#include <r.h>

typedef struct {
	string Asset1;
	string Asset2;
} Pairs;


function run() 
{
	set(PLOTNOW);
	
	StartDate = 2015;
	EndDate = 20190101;
	BarPeriod = 5 * 1440;
	
	static Pairs Pair1;
	
// ---------------------------------------
// Startup and data loading
// ---------------------------------------  
	if(is(INITRUN)) 
	{
		printf("Script is running!\n");
		// start R
		if(!Rstart("Test.R", 2)) 
		{
			print("Error - can't start R session!");
			quit();
		}
	}
	
	Rx("testnames <- testfunction()", 2);

	Pair1.Asset1 = Rd("testnamen[[1]]"); // Doens't work due to Pointer Error - Can't figure it out
        Rv("testnamen", Pair1.Asset1, 2); // No Error but no value in Pair.Asset1

	printf("Test Wert 1: %s\n", Pair1.Asset1);

	watch("!----\n");
}

R Code:

testfunction <- function() {
  return(c("Asset", "Name"))
}



I was trying to store assetnames into a struct containing strings.

Thanks for you help!

Re: Strings from Zorro to R [Re: Smon] #485312
02/22/22 17:32
02/22/22 17:32
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Online
Serious User
AndrewAMD  Online
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Three things:

1) In Zorro, "string" is only a typedef of char*. Do you know how pointers work?
2) Rd is for retrieving a double, yet you're casting a double to a char*. How did you expect this to work?
3) Rv is for retrieving an array of doubles into a user-supplied array of doubles. That's not remotely what you seem to be wanting to do.

Read this manual page for details on how the functions work:
https://zorro-project.com/manual/en/rbridge.htm

For asset names, you might as well set up key-value pairs on the R side and retrieve assets names by index lookup on the Zorro side.

See also this post.

Re: Strings from Zorro to R [Re: Smon] #485321
02/23/22 17:28
02/23/22 17:28
Joined: Dec 2021
Posts: 7
B
budfox Offline
Newbie
budfox  Offline
Newbie
B

Joined: Dec 2021
Posts: 7
Hey AndrewAMD,

thank you very much for the quick and detailed answer!

I know how pointers work, but I have problems from time to time to insert them correctly.

Also, I will definitely take another look at the manual! The devil is in the details...

As for your suggestion, that sounds like an even more practical solution to me, thanks for the tip!


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1