I think this problem can be solved using either itertools or cartesian, but I am pretty new to Python and struggling to use them:
I have a portfolio of 5 stocks, where each stock can have a weight of -0.4, -0.2, 0, 0.2 or 0.4, with a weight summing up to 0. How to create a function that creates a list of all possible combinations of weights. for example [-0,4, 0,2, 0, 0,2, 0] ... etc.
Ideally, the function will work for n stocks, since in the end I want to do the same process for 50 stocks.
edit: To clarify, I search for all combinations of length n (in this case 5), adding up to 0. Values โโcan be repeated : for example: [0.2, 0.2, -0.4, 0, 0], [0,4, 0, -0 , 2, -0,2, 0,4], [0,0,0,0,2,2, -0,2], [0, 0,4, -0,4, 0,2, -0,2 ] etc. 0,0,0,0,0] would be a possible combination. The fact that there are 5 possible weights and 5 reserves is a coincidence (which I should have avoided!). The same question could be with 5 possible weights and 3 reserves or 7 reserves. Thanks.