SpreadTrade - Distance (ps4 ver. 2)This script implements a rebrushed distance-based pair trading strategy. In this strategy, normally they trade the difference between the prices of two instruments. This difference is also called spread. Here, however we’ll trade the difference between two time frames of one instrument. And that's the main trick. Common procedure consists of the following steps:
1. Select two CORRELATED stocks. Here we'll use the same instrument in different TFs.
2. Generate the spread by calculating the difference between the prices/instruments. For distance based pair trading, we need to (rescale the data first and then) check the distance between them.
3. Define the logic to trade the spread and generate the trading signals. In this example we’ll calculate the rolling mean and rolling standard deviation of the spread. Whenever the spread goes above a rolling mean by one standard deviation, we’ll short the spread expecting the mean reversion behavior to hold true. And whenever the spread goes below its rolling mean by one standard deviation, we’ll go long on the spread.
Mind that the meaning of the orange and blue signals depends on whether tf variable is smaller or larger than the built-in timeframe.multiplier variable, i.e. tf of the chart.
For details see analyticsprofile.com
Donate: PayPal (paypal.me)
Distance
Volume Weighted DistanceThis script holds several useful functions from statistics and machine learning (ML) and takes measurement of a volume weighted distance in order to identify local trends. It attempts at applying ML techniques to time series processing, shows how different distance measures behave and gives you an arsenal of tools for your endeavors. Tested with BTCUSD.
REM: oddly enough, many people forget that the scripts in PS are generally just STUDIES, i.e. exercises, experiments, trials, and do not embody a final solution. Please treat them as intended ;))
[RS]Function - Minkowski_distancecopy pasted description..
Minkowski distance is a metric in a normed vector space. Minkowski distance is used for distance similarity of vector. Given two or more vectors, find distance similarity of these vectors.
Closed Form Distance VolatilityIntroduction
Calculating distances in signal processing/statistics/time-series analysis imply measuring the distance between two probability distribution, i am not really familiar with distances but since some formulas are in closed form they can be easily used for volatility estimation. This volatility indicator will use three methods originally made to measure the distance of gaussian copulas, using those methods for volatility estimation is fairly easy and provide a different approach to statistical dispersion.
The indicator have a length parameter and a method parameter to select the method used for volatility estimation, i describe each methods below.
Hellinger Method
Each method will use the rolling sum of the low price and the rolling sum of the high price instead of probability distributions. The Hellinger method have many application from the measurement of distances to the use as a cost function for neural networks.
Its closed form is defined as the square root of 1 - a^0.25b^0.25/(0.5a + 0.5b)^0.5 where a and b are both positive series. In our indicator a is the rolling sum of the high price and b the rolling sum of the low price. This method give a classic estimation of volatility.
Bhattacharyya Method
The Bhattacharyya method is another method who use a natural logarithm, this method can visually filter small volatility variation. It is defined as 0.5 * log((0.5a+0.5b)/√(ab)) .
Wasserstein Method
This method was originally using a trimmed mean for its calculation. The original method is defined as the square of the trimmed mean of a + b - 2√(a^0.5ba^0.5) , a median has been used instead of a trimmed mean for efficiency sake, both central tendency estimators are robust to outliers.
Conclusion
I showed that closed form formulas for distance calculation could be derived into volatility estimators with different properties. They could be used with series in a range of (0,1) to provide a smoothing variable for exponential smoothing.
Inverse Distance Weighted Moving AverageThe weights of this moving average are the sums of distances between points.
Good luck!
Distance Weighted Moving AverageAdopted to Pine from systemtradersuccess.com
They wrote that this average is designed to be a robust version of a moving average to reduce the impact of outliers, but I dont see a significant difference comparing it with SMA. So, I published it for the educational purposes.
To learn more about the robust filters and averages google Hampel Filter, Interquartile Range Filter and Recursive Median Filter (or any other filter that is based on quartiles).
Good luck!
Ehlers Distance Coefficient FilterThis indicator was described by John F. Ehlers in his book "Rocket Science for Traders" (2001, Chapter 18: Ehlers Filters).
percentage distanceI do not know good english for explanation sorry.
Percentage distance of price to 21ma. Percentage distance of 21ma to 55ma
if such a thing is needed, it's here