Loop through all combinations of strings in a string

Hi, I'm having a brain freeze and can't suss this out - any help is appreciated :slight_smile:

I have a csv string:

ee = "A,B,C,D";

As part of wider code, in an optimize loop, I need to go thru all combinations of letters together, eg:

opt loop 1 = A
opt loop 2 = A,B
opt loop 3 = A,B,C
opt loop 4 = A,B,C,D
opt loop 5 = B
opt loop 6 = B,A

eg- but more than the below....

opt = optimize("loop", 0, 0, StrCount(ee, "," ), 1);

Since you have not described the actual problem you're trying to solve nor the constraints of the example, it's difficult to suggest solutions. For example, it would be useful to know:

  1. How many comma-separated elements will be in the string?
  2. Is B,A different than A,B?
  3. You include B,B in your list, but not A,A. Is that intentional?

1 - This changes. That's why I put StrCount(ee, ",") out there

2- B,A is the same as A,B. I'm not worried about duplicates in the loop like this, they are fine to be included

3- Good spot! Typo should be B,A

More specifically, can there be more than 9? If not, you could represent each position (i.e. each element) by a digit 1-9. Then your optimization loop becomes quite straightforward. You could also come up with a bitmapping scheme where each of the string elements is represented by a single bit.

I think on reflection I have got confused with what I am asking for here. And it's probably not do-able, because with 10 symbols in ee, the total combinations would be 10^10!

I will have to scale back the number of symbols in ee and use multiple Optimize loops, one for each symbol - each one on/off.

mradtke - thank you for your help all the same :slight_smile:


The number of combinations is

x = 2^n - 1;

For two symbols it is 3, right?
(Or 1 if you just count "A,B" result)

For 3 symbols it is 7,
(Or 4 if you just count group result)

For 4 symbols it is 15,
(Or 11 if you just count group result)

...So for 10 symbols it is "just" 1023 but not 10^10 (1e10).
(Or 1013 if you just count group result)

ee = "A,B,C,D";
n = StrCount(ee, ",")+1;
number_of_combinations = 2^n-1;

Now I'm more confused, LOL!

To simplify - for "A,B,C":


There are repetitions, this is OK.

(Ie. a way of doing every combination of every letter with every other letter - if the above list is not complete)

And what do you need the repetitions for?

Hi fxshrat, I have a watchlist of 200 stocks and am using a rotational backtest on this watchlist.

I want to see the effect of dropping different symbols from the rotation. hence using an Optimize loop.

ee could be "AAPL,MSFT,NVDA,BIDU" - or even more.

and I want to see the effect of dropping combinations of these stocks from the overall results.

I'd rather do it in code than manually remove each stock from the WL each time, etc...

After some thought I know how to do it programmatically.
And you do not need repetitions like A,B and then B,A.
(The lists -symbol combinations- are created as explained here.)
If you need code then drop mail.

1 Like

Thank you fxshrat. I have emailed your email address, as I cant see how to PM on this site

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