Recursive Linear Regression Open - Close For ThinkOrSwim

armybender

Active member
I got this idea from the Linear Regression bars recently posted, and from the Recursive EMA posted a month or so ago. I combined the two (sort of) and created a recursive linear regression that subtracts the open from the close. It's something like an EMA, but since it's based on Linear regression you get a smoother plot.

I don't know that I'll use this or not, but it might have value for some folks out there.

Here's a picture on a 1,000 tick chart.
1690502939911.png


Here's a picture on a daily chart:
1690502998197.png



Here's the code:
Ruby:
#DECLARATIONS
declare lower;


#USER INPUTS
input linRegLength = 21;
input minLRLength = 10;             # "Minimum Moving Average Length"
input maxLRLength = 150;            # "Maximum Moving Average Length"
input lengthMoveBy = 5;             # "Move By"
input smoothLength = 5;             # "Smooth Length"
input adjValue = 1000;              # "Adjustment Value"


#GLOBAL COLOR DEFINITIONS
DefineGlobalColor("grow_above" , CreateColor(38, 166, 154));
DefineGlobalColor("fall_above" , CreateColor(178, 223, 219));
DefineGlobalColor("grow_below" , CreateColor(205, 155, 160));
DefineGlobalColor("fall_below" , CreateColor(180, 75, 75));
DefineGlobalColor("Signal"     , CreateColor(255, 235, 59));


#DEFINITIONS AND CALCULATIONS
def isLast = isNaN(close);

def lrOpen = LinearRegCurve(length = linRegLength, price = open);
def lrClose = LinearRegCurve(length = linRegLength, price = close);


def lrOpenTotalLength = fold i1 = minLRLength to maxLRLength
                        with p1 do
                        if  (i1 % lengthMoveBy) == 0 then
                            p1 + i1
                        else
                            p1;

def lrOpenWtdAvg = fold i2 = minLRLength to maxLRLength
                   with p2 do
                   if (i2 % lengthMoveBy) == 0 then
                       p2 +
                       (((lrOpen - ((fold j2 = 0 to i2
                                    with q2 do
                                    q2 +  GetValue(lrOpen, j2)) / i2)) /  lrOpen) * i2)
                   else
                       p2;


def lrCloseTotalLength = fold i3 = minLRLength to maxLRLength
                         with p3 do
                         if  (i3 % lengthMoveBy) == 0 then
                             p3 + i3
                         else
                             p3;

def lrCloseWtdAvg = fold i4 = minLRLength to maxLRLength
                   with p4 do
                   if (i4 % lengthMoveBy) == 0 then
                       p4 +
                       (((lrClose - ((fold j4 = 0 to i4
                                    with q4 do
                                    q4 +  GetValue(lrClose, j4)) / i4)) /  lrClose) * i4)
                   else
                       p4;

def openWtdAvg = lrOpenWtdAvg / lrOpenTotalLength;
def closeWtdAvg = lrCloseWtdAvg / lrCloseTotalLength;

def lrDirectionData = (ExpAverage(closeWtdAvg, smoothLength) - ExpAverage(openWtdAvg, smoothLength)) * adjValue;

def color = if lrDirectionData > 0 then if lrDirectionData > lrDirectionData[1] then 2 else 1 else
          if lrDirectionData > lrDirectionData[1] then -1 else -2;


#PLOTS
plot zeroLine = if isLast then Double.NaN else 0;

plot lrDirection = lrDirectionData;
 

#FORMATTING
lrDirection.AssignValueColor(
    if color == 2 then GlobalColor("grow_above") else
    if color == 1 then GlobalColor("fall_above") else
    if color == -1 then GlobalColor("grow_below") else GlobalColor("fall_below")
);
lrDirection.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
lrDirection.SetLineWeight(5);
lrDirection.HideBubble();

zeroLine.SetDefaultColor(Color.GRAY);
zeroLine.HideTitle();
zeroLine.HideBubble();
 

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
479 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