I am finding below attached error. How to solve it

image

It looks like you have a Prime symbol ' before and after the first parenthesis in each row. You should remove them.

Hi,
you have to add one more parameter, according with user guide:
IIf( EXPRESSION, TRUE_PART, FALSE_PART )

In your case
weight = iif(Name()=="QQQ", 48, 31);

Hi,
you have to use "'," instead ";", according with user guide:
IIf( EXPRESSION, TRUE_PART, FALSE_PART )

In your case

weight = iif(Name()=="DLF", 48,
iif(Name() =="DHFL",40,

.......

Sorry, tihis is incorrect replay

Try this:

Weight = IIf(Name() == " dlf",40,IIf(Name() == "dhfl",40,IIf(Name() == "reliance",10,IIf(Name() == "hdfc",10,0))));

1 Like

Guys, you should definitely read about the differences between IIF and if. In this case, you should use if instead of IIF, but the best solution is using the Switch statement :

https://www.amibroker.com/guide/keyword/switch.html

N = Name();
switch( N )

{
case "DHFL":
    weight = 48;
    break;

case "RELIANCE":
    weight = 10;
    break;
// etc ...
}

@Milosz,

IIf() is OK too as been used by Anthony because it is polymorph function which means it takes arguments being type number or array as well as it returns number or array (depending on function arguments).

I.e.

cond = Name() == "dlf";
printf( "type of condition is: %s\n", typeof(cond));

is type number. And iif() true and false parts of below code are type number also.

So options to use are if, IIf or switch for assigning some number to weight variable.

nm = Name();
Weight = IIf(nm == "dlf", 40,
		 IIf(nm == "dhfl", 40,
		 IIf(nm == "reliance", 10,
		 IIf(nm == "hdfc", 10, 0 ))));


for testing:

nmstr = "dlf,dhfl,reliance,hdfc";

for( i = 0; ( sym = StrExtract( nmstr, i ) ) != ""; i++ )
{
	nm = sym;
	Weight = IIf(nm == "dlf", 40,
			 IIf(nm == "dhfl", 40,
			 IIf(nm == "reliance", 10,
			 IIf(nm == "hdfc", 10, 0 ))));
	//	 
	printf("\nSymbol is %s, weight: %g", nm, weight );
}

183443


Testing the same with if-else

nmstr = "dlf,dhfl,reliance,hdfc";

for( i = 0; ( sym = StrExtract( nmstr, i ) ) != ""; i++ )
{
	nm = sym;
	
	if(nm == "dlf")				Weight = 40;
	else if(nm == "dhfl")		Weight = 40;
	else If(nm == "reliance")	Weight = 10;
	else if(nm == "hdfc")		Weight = 10;
	else						Weight = 0;
	//	 
	printf("\nSymbol is %s, weight: %g", nm, weight  );
}

183443

4 Likes

@fxshrat thank you for pointing my mistake and for the explanation with examples :+1:

1 Like

@kundanbagchi, here is an alternative way to code this kind of things.

Put all the symbols and their weights directly in the comma-separated string that you'll parse, creating a sequence of pairs: symbol01,weight01,symbol02,weight02, .etc.

The main advantage of this method is that it will avoid you any mistakes in the code regarding potentially mispelelled symbols in the conditional statements.

At execution, it is a bit slower since it parses a wider string, and convert strings to numbers.
Anyway, if your list contains more tickers, and you frequently need to add/remove symbols and/or change weights. probably it it will be easier to maintain than the if/else /iif/switch constructs,

nwstr = "dlf,40,dhfl,40,reliance,10,hdfc,10";
for( i = 0; ( nm = StrExtract( nwstr, i ) ) != ""; i += 2 ) // we increment by 2 for a pair, will be 3 for a triplet...
{
	weight = StrToNum(StrExtract( nwstr, i+1 ));
	printf(nm + " weight: %g\n", weight);
}

Naturally, in this way, you can also easily adapt the code if you need additional values associated with each symbol.