I'm trying to create a variable position that has a max constraint of n securities in the portfolio at a time. Let's say a max of 5 positions at the time, which can be randomly selected after keeping the previous positions. The selection criteria is when escore == 5, I have a buy signal. Otherwise, it's a sell signal. Below is a reproduced data example.
If there is a sell signal on 2 of these securities in the next period, then the "position" variable becomes 0 and randomly add 2 other securities out of the qualified basket (when escore == 5) for that period. v1 indicates the names of securities.
Here are the details of the output. You might have to change k (the number of securities) to 100 or more to have more securities to select from. My original dataset has 1000 to 3000 securities. I'm try to create a dummy variable called "position". My initial trade is in Jan 2018, with 4 securities with buy signals (d, f, i, and j). So the position column = 1 for v1=(d, f, i, j) and 0 for the remaining securities.
Next period, Feb 2018, d, f, i has a sell signal, so I sell them, while j still has a buy signal, so keeping j. I will have to turn around and buy 3 other securities that have the buy signal. In Feb 2018, I have 5 securities (a, l, o, q, s) with a buy signal but can only buy 3 of them. I'll select the first 3 securities (alphabetically) with the buy signal, so the "position" column will be 1 for j (as in Jan 2018), and 1 for the first 3 of 5 securities with a buy signal, which are (a, l, o). The position should be 0 for q, and s. I'll continue to fill the position column for the remaining period with the same rule. Does that make sense?