Volume-Supported Linear Regression Trend for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
yMoj96c.png

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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
499 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top