Run TimerThis is a simple utility which counts the number of bars and time elapsed after starting the script. This can be used in time or bars based conditions to modify script behavior.
This particular script does the following:
Starts timer when script is added to chart
Timer is green when starting and continues to be in green if the right Auth key is used in input (Which is hardcoded as 1234 here)
If right auth key is not provided in the input, timer background turns red after trial bars.
Script can be modified to use elapsed time instead.
Thanks to @Bjorgum on assisting on few calculations :)
Utility
Function - Forecast LinearFunction to calculate a forecast using a linear regression approach, this is the same function used on excel and other data sheet programs.
reference:
- support.microsoft.com
- stackoverflow.com
Function - Sequence From SeriesFunction to create a array from a sample taken from a series (ex:. close, hlc3...).
Filter Information Box - PineCoders FAQWhen designing filters it can be interesting to have information about their characteristics, which can be obtained from the set of filter coefficients (weights). The following script analyzes the impulse response of a filter in order to return the following information:
Lag
Smoothness via the Herfindahl index
Percentage Overshoot
Percentage Of Positive Weights
The script also attempts to determine the type of the analyzed filter, and will issue warnings when the filter shows signs of unwanted behavior.
DISPLAYED INFORMATION AND METHODS
The script displays one box on the chart containing two sections. The filter metrics section displays the following information:
- Lag : Measured in bars and calculated from the convolution between the filter's impulse response and a linearly increasing sequence of value 0,1,2,3... . This sequence resets when the impulse response crosses under/over 0.
- Herfindahl index : A measure of the filter's smoothness described by Valeriy Zakamulin. The Herfindahl index measures the concentration of the filter weights by summing the squared filter weights, with lower values suggesting a smoother filter. With normalized weights the minimum value of the Herfindahl index for low-pass filters is 1/N where N is the filter length.
- Percentage Overshoot : Defined as the maximum value of the filter step response, minus 1 multiplied by 100. Larger values suggest higher overshoots.
- Percentage Positive Weights : Percentage of filter weights greater than 0.
Each of these calculations is based on the filter's impulse response, with the impulse position controlled by the Impulse Position setting (its default is 1000). Make sure the number of inputs the filter uses is smaller than Impulse Position and that the number of bars on the chart is also greater than Impulse Position . In order for these metrics to be as accurate as possible, make sure the filter weights add up to 1 for low-pass and band-stop filters, and 0 for high-pass and band-pass filters.
The comments section displays information related to the type of filter analyzed. The detection algorithm is based on the metrics described above. The script can detect the following type of filters:
All-Pass
Low-Pass
High-Pass
Band-Pass
Band-Stop
It is assumed that the user is analyzing one of these types of filters. The comments box also displays various warnings. For example, a warning will be displayed when a low-pass/band-stop filter has a non-unity pass-band, and another is displayed if the filter overshoot is considered too important.
HOW TO SET THE SCRIPT UP
In order to use this script, the user must first enter the filter settings in the section provided for this purpose in the top section of the script. The filter to be analyzed must then be entered into the:
f(input)
function, where `input` is the filter's input source. By default, this function is a simple moving average of period length . Be sure to remove it.
If, for example, we wanted to analyze a Blackman filter, we would enter the following:
f(input)=>
pi = 3.14159,sum = 0.,sumw = 0.
for i = 0 to length-1
k = i/length
w = 0.42 - 0.5 * cos(2 * pi * k) + 0.08 * cos(4 * pi * k)
sumw := sumw + w
sum := sum + w*input
sum/sumw
EXAMPLES
In this section we will look at the information given by the script using various filters. The first filter we will showcase is the linearly weighted moving average (WMA) of period 9.
As we can see, its lag is 2.6667, which is indeed correct as the closed form of the lag of the WMA is equal to (period-1)/3 , which for period 9 gives (9-1)/3 which is approximately equal to 2.6667. The WMA does not have overshoots, this is shown by the the percentage overshoot value being equal to 0%. Finally, the percentage of positive weights is 100%, as the WMA does not possess negative weights.
Lets now analyze the Hull moving average of period 9. This moving average aims to provide a low-lag response.
Here we can see how the lag is way lower than that of the WMA. We can also see that the Herfindahl index is higher which indicates the WMA is smoother than the HMA. In order to reduce lag the HMA use negative weights, here 55% (as there are 45% of positive ones). The use of negative weights creates overshoots, we can see with the percentage overshoot being 26.6667%.
The WMA and HMA are both low-pass filters. In both cases the script correctly detected this information. Let's now analyze a simple high-pass filter, calculated as follows:
input - sma(input,length)
Most weights of a high-pass filters are negative, which is why the lag value is negative. This would suggest the indicator is able to predict future input values, which of course is not possible. In the case of high-pass filters, the Herfindahl index is greater than 0.5 and converges toward 1, with higher values of length . The comment box correctly detected the type of filter we were using.
Let's now test the script using the simple center of gravity bandpass filter calculated as follows:
wma(input,length) - sma(input,length)
The script correctly detected the type of filter we are using. Another type of filter that the script can detect is band-stop filters. A simple band-stop filter can be made as follows:
input - (wma(input,length) - sma(input,length))
The script correctly detect the type of filter. Like high-pass filters the Herfindahl index is greater than 0.5 and converges toward 1, with greater values of length . Finally the script can detect all-pass filters, which are filters that do not change the frequency content of the input.
WARNING COMMENTS
The script can give warning when certain filter characteristics are detected. One of them is non-unity pass-band for low-pass filters. This warning comment is displayed when the weights of the filter do not add up to 1. As an example, let's use the following function as a filter:
sum(input,length)
Here the filter pass-band has non unity, and the sum of the weights is equal to length . Therefore the script would display the following comments:
We can also see how the metrics go wild (note that no filter type is detected, as the detected filter could be of the wrong type). The comment mentioning the detection of high overshoot appears when the percentage overshoot is greater than 50%. For example if we use the following filter:
5*wma(input,length) - 4*sma(input,length)
The script would display the following comment:
We can indeed see high overshoots from the filter:
@alexgrover for PineCoders
Look first. Then leap.
Bands-Trailing Stop UtilityIntroduction
Bands and trailing stops are important indicators in technical analysis, while we could think that both are different they can be in fact closely related, at least in the way they are made. Bands and trailing stops can be made from a simple central tendency estimator, like a moving average, and from a volatility estimator like standard deviation, atr...etc.
This is why i propose this utility that allow you to make bands and trailing stops from any indicator in the price chart.
How To Use
All you have to do is select the indicator you want to make bands from in the settings, so just open the Bands-Trailing Stop Utility indicator settings and select your indicator in "Source". Make sure your source indicator is not in "hide" mode.
For example here i'am using a moving average as source for the indicator. Mult control how spread the bands are from each others, by default mult = 1, if we use mult = 2 we get :
Mult can be non-integer as well as lower than 1 (when lower than 1 the bands would be closer to each others)
Error/Volatility Estimators
You can choose from a wide variety of volatility estimators, select the estimator from the "Method" scrolling parameter in settings, by default the indicator will use the running mean absolute error (MAE) which don't use length. Other estimators use length, making length = to the period of the source indicator can help get better results.
The root moving averaged squared error (RMASE) is just the square root of the simple moving average of the squared difference between the closing price and the source indicator. length control the period of the moving average of RMASE.
You can also use the average true range with period length. It might work better with low lagging moving averages.
The range is simply the difference between the highest and lowest over length periods of the source indicator.
Stdev is simply the price running standard deviation.
Trailing Stop
When the trailing stop mode is checked the bands will be replaced by a trailing stop, the trailing stop will still depend on every settings of the indicator like mult/volatility estimator...etc.
Conclusion
You might find an use to this tool if you want to make bands/trailing stops from pretty much everything. The indicator used as source for the examples is a smooth exponential averager that i could share if i see interest from peoples.
Thanks for reading !
Sort pseudo-array v3Based on Sort pseudo-array v2 by apozdnyakov
- fixed issue where if the same number is repeated in the list it gets skipped
- replaced hardcoded 10000 and -10000 values with na in case those values are in the list
- added sort_all to demonstrate how to sort a list if the length is fixed
Data Gap DetectionThis simple script checks for data gaps in an intra-day TradingView chart. I have found that BitMEX 1-minute data is coming in rather holey lately, so I wrote this just to see how prevalent the problem is. It should work on any intra-day timeframe, not just 1-minute.
V1: initial release.
Moving Average Bundle3 Moving Average Lines. All parameters are configurable via user input.
Each of these moving average lines already exist as individual indicators in TradingView. This script just bundles them for one stop shopping. It's helpful if you're limited by the number of indicators per chart. And highly educational: quickly compare the different averaging methods!
sma: Simple Moving Average
rma: Recursive Moving Average
ema: Exponential Moving Average
wma: Weighted Moving Average
vma: Volume Weighted Moving Average
wma: Weighted Moving Average
alma: Arnaud Legoux Moving Average
Note: Only Pine built-in functions are included.
[RS]Color Gradient FunctionA simple function method to build color gradient palettes to use in scripts