TradingView
Gentleman-Goat
Jan 28, 2020 3:58 AM

Linear Regression - Auto Adjust To Ideal Pearson's R (Min & Max) 

Litecoin / United States DollarCoinbase

Description

This is meant as a partner to my linear regression strategy script. This one however is just the indicator only so you can overlay the technique over other strategies.

To use this script you need to understand what Pearson's R is; which is how correlated a set of data is to a line within a given deviation (+ or -).

If the Pearson's R is negative (-) then the trend is going upwards. If the value is positive (+) then the trend is going downwards.

There is a lot of debate as to what the ideal negative and positive values are for the Pearson's R, so you can change these in the settings. What the script will do is attempt to automatically find (and draw the trend lines) based on a given minimum period and a maximum period (both set in the settings) that match the ideal Pearson's R settings selected.

Ideally the data is MOST correlated the closest it is to -1 or 1. This means the pattern is very reliable.

This script was designed and works very well with the bar replay mode. You may need a more professional version of TradingView to use this correctly. This mode allows you to see the script in action as it mutates and changes according to the new data being introduced.

I've pre-set the Pearson's R (+) and (-) ranges to what I think is ideal.

Release Notes

This new version cleans up the code a bit and adds a better infobox thanks to richard santos and some modifications I did to his original code base. The colors of the info box are designed to better match the new oscillator partner script I uploaded as well.

Release Notes

The box adjustment was not working so well so removed it for now.

Release Notes

I changed the default values to 0.85 for both positive and negative to match the oscillator indicator. These can be changed in the settings. I also added alerts for the upper and upper max + lower and lower max trend lines. You can enable these alerts by going to alerts and adding a new alert then selecting my indicator and selecting from the alert options. Enjoy!

Release Notes

Added new alerts for detecting bullish,bearing or sideways trends that you can set and alerts for both the long term and short term when these occurs so users can use webhooks to trigger trades based on these conditions. Some traders like to trade differently during different cycles, so this will allow you to do this programmatically now :). Thanks to TradeBoneDog for this suggestion :).

You can disable what shows in the infobox as well in the settings if you only care about seeing certain results so it doesn't get so cluttered in there ;).
Comments
Gentleman-Goat
I recently added alerts to this script for the upper and max upper, lower and max lower trend lines. You can enable them by selecting the alerts tab option on the right menu bar then pressing the 'create alert' button or the + button above and then selecting my indicator from the 'condition' dropdown and then selecting the condition below it. If you need any help just let me know.
mfa_supertrader
@x11joe, This indicator helps me a lot now! Many thanks for the additional features!
Gentleman-Goat
@mfa_supertrader, antime, let me know if you have any other ideas!
Gentleman-Goat
@mfa_supertrader, I added more alerts that may be useful, let me know.
js1pxl1111
Cool script, man. Really helping me to learn more about linear regression trends and Pearson's R.
GoodMD
Hi, please advise, how to use this script, do i use it as a trend following or a counter trend, as the box description says in the the chart (should i follow what it said or counter it). Thank you.
ilesanmipepsi
Thanks for sharing you script with the community, you're doing a great job! It will be great if you can add arrow to show buy and sell direction with this script. Thanks.
manojm4
I'm not able to see the linear regression lines on the chart . Cannot add a screen shot here in the comment. Only the infobox is coming .
Gentleman-Goat
@manojm4, This can happen if it's trying to plot something too large. I'm going to make another version of this script that is faster soon.
IdeasSJ
Thank you for helping the community with such a wonderful script. During Tradingview replay mode back-test I found it complement well with different strategies.

Before trying it in Real-time need your help to clarify the followings. (below mentioned setting changes are done through setting gear of Indicator and NOT directly inside code. If it needs to be changed in script, please advise)

On Intraday Chart (as you mentioned default settings work well with 4H chart and not work with lower time frame)
To apply it on 5minutes chart, I am trying to change the setting to> Min period 8, Max period 24 and Step by as 8 (multiple of min period 8 and hope this is what you meant)
But when Info Box display comes, at start shows Long Trend(LT) as 24 and Short Trend(ST) as 8 but after sometime ST keep changing to 12 & 16 and back to 8 instead of fixed Min & Max period set. Tried multiple settings and think that the numbers I entered is incompatible. So need your help to clarify.

1. As per your observations using this indicator what is the best intraday 5min chart Min Period, Step By and Max Period? (I tried 8 & 24 as Step by need to be multiples of them and it is closer to Fib 8 & 21 indicating short term trend). If possible, provide 2 or 3 setting options to experiment.
2. What is impact (better or worse) of increasing or decreasing the Step by other than PC processing power consumption?

On Daily chart (if understood correctly, the default setting of Min period 48 & Max period 360 is recommended for 4 hour chart. So, want to reconfirm whether you recommend other settings for daily or weekly chart too.
3. As per your observations using this indicator what is the best settings for Min Period, Step By and Max Period on Daily chart? If possible, provide 2 or 3 setting options to experiment.

On Weekly chart
4. As per your observations using this indicator what is the best settings for Min Period, Step By and Max Period on weekly chart?

General question
5. If understood correctly, you meant to enter "LONG" when Pearson R value is -0.85 for high probable solid reliable continuing trend and go "SHORT" when Pearson R value is 0.71 for high probable solid reliable trend. Please confirm this understanding is correct.
6. If above understanding is correct, whether changing default of -0.85 & 0.85 to -0.85 & 0.71 in settings of Oscillator and Strategy script is fine? Also whether it have any impact to other settings and need to be changed in settings or script.

Sorry to bother you with above questions as I am finding this indicator have high potential to filter out good probability trades when used correctly with any strategy. So clarifying above points will help me to try out this indicator.

Awaiting your reply.
Once again thank you for the efforts and sharing it to the community.
More