The R2 function in ProBuilder language calculates the R-Squared value, which is a statistical measure of how well the regression predictions approximate the real data points. An R-Squared value is used in trading to determine the strength of a trend based on linear regression over a specified number of periods. The closer the value is to 1, the stronger the trend.
R2[N](price)
Where N represents the number of periods over which the R-Squared value is calculated, and price indicates the price series used in the calculation.
i1 = R2[20](close)
LR1 = linearregression[20](close)
LR2 = linearregression[20](close[1])
if(i1 > 0.4 AND LR1 < LR2) THEN
bullish = 0
bearish = -1
ELSIF(i1 > 0.4 AND LR1 > LR2) THEN
bullish = 1
bearish = 0
ELSE
bullish = 0
bearish = 0
ENDIF
RETURN bullish, bearish
In this example, the R-Squared value is calculated over the last 20 periods of the close price. The script then compares the current linear regression value (LR1) with the previous period’s linear regression value (LR2). Depending on the R-Squared value and the comparison of LR1 and LR2, the script sets the bullish and bearish indicators.
This function is particularly useful for traders who use statistical methods to validate the strength of trends and make informed decisions based on historical price movements.