TTP Pair Slope/HedgePair slope/hedge uses linear regression to calculate the hedge ratio (slope) between the two assets within a period.
It allows you to specify a "from" and a "to" candle.
Example:
"A regression from 1000 candles back in time and ignore the last 100 candles. This would result in making a regression of 900 candles in total."
The formula used to perform the regression with the assts X and Y is:
Hedge =
mean( (X-mean(X))^2 )
——————————————————
mean( (X-mean(X)) * (Y-mean(Y)) )
You can later use the hedge in a chart of X - Hedge * Y
(Confirm with 1 / hedge )
If the plot is stationary the period tested should look like stationary.
If you cross an imaginary horizontal line across all the values in the period used it should look like a flat channel with values crossing above and below the line.
The purpose of this indicator is to help finding the linear regression test used for conintegration analysis. Conintegration assets is one of the requirements to consider assets for pair and hedge trading.
Cointegration
SpreadTrade - Auto-Cointegration (ps5)Decsription: Auto-Cointegration-Based Pair Trading Strategy (revised version)
To review, there are three popular styles of Pair trading: distance-based pair trading, correlation-based pair trading and cointegration-based pair trading. Typically, they require preliminary statistical estimation of the viability of the corresponding strategy.
Basically a pair trade strategy boils down to shorting the outperforming instrument and going long on the underperforming instrument whenever the temporary correlation weakens which means one instrument is going up and another is going down. Apart from the typical cointegration strategy which employs two cointegrated instruments, this script uses just one instrument, in base timeframe and in lagged timeframe, actually making it an auto-cointegration, or better still, an auto-correlation strategy.
Notice that each moving average function may require different Threshold settings.The orange cross symbol indicates the exit points. To filter out the signals use higher values for the LongWindow and the Threshold parameters. Also pay attention that in some cases with some moving averages the color of the signals has to be inverted.
Cointegration Matrix (Indices)This indicator plots a cointegration matrix for pairings of most major stock indices. The matrix is populated with ADF t-stats (from an ADF-test with 1 lag). An ADF-test (Augmented Dickey-Fuller test) tests the null hypothesis that an AR process has a unit root. If rejected, the alternative hypothesis is usually that the AR process is either stationary or trend-stationary.
Technically what we're doing is running an ADF-test on the residuals (spread) of each pairing (i.e. a cointegration test). So we're testing if there is a unit root in the spread between the two assets of a pairing. A unit root being present in the spread essentially means the spread varies randomly over time, and any mean reversion in the spread is very hard to predict, or may never happen. If there is no evidence of a unit root, the spread (distance between the assets) should remain more or less constant over time, or rise/fall in close to the same rate over time (if it can be modelled as an AR process). The more negative the number, the stronger the rejection of the idea that the spread has a unit root. So in statistics we choose a critical value that corresponds to a confidence level of the test. In this case, -3.238 equals a confidence level of 90%, -3.589 equals a confidence level of 95% and -4.375 equals a confidence level of 99%. So the colors are based on the confidence level of the test statistic (the t-stat, i.e. the number of the pairing in the matrix). So if the number is greater than -3.238 it is green, if it's between -3.238 and -3.589 it's yellow, if it's between -3.589 and -4.375 it's orange, and if its lower than -4.375 it's red.
There are multiple ways to interpret the results. A strong rejection of the presence of a unit root (i.e. a value of -4.375 or below) is not a guarantee that there is no unit root, or that any of the two alternative hypotheses (that the spread is stationary or trend-stationary) are correct. It only means that in 99% of the cases, if the spread is an AR process, the test is right, and there is no unit root in the spread. Therefore, the results of this test is no guarantee that the result proves one of the alternative solutions. Green therefore means that a unit root cannot be ruled out (which can be interpreted as "the two indices probably don't move together over time"), and red means that a unit root is likely not present (which can be interpreted as "the two indices may move together over time").
NOTE: The indicator can take a while to load since it is loading data from 20 stock indices and calculating the ADF t-stat for 380 pairings.
Cointegration Matrix (FX)This indicator plots a cointegration matrix for pairings of all 28 major forex pairs. The matrix is populated with ADF t-stats (from an ADF-test with 1 lag). An ADF-test (Augmented Dickey-Fuller test) tests the null hypothesis that an AR process has a unit root. If rejected, the alternative hypothesis is usually that the AR process is either stationary or trend-stationary.
Technically what we're doing is running an ADF-test on the residuals (spread) of each pairing (i.e. a cointegration test). So we're testing if there is a unit root in the spread between the two assets of a pairing. A unit root being present in the spread essentially means the spread varies randomly over time, and any mean reversion in the spread is very hard to predict, or may never happen. If there is no evidence of a unit root, the spread (distance between the assets) should remain more or less constant over time, or rise/fall in close to the same rate over time (if it can be modelled as an AR process). The more negative the number, the stronger the rejection of the idea that the spread has a unit root. So in statistics we choose a critical value that corresponds to a confidence level of the test. In this case, -3.238 equals a confidence level of 90%, -3.589 equals a confidence level of 95% and -4.375 equals a confidence level of 99%. So the colors are based on the confidence level of the test statistic (the t-stat, i.e. the number of the pairing in the matrix). So if the number is greater than -3.238 it is green, if it's between -3.238 and -3.589 it's yellow, if it's between -3.589 and -4.375 it's orange, and if its lower than -4.375 it's red.
There are multiple ways to interpret the results. A strong rejection of the presence of a unit root (i.e. a value of -4.375 or below) is not a guarantee that there is no unit root, or that any of the two alternative hypotheses (that the spread is stationary or trend-stationary) are correct. It only means that in 99% of the cases, if the spread is an AR process, the test is right, and there is no unit root in the spread. Therefore, the results of this test is no guarantee that the result proves one of the alternative solutions. Green therefore means that a unit root cannot be ruled out (which can be interpreted as "the two forex pairs probably don't move together over time"), and red means that a unit root is likely not present (which can be interpreted as "the two forex pairs may move together over time").
One possible way to use this indicator is to make sure you don't trade two pairs that move together at the same time. So basically the idea is that if you already have a trade open in one of the currency pairs of the pairing, only enter a trade in the other currency pair of that pairing if the color is green, or you may be doubling your risk.
NOTE: The indicator can take a while to load since it is loading data from 28 pairs and calculating the ADF t-stat for 756 pairings.
SpreadTrade - CointegrationThe Cointegration strategy is to short the outperforming instrument and go long on the underperforming instrument
whenever the temporary correlation weakens which means one instrument going up and another going down.
Here, instead of two different instruments two timeframes of the same instrument are used, lower and higher.