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

Many of the Amibroker functions are/can be written in Amibroker itself. For example ValueWhen
So far I have not implemented code of 3rd parameter
Actually no need to say 1 for 3rd parameter, even if 3rd parameter is omitted it will consider as 1 automatically

_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );

function vwhen( cond, ret, n ) // n is not used and implimented in function
{
    retx = temp = Null;// -1e10;
    for( i =0; i < BarCount; i++ )
    {
        if( cond[i] == 1 )
        {
            retx[i] = ret[i] ;
            TEMP = RET[i];
        }
        else
        {
            retx[i] =  temp;
        }
    }
		return retx; }
Plot(vwhen(C>O,H ,1 ),"\nCustom ",colorBlue); ; // Actually no need to say 1 even if 3rd parameter is omitted it will consider as 1
Plot(valuewhen(C>O,H /*,1*/ ),"Original",colorred); // Actually no need to say 1 even if 3rd parameter is omitted it will consider as 1
Plot(C,"Close",colorBlack,styleCandle);