Parallel Pivot Lines [LuxAlgo]Displays lines connecting past pivot high/low points with each line having the slope of a linear regression. This slope can also be controlled by the user with the 'Slope' setting. Each line can be used as a support or resistance by the user.
Settings
Length : Pivot length. Use higher values for having lines connected to more significant pivots points.
Lookback : Number of lines connecting a pivot high/low to display, with a total of lines equal to Lookback*2
Slope : Allows the user to multiply the linear regression slope by a number within -1 and 1
Limitations
The script has currently several real time behavior limitations. Lines are displayed retrospectively and will not update with the arrival of new bars. Readjusting the indicator to newer pivots will require the user to either hide/unhide the indicator or change its settings.
High Length or Lookback values might not return any lines if the location of a pivot point is outside the defined buffer size of the indicator (set as 5000 bars).
How To Use
The indicator can be used to get supports and resistances and is more so closer to a drawing tool due to its limitations. The lines not updating with the arrival of new bars have the advantage of providing fixed supports/resistances.
The Slope setting allows the user to control the angle and direction of the lines. Using a Slope of 1 will return lines with the same slope as the one of a linear regression fit from the farthest pivot point displayed by the indicator to the most recent bar.
The chart above shows the indicators and a linear regression in orange.
If you want to have horizontal lines, use a Slope equal to 0.
Finally using a negative slope value will allow the user to have lines in opposite directions to the main trend.
Conclusion
We hope you like this indicator (drawing tool) and find it useful for drawing your support & resistances in a unique way!
Doğrusal Regresyon
Robust Channel [tbiktag]Introducing the Robust Channel indicator.
This indicator is based on a remarkable property of robust statistics , namely, the resistance to the presence of data points that deviate significantly from the established trend (generally speaking, outliers ). Being outlier-resistant, the Robust Channel indicator “remembers” a pre-existing trend and thus exhibits a very peculiar "lag" in case of a sharp price change. This allows high-confidence identification of such price actions as a trend reversal, range break, pullback, etc.
In the case of trending and range-bound market conditions, the price remains within the channel most of the time, fluctuating around the central line.
Technical details
The central line is calculated using the repeated median slope algorithm. For each data point in a lookback window of a user-specified Length , this method calculates the median slope of the lines that connect that point to all other points inside the window. The overall median of these median slopes is then calculated and used as an estimate of the trend slope. The algorithm is very efficient as it uses an on-the-fly procedure to update the array containing the slopes (new data pushed - old data removed).
The outer line is then calculated as the central line plus the Length -period standard deviation of the price data multiplied by a user-defined Channel Width Factor . The inner line is defined analogously below the central line.
Usage
As a stand-alone indicator, the Robust Channel can be applied similarly to the Bollinger Bands and the Keltner Channel:
A close above the outer line can be interpreted as a bullish signal and a close below the inner line as a bearish signal.
Likewise, a return to the channel from below after a break may serve as a bullish signal, while a return from above may indicate bearish sentiment.
Robust Channel can be also used to confirm chart patterns such as double tops and double bottoms.
If you like this indicator, feel free to leave your feedback in the comments below!
Matrix Library (Linear Algebra, incl Multiple Linear Regression)What's this all about?
Ever since 1D arrays were added to Pine Script, many wonderful new opportunities have opened up. There has been a few implementations of matrices and matrix math (most notably by TradingView-user tbiktag in his recent Moving Regression script: ). However, so far, no comprehensive libraries for matrix math and linear algebra has been developed. This script aims to change that.
I'm not math expert, but I like learning new things, so I took it upon myself to relearn linear algebra these past few months, and create a matrix math library for Pine Script. The goal with the library was to make a comprehensive collection of functions that can be used to perform as many of the standard operations on matrices as possible, and to implement functions to solve systems of linear equations. The library implements matrices using arrays, and many standard functions to manipulate these matrices have been added as well.
The main purpose of the library is to give users the ability to solve systems of linear equations (useful for Multiple Linear Regression with K number of independent variables for example), but it can also be used to simulate 2D arrays for any purpose.
So how do I use this thing?
Personally, what I do with my private Pine Script libraries is I keep them stored as text-files in a Libraries folder, and I copy and paste them into my code when I need them. This library is quite large, so I have made sure to use brackets in comments to easily hide any part of the code. This helps with big libraries like this one.
The parts of this script that you need to copy are labeled "MathLib", "ArrayLib", and "MatrixLib". The matrix library is dependent on the functions from these other two libraries, but they are stripped down to only include the functions used by the MatrixLib library.
When you have the code in your script (pasted somewhere below the "study()" call), you can create a matrix by calling one of the constructor functions. All functions in this library start with "matrix_", and all constructors start with either "create" or "copy". I suggest you read through the code though. The functions have very descriptive names, and a short description of what each function does is included in a header comment directly above it. The functions generally come in the following order:
Constructors: These are used to create matrices (empy with no rows or columns, set shape filled with 0s, from a time series or an array, and so on).
Getters and setters: These are used to get data from a matrix (like the value of an element or a full row or column).
Matrix manipulations: These functions manipulate the matrix in some way (for example, functions to append columns or rows to a matrix).
Matrix operations: These are the matrix operations. They include things like basic math operations for two indices, to transposing a matrix.
Decompositions and solvers: Next up are functions to solve systems of linear equations. These include LU and QR decomposition and solvers, and functions for calculating the pseudo-inverse or inverse of a matrix.
Multiple Linear Regression: Lastly, we find an implementation of a multiple linear regression, including all the standard statistics one can expect to find in most statistical software packages.
Are there any working examples of how to use the library?
Yes, at the very end of the script, there is an example that plots the predictions from a multiple linear regression with two independent (explanatory) X variables, regressing the chart data (the Y variable) on these X variables. You can look at this code to see a real-world example of how to use the code in this library.
Are there any limitations?
There are no hard limiations, but the matrices uses arrays, so the number of elements can never exceed the number of elements supported by Pine Script (minus 2, since two elements are used internally by the library to store row and column count). Some of the operations do use a lot of resources though, and as a result, some things can not be done without timing out. This can vary from time to time as well, as this is primarily dependent on the available resources from the Pine Script servers. For instance, the multiple linear regression cannot be used with a lookback window above 10 or 12 most of the time, if the statistics are reported. If no statistics are reported (and therefore not calculated), the lookback window can usually be extended to around 60-80 bars before the servers time out the execution.
Hopefully the dev-team at TradingView sees this script and find ways to implement this functionality diretly into Pine Script, as that would speed up many of the operations and make things like MLR (multiple linear regression) possible on a bigger lookback window.
Some parting words
This library has taken a few months to write, and I have taken all the steps I can think of to test it for bugs. Some may have slipped through anyway, so please let me know if you find any, and I'll try my best to fix them when I have time to do so. This library is intended to help the community. Therefore, I am releasing the library as open source, in the hopes that people may improving on it, or using it in their own work. If you do make something cool with this, or if you find ways to improve the code, please let me know in the comments.
Linear Regression CandlesThere are many linear regression indicators out there, most of them draw lines or channels, but this one actually draws a chart.
Repeated Median Regression ChannelThis script uses the Repeated Median (RM) estimator to construct a linear regression channel and thus offers an alternative to the available codes based on ordinary least squares.
The RM estimator is a robust linear regression algorithm. It was proposed by Siegel in 1982 (1) and has since found many applications in science and engineering for linear trend estimation and data filtering.
The key difference between RM and ordinary least squares methods is that the slope of the RM line is significantly less affected by data points that deviate strongly from the established trend. In statistics, these points are usually called outliers, while in the context of price data, they are associated with gaps, reversals, breaks from the trading range. Thus, robustness to outlier means that the nascent deviation from a predetermined trend will be more clearly seen in the RM regression compared to the least-squares estimate. For the same reason, the RM model is expected to better depict gaps and trend changes (2).
Input Description
Length : Determines the length of the regression line.
Channel Multiplier : Determines the channel width in units of root-mean-square deviation.
Show Channel : If switched off , only the (central) regression line is displayed.
Show Historical Broken Channel : If switched on , the channels that were broken in the past are displayed. Note that a certain historical broken channel is shown only when at least Length / 2 bars have passed since the last historical broken channel.
Print Slope : Displays the value of the current RM slope on the graph.
Method
Calculation of the RM regression line is done as follows (1,3):
For each sample point ( t (i), y (i)) with i = 1.. Length , the algorithm calculates the median of all the slopes of the lines connecting this point to the other Length -1 points.
The regression slope is defined as the median of the set of these median slopes.
The regression intercept is defined as the median of the set { y (i) – m * t (i)}.
Computational Time
The present implementation utilizes a brute-force algorithm for computing the RM-slope that takes O ( Length ^2) time. Therefore, the calculation of the historical broken channels might take a relatively long time (depending on the Length parameter). However, when the Show Historical Broken Channel option is off, only the real-time RM channel is calculated, and this is done quite fast.
References
1. A. F. Siegel (1982), Robust regression using repeated medians, Biometrika, 69 , 242–244.
2. P. L. Davies, R. Fried, and U. Gather (2004), Robust signal extraction for on-line monitoring data, Journal of Statistical Planning and Inference 122 , 65-78.
3. en.wikipedia.org
Moving Regression Band Breakout strategyFollowing the introduction of the Moving Regression Prediction Bands indicator (see link below), I'd like to propose how to utilize it in a simple band breakout strategy :
Go long after the candle closes above the upper band . The lower band (alternatively, the lower band minus the 14-period ATR or the central line ) will serve as a support line .
Exit as soon as the candle closes below the support line .
To manage the risk of false breakouts, a fixed stop loss is set to the value of the support line at the time of opening a position. When the support line moves above the position opening price, shift the stop loss to breakeven.
The same logic but in reverse applies to short positions.
As an option, it is possible to allow long entries only when the slope of the Moving Regression curve is positive (and short entries when the slope is negative).
Model parameters:
Length and Polynomial Order define the lag and smoothness of the model.
Multiplier specifies the width of the channel.
As the default model parameter values, I set those that I found to provide optimal risk / reward ratio on the daily timeframe (for both trending and range-bound market). However, the settings are very flexible and can be well-adjusted to particular market conditions. Feel free to play around and leave feedback in the comments!
Here's the original Moving Regression Prediction Bands script:
Moving Regression Prediction BandsIntroducing the Moving Regression Prediction Bands indicator.
Here I aimed to combine the principles of traditional band indicators (such as Bollinger Bands), regression channel and outlier detection methods. Its upper and lower bands define an interval in which the current price was expected to fall with a prescribed probability, as predicted by the previous-step result of the local polynomial regression (for the original Moving Regression script, see link below).
Algorithm
1. At every time step, the script performs local polynomial regression of the sample data within the lookback window specified by the Length input parameter.
2. The fitted polynomial is used to construct the Moving Regression time series as well as to extrapolate data, that is, to predict the next data point ( MRPrediction ).
3. The accuracy of local interpolation is estimated by means of the root-mean-square error ( RMSE ), that is, the deviation between the fitted polynomial and the observed values.
4. The MRPrediction and RMSE values calculated for the previous bar are then used to build the upper and lower bands , which I define as follows:
Upper Band = MRPrediction_prev + Multiplier *( RMSE_prev )
Lower Band = MRPrediction_prev - Multiplier *( RMSE_prev )
Here the Multiplier is a user-defined parameter that should be interpreted as a quantile in the standard normal distribution (the default value of 2.0 roughly corresponds to the 95% prediction interval).
To visualize the central line , the script offers the following options:
Previous-Period MR Prediction: MRPrediction_prev time series from the above equation.
MR: Conventional Moving Regression time series.
Ribbon: “Previous-Period MR Prediction” and “MR” curves plotted together and colored according to their relative value (green if MR > Previous MR Prediction; red otherwise).
Usage
My original idea was to use the band breakouts as potential trading signals. For example, the price crossing above the upper band is a bullish signal , being a potential sign that price is gaining momentum and is out of a previously predicted trend. The exit signal could be the crossing under the lower band or under the central line.
However, be aware that it is an experimental indicator, so you might fin some better strategies.
Feel free to play around!
Nico's SPX Dynamic ChannelsTest of dynamic channels and some statistics made by hand.
This indicator was done specifically for the S&P500 index.
As you can see, below the 125 EMA there's a lot more volatility than in the upside. I've made some kind of a dynamic linear regression of the lows and the highs.
I've chosen the MA that best fits the SPX, and then calculated in Excel the percental mean and SDs of most important peaks and valleys that I've chosen in comparison to the 125 MA. This lead to the green, orange and red zones. BUT, I've calculated the peaks and valleys separately, as I assumed that a bear market and crashes have way more volatility than bull markets. That's why the difference between the upper and the lower channels.
The neutral blue zone is composed by an upper EMA of the highs and lower EMA of the lows. No MA in this script uses the close price as a source.
This MA makes sense because it represents a semester of trading, for this particular asset.
Backtest results
It's also interesting to try it here too, as it has a little bit more of data:
SPCFD:SPX
As it's not a trading system, I have no batting average nor ratios for this.
Still, the measures of the peaks and valleys are very accurate and repeat themselves over and over again. The results were:
3rd resistance: 12.88%
2nd resistance: 10.12%
1st resistance: 7.36%
1st support: -6.42%
2nd support: -14.8%
3rd support: -23.18%
All referred to the mean, which is the 125 EMA zone.
After the 1950's works like magic, but not before. You will see that it doesn't work in the great depression and it's crash.
How to use this indicator
Green = First grade support/resistance .
Orange = Second grade support/resistance . Caution.
Red = Third grade support/resistance . High chances of mean reversal.
Blue zone = This is the neutral zone, where the prices are not cheap nor expensive.
Often in a trending market, the price will have the blue zone as it's main support and when trending the price will stick to the green MA.
When the price touches the orange MA, the most probable is that it will return to the green MA.
If the price touches the red zone, there's a high chance that this is a big turning point and it will reverse to the mean (green or blue zone).
Imagine you've bought each time the price touched the red support, check that and you'll start liking this indicator. I think it is a great entry point for investors. The red resistance is good too, but of course it works for a short period of time.
I've backtested this indicator since the beginning of the dataset and it works like magic, but ONLY for the SPX index (spot price).
Leave a comment or some coins if you like it!!!
(I've posted it before like an analysis, not as a script, my bad)
Forecast OscillatorThe Forecast Oscillator is a technical indicator that compares a security close price to its time series forecast. The time series forecast function name is "tsf" and it calculates the projection of the price trend for the next bar.
The Forecast Oscillator and therefore the time series forecast are based on linear regression. The time series forecast indicator is equal to the sum of two other indicators: the linear regression (LinearReg) and the linear regression slope (LinearReg_Slope).
If the Forecast Oscillator stays above the zero line for an extended period, then it signals that the price may rise in the future and if it stays below the zero line for an extended period, then it signals a coming fall in the security price.
The indicator name is "Forecast Oscillator" and it accepts two arguments. The first argument is the time series that is used in the next bar forecast (It is usually the close price) and the second one is the period that will be passed to the time series forecast function during calculation . The technical analysis indicator returns a value in percentage that corresponds to the close price minus the previous value of the time series forecast, multiplied by 100 and divided by the close price.
Author's Note:
Just look at the exaggerated movements of the oscillator especially in trend changes . Some examples can be experienced on the chart in rectangles.
Kıvanç Özbilgiç
Volume-Supported Linear Regression TrendHello Traders,
Linear Regression gives us some abilities to calculate the trend and if we combine it with volume then we may get very good results. Because if there is no volume support at up/downtrends then the trend may have a reversal soon. we also need to check the trend in different periods. With all this info, I developed Volume-Supported Linear Regression Trend script. The script checks linear regression of price and volume and then calculates trend direction and strength.
You have option to set Source, Short-Term Period and Long-Term Period. you can set them as you wish.
By default:
Close is used as "Source"
Short-Term Period is 20
Long-Term Period is 50
in following screenshot I tried to explain short term trend (for uptrend). Volume supports the trend? any volume pressure on trend? possible reversal? same idea while there is downtrend.
in following screenshot I tried to explain long term trend:
You can also check Positive/Negative Divergences to figure out possible reversals (to automate it, you can use Divergence for Many Indicators v4 , it has ability to check divergences on external indicators)
Enjoy!
Theil–Sen EstimatorThe Theil-Sen estimator is a nonparametric statistics method for robustly fitting a regression line to sample points (1,2).
As stated in the Wikipedia article (3), the method is " the most popular nonparametric technique for estimating a linear trend " in the applied sciences due to its robustness to outliers and limited assumptions regarding measurement errors.
Relation with other Methods
The Theil-Sen estimator can be significantly more accurate than simple linear regression (least squares) for skewed and heteroskedastic data.
Method Description
The script computes all the slopes between pairs of points and takes the median as the estimate of the regression slope, m . Subsequently, the intercept, b , is determined from the sample points as the median of y(i) − m x(i) values. The regression line in the slope–intercept form, y = m x + b , is then plotted along with the calculated prediction interval (estimated by means of the root-mean-square error).
I have added two options for how to handle pairs of points:
Method == "All" to use the slopes of all pairs of points;
Method == "Random" to use the slopes of randomly generated pairs of points.
The random choice of the pairs of points is based on the Wichmann–Hill is a pseudorandom number generator.
The reason for introducing the "Random" method is that the calculation of the median involves sorting the array of slopes (the size of N*(N-1)/2, where N is the number of sample points). This is a computationally demanding procedure, which runs into the limit on the cycle computation time (200 ms) set in TradingView. Therefore, the "All" method works only with Length < 50.
Also note that the number of lookback points is limited by by the maximum array size allowed in TradingView.
Literature
1. Sen, P. K. (1968) "Estimates of the regression coefficient based on Kendall's tau." JASA, 1379-1389.
2. Theil, H. (1950) "A rank-invariant method of linear and polynomial regression analysis." Reprinted in 1992 in Henri Theil’s contributions to economics and econometrics, Springer, 345-381.
3. en.wikipedia.org
Exponential Regression Slope Annualized with R-squared HistogramMy other indicator shows the linear regression slope of the source. This one finds the exponential regression slope and optionally multiplies it by R-squared and optionally annualizes it. Multiplying by R-squared makes sure that the price movement was significant in order to avoid volatile movements that can throw off the slope value. Annualizing the exponential slope will let you see how much percentage you will make in a year if the price continues at its current pace.
The annualized number is the number of trading days in a year. This and the length might need adjusting for the extra bars that might be in futures or other markets. The number does not have to be a year. For example, it can be a month if you set the number to 20 or so trading days to find how much you would make in a month if price continues at its current pace, etc. This can also be used as an alternative to relative strength or rate of change.
Moving RegressionMoving Regression is a generalization of moving average and polynomial regression.
The procedure approximates a specified number of prior data points with a polynomial function of a user-defined degree. Then, polynomial interpolation of the last data point is used to construct a Moving Regression time series.
Application:
Moving Regression allows one to smooth noise on the analyzed chart, assess momentum, confirm trends, and establish areas of support and resistance.
In addition, it can be used as a simple stand-alone forecasting method to identify trend direction and trend reversal points. When the local polynomial is predicted to move up in the next time step, the color of the Moving Regression curve will be green. Otherwise, the color of the curve is red. This function is (de)activated using the Predict Trend Direction flag.
Selecting the model parameters:
The effects of the moving window Length and the Local Polynomial Degree are confounded. This allows for finding the optimal trade-off between noise (variance) and lag (bias). Higher Length and lower Polynomial Degree (such as 1, i.e. linear), will result in "smoother" time series but at the cost of greater lag. Increasing the Polynomial Degree to, for example, 2 (squared) while maintaining the Length will diminish the lag and thus compromise the noise-lag tradeoff.
Relation to other methods:
When the degree of the local polynomial is set to 0 (i.e., fitting data to a constant level), the Moving Regression time series exactly matches the Simple Moving Average of the same length.
Linear Regression - Reverse Up/Down StrategyFor my first foray into pine script I took the code from the generic "Consecutive up/down" and flipped the logic. I added a linear regression filter to try and stay with the overall trend. ATR added for visual, I eventually want to use it as part of the money management.
Rules to open trade or close the opposite:
IF the linear regression slope is >=0 AND the last candle closes lower, BUY
IF the linear regression slope is <=0 AND the last candle closes higher SELL
Rules to close the opposite:
IF the linear regression slope is >=0 AND the last candle closes higher, close any open SELL
IF the linear regression slope is <=0 AND the last candle closes lower, close any open BUY
Pearsons R historic breaks ( for making strategy out of it )Pearsons R adjustments of linear regressions is a good tool to analyse visually the trend basis channeling of the market,
one of the common challenges in the scripts which i came across the tradingview community is the lack of the historic places put over the charts where the price actually crossed up/below the channel boundaries,
For me in order to do so we would have to replay the scripts over and over.
So I came up with an addon extension to a good script on the community ()
I have added the historic marks over the charts ( blue/green/red/orange Arrows which you see on the charts)
These arrows are the places where the price have crossed the channel boundaries historically
So,
Blue arrow = Bull Intent ,Place where the upper channel boundary line have been crossed
Orange arrow = Bear Intent , Place where the Lower channel boundary line have been crossed
Green arrow = Bull Max , Place where the Maximum Upper channel boundary line have been crossed
Red arrow = Bear Max , Place where the Minimum Lower channel boundary line have been crossed
For me this type of approach is good for making a strategy out of the concepts visually, in congestion to other indicators
I would suggest you guys to check this on alert basis with your setups to derive out the best from linear regressions
Lastly it would be a pleasure to give credits to the creators of Pearsons R scripts in Tradingview
@midtownsk8rguy
@TradeBoneDogs
@x11joe
Also thanking Tradingview for providing us such an awesome community and platform :)
Daily Play Ace SpectrumSo the idea of the Daily Play Ace Spectrum is to extend the Ace Spectrum .
By exposing more parameters, making a variation of the Ace Spectrum which is more configurable.
The idea is this makes the Daily Play Ace Spectrum more suitable for use on shorter (hourly and minute) time scales.
These specific parameters exposed still maintain the original form of the original Ace Spectrum, but loosen up the hard coded assumptions of the original indicator.
By exposing more parameters this now makes the Daily Ace Spectrum more sensitive to input.
Meaning the parameters you choose are important and will set the characteristic reaction of the indicator to the series you give it.
This presents a trade-off, the simplicity of the original indicator is sacrificed.
But what's gained is a more comprehensive indicator that now needs more careful parameter adjustment .
Related to the Ace Spectrum:
Linear Regression ChannelHello Traders,
There are several nice Linear Regression Channel scripts in the Public Library. and I tried to make one with some extra features too. This one can check if the Price breaks the channel and it shows where is was broken. Also it checks the momentum of the channel and shows it's increasing/decreasing/equal in a label, shape of the label also changes. The line colors change according to direction.
using the options, you can;
- Set the Source (Close, HL2 etc)
- Set the Channel length
- Set Deviation
- Change Up/Down Line colors
- Show/hide broken channels
- Change line width
meaning of arrows:
⇑ : Uptrend and moment incresing
⇗ : Uptrend and moment decreasing
⇓ : Downtrend and moment incresing
⇘ : Downtrend and moment decreasing
⇒ : No trend
An example for how color of lines, arrow direction and shape of label change.
Enjoy!
Linear Regression Slope HistogramThis is the slope of linear regression plotted as a histogram. It has a threshold for the slope in case there is a minimum slope amount that the user wants for it to show green or red. The color shows a dim and bright effect depending on the direction of the slope to better show its rise and fall. You can set the length of the slope in the input setting screen.
Some interesting things about linear regression slope that I found out: It kind of looks like the histogram of ADX or the directional movement lines depending on the length used for the slope. According to Tushar Chande, linear regression can be used as an alternative of finding trend direction, although it is hard to say what length to use for it, I haven't been able to figure out a good way to apply it yet. According to Bulkowski, linear regression of the volume can be used with certain chart patterns that he listed to increase their chance of success by taking trades where the linear regression line points towards, so if you use volume as a source on this indicator you can take trades in whatever said direction. Got any other ideas on how to use linear regression? Feel free to let me knows, would gladly appreciate it.
Variable Linear Regression With Pearsons R OscillatorThis is the Oscillator that is meant to go with the Variable Linear Regression With Pearsons R Script. Set this to match the settings you have on the other script.
To use this, you want to do trades something like this:
The bottom -0.80 (green line) means that the trend is very strong in the upward direction. This almost always means it's about to finish its trend soon and reverse the other direction. If you see the other linear regression lines also coincide with this result it would best to get ready to reverse your position and do a short once some other indicators show you it is turning the other direction in addition to this.
The reverse for longing would be the case after a large downtrend and the 0.80 line (red line) is touched by many of the linear regressions, usually it means the downtrend is ending and there is a good buy opportunity soon.
This is called a variable linear regression systems because you can set as many variable linear regressions as you want. I have capped it at 10 but you can modify the code to raise this if really necessary. Every color is customizable for each different linear regression trend.