Author Message:
https://www.tradingview.com/v/XVPeRgdt/
Linear Regression gives us some abilities to calculate the trend and if we combine it with volume then we may get very good results. Because if there is no volume support at up/downtrends then the trend may have a reversal soon. we also need to check the trend in different periods. With all this info, I developed Volume-Supported Linear Regression Trend script. The script checks linear regression of price and volume and then calculates trend direction and strength.
You have option to set Source, Short-Term Period and Long-Term Period. you can set them as you wish.
By default:
Close is used as "Source"
Short-Term Period is 20
Long-Term Period is 50
CODE:
CSS:
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © LonesomeTheBlue
#study("Volume-Supported Linear Regression Trend", "VSLRT", overlay = false)
# Converted and mod by Sam4Cok@Samer800 - 02 /2023
declare lower;
input BarColor = yes;
input src = close;
input ShortTermLength = 20; # "Short Term Length"
input LongTermLength = 50; # "Long Term Length"
input Smoothing = yes;
input SmoothingLength = 5;
DefineGlobalColor("ccol11" , CreateColor(0, 255, 0));
DefineGlobalColor("ccol12" , CreateColor(0, 188, 0));
DefineGlobalColor("ccol13" , CreateColor(0, 112, 0));
DefineGlobalColor("ccol21" , CreateColor(255, 0, 0));
DefineGlobalColor("ccol22" , CreateColor(191, 0, 0));
DefineGlobalColor("ccol23" , CreateColor(115, 0, 0));
DefineGlobalColor("col11" , CreateColor(0, 142, 255));
DefineGlobalColor("col12" , CreateColor(0, 110, 197));
DefineGlobalColor("col13" , CreateColor(2, 68, 120));
DefineGlobalColor("col21" , CreateColor(253, 151, 1));
DefineGlobalColor("col22" , CreateColor(206, 122, 0));
DefineGlobalColor("col23" , CreateColor(102, 61, 0));
def len1 = max(ShortTermLength, 5);
def len2 = max(LongTermLength, 5);
script linreg {
input y = close;
input n = 20;
input offset = 1;
def x = x[1] + 1;
def a = (n * Sum(x * y, n) - Sum(x, n) * Sum(y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
def b = (Sum(Sqr(x), n) * Sum(y, n) - Sum(x, n) * Sum(x * y, n) ) / ( n * Sum(Sqr(x), n) - Sqr(Sum(x, n)));
plot Inertia = a * (x - offset) + b;
}
#//Calculate Buy/Sell Volume
#_rate(cond) =>
script _rate {
input cond = yes;
#// get the size of top/bottom/body of the candle
def tw = high - Max(open, close);
def bw = Min(open, close) - low;
def body = AbsValue(close - open);
def ret1 = 0.5 * (tw + bw + (if cond then 2 * body else 0)) / (tw + bw + body);
def ret = if IsNaN(ret1) then 0.5 else ret1;
plot result = ret;
}
#// Calculate Regression Slope for Buy/Sell Volumes
#_get_trend(len)=>
script _get_trend {
input len = 20;
def deltaup = volume * _rate(open <= close);
def deltadown = volume * _rate(open > close);
def slope_volume_up = Inertia(deltaup, len) - linreg(deltaup, len, 1);
def slope_volume_down = Inertia(deltadown, len) - linreg(deltadown, len, 1);
plot up = slope_volume_up;
plot dn = slope_volume_down;
}
#// get short/long-term regression slope
def slopePrice = Inertia(src, len1) - linreg(src, len1, 1);
def slopePrice_lt = Inertia(src, len2) - linreg(src, len2, 1);
def slope_price = if Smoothing then ExpAverage(slopePrice, SmoothingLength) else slopePrice;
def slope_price_lt = if Smoothing then ExpAverage(slopePrice_lt, SmoothingLength) else slopePrice_lt;
#// get buy/sell volume regression slopes for long term period
def slopeVolume_up_lt = _get_trend(len2).up;
def slopeVolume_down_lt = _get_trend(len2).dn;
def slope_volume_up_lt = if Smoothing then ExpAverage(slopeVolume_up_lt, SmoothingLength) else slopeVolume_up_lt;
def slope_volume_down_lt = if Smoothing then ExpAverage(slopeVolume_down_lt, SmoothingLength) else slopeVolume_down_lt;
#// coloring columns
def Line_col = if slope_price_lt > 0 then if slope_volume_up_lt > 0 then
if slope_volume_up_lt > slope_volume_down_lt then 3 else 2 else 1 else
if slope_price_lt < 0 then if slope_volume_down_lt > 0 then
if slope_volume_up_lt < slope_volume_down_lt then -3 else -2 else -1 else 0;
#// Long term trend
plot LongTermTrend = slope_price_lt * len2;
LongTermTrend.SetLineWeight(2);
LongTermTrend.AssignValueColor(if Line_col == 3 then GlobalColor("col11") else
if Line_col == 2 then GlobalColor("col12") else
if Line_col == 1 then GlobalColor("col13") else
if Line_col ==-3 then GlobalColor("col21") else
if Line_col ==-2 then GlobalColor("col22") else
if Line_col ==-1 then GlobalColor("col23") else Color.GRAY);
#// get buy/sell volume regression slopes for short term period
def slopeVolume_up = _get_trend(len1).up;
def slopeVolume_down = _get_trend(len1).dn;
def slope_volume_up = if Smoothing then ExpAverage(slopeVolume_up, SmoothingLength) else slopeVolume_up;
def slope_volume_down = if Smoothing then ExpAverage(slopeVolume_down, SmoothingLength) else slopeVolume_down;
#// coloring columns
def column_col = if slope_price > 0 then if slope_volume_up > 0 then
if slope_volume_up > slope_volume_down then 3 else 2 else 1 else
if slope_price < 0 then if slope_volume_down > 0 then
if slope_volume_up < slope_volume_down then -3 else -2 else -1 else 0;
#/ short term trend
plot ShortTermTrend = slope_price * len1;
ShortTermTrend.SetLineWeight(4);
ShortTermTrend.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
ShortTermTrend.AssignValueColor(if column_col == 3 then GlobalColor("ccol11") else
if column_col == 2 then GlobalColor("ccol12") else
if column_col == 1 then GlobalColor("ccol13") else
if column_col ==-3 then GlobalColor("ccol21") else
if column_col ==-2 then GlobalColor("ccol22") else
if column_col ==-1 then GlobalColor("ccol23") else Color.GRAY);
AssignPriceColor(if !BarColor then Color.CURRENT else
if column_col == 3 then GlobalColor("ccol11") else
if column_col == 2 then GlobalColor("ccol12") else
if column_col == 1 then GlobalColor("ccol13") else
if column_col ==-3 then GlobalColor("ccol21") else
if column_col ==-2 then GlobalColor("ccol22") else
if column_col ==-1 then GlobalColor("ccol23") else Color.GRAY);
#--- END CODE