[amibroker] Nested Loop Question (all possible combinations of portfolio weights)

 

Hello,
the following code lists all possible combinations of portfolio weights for a given number of stocks to the interpretation window.

//code for 2 stocks in Portfolio
number = 0;
for( w1 = 0; w1 <= 100 ; w1++ )
{
for( w2 = 0; w2 <= 100 ; w2++ )
{
//only wsum = 100
wsum = w1 + w2 ;
if (wsum == 100)
{
n = 1;
number = number + n;
VarSettext ( "weight_comb_"+number, NumToStr(w1,1) + "," + NumToStr(w2,1) );
printf (NumToStr(number,1) + " | " + VarGetText("weight_comb_"+number) + "\n" );
}
}
}


//code for 3 stocks in Portfolio
number = 0;
for( w1 = 0; w1 <= 100 ; w1++ )
{
for( w2 = 0; w2 <= 100 ; w2++ )
{
for( w3 = 0; w3 <= 100 ; w3++ )
{
//only wsum = 100
wsum = w1 + w2 + w3;
if (wsum == 100)
{
n = 1;
number = number + n;
VarSettext ( "weight_comb_"+number, NumToStr(w1,1) + "," + NumToStr(w2,1) + "," + NumToStr(w3,1) );
printf (NumToStr(number,1) + " | " + VarGetText("weight_comb_"+number) + "\n" );
}
}
}
}

My Question now is, how can I convert this into a dynamical code depending on the number of stocks. I was thinking of something like:

number_of_stocks = 4;
for( $i = 1; $i <= number_of_stocks ; $i++ )
{
for( w$i = 0; w$i <= 100 ; w$i++ ) // as number of for-statements depends on the number of stocks in portfolio
{...


However my Afl-Know-How is not good enough to finish this. Maybe someone can give a hint?


__._,_.___

Posted by: stan_12@ymail.com
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)

Upgrade your account with the latest Yahoo Mail app
Get organized with the fast and easy-to-use Yahoo Mail app. Upgrade today!

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/


.

__,_._,___

Related Posts


EmoticonEmoticon

:)
:(
=(
^_^
:D
=D
=)D
|o|
@@,
;)
:-bd
:-d
:p
:ng
:lv