Longterm technical scans needed

Hello P123 Development!

We had a 3-sigma even in the stock market this week, and that spurred on this request.

I often use P123 for technical scans.

I needed a scan to answer this:
How many times has a stock been below its 3SD Bollinger Band?

Constructing this is very straightforward:
LoopSum("BBLower(20,3,CTR) > Close(CTR)",100)

Only 100 days?

I need to run this for at least 2 years, ideally 5.

I hope you'll open up the loop limits so we can test for technical conditions like these.

If I'm missing something and you have another method, I'm always ready for suggestions.

-Dave

Try this
image

It should take the 100 day loopsum. And then repeat it every 20 weeks (or 100 days). And it does so 10x. So 200 weeks of loopsums

I had to add a picture because if I add text it removes some of the characters here. Just double check that this works. I think it should but test it first please.

Here is a public screen for easier copying of formulas
https://www.portfolio123.com/app/screen/summary/310016?mt=1

1 Like

Thank you for the wonderful surprise this morning. I'm working with it now.
Thank you for the clever approach. This is such a flexible/powerful language, and I need to learn it more broadly.
Cheers!

1 Like

Clever! One caution, BBLower uses bars while FHistSum uses weekly spans. Because of holidays, one hundred trading bars can span longer than 20 calendar weeks. I think I have that right.

Try shortening the covered time frame if you get unusual counts.

ShowVar(@4B,fhistsum(`loopsum("BBLower(20,3,CTR) > Close(CTR)",5,0,1)`,104,1))
1 Like

Thanks for the tip, Walter. I have been testing it, and having it report the test days and starting date (AsOfDate).

So far the date math is working out perfectly when I compare using google sheets date math (networkdays).

Cheers!

Kurtis - use [code] and [/code] before and after your phrase. Then you won't have to add a picture.

2 Likes

It's an easy fix. We'll support longer offsets (1000?) in BBLower/BBupper

That's fantastic, Marco. Much appreciated!!!!
As close to 5 years (1300) as you can handle.
That corresponds with the "Fundamental Charts" Section which also renders BB's.
Cheers!
Viva Italia1

Hi Marco - In a similar vein, I am trying to develop a ranking system using Linear Regression slope and in my work prior to joining P123, I found that periods of 125 and even 250 were most effective for longer-term investment strategies. However, the limit on LinReg is just 100 days. Can we also add a zero to that limit and push it to 1,000 ? Thank you much in advance!

Hi Chawasri,

Check out this post: LinRegXY iterations[, start, increment]) - #3 by marco. It might provide a fix for you, at least for now.

Best,

Victor

Thank you Victor. However, I don't believe that will work for my needs because I am not using two series. I need to rank the Linear Regression slope on each stock in the universe. I don't understand why there are such short-term limits on many technical measures, but perhaps there's a reason.

Again, I appeal to Marco to PLEASE increase the limit on LinReg to 1,000 or higher from the current 100-day limit. Linear Regression is a critical component in my investment approach. Thank you.

LinReg has to be adjusted to the factor that you're using. If, for example, you're using Sales(Ctr,Qtr), for 250 bars you'd want to use only 4 iterations. If you're doing a linear regression on a stock's or ETF's price, for 250 bars you'd use LinReg("Close_W(Ctr)",50). Maybe there's something else that you're doing that requires more than 100 iterations, but for most things it should be sufficient. Another thing: LinReg is equivalent to LinRegXY with the X formula being simply iterations-Ctr. So you can get around the fact that LinReg doesn't have the increment parameter by using LinRegXY("50-Ctr","your formula(Ctr)",50,0,5) if Ctr pertains to something daily.

Hi Yuval,

If you have access to my account, please have a look at Ranking System ID 514231. I have previously found 125 days and 250 days to produce the optimum exponential linear regression slopes, depending on the universe being ranked.

Marco earlier said it would be an "easy fix" to extend the limit on Bollinger Bands to 1000 from 100 days. I assume it would also be easy to add a zero to the limit on LinReg. Please let me know if I am misguided in that assumption. Thank you.

I can't speak to whether Marco will extend the limit on LinReg. But I can affirm that LinReg("Close(CTR)",100) is pretty close to LinReg("Close_W(Ctr)",20), so a good approximation of LinReg("Close(Ctr)",250) would be LinReg("Close_W(Ctr)",50). (If you're using RegGr% with that, you'd want to divide the parameter by 5 as well.)

Thank you, Yuval. I am trying your suggestions now. I'll follow up with results.

Reporting as I promised, I created several new ranking systems that are the supposed weekly equivalent of the daily version of exponential linear regression slope. For example, without getting into the entire formula, the critical part converted the following:

LinReg("Close(CTR)",100)
to
LinReg("Close_W(CTR)",20)

I found:
• Ranking of the top 10 stocks or ETFs showed only a 60% position equivalence and in significantly different orders

• There is a 6% difference on the top quantile of return performance between the two RS

• Perhaps most significantly, the performance of a simulation switching between the two ranking systems showed a -15% decline in total return when using the weekly version of the 100-day linear regression

Logically, 20 weeks should be equivalent to 100 days, but these results show that for some reason they are not the same.

Again, I simply request that another zero be added in a line of P123 code that limits the maximum periods in Linear Regression to enable more investment-oriented (vs short-term trading-oriented) use of this (for me) critical performance measure. Thank you.