Linear Regression Divergence Scanner

caspara2

New member
Hello! I am in search of a way to scan for stocks in ThinkorSwim that look similar to AEO or DAC on the 1Y:1D chart, with predictable upward movement of price in a nearly straight line over a six months or a year timeframe. The key here is that I do not want stocks that have huge and risky one-day price changes.

In my search online, I did find this site that has their own scan technology and methodology. It apparently uses Linear Regression Divergence min and max defined values to perform the scan. I am just needing something similar using ThinkorSwim's stock scanner, although it does not have to use Linear Regression.

My plan is to combine the above needed study with an existing scan that I use that simply uses six copies of the Price Change study using equally increasing (linear) ratios of percentage change and number of bars going back to around a year. I get very strong, mostly linear trending stocks using this scan, but figured there must be a way to limit the results to consistent and friendlier "straight-line" upward price movement stocks.

Can anyone help?
 
Last edited:
Hello! I am in search of a way to scan for stocks in ThinkorSwim that look similar to BXC on the 1Y:1D chart, with predictable upward movement of price in a nearly straight line over a six months or a year timeframe. The key here is that I do not want stocks that have huge and risky one-day price changes.

In my search online, I did find this site that has their own scan technology and methodology. It apparently uses Linear Regression Divergence min and max defined values to perform the scan. I am just needing something similar using ThinkorSwim's stock scanner, although it does not have to use Linear Regression.

My plan is to combine the above needed study with an existing scan that I use that simply uses six copies of the Price Change study using equally increasing (linear) ratios of percentage change and number of bars going back to around a year. I get very strong, mostly linear trending stocks using the latter study, but figured there must be a way to limit the results to consistent and friendlier "straight-line" upward price movement stocks.

Can anyone help?
this is just an idea. idk. you might want to use rsquare. what rsquare evaluates is how close price is to the trend or how close its following it. so anything over .6 means that its more predictable. so what you can do is set rsquare to maybe 120( 6 months x 20 days) and scan for stocks over .6 rsquare, or even higher.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

this is just an idea. idk. you might want to use rsquare. what rsquare evaluates is how close price is to the trend or how close its following it. so anything over .6 means that its more predictable. so what you can do is set rsquare to maybe 120( 6 months x 20 days) and scan for stocks over .6 rsquare, or even higher.
@germanburrito, that did seem to help. Apparently my existing scan was producing fairly linear results anyway, but when I added the rsquare study with a value of 0.935 over the entire period (approximately half a year), it refined the results further. Many thanks!

The results when using this in combination with my previous scan studies are an average of 0.6-1.5% positive change in average prices per day of the stocks in the scan results over the one-half year time frame, all with a much lesser chance of any stock dropping (bleeding) 5 or more percent in a day within that time frame. Excellent!
 
Last edited:
@germanburrito, that did seem to help. Apparently my existing scan was producing fairly linear results anyway, but when I added the rsquare study with a value of 0.935 over the entire period (approximately half a year), it refined the results further. Many thanks!

The results when using this in combination with my previous scan studies are an average of 0.6-1.5% positive change in average prices per day of the stocks in the scan results over the one-half year time frame, all with a much lesser chance of any stock dropping 5 or more percent in a day within that time frame. Excellent!
thats awesome man!!! what else do you use to scan?
 
Sure! I am a firm believer that your scan is the most important part of any trading strategy. The scans don't lie, but our emotions do! Patience and compounding average daily percentage gains always win in the end.

These TOS scan settings (ALL set to Wk aggregation):
Stock: Volume min 50,000 (5,000 if scanning premarket) to max unlimited
Stock: Last min 1 to max 500
Study: Price_Change CLOSE is at least 80 % greater than 32 bars ago
Study: Price_Change CLOSE is at least 40 % greater than 16 bars ago
Study: Price_Change CLOSE is at least 20 % greater than 8 bars ago
Study: Price_Change CLOSE is at least 10 % greater than 4 bars ago
Study: Price_Change CLOSE is at least 2.5 % greater than 1 bars ago
Study: Custom... RSquared() is greater than or equal to 0.935 within 32 bars <- NOTE: I see now that this should be changed to something like within 3 AND within 17 bars.

I had read about r-squared on a couple math websites and YouTube videos the past couple days, but didn't think to look to see if TOS had this study. Thanks again for the recommendation. It seems to be a good tool to have in the toolbelt.
 
Last edited:
Sure! I am a firm believer that your scan is the most important part of any trading strategy. The scans don't lie, but our emotions do! Patience and compounding average daily percentage gains always win in the end.

These TOS scan settings (ALL set to Wk aggregation):
Stock: Volume min 50,000 (5,000 if scanning premarket) to max unlimited
Stock: Last min 1 to max 500
Study: Price_Change CLOSE is at least 80 % greater than 32 bars ago
Study: Price_Change CLOSE is at least 40 % greater than 16 bars ago
Study: Price_Change CLOSE is at least 20 % greater than 8 bars ago
Study: Price_Change CLOSE is at least 10 % greater than 4 bars ago
Study: Price_Change CLOSE is at least 2.5 % greater than 1 bars ago
Study: Custom... RSquared() is greater than or equal to 0.935 within 32 bars

I had read about r-squared on a couple math websites and YouTube videos the past couple days, but didn't think to look to see if TOS had this study. Thanks again for the recommendation. It seems to be a good tool to have in the toolbelt.
thank you, i believe its fairly importan in statistics, i sorta use it in my trading sttrategy.
 
Sure! I am a firm believer that your scan is the most important part of any trading strategy. The scans don't lie, but our emotions do! Patience and compounding average daily percentage gains always win in the end.

These TOS scan settings (ALL set to Wk aggregation):
Stock: Volume min 50,000 (5,000 if scanning premarket) to max unlimited
Stock: Last min 1 to max 500
Study: Price_Change CLOSE is at least 80 % greater than 32 bars ago
Study: Price_Change CLOSE is at least 40 % greater than 16 bars ago
Study: Price_Change CLOSE is at least 20 % greater than 8 bars ago
Study: Price_Change CLOSE is at least 10 % greater than 4 bars ago
Study: Price_Change CLOSE is at least 2.5 % greater than 1 bars ago
Study: Custom... RSquared() is greater than or equal to 0.935 within 32 bars

I had read about r-squared on a couple math websites and YouTube videos the past couple days, but didn't think to look to see if TOS had this study. Thanks again for the recommendation. It seems to be a good tool to have in the toolbelt.
@germanburrito, that did seem to help. Apparently my existing scan was producing fairly linear results anyway, but when I added the rsquare study with a value of 0.935 over the entire period (approximately half a year), it refined the results further. Many thanks!

The results when using this in combination with my previous scan studies are an average of 0.6-1.5% positive change in average prices per day of the stocks in the scan results over the one-half year time frame, all with a much lesser chance of any stock dropping (bleeding) 5 or more percent in a day within that time frame. Excellent!
https://medium.com/geekculture/usin...ading-signals-a-python-back-test-bfa57222c936
 
Sure! I am a firm believer that your scan is the most important part of any trading strategy. The scans don't lie, but our emotions do! Patience and compounding average daily percentage gains always win in the end.

These TOS scan settings (ALL set to Wk aggregation):
Stock: Volume min 50,000 (5,000 if scanning premarket) to max unlimited
Stock: Last min 1 to max 500
Study: Price_Change CLOSE is at least 80 % greater than 32 bars ago
Study: Price_Change CLOSE is at least 40 % greater than 16 bars ago
Study: Price_Change CLOSE is at least 20 % greater than 8 bars ago
Study: Price_Change CLOSE is at least 10 % greater than 4 bars ago
Study: Price_Change CLOSE is at least 2.5 % greater than 1 bars ago
Study: Custom... RSquared() is greater than or equal to 0.935 within 32 bars <- NOTE: I see now that this should be changed to something like within 3 AND within 17 bars.

I had read about r-squared on a couple math websites and YouTube videos the past couple days, but didn't think to look to see if TOS had this study. Thanks again for the recommendation. It seems to be a good tool to have in the toolbelt.
I have been looking for something that stockbee has on his blog
link is https://stockbee.blogspot.com/2009/07/look-for-linear-trends-when-swing.html
his scan is for TC2000, wondering if a smart person can recode the following to thinkscript please.
copy paste from the link

When swing trading look for linear trends. Buying pullbacks in linear trends is low risk trade. One of the easy way to find linear trends is to use a Fractal efficiency scan in Telechart.
(C - C20) / (ABS(C - C1) + ABS(C1 - C2) + ABS(C2 - C3) + ABS(C3 - C4) + ABS(C4 - C5) + ABS(C5 - C6) + ABS(C6 - C7) + ABS(C7 - C8) + ABS(C8 - C9) + ABS(C9 - C10) + ABS(C10 - C11) + ABS(C11 - C12) + ABS(C12 - C13) + ABS(C13 - C14) + ABS(C14 - C15) + ABS(C15 - C16) + ABS(C16 - C17) + ABS(C17 - C18) + ABS(C18 - C19) + ABS(C19 - C20))

What exactly does this scan does. It is a efficiency ratio.
It divided net price movement (C - C20) (or in other words how much a stock moved in points term in 20 days) by the total price movement in 20 days. (ABS(C - C1) + ABS(C1 - C2) + ABS(C2 - C3) + ABS(C3 - C4) + ABS(C4 - C5) + ABS(C5 - C6) + ABS(C6 - C7) + ABS(C7 - C8) + ABS(C8 - C9) + ABS(C9 - C10) + ABS(C10 - C11) + ABS(C11 - C12) + ABS(C12 - C13) + ABS(C13 - C14) + ABS(C14 - C15) + ABS(C15 - C16) + ABS(C16 - C17) + ABS(C17 - C18) + ABS(C18 - C19) + ABS(C19 - C20))
Let us say a stock is trading at 10 and in 20 days it goes to 30. So it had 20 points move. Now this 20 point move is highly efficient if this stock goes up exactly by 1 dollar every day. In other words it will have very smooth trend.
Now supposing this same stock makes the 20 point move with lot of volatility. It goes up 1 dollar one day, goes down 2 dollar next day goes up 50 cents next, goes up 3 dollar next and so on. Then what will happen in the denominator part of equation (ABS(C - C1) + ABS(C1 - C2) + ABS(C2 - C3) + ABS(C3 - C4) + ABS(C4 - C5) + ABS(C5 - C6) + ABS(C6 - C7) + ABS(C7 - C8) + ABS(C8 - C9) + ABS(C9 - C10) + ABS(C10 - C11) + ABS(C11 - C12) + ABS(C12 - C13) + ABS(C13 - C14) + ABS(C14 - C15) + ABS(C15 - C16) + ABS(C16 - C17) + ABS(C17 - C18) + ABS(C18 - C19) + ABS(C19 - C20))
you will get values higher than 20. Greater the value on denominator more volatile is the stocks move.

So the fractal efficiency ratio is ratio of price direction to volatility. From risk management perspective we want a stock with high fractal efficiency as it will likely move smoothly without stopping us out.
What causes the fractal efficiency to be higher is aggressive buyers. If buyers are aggressive in a stock, it seldom has big pullbacks. Such stocks are being aggressively accumulated by buyers and hence are better. That is the basic hypothesis for using it.

The Fractal Efficiency Ratio is described in detail inNew Trading Systems and Methods by Perry Kaufman.

Fractal Efficiency ratio is derived by dividing the net change in price movement over n periods by the sum of all component moves, taken as positive numbers, over the same n periods. If the ratio approaches the value 1, then the movement is smooth, if the ratio approaches 0, then there is great inefficiency or chaos. So fractal efficiency basically is a measure of relative market speed to volatility, and can be used as a trading filter to avoid choppy or flat stocks.

To understand this better lets look at a stock price move for 60 days. Stock A makes 60 point move in 60 days. Each of the day the stock moves up exactly by 1 dollar. This would give an efficiency ratio of 1. Now say for stock B, it also moved 60 points in 60 days, but with higher volatility, some days up 3 points, some days down 2 points. The efficiency ration would be lower as the denominator in the ratio will be large.
Now if you want to calculate the efficiency ratio in Telechart, you can try this modified scan for 60 days.

60 Day Efficiency Ratio

(C - C60) / (ABS(C - C1) + ABS(C1 - C2) + ABS(C2 - C3) + ABS(C3 - C4) + ABS(C4 - C5) + ABS(C5 - C6) + ABS(C6 - C7) + ABS(C7 - C8) + ABS(C8 - C9) + ABS(C9 - C10) + ABS(C10 - C11) + ABS(C11 - C12) + ABS(C12 - C13) + ABS(C13 - C14) + ABS(C14 - C15) + ABS(C15 - C16) + ABS(C16 - C17) + ABS(C17 - C18) + ABS(C18 - C19) + ABS(C19 - C20) + ABS(C20 - C21) + ABS(C21 - C22) + ABS(C22 - C23) + ABS(C23 - C24) + ABS(C24 - C25) + ABS(C25 - C26) + ABS(C26 - C27) + ABS(C27 - C28) + ABS(C28 - C29) + ABS(C29 - C30) + ABS(C30 - C31) + ABS(C31 - C32) + ABS(C32 - C33) + ABS(C33 - C34) + ABS(C34 - C35) + ABS(C35 - C36) + ABS(C36 - C37) + ABS(C37 - C38) + ABS(C38 - C39) + ABS(C39 - C40) + ABS(C40 - C41) + ABS(C41 - C42) + ABS(C42 - C43) + ABS(C43 - C44) + ABS(C44 - C45) + ABS(C45 - C46) + ABS(C46 - C47) + ABS(C47 - C48) + ABS(C48 - C49) + ABS(C49 - C50) + ABS(C50 - C51) + ABS(C51 - C52) + ABS(C52 - C53) + ABS(C53 - C54) + ABS(C54 - C55) + ABS(C55 - C56) + ABS(C56 - C57) + ABS(C57 - C58) + ABS(C58 - C59) + ABS(C59 - C60) + 0.001)

Stocks do not have a perfect efficiency reading of 1. Even a small anti trend move lowers the efficiency reading. The above formula scan will give you values between 1 to -1. If you sort by this scan, the higher ratio stocks will have smoother trends, while reading between .30 to 0 will show very volatile stocks. Generally Efficiency Ratio readings above +.30 are very favorable to define persistent uptrends while readings under -.30 often denote steady downtrend.

You can try various time periods on this ratio. Generally you should use lower time frames of 20 days or so.
20 Day Efficiency ratio
(C - C20) / (ABS(C - C1) + ABS(C1 - C2) + ABS(C2 - C3) + ABS(C3 - C4) + ABS(C4 - C5) + ABS(C5 - C6) + ABS(C6 - C7) + ABS(C7 - C8) + ABS(C8 - C9) + ABS(C9 - C10) + ABS(C10 - C11) + ABS(C11 - C12) + ABS(C12 - C13) + ABS(C13 - C14) + ABS(C14 - C15) + ABS(C15 - C16) + ABS(C16 - C17) + ABS(C17 - C18) + ABS(C18 - C19) + ABS(C19 - C20))

So when choosing stocks from IBD 100 or 200 to swing trade, if you use the Efficiency Ratio as filter you can find more smoother trends. Or for any given stock the Efficiency ratio quickly tells you how smooth the trend is. Generally it is observed that smoother trends continue to be smooth and volatile trends continue to be volatile.
 
Last edited by a moderator:
Hi,

A little global search and replace in a good programming editor (TextPad in this case) using the 60 Day Efficiency Ratio set produced this result that compiles and runs in TOS as a "custom" study filter in the stock scanner.
Enjoy!

(close - close[60]) / (AbsValue(close - close[1]) + AbsValue(close[1] - close[2]) + AbsValue(close[2] - close[3]) + AbsValue(close[3] - close[4]) + AbsValue(close[4] - close[5]) + AbsValue(close[5] - close[6]) + AbsValue(close[6] - close[7]) + AbsValue(close[7] - close[8]) + AbsValue(close[8] - close[9]) + AbsValue(close[9] - close[10]) + AbsValue(close[10] - close[11]) + AbsValue(close[11] - close[12]) + AbsValue(close[12] - close[13]) + AbsValue(close[13] - close[14]) + AbsValue(close[14] - close[15]) + AbsValue(close[15] - close[16]) + AbsValue(close[16] - close[17]) + AbsValue(close[17] - close[18]) + AbsValue(close[18] - close[19]) + AbsValue(close[19] - close[20]) + AbsValue(close[20] - close[21]) + AbsValue(close[21] - close[22]) + AbsValue(close[22] - close[23]) + AbsValue(close[23] - close[24]) + AbsValue(close[24] - close[25]) + AbsValue(close[25] - close[26]) + AbsValue(close[26] - close[27]) + AbsValue(close[27] - close[28]) + AbsValue(close[28] - close[29]) + AbsValue(close[29] - close[30]) + AbsValue(close[30] - close[31]) + AbsValue(close[31] - close[32]) + AbsValue(close[32] - close[33]) + AbsValue(close[33] - close[34]) + AbsValue(close[34] - close[35]) + AbsValue(close[35] - close[36]) + AbsValue(close[36] - close[37]) + AbsValue(close[37] - close[38]) + AbsValue(close[38] - close[39]) + AbsValue(close[39] - close[40]) + AbsValue(close[40] - close[41]) + AbsValue(close[41] - close[42]) + AbsValue(close[42] - close[43]) + AbsValue(close[43] - close[44]) + AbsValue(close[44] - close[45]) + AbsValue(close[45] - close[46]) + AbsValue(close[46] - close[47]) + AbsValue(close[47] - close[48]) + AbsValue(close[48] - close[49]) + AbsValue(close[49] - close[50]) + AbsValue(close[50] - close[51]) + AbsValue(close[51] - close[52]) + AbsValue(close[52] - close[53]) + AbsValue(close[53] - close[54]) + AbsValue(close[54] - close[55]) + AbsValue(close[55] - close[56]) + AbsValue(close[56] - close[57]) + AbsValue(close[57] - close[58]) + AbsValue(close[58] - close[59]) + AbsValue(close[59] - close[60]) + 0.001)
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
352 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top