replacement only sell rule

Is there a way to create a sell rule in a simulation that will trigger the sale of a stock only if:

  1. the stock is the lowest ranked stock currently in the portfolio
  2. there exists a new stock (not already in the portfolio) with a higher rank that would pass all buy rules

If multiple stocks in the portfolio would meet these conditions independently they should all be sold (i.e. three new stocks satisfy buy rules so sell the three lowest ranked stocks in the portfolio to make room for them).

The idea would be to slowly build up a portfolio to a maximum number of positions and only sell stocks when there is a viable, more desirable replacement available. This would work well with a buy and hold strategy that is very picky about what it buys but is willing to hold on to existing positions as long as possible.

I think rankpos > 1 with daily rebalance that allows buying back stocks just sold should allow you to throw the lowest ranked stocks and replace with better ones if available

I think you’re overthinking it; you can do this with a single and simple sell rule.
RankPos >x, where x is the number of stocks in your portfolio

However, you’ll be trading weekly most likely due to movements with such a strict sell rule

Thanks mmasand. But this isn’t what I need. It will cause stocks that no longer satisfy the buy rules to be sold immediately even if there is no replacement to take their place in the portfolio.

What I need is to sell only if a new stock will be bought to replace the one I am selling in the same period but to NOT sell if there is no stock that would be bought to replace it.

Thanks nisser. Does that imply that RankPos counts stocks you currently own plus stocks that would be purchased by the buy rules? If so that should work perfectly. But keep in mind I don’t want to sell unless there is a replacement that would pass all of the buy rules (not just rank, for example maybe rank is good but the sector is already overweighted so a buy rule would prevent it from being bought). Otherwise I’d sell the stock and not buy a replacement, which is not what I want.

If you can move the buy rules into the universe rules you can approximate this.

If you can move the rules, then you would make sure that Force Positions into Universe is set to “Yes.”

So, even if the buy rules are not longer met for a stock you now hold it will remain in the universe and be rebought, I think. It will be repurchased whether the buy rule that WAS in the sim (as apposed to the universe rules) still applies or not.

You would then use Mmasand’s idea.

This should solve the potential problem that you could (with Mmasand’s idea) buy a lower ranking stock because it meets the buy rules while you sell a higher ranking stock (that you already hold) simply because the buy rules no longer apply to the held stock.

I have not tried this but I do not THINK the stock would be removed from the universe before it was repurchased. I think P123 selects the universe early and does not change the universe later (but I could be wrong). And of course, this depends on whether you can move the buy rules to the universe.

Caveat: this will affect the relative ranks (because the buy rules are changing the universe that is being ranked) but often it will not do so in an unwanted manner. Sometimes the relative ranks are better—often with little understanding of exactly why the relative ranking has changed.

Probably will not help but this is my best idea at the time.

-Jim

Aaron and I discussed this for a while. Jrinne’s solution is the best that we came up with, with the addition of setting Allow Immediate Repurchase to Yes. Then you can just set your sell rules to True (to always sell) and take the net transactions.

We had some theoretical ways around it, but they’re so much work relative to Jrinne’s solution and accepting the adjustment transactions that it doesn’t seem worth it.

If you want to play around with them, I have, in the past, helped someone set up a custom formula that encompasses all of the buy rules into a single formula. Basically, it would be a series of custom formulas that feeds into a single custom formula that would resolve to either true or false. It would be true if all of the buy rules are true – meaning that the given stock would pass the buy rules – or false if any were false. It can be used in a sell rule to evaluate whether a stock would pass the buy rules now.

It looks like this:

$BuyRule1

MktCap>2000

$BuyRule2

ROE%TTM>5

$BuyRules

$BuyRule1*$BuyRule2

A quirk of the system is that a true inequality resolves to one and a false inequality resolves to zero. So $BuyRules is going to be true only when both $BuyRule1 and $BuyRule2 are true.

The other idea is that the Portfolio function might help. It will be true for any given security that is in the Portfolio, and can be used to evaluate in the Portfolio itself. It is worthless in a sell rule – the sell rules only evaluate for stocks in the portfolio anyway – but it might be handy in a buy rule.

My initial thought was something like:

Portfolio(blah blah) and RankPos<10

as a sell rule, but that is exactly identical to RankPos<10, so there’s no point to it.

The offshoot was that we couldn’t come up with a more precise and practical solution that is better than just moving whatever buy rules that you can to a universe, and then setting both Allow Immediate Repurchase and Force Positions Into Universe to yes.

Oh, and I think, separately, that I should emphasize that the portfolio algorithm attempts to be fully invested at all times. The problem of “there’s no other stock to invest in” is rare. So rare, in fact, that if you’re seeing it, I would consider how restrictive your rules are and maybe ease up a little.

I’m trying something a little out of the ordinary so I’d like to provide an explanation of why. I know this isn’t how quantitative investing normally works but it’s an attempt to automate what a human investor might do.

I am simulating a buy and hold investor who maintains at most N stocks in a portfolio. He does not rebalance and refresh his holdings very often. When a stock looks very good to the investor he sells an existing holding to make room for the new stock. If a company has a real problem he sells it but otherwise he holds onto whatever he owns until something better comes along.

To simulate this so far I’ve created a portfolio using formula weights and enabled force readjustment of weights to remain 100% invested. I’ve set up very selective buy rules which generally only have a few selections available at any given time. I set the ideal number of positions to a large number (40-50) so there is plenty of room to absorb new holdings without having to sell.

This works reasonably well for a while. The portfolio starts out fully invested in just a few stocks and gradually increases the number of holdings over time. Occasionally stocks are sold by the sell rules but mostly they remain in the portfolio. Stocks tend to do well over time because the buy rules generally pick good companies at depressed prices.

The problem hits when the ideal portfolio size has been reached. Once that happens no new companies can be purchased unless existing ones do very poorly (i.e. Rank drops below 25).

That’s the reason for my question. I’m grateful for all of the answers received so far. I was hoping there might be a simple solution that I just hadn’t found in the help files.

Thanks Jrinne. I like the idea of using a universe but can you really put the buy rules into there? For example I have buy rules like Rank > 75 and Rating("SBI Quality") > 80

I don’t think those can work in a Universe. My Ranking Systems is a composite with 3 parent nodes and 2-3 children per node. I’d hate to have to try and convert that to a formula. Is there some way to reference a Ranking System from within a Universe? Maybe using Frank()? If I could do that then this might work perfectly.

If we had a function that gave the number of current positions in a different specified port, then Brian’s desired action might be doable with a two port process. Let’s call the proposed function HoldCount(“port name or id”) for example purposes.

Example: Ports A and B use the same universe and ranking system. Port A gets rebalanced before Port B. Port A uses the desired Buy rules and RankPos <= n, where n is the maximum desired rank of new positions to be held. Then Port A might hold fewer than n stocks if the top ranked n don’t all pass the other buy rules. Port A has a Sell rule of True and does not force existing positions into the universe.

Port B is the traded port. It has the same Buy rules and “Force existing positions into universe” set to yes, which allows it to hold onto a position longer. Port B would sell if (RankPos = NA OR RankPos > n) and HoldCount(“Port A”) = n, meaning the position is no longer in the top n candidates that pass the buy rules. Note: This assumes that the RankPos of a stock which is forced into the universe is NA, which I haven’t tested.

Any problems foreseen with this, if the function existed?

Edit: A comparison of the number of holdings in each of the ports might be preferred, such that the count of holdings in Port A are >= the current count in Port B, which is already available via PosCnt.

One thing to consider, I’d say, is that Rank<25 happens far after the company has deteriorated. Part of that is due to nulls and NAs Negative, where the bottom of a ranking system is more meaningless than negative, but it basically means that EVERYTHING had to go to pot. You probably want to be out well before that.

Also, if you’re demanding that the rating position is in the top 10 when you buy it and that you’re using, for illustrative purposes, the S&P 500 as your universe, then a rating position of 50 – the 90th percentile – is about five times worse than when you bought it.

In other words, I think that you’re waiting for the analysts to give you a Strong Sell when we all know that Hold means sell. Try using a higher Rank sell criterion and see if it helps. Try Rank<50 to start and then work from there. (When making similar decisions, I tend to use Rank<80 or Rank<75 or so, but that’s me.)

Also, you could try something like this:

PosCnt=10 and Rank<60

That would count the number of positions in the portfolio and only then start applying the rank rule only when the portfolio is full (10 positions in this case). Personally, I wouldn’t do that, though.

The more general problem is that I want to be out of a stock at some point: After I hold it for a year, or it makes enough profit, or it loses enough, or it deteriorates, but something is going to be enough for me to push it out of the nest. You might want to consider what that is for you and then start applying a more diverse sell discipline.

Parenthetically, buy and hold isn’t necessarily the best fit on P123. We don’t really do dividend reinvestment without a whole lot of effort that is going to be problematic considering your other requirements.

Thanks Paul. The advice about Rank makes a l ot of sense. In this case the universe and ranking system are designed to work together. All of the criteria used in the ranking system are part of the universe criteria and I have “force into universe” set to No so I wouldn’t expect to see many NA’s in the rank computation.

Your point about dividend reinvestment is a good one. I have never been interesting in DRIP reinvestment so it’s fine with me that P123 doesn’t support that. Rather I prefer to let cash accumulate and use it in new purchases. This appears to work well with Portfolio123. Also the rebalancing done by the formula weight system appears to be using the cash from dividends.

The port I’m working on for this experiment has about 40% turnover so that gives some scope for investing any dividend cash.

BTW - my live ports don’t work the way this one. I’d just like to get this working as a control and to consider how manual strategies compare to automated ones. I.e. if I can simulate how a relatively inactive “normal” person might invest it gives me a better idea how automation can outperform that person. Also from my results so far it seems that something along these lines could be moderately successful with low turnover.

https://www.portfolio123.com/mvnforum/viewthread_thread,10370#56920

This is a feature request I made back in 2017. Vote for it, please, if you still can. I’ve been doing buy-driven rather than sell-driven transactions for two years now (in real life) and I still can’t simulate them in P123. It drives me nuts.

Here are my two cents.

The problem you are trying to solve is difficult.

For example, if I have a stock I no longer want to hold because there are more attractive opportunities elsewhere, the liquidation and transaction costs of the undesired stock at least some impact of my decision regarding how aggressively I want to sell. If the stock is liquid, then it matters less. Additionally, if the stock has been held for a net loss, that also helps me justify the sale for tax purposes. This is easy enough, but the fact that these decisions are interrelated makes things far more complicated (vis-a-vis, the relative attractiveness of competing investments impacts both my eagerness to buy and sell).

These kinds of non-linear and recursive thought processes naturally go through our heads when rebalancing, but are difficult to put into rank-based rules. As a result, we often rely on heuristics versus structured approaches. Yet, the net result is easily stated: real world costs and pressures discourage rebalancing.

In order to get more granular than that, it might be possible to use combinatorics and/or quadratic solvers to solve for global optima. However, these things are not available to most users. The alternative I use (which is proprietary) DOES NOT state the trade-offs in rank-based terms.