Is it possible to use switch case for this?

Hi guys!

On other occasions I've been able to use the switch to execute various combinations of parameters, but they were always numbers.

Example

RankingOptimize = Optimize("RankingOptimize", 3, 0, 31, 1);

switch (RankingOptimize)
{
	case 1: PositionScore = C/MA(C,200) ; break; 		// More stretch away from their MA200
	case 2: PositionScore = 100-C/MA(C,200) ; break;  	//Are closest to their MA200
	case 3: PositionScore = 1000-ROC(C, 21*1); break; 	// With the worst 1 month return
	// etc...
}

But now

I would like to do something similar with these portfolios, but instead of having numbers, I want to be able to select which portfolio I want to display (in parameter section) but also have that ability to run them all like a optimization number, do you know?

The problem is I have a paramlist and I don't know if it possible combine to switch case.

// N portfolios
Portfolio  = Paramlist("Portfolio","01-Larry Portfolio|02-Ivy Portfolio|03-Rick Ferri",0);
//number = StrExtract( Portfolio, 2 ); // extract number

PortfolioBulk = Optimize("PortfolioBulk", 2, 1, 3, 1);

switch(PortfolioBulk)
{
	case 1:
		Cartera="Larry Portfolio";
		if ( Name() == "IEF"  ) PosSize = 70;
		if ( Name() == "IWN"  ) PosSize = 15;
		if ( Name() == "GLD"  ) PosSize = 7.5;
		if ( Name() == "EEM"  ) PosSize = 7.5;
		
	break;
	
	case 2:
		Cartera="Ivy Portfolio";
		if ( Name() == "VTI"  ) PosSize = 20;
		if ( Name() == "VEU"  ) PosSize = 20;
		if ( Name() == "VNQ"  ) PosSize = 20;
		if ( Name() == "AGG"  ) PosSize = 20;
		if ( Name() == "DBC"  ) PosSize = 20;
		
	break;
	
	case 3:
		cartera="Rick Ferri";
		if ( Name() == "VTI"  ) PosSize = 48;
		if ( Name() == "VEU"  ) PosSize = 24;
		if ( Name() == "AGG"  ) PosSize = 20;
		if ( Name() == "VNQ"  ) PosSize = 8;
	break;
}

Do you know if its possible?

I was trying to add a number prefix and extract it but it didn't work.

The idea is to be able to select which portfolio you want to execute from the parameters, but for example have one parameter that executes them all?

I can't think of any way without having to duplicate the entire switch

Thank you very much

If you are just asking how to get the number prefix part as numeric try this:

//number = StrExtract( Portfolio, 2 ); // extract number
number = StrToNum( StrExtract( Portfolio, 0, '-' ) );
1 Like

In addition to what @JohnHT said about converting text to string, it is also possible to index the string list.

Cartera = StrExtract( Portfolio, PortfolioBulk + 1, '|' );

If you want that without firsts 3 characters you can do this:

Cartera = StrMid( StrExtract( Portfolio, PortfolioBulk + 1, '|' ), 3 );

https://www.amibroker.com/f?strextract

1 Like

Thank you @JohnHT and @Tomasz I've tried both, but with this I extract the number but how I can do the optimize at the same time?

I mean, with this the optimize works

// N portfolios
Portfolio  = Paramlist("Portfolio","1-Larry Portfolio|2-Ivy Portfolio|3-Rick Ferri",0);

PortfolioBulk = Optimize("PortfolioBulk", 2, 1, 3, 1);

number = StrToNum( StrExtract( Portfolio, 0, '-' ) );
Cartera = StrExtract( Portfolio, PortfolioBulk + 1, '|' );

switch(PortfolioBulk)
{
	case 1:
		//Cartera="Larry Portfolio";
		if ( Name() == "IEF"  ) PosSize = 70;
		if ( Name() == "IWN"  ) PosSize = 15;
		if ( Name() == "GLD"  ) PosSize = 7.5;
		if ( Name() == "EEM"  ) PosSize = 7.5;
		
	break;
	
	case 2:
		//Cartera="Ivy Portfolio";
		if ( Name() == "VTI"  ) PosSize = 20;
		if ( Name() == "VEU"  ) PosSize = 20;
		if ( Name() == "VNQ"  ) PosSize = 20;
		if ( Name() == "AGG"  ) PosSize = 20;
		if ( Name() == "DBC"  ) PosSize = 20;
		
	break;
	
	case 3:
		//cartera="Rick Ferri";
		if ( Name() == "VTI"  ) PosSize = 48;
		if ( Name() == "VEU"  ) PosSize = 24;
		if ( Name() == "AGG"  ) PosSize = 20;
		if ( Name() == "VNQ"  ) PosSize = 8;
	break;
}

But if I want also be able to execute an specific portfolio, how do I tell it to make it work?

Because if I put this one I can execute one specific but then I can not execute the optimize

switch(number)

The goal I'm looking for is if there is a way to run both, or do I have to create a paramtoggle to select if I want to enable the optimization or not?

Thank you very much

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.