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.
Here's a picture on a daily chart:
Here's the code:
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.
Here's a picture on a daily chart:
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();