Multicurrency Currency Strength

Posted By: JJ95

Multicurrency Currency Strength - 09/08/22 20:58

Greetings all,

I would like to ask and consult your opinions. I am a big fan of the currency strength, and I was exposed to a concept, which is when a currency is too strong or to weak, it would likely to reverse and start a new trend (similarly to the RSI).

However, I really need a visual input. It make me feel more confident when I look at the chart and inspect it. Is it possible to display the ccyStrength on to the chart (plotting it, like a sub-window).

Visual Clarification can be found in image attached.
In the image attached, we can see that in a sub-window we see different lines, with different color. Each line belong to a specific currency. Can this be achieved in Zorro?

Sincerely

Attached picture Screenshot_20220909-005443_TradingView.jpg
Posted By: Grant

Re: Multicurrency Currency Strength - 09/09/22 13:17

Yes, you can.

https://zorro-project.com/manual/en/ccy.htm
https://zorro-project.com/manual/en/plot.htm
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/10/22 16:30

Hello,

Thank you so much for your reply! The links were very insightful.

I did notice in the links stating to have a look over the source code if needed. As I was looking over it, I noticed the following:

#define MAXCURRENCIES 32
var CStren[MAXCURRENCIES],CStrenSum[MAXCURRENCIES];
int CNum[MAXCURRENCIES];
char CNames[MAXCURRENCIES*4];
int NumCurrencies = 0;

Max Currencies is defined at 32.

My question is:

Why is it 32 (why did they choose this number specifically)?

In my case for instance, I am interested in trading the following currencies EUR, USD, AUD, CHF, NZD, GBP, JPY (which makes up around 28 currency pair). Am I suppose to change that number?

Once again, thank you so much for your reply earlier, it was very helpful!
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/10/22 17:04

Count it again: You only have seven currencies (it has nothing to do with pairs). Having a max allows the use of fixed buffers. No need to change it, since you are well below the maximum.
Posted By: Grant

Re: Multicurrency Currency Strength - 09/10/22 17:16

I guess (but I don't know for sure) that 32 was chosen due to the 32-bit architecture of Zorro, although they made a transition to 64-bit lately. edit: see Andrew's answer above.

Anyhow, 28 pairs is no problem. Just throw them in a loop. See https://zorro-project.com/manual/en/loop.htm for code examples.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/11/22 16:39

Hello,

I think I understood what you are saying. At the moment, I am trying to understand the codes regarding currency strengths.

#define MAXCURRENCIES 32
var CStren[MAXCURRENCIES],CStrenSum[MAXCURRENCIES];
int CNum[MAXCURRENCIES];
char CNames[MAXCURRENCIES*4];
int NumCurrencies = 0;

Regarding CNames, the size of the array is 32 * 4 = 128.

Does this mean that CNames can hold 128 elements?
If yes, is there any reason why number 4 is chosen?

Thank you for your response earlier, it was much appreciated
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/11/22 18:58

It’s because in the C language, you need a null terminator to end a string. So USD is three characters plus a null terminator, which is 4 total. So you have room for 32 currency names. For that, you need 128 characters to fit them.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/11/22 21:39

Hi,

So, just for clarification. We are working with 32 currencies (not currency pairs), and each currency is an element in an array that has 3 letters plus null terminator, so that is 4 characters total per element (32 × 4 = 128).

Is that correct?
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/11/22 21:46

Yes.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/12/22 15:14

Hello,
I have a small question to ask: Below is a simple code I wrote:

CODE:

function main()
{
for(int i=0; i<10; i++){
printf("%d\n", i);
}
}

When I run the code above, I get: "Error in line 4: syntax error". I cannot find this syntax error

However, when I run the code below, I do not get an error:

function main()
{
int i;
for(i=0; i<10; i++){
printf("%d\n", i);
}
}

To my knowledge both codes are the same. I am studying the ccyReset() in the indicator source code file and they have it written as the first code written above.
Can you please explain to me where is my syntax error?
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/12/22 15:18

The original code uses standard C/C++ and is compiled into the Zorro executable. In standard C/C++, it is legal to declare an int in a for loop initialization, but not in Lite-C.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/12/22 15:33

Hello,

Thank you so much for your earliest reply.

Just out of curiosity, in the manual (https://zorro-project.com/manual/en/tutorial_var.htm), it mentions that we can switch from Lite-C to C++.

Quote:

"Zorro gives you the choice to develop strategy scripts either in lite-C, a 'lightweight' version of the C programming language, or in 'full grown' C++"

Do you know how to switch?
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/12/22 15:43

Yes, instructions are here:
https://zorro-project.com/manual/en/dlls.htm
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/15/22 09:53

Hello,

I am trying to run the code below in order to better understand assigning index to a currency name:

function main()
{
#define MAXCURRENCIES 32
var CStren[MAXCURRENCIES],CStrenSum[MAXCURRENCIES];
int CNum[MAXCURRENCIES];
char CNames[128];
int NumCurrencies = 0;

int ccyIdx(char* name)
{
char Ccy[4];
memcpy(Ccy,name,4);
Ccy[3] = 0;

for(int n=0; n<MAXCURRENCIES; n++)
{
if(!CNames[n*4])
{ // not yet stored
strcpy(CNames+n*4,Ccy);
NumCurrencies++;
return n;
}
if(strstr(CNames+n*4,Ccy))
return n;
}

return 0;
}

}

I am receiving the following error:

Error in 'line 14:
'name' undeclared identifier
memcpy(Ccy,name,4);

I kindly ask for some clarification
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/16/22 13:56

Step 1 is to learn the C language. You cannot declare functions inside another function.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/16/22 16:06

Hello,

Understood. I am trying to better understand C through learning the indicators source codes and applying them in my scripts. I grabbed a little of tips as I progress.

Regarding the assigning an index to a currency name. I can almost see the loop and the logic, yet it remains slightly unclear.

Code:
Code

#define MAXCURRENCIES	32
var CStren[MAXCURRENCIES],CStrenSum[MAXCURRENCIES];
int CNum[MAXCURRENCIES];
char CNames[MAXCURRENCIES*4];
int NumCurrencies = 0;

// assign an index to a currency name
int ccyIdx(char* name)
{
	char Ccy[4];
	memcpy(Ccy,name,4);
	Ccy[3] = 0; 
	for(int n=0; n<MAXCURRENCIES; n++) {
		if(!CNames[n*4]) { // not yet stored
			strcpy(CNames+n*4,Ccy);
			NumCurrencies++;
			return n;
		}
		if(strstr(CNames+n*4,Ccy))
			return n;
	}
	return 0;
}


I do not understand the !CNames[n*4] part.
The ! stands for not. I cannot see or understand the logic. How do we know if it is stored or not.

The way I am reading it at first is, if (CNames[0] is not, then copy the string from Ccy to CNames with a 0 etc)
But what is the logic and where is it written.
If the first element in the CName array is not what exactly?

Thank you so much for the help
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/16/22 17:47

It’s checking for null terminators at the beginning of each string.
Posted By: alun

Re: Multicurrency Currency Strength - 09/18/22 17:08

Think of this value in memory (0 is literal zero not the character for 0)

CNames = "EUR0USD0GBP00000"

n = 3
i = 3*4 = 12

CNames[12] == 0 <- true
!CNames[12] is !0 which is true as well

Posted By: JJ95

Re: Multicurrency Currency Strength - 09/20/22 12:42

Greetings,

I would like to thank everyone who has replied to me (especially AndrewAMD).

I just wanted to ask a small question. In the earlier post it was mentioned that you cannot declare a function inside a function. I am assuming this referred to int ccyIdx(char* name) inside function main(). So how do we run the function ccyIdx(char* name) without function main()?
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/20/22 20:23

Declare and define the functions outside of main and prior to main. Inside main, invoke the functions you defined.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/22/22 17:22

I see what you are saying.

I would like to ask a question reagarding the ccyIdx function found in the indicator source code:

Quote

char CNames[128]; // MAXCURRENCIES * 4
int NumCurrencies = 0;
char* CURRENCY_NAME = "EUR";

ccyIdx(CURRENCY_NAME )


If we placed the function in a live example:

Inside the parenthesis, we are expecting to pass only 1 currency name. If we pass a currency such as EUR, the function will add EUR0 to CNames, then add 1 to NumCurrencies. If we wanted to add another currency, we will have to call the function again and pass another currency. Am I correct?

Thank you so much for your earliest replies
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/22/22 18:10

Let me refer you to the documentation:
https://zorro-project.com/manual/en/ccy.htm

Notice that ccyIdx() is not documented. This is intentional. The script writer is not supposed to access it, since it is a helper function designed for the documented functions to use from Zorro's backend.

Really, the source code for ccyIdx is for your reference only. It's already compiled.

Scroll down to the bottom of the page to see an excellent example of how to use the currency strength functions from the script-side. It loops through your forex assets (pairs like "EUR/USD"), and it generates strength values for all currencies (like "USD" or "EUR"). Then it selects a strongest and weakest pair based on those values. Then it goes long on the strongest pair and short on the weakest pair, given a minimum strength threshold value is crossed.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/22/22 18:39

Understood.

I will skip it and proceed to understanding ccySet function.
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/26/22 14:52

Hello,

I need a small explanation regarding adding strength to currency and subtracting it from counter currency.

Code

void ccySet(var Strength)
{
	if(!isForex(g->asset)) return; 
	int n = ccyIdx(g->asset->sName);
	CStrenSum[n] += Strength; 
	CNum[n]++;
	CStren[n] = CStrenSum[n]/CNum[n];
	g->asset->vStrength = CStren[n];
	
	n = ccyIdx(g->asset->sName+4); // counter currency
	CStrenSum[n] -= Strength; 
	CNum[n]++;
	CStren[n] = CStrenSum[n]/CNum[n];
	g->asset->vStrength -= CStren[n];
}



Regarding the function isForex().
I am assuming it is checking if what is inside the parenthesis is a forex or not. However, I was unable to find it in the trading manual. Can I please receive an explanation about it or reference to it (link) that explains it's process?

Also, I would like to ask about the arrow (->) and how it works in Zorro. For instance, g->asset. To my knowledge we use arrow with pointers, correct? So, g is a pointer to a struct. But that has not been defined, correct? So, how is it operating then?

Thank you for your responses
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/26/22 15:13

Zorro knows whether or not it is forex based on the asset name. "EUR/USD" has seven characters and a slash in the middle.

g is defined in the headers. Pretty much everything in Zorro is defined in the headers.

On structs and pointers:
https://zorro-project.com/manual/en/structs.htm
https://zorro-project.com/manual/en/apointer.htm
Posted By: JJ95

Re: Multicurrency Currency Strength - 09/26/22 15:31

I see.

Where can I see how g is defined?

I apologize for the questions I ask. But I am trying to learn C, I feel that I am progressing.

But I cannot understand the codes, if I cannot see how the codes are written and how are they defined. Part of understanding the codes is to read through the logic, and I have failed to see the logic, because I cannot read it.

Is there any reference available, where can I read the definitions?

Once again, I thank you for your response
Posted By: AndrewAMD

Re: Multicurrency Currency Strength - 09/26/22 15:52

The header files are the ones in the include directory. Do a control-F search on all of them. Notepad++ also has a "find in files" feature which is also useful.

g is a pointer to a GLOBAL struct. That struct is also defined. Many macros are set up in the headers so that you can avoid pointer logic for simple scripts.
© 2024 lite-C Forums