rankpos sell rule help

Is there a way to write a sell rule that takes into account the rankpos at which a stock was bought?

Let’s say you have a 15-stock sim that buys the top 15 stocks and you want to sell when a stock decreases in rank position by, say, 20. So a stock that is bought at rankpos = 5 would sell at rankpos > 25 and a stock bought at rankpos = 15 would sell at rankpos > 35. Is there a way to write a sell rule accordingly? I know from transaction records that the sim remembers the rank at which the stock was bought, but I don’t know how to get it to recall that rank during the simulation.

Thanks,

  • Yuval

I believe the closest you can get is RankPrev(). I’ve tried to use some combination of NoBars and RankPrev() to do what you’re describing, but there appears to be a bug where RankPrev will not accept a variable as a parameter.

You would think just calculate the number of weeks using nobars / 5 and pass that in. But if you try that you get the error “Invalid parameters for RankPrev()”

The best I’ve been able to do is this:

Rank < RankPrev(1) * .9

At least this will detect a sudden drop in rank since a week ago. I’ve found this to be somewhat useful.

Yuval,
I do not have a solution.

Darcy,
Could you do most of what you want with the following set of sell rules (or similar rules specific to your port)?

Sell rule 1: Eval(NoBars/5 = 1, .9 *RankPrev(1) > Rank, 0)

Sell rule 2: Eval(NoBars/10 = 2, .9 * RankPrev(2) > Rank, 0) …

Sell Rule 5: Eval(NoBars/25 = 5, .9 * RankPrev(5) > Rank, 0)

RankPrev can only be used 5 times as you probably know.

-Jim

Thanks. Too bad.

Here’s my IDEAL simulation, which I presume is absolutely impossible with current rules. With a 20-stock portfolio, always buy all stocks with rankpos <= 10, and sell a sufficient number of stocks–those ranked the lowest–in order to do so. In other words, if three new stocks come into the top ten, sell the worst-ranked three stocks in order to buy them, no matter what their current rank is. Is there a sell rule that can do this?

If not, then the problem with simulations as designed is that they place more emphasis on the sell point than on the buy point because you can only buy a top-ranked stock once another has been sold. It seems to me that we should have the option to make the emphasis be on the buy point because entry points are extremely important. In particular, the sooner you get in after either a) a decent earnings release or b) a price drop caused by an overreaction to bad news, the better.

By the way, I apologize for posting this in the wrong forum by mistake.

Ah, that’s a good solution, I’m going to try that!

Delete/edit: Yuval, what I proposed (deleted) sort of worked but was not what you are looking for.

That might be a good feature request!
-Jim

If FHist could accept the second parameter as constant…

SetVar(@weeksago,NoBars/5) AND Rankpos > 20 + FHist("RankPos", @weeksago)

…but unfortunately it doesn’t :frowning:

Perhaps this would be an easy one to implement for Marco and the team?

I figured out how I could simulate a system that will always buy the top ten stocks and hold them until 20 stocks are held. But it’s complicated and only approximate.

First, run a bunch of sims using a fixed amount rather than a % of the total, and make sure your fixed amount is no more than 1/20th of your total. The buy rule will always be rankpos <= 10. The sell rule will be rankpos > X. Each time you run it, look at the number of holdings on a bunch of random dates. When they average 20, then you’ve set your X.

Then take the realized transactions and download the Excel file. Divide the pct return of each stock by 20 and add 1. Then take the product of all those, raise it to the power of 20*365/sum(days), and subtract 1. That’ll get you the CAGR.

Then you can run a regular sim of 20 positions with no buy rule and a sell rule of, say, rankpos > 32 or whatever, and see which sim gets a higher CAGR. I haven’t done this yet, and the answer will probably vary depending on the ranking system . . .

Can be done.
Set to 20 stocks (5% position)
Only sell rule: Rank < 101.
Buy rule: RankPos <= 10 or NoBars > 5 (for weekly rebalance).
Allow sold positions to be repurchased on current rebalance.

You will have to deal with buy/sell difference.

Should work but using iPhone and cannot test. It should constantly hold the top 10 ranked stocks and the highest ranked stocks that were purchased while their RankPos was <= 10. With a total of 20 stocks.

It will start with just 10 stocks but grow to 20 stocks.

-Jim

You can’t have nobars in a buy rule.

Whoa! Thanks.

I like your idea.

Does this work? I’m not sure it’s accomplishing what Yuval wants. I also want something very similar to this.

My ideal scenario is for the simulation to always buy the top 10 or so stocks replacing the the lowest ranked ones. The problem is, you can’t buy until you’ve sold and often you get a bunch of stocks that languishes in the bottom ranks but doesn’t quite meet the sell rule.

Is there anyway to accomplish this?

Yes, with formula weight rebalancing you can come close.

Set your rebalance formula as follows: Eval(rankpos < 50 and rankpos > 10, 50 - rankpos, eval(rankpos <=10, 50, 0)). This will give a stock ranked between 1 and 10 50 points, a stock ranked at 20 30 points, a stock ranked at 45 5 points, and will sell any stocks ranked over 50.

Then set your buy rule as rankpos <= 10 and no sell rule.

Of course you can use numbers greater than 50 if you want to keep more stocks. And make sure to set your maximum position drift and minimum rebalance transactions high enough to avoid too much minor rebalancing when rankpos changes are minor.

  • Yuval

The problem is that rankpos applies to the Universe. If you have buy rules in your screen or sim that filter out companies from the universe, like mediandailytot>500000, some of those rankpos will be skipped and then you are SOL. To effectively use rankpos, you can really only use ranking to pick stocks and there are problems with that approach. I have asked previously for a function like FOrder(“Rank”,#Portfolio) that would only order the stocks held already. Then you could have a buy rule that says rank>portfoliorank.

You can effectively sidestep much of the problem by pre-screening in a custom universe. It’s not perfect, but most things can be moved in there, allowing for better use of rank based buying, selling and position sizing.

I put all my buy rules into my universe rules. I suppose there may be a few buy rules that won’t work there, but most of them will.

I would love to put all my buy rules in the universe but here is my problem: After passing all my buy rules, I am left with, say, 150 stocks. I want to have a twenty stock portfolio. If I just rank the 150 stocks I don’t feel that I have a big enough sample to compare against with my ranking factors, especially if I am comparing certain factors to the industry. How do you deal with that problem?

Richard, I’m no pro, and you likely have much more experience at this than I do, but I’ll share my learnings It may not fit your situation, but I think it might add a bit to the conversation.

I try to express buy rules in the ranking system. Instead of making each buy rule a disqualifying event, it can instead be changed into a ranking event. So instead of eliminating a company from consideration for a single factor, I try to rate it based on that factor so that it gets sorted lower or higher, but not completely eliminated.

Piotroski’s F Score is a good example of this - of using binary 0-1 scoring on 9 criteria. Failing 1 test doesn’t eliminate a stock, but it scores that stock lower. That way bad results in areas important to you get considered, but but no single factor excludes a stock from consideration. The ranking system becomes just a more graduated version of the piotroski score approach with full range of percentile ranks and weights on more factors.

I tend to get better results with few rules (or no rules) inside the screen, and instead try to put almost all of my buying criteria into the ranking system. It seems companies that score well on most of the rules in some weighted way seem to do (far) better than companies that strictly pass all threshold rules. For me it’s very tough to build a screen that outperforms a ranking system. I’ll go so far as to say most rules I add to a screen tend to reduce performance, so I try not to do it.

In a screen when I’m testing, I

  • attach the universe (pre-filtered to exclude things like markets, liquidity, and technical things like “IssueChange(8) = 0 and DaysLate <=5 and DaysFromMergerAnn = NA”;
    I’m sure others have better criteria, it’s just what I have), and
  • attach the ranking system,
  • and generally I test a screen with a single rule like “Rank > 95”.

I make all the changes in selection criteria in the ranking system.

Recently because of tax changes I’ve added a screen rule to filter out companies that are seeing eps/margin surge due to 1-time really low tax rate, but once we’re past that I’ll probably remove that. I have not incorporated that into the ranking system. Not sure how to deal with it inside ranking as it’s a one-time type tax thing.

None of this may apply to your situation, but hope some of this may be helpful.

Yeah, I do the same.

You’re better off ranking the 150 stocks in your universe than using a ranking system that’s built for stocks that aren’t in your universe.

Here’s an example. Let’s say your buy rules exclude all stocks except for two. And let’s say your ranking system includes three equally weighted factors: an accruals factor, measured as the ratio of net income minus operating cash flow to total assets, with lower numbers better; return on assets; and p/e.

Now in your two-stock universe are company A, with net income of $5 billion and operating cash flow of $15 billion, and company B, with net income of $15 billion and operating cash flow of $5 billion. Both companies have total assets of $30 billion, and both have a market cap of $150 billion. So company B will rank higher than company A since it has a better ROA and a lower P/E, even though it has a worse accrual ratio.

However, in a larger universe, using the same ranking system, the ranking may be completely different. If company A’s accrual ratio gives it a rank of 80 and its ROA rank and P/E rank are both 50, it’ll get a combined rank of 60; if company B’s accrual ratio gives it a rank of 10 and its ROA rank is 80 and its PE rank is 60, its combined rank will be 50, and company A will get a higher rank.

In other words, by putting your buy rules into your simulation rather than into your universe, you’re opening yourself up to a lot of factors that you very well may not want to consider, and your carefully designed system will not be ranking the way you wanted it to.

If you’re interested in comparing your stocks to those in the same industry, you can always incorporate industry averages into your factors.