TradingView
vnhilton
11 יוני 2023 23:04

Visible Range Linear Regression Channel [vnhilton] 

SPDR S&P 500 ETF TRUSTArca

תיאור

(OVERVIEW)
This indicator calculates the linear regression channel for the visible bars shown on the chart instead of the traditional fixed length linear regression channel TradingView provides (and is more accurate I believe). Inspired by TradingView's Linear Regression Channel and Visible Average Price indicator, and the DAS Trader linear regression indicator.

(FEATURES)
- Ability to extend lines to the right
- Show/hide individual lines
- Adjust standard deviation of bands
- Adjust line style and width of basis and band lines
- Change individual line colours and plot fills between the lines

(DIFFERENCES)
If you compare this indicator to TradingView's Linear Regression Channel, you will notice some differences (as of 11th June, 2023). Differences and reasons are:

1) The intercept is wrong. The formula TradingView uses to calculate the intercept includes the addition of the gradient, which I believe is incorrect. Difference #2 is also why the intercept is wrong. This indicator omits that addition. This was verified by comparing the gradient calculated in this indicator with the gradient determined by Excel with the same data.

2) The gradient is "wrong". In quotations as essentially TradingView's code attempts to find the line of best fit, with the y-axis on the most recent bar instead of the oldest bar. This leads to the gradient being the opposite to the gradient found in this indicator, which isn't wrong, but the later formula used to calculate the intercept doesn't take this into account, resulting in an incorrect intercept value. The gradient and intercept values in this indicator matches those found in Excel.

3) Standard deviation bands of both indicators. I believe the code TradingView uses to calculate standard deviation is incorrect (basing this just through visuals). This indicator uses the array.stdev function to find the correct value (verified with Excel numbers).
תגובות
netswimmer
can you add option for fix range please ?
vnhilton
@netswimmer, Hi netswimmer, if by fixed range you mean manually choosing 2 points on the chart to calculate the linear regression, TradingView has a drawing tool called "Regression Trend" where you can do this. If however you prefer to use this indicator, simply do the following to the code:

1) Comment out lines 72 to 74, and lines 79 to 80 using "//". These are responsible for the visible range feature.
2) Edit lines 90, 98, 104, 105 and 114, by replacing "length" with "array.size(prices)". Determines length via number of prices (bars) within the fixed range.
3) Edit lines 127, 130-132, 134-135, 138-139, by replacing "leftIndex" and "rightIndex" with "leftTime" and "rightTime" respectively. Also, add another argument to these lines with "xloc = xloc.bar_time". We use time instead of the bar index to do plots.
4) Add inputs leftTime = input.time(0, "Start Time", confirm = true) and rightTime = input.time(0, "End Time", confirm = true) to say line 13. The "confirm" argument lets you manually select a fixed range to draw the linear regression.

Let me know if you have any issues implementing this. Thanks
netswimmer
@vnhilton, is it possible to add input for visible and fix range so we can choose it any time we want
xmd1979
very good script, thank you )
עוד