//@version=4 // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // Linear Regression Slope Annualized with R-squared Histogram // by SparkyFlary study(title="Linear Regression Slope Annualized with R-squared Histogram", shorttitle="Linear Regression Slope", overlay=false) length = input(80, title="Slope length") rSquaredThreshold = input(0.0, title="R-squared threshold", minval=0, maxval=1) withAnnualize = input(true, title="Annualize slope?") daysInYear = input(240, title="Number of bars in a year(if slope is annualized)") src = input(close, title="Source") slopeCalcMethod = input("wma-sma", options=["wma-sma", "(linreg-linreg[length])/length"]) nlPrice = log(src) slope = slopeCalcMethod=="wma-sma" ? 6*(wma(nlPrice, length) - sma(nlPrice, length))/(length-1) : (linreg(nlPrice,length,0)-linreg(nlPrice,length,0)[length])/length rSquared = pow(correlation(cum(1), nlPrice, length), 2) slopeAnnualized = (pow(exp(slope),daysInYear)-1) * (rSquared) * 100 mult = withAnnualize ? slopeAnnualized * rSquared : slope * rSquared trend = rSquared>rSquaredThreshold rising = mult>mult[1] falling = mult0 down = mult<0 up_rising_color = color.new(color.green, 0) up_falling_color = color.new(color.green, 50) down_falling_color = color.new(color.red, 0) down_rising_color = color.new(color.red, 50) c = trend and up and rising?up_rising_color:trend and up and falling?up_falling_color:trend and down and falling?down_falling_color:trend and down and rising?down_rising_color : color.gray plot(mult, title="Annualized slope x R-squared", style=plot.style_histogram, color = c, linewidth=3)