Random does not work when IndWeight/IndCount/SecWeight/etc rules are used

Hi p123 team,

I was just re-testing using a buy rule with the random function, e.g. "Random < 0.5", but discovered it was having no effect for some of my strategies. I was able to distill the problem down to the presence of any of the diversification rules. If any of IndWeight, IndCount, SecWeight, etc. are also present in the buy rules, the Random rule doesn't seem to work. Occasionally the first set of trades will differ, but all other trades after that will be exactly the same as the original strategy without the random rule. Ordering of the rules does not seem to matter.

Here's a reproducible test case copied from the Small Cap Focus strategy.

Small Cap Focus with IndWeight <= 20
Small Cap Focus with IndWeight <= 20 and Random < 0.5

Thanks.

What are you trying to do with "Random < 0.5"? I don't understand it. The rule does not pertain to individual stocks.

If you want something that will exclude random stocks, try this: Mod(100*Price,10) < 5. This will look at the last digit of the stock's price and if it's 0 through 4 it'll buy it and if it's 5 through 9 it won't. Different stocks will pass the rule every week.

Another way to make only half the stocks available is to use EvenID or OddID. That gives you a randomly selected half universe that doesn't change much from week to week.

The idea, as described in this post, is to randomize the trading path taken by the strategy, effectively performing a Monte Carlo Simulation, to see the stability of trading results when different subsets of top-ranked stocks are selected each run.

The benefit of using Random (as opposed to some Mod(SomeFunction(Price)) or EvenID/OddID) is that it's trivial to generate any number of N randomized trials. The downside is that strategy runs are non-deterministic, though some may consider that a plus.

It works in the general case, just appears to stop functioning if a portfolio-level Sec/Ind rule is also applied in the buy rules.

Some of the same things you use mod() for. Not sure if feldy has what is considered a new use for that kind of thing or if he is just duplicating what you have done with mod() by using random(). But as he suggests:

He also said:

P123 might consider a random seed for random() so feldy can make it deterministic if he wanted to make it deterministic. As he suggests that is not always a benefit but at least he would have the option. Random seed is something that Sklearn offers as an option with almost all of its algorithms.

Jim

Because the implementation evaluates diversification rules twice, additional functionality was required to ensure formulas like IndWeight < 20 & Random < 0.5 wouldn't see a different value from Random on the second pass. There were a few issues with state management that have been resolved, so it should now be working as intended.

Let me know if anything else seems amiss here.

1 Like

Thanks Aaron. Confirmed that this is now working for me. I appreciate the quick turnaround on the fix.