How to translate ValueWhen from AFL to DLL by C++?

I'm converting an indicator from AFL to DLL by C++ but having difficulty converting function ValueWhen().

AmiVar avd     = gSite.AllocArrayResult(); 	avd.type = VAR_ARRAY;
AmiVar avdTrue = gSite.AllocArrayResult(); 	avdTrue.type = VAR_ARRAY;
AmiVar avnTrue = gSite.AllocArrayResult(); 	avnTrue.type = VAR_ARRAY;

for (i = 0; i < sa2->Size; i++)
{
	avd.array[i] = 0;
	avdTrue.array[i] = 0;
	if (i > 0)
	{
		if (Close[i] > res.array[i - 1])
		{
			avd.array[i] = 1;
			avdTrue.array[i] = 1;
		}
		else if (Close[i] < sup.array[i - 1])
		{
			avd.array[i] = -1;
			avdTrue.array[i] = 1;
		}
	}
}
		
arg5[0].type = VAR_ARRAY;	arg5[0].array = avdTrue.array;
arg5[1].type = VAR_ARRAY;	arg5[1].array = avd.array;
arg5[2].type = VAR_FLOAT;	arg5[2].val = 1;
AmiVar avn = gSite.CallFunction("ValueWhen", 3, arg5);

result.type = VAR_ARRAY;
result.array = avn .array;

The result from the DLL is different from those in AFL.

Please help me how to use the function ValueWhen () in C++.

AmiVar avn = gSite.CallFunction("ValueWhen", 3, arg5);

Just looking at that line of code,

it appears that the arguments don't seem to be in the order as defined
https://www.amibroker.com/guide/afl/valuewhen.html

ValueWhen(EXPRESSION, ARRAY, n = 1)

Is 3 the expression? it should be something that returns a bool in the Array passed to it.

in place of 3, see something that evaluates to True/False as bool when matched in the Array instead.

PS: I'm not proficient in DLL by C++

1 Like

Thank you for your reply!

I checked again and my code above is not wrong. But due to the bottom that code have another code has changed avn

The wrong code has changed avn

if (avn.array[i] = 1) //Wrong because use =
{
}

I thought so, i'd checked the CallFunction() definition, it requires the argument count for the entire Array passed to it in Type & Value format.

glad it works.

2 Likes