The functions shared in this script include:
- Rolling sum: Sum( src ,p)
- Simple moving average: Sma( src ,p)
- Rolling variance: Variance( src ,p)
- Rolling standard deviation: Stdev( src ,p)
- Rolling covariance: Covariance(x,y,p)
- Rolling correlation: Correlation(x,y,p)
If p is a float then it is rounded to the nearest int.
How to Use the Script
Most of the functions in the script are dependent on the Sma function. The Correlation function uses the Covariance and Stdev functions. Be sure you include all the required functions in your script.
Make sure the series you use as the length argument is greater than 0, else the functions will return na. When using a series as length argument, the following error might appear:
Pine cannot determine the referencing length of a series. Try using max_bars_back in the study or strategy function.
The chart shows the Sma, Stdev, Covariance and Correlation functions. The uses the closing price as input and bars as period length where:
bars = barssince(change(security(syminfo.tickerid,"D",close,lookahead=true)))
The Stdev uses the closing price as input and bars + 9 as period length. The Covariance and Correlation use the closing price as x and bar_index as y, with bars + 9 as period length.