Least Squares Indicator for ThinkorSwim

ebtrader

New member
Good morning all! Thanks for including me in this group.

Here is one of my favorite studies. It uses the Least Squares Method. I would love your thoughts.

There is nothing out there that can catch the turns as well as this baby can!

Code:
 #LeastSquares created by tradescripter
    #Dec. 24, 2010 Merry Christmas
    #It uses the Least-Squares Method to forecast a new price
    #For an explanation of Least-Squares method see:
    #http://en.wikiversity.org/wiki/Least-Squares_Method


#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2


#FuturePredictor

input price = close;
input length = 9;
input PredictionLineOn = Yes;
# Input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;


def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;

def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
    #sx is the sum of all the deviations from time for the last x bars
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
        #sy is the sum of all the deviations from price for the last x bars
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
        #m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();

Sb1agbv.png
 

Attachments

  • Sb1agbv.png
    Sb1agbv.png
    64.5 KB · Views: 112
Last edited:

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

Version with some additions.

Code:
# LeastSquaresMethodForecast_R1V2
    #
    # http://finance.groups.yahoo.com/group/TOS_thinkscript/message/6671
    # R1V0   2010.12.24          TradeScripter
    #
    # R1V1   2010.12.26:06:22    ebtrader
    # R1V2   2010.12.27:08:00    KumoBob
    #        Added Price Color and Arrows
    #        Option to remove any plot
    #        Added Chart Labels
    #         
# R1V3   2010.12.27:16:10    KumoBob
#        Added plot Today Only
#        Added Hints
# R2V1   2010.12.28:15:42 EBTrader
#        Added Regression Channel
#        Added Money Management
#        Added Bubble Option

# R2V2   2010.01.07: 16:06 KumoBob
#         Alternated Arrows

# R2V3   2010.01.11: 18:30 EBTrader
#         Made colors more consistent and added sell bubble


    #This program created by tradescripter
    #Dec. 24, 2010 Merry Christmas
    #It uses the Least-Squares Method to forecast a new price
    #For an explanation of Least-Squares method see:
    #http://en.wikiversity.org/wiki/Least-Squares_Method

#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2


AssignBackgroundColor(CreateColor(8, 0, 15));
input length = 9;
input price = HL2;
input PriceColorOn = Yes;
input ArrowsOn = Yes;
input PredictionLineOn = Yes;
input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;
def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;

def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
    #sx is the sum of all the deviations from time for the last x bars
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
        #sy is the sum of all the deviations from price for the last x bars
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
        #m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();


AssignPriceColor(if !PriceColorOn then Color.CURRENT else if Prediction > Prediction[1] then (CreateColor(000, 255, 255)) else if Prediction < Prediction[1] then (CreateColor(255, 160, 000)) else Color.GRAY);
AddLabel(PriceColorOn , "Price Color On", if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);

AddLabel(ArrowsOn, "MagentaArrows=LeastSquares",  if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);

def spaceman = Average(high - low) * space;
def xover = Crosses(Prediction, Prediction[1], CrossingDirection.ANY);
plot UpArrow = if Today && xover && Prediction > Prediction[1] then low - spaceman else Double.NaN;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow.SetDefaultColor(Color.MAGENTA);
plot DnArrow = if Today && xover && Prediction < Prediction[1] then high + spaceman else Double.NaN;
DnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow.SetDefaultColor(Color.MAGENTA);

    #    plot firstup = crosses((Prediction > low), yes) && Prediction < Prediction[1];
    #    firstup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

    #    plot firstdown = crosses((Prediction < high), yes) && Prediction > Prediction[1];
    #    firstdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

rec LastArrow = if low > Prediction && LastArrow[1] <> 1 && Prediction < Prediction[1] then 1 else
    if high < Prediction && LastArrow[1] <> -1 && Prediction > Prediction[1] then -1 else LastArrow[1];

plot firstup = if Today && LastArrow > 0 && LastArrow <> LastArrow[1]  then low else Double.NaN;
firstup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstup.SetLineWeight(5);
firstup.SetDefaultColor(Color.YELLOW);
plot firstdown = if Today && LastArrow < 0 && LastArrow <> LastArrow[1]  then high else Double.NaN;
firstdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
firstdown.SetLineWeight(5);
firstdown.SetDefaultColor(Color.YELLOW);






#
# Linear Regression with Standard Deviation Channels
#
#  Written by Robert Hall
#   Released 10 April 2009
#
#
#  Standard Deviation can be input to 2 decimal places
#

input standardDeviation = 1.5;
input startDateYyyyMmDd = 20110105;
input startTime = 0030;



plot MiddleLR = InertiaAll(price, length, startDateYyyyMmDd, startTime, extendtoright = 1);

def dist = HighestAll(AbsValue(MiddleLR - price)) * standardDeviation / 2;

plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.SetDefaultColor(Color.WHITE);
MiddleLR.SetLineWeight(1);
MiddleLR.SetStyle(Curve.FIRM);

UpperLR.SetDefaultColor(Color.CYAN);
UpperLR.SetLineWeight(1);
UpperLR.SetStyle(Curve.FIRM);

LowerLR.SetDefaultColor(Color.CYAN);
LowerLR.SetLineWeight(1);
LowerLR.SetStyle(Curve.FIRM);

#    Money Management

# Longs

def exposure20pct = LastArrow < 0 && LastArrow <> LastArrow[1];

def exposure40pct = Prediction[3] > Prediction[4] && Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1] ;

def exposure60pct = Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1];

def exposure80pct = Prediction[1] > Prediction[2] && Prediction > Prediction[1];

def exposure100pct = xover && Prediction > Prediction[1];

# Shorts

def exposureneg20pct = LastArrow > 0 && LastArrow <> LastArrow[1];

def exposureneg40pct = Prediction[3] < Prediction[4] && Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg60pct = Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg80pct = Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg100pct = xover && Prediction < Prediction[1];



AddLabel(yes, Concat( "", Concat(" ", 
if exposure20pct then "Long 20% " else 
if exposure40pct then "Long 40% " else
if exposure60pct then "Long 60%" else
if exposure80pct then "Long 60%" else
if exposure100pct then "Long 100%" else

if exposureneg20pct then "Short 20% " else 
if exposureneg40pct then "Short 40% " else
if exposureneg60pct then "Short 60%" else
if exposureneg80pct then "Short 60%" else
if exposureneg100pct then "Short 100%" else
"Go Flat")), 

if exposure20pct then Color.CYAN else 
if exposure40pct then  Color.CYAN else 
if exposure60pct then  Color.CYAN else
if exposure80pct then Color.CYAN else 
if exposure100pct then  Color.CYAN else
if exposureneg20pct then Color.ORANGE else 
if exposureneg40pct then Color.ORANGE else
if exposureneg60pct then Color.ORANGE else
if exposureneg80pct then Color.ORANGE else
if exposureneg100pct then Color.ORANGE else
Color.WHITE);

AddChartBubble(BubblesOn && xover && Prediction > Prediction[1] , low, Concat("Buy Baby", HL2), Color.CYAN, yes);

AddChartBubble(BubblesOn && xover && Prediction < Prediction[1] , high, Concat("Sell That Bad Boy", HL2), Color.ORANGE, yes);

# ************************************************************
 
i love those additions as well. feel free to ask me about any of the stuff in the code. If anyone is interested, there is a way to extend those regression channels into the future as well.

has anyone used this indicator at all in the past or currently?
 
Double LS lower study I did a while back. Different lengths to look for color agreement.

yfhXTsF.jpg

Ruby:
# Double Least Squares lower study
# Look for confirmation of agreeing colors.
# Horserider 6/5/2019

# LeastSquaresMethodForecast_R1V2
    #
    # http://finance.groups.yahoo.com/group/TOS_thinkscript/message/6671
    # R1V0   2010.12.24          TradeScripter
    #
    # R1V1   2010.12.26:06:22    ebtrader
    # R1V2   2010.12.27:08:00    KumoBob
    #        Added Price Color and Arrows
    #        Option to remove any plot
    #        Added Chart Labels
    #         
# R1V3   2010.12.27:16:10    KumoBob
#        Added plot Today Only
#        Added Hints
# R2V1   2010.12.28:15:42 EBTrader
#        Added Regression Channel
#        Added Money Management
#        Added Bubble Option

# R2V2   2010.01.07: 16:06 KumoBob
#         Alternated Arrows

# R2V3   2010.01.11: 18:30 EBTrader
#         Made colors more consistent and added sell bubble


    #This program created by tradescripter
    #Dec. 24, 2010 Merry Christmas
    #It uses the Least-Squares Method to forecast a new price
    #For an explanation of Least-Squares method see:
    #http://en.wikiversity.org/wiki/Least-Squares_Method

#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2

declare lower;
AssignBackgroundColor(CreateColor(8, 0, 15));
input length = 9;
input price = HL2;
input PriceColorOn = Yes;
input ArrowsOn = Yes;
input PredictionLineOn = Yes;
input BubblesOn = No;
input ShowTodayOnly = no;
input ShowExtraDays = 0;
input space = 0.333;
def Today = if !ShowTodayOnly then 1 else if GetDay() + ShowExtraDays >= GetLastDay() then 1 else 0;

def AvgPrice = Average(price, length);
def SumTime = fold i = 1 to length + 1 with x = 0 do x + i;
def AvgTime = SumTime / length;
    #sx is the sum of all the deviations from time for the last x bars
def sx = fold j = 1 to length + 1 with y = 0 do y + ((j - AvgTime) * (GetValue(price, length - j, length + 1) - AvgPrice));
        #sy is the sum of all the deviations from price for the last x bars
def sy = fold k = 1 to length + 1 with z = 0 do z + (Power(k - AvgTime, 2));
        #m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m = sx / sy;
def b = AvgPrice - m * AvgTime;
def Prediction = (m * (length + 1)) + b;
plot FuturePrediction = if !PredictionLineOn then Double.NaN else Prediction;
FuturePrediction.AssignValueColor(if FuturePrediction > FuturePrediction[1] then Color.GREEN else Color.RED);
FuturePrediction.SetLineWeight(3);
FuturePrediction.HideBubble();


AssignPriceColor(if !PriceColorOn then Color.CURRENT else if Prediction > Prediction[1] then (CreateColor(000, 255, 255)) else if Prediction < Prediction[1] then (CreateColor(255, 160, 000)) else Color.GRAY);
AddLabel(PriceColorOn , "Price Color On", if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);

AddLabel(ArrowsOn, "MagentaArrows=LeastSquares",  if Prediction > Prediction[1] then Color.GREEN else if Prediction < Prediction[1] then Color.RED else Color.GRAY);

def spaceman = Average(high - low) * space;
def xover = Crosses(Prediction, Prediction[1], CrossingDirection.ANY);
plot UpArrow = if Today && xover && Prediction > Prediction[1] then low - spaceman else Double.NaN;
UpArrow.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow.SetDefaultColor(Color.MAGENTA);
plot DnArrow = if Today && xover && Prediction < Prediction[1] then high + spaceman else Double.NaN;
DnArrow.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow.SetDefaultColor(Color.MAGENTA);

    #    plot firstup = crosses((Prediction > low), yes) && Prediction < Prediction[1];
    #    firstup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

    #    plot firstdown = crosses((Prediction < high), yes) && Prediction > Prediction[1];
    #    firstdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

rec LastArrow = if low > Prediction && LastArrow[1] <> 1 && Prediction < Prediction[1] then 1 else
    if high < Prediction && LastArrow[1] <> -1 && Prediction > Prediction[1] then -1 else LastArrow[1];

plot firstup = if Today && LastArrow > 0 && LastArrow <> LastArrow[1]  then low else Double.NaN;
firstup.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstup.SetLineWeight(5);
firstup.SetDefaultColor(Color.YELLOW);
plot firstdown = if Today && LastArrow < 0 && LastArrow <> LastArrow[1]  then high else Double.NaN;
firstdown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
firstdown.SetLineWeight(5);
firstdown.SetDefaultColor(Color.YELLOW);






#
# Linear Regression with Standard Deviation Channels
#
#  Written by Robert Hall
#   Released 10 April 2009
#
#
#  Standard Deviation can be input to 2 decimal places
#

input standardDeviation = 1.5;
input startDateYyyyMmDd = 20110105;
input startTime = 0030;



plot MiddleLR = InertiaAll(price, length, startDateYyyyMmDd, startTime, extendtoright = 1);

def dist = HighestAll(AbsValue(MiddleLR - price)) * standardDeviation / 2;

plot UpperLR = MiddleLR + dist;
plot LowerLR = MiddleLR - dist;

MiddleLR.SetDefaultColor(Color.WHITE);
MiddleLR.SetLineWeight(1);
MiddleLR.SetStyle(Curve.FIRM);

UpperLR.SetDefaultColor(Color.CYAN);
UpperLR.SetLineWeight(1);
UpperLR.SetStyle(Curve.FIRM);

LowerLR.SetDefaultColor(Color.CYAN);
LowerLR.SetLineWeight(1);
LowerLR.SetStyle(Curve.FIRM);

#    Money Management

# Longs

def exposure20pct = LastArrow < 0 && LastArrow <> LastArrow[1];

def exposure40pct = Prediction[3] > Prediction[4] && Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1] ;

def exposure60pct = Prediction[2] > Prediction[3] && Prediction[1] > Prediction[2] && Prediction > Prediction[1];

def exposure80pct = Prediction[1] > Prediction[2] && Prediction > Prediction[1];

def exposure100pct = xover && Prediction > Prediction[1];

# Shorts

def exposureneg20pct = LastArrow > 0 && LastArrow <> LastArrow[1];

def exposureneg40pct = Prediction[3] < Prediction[4] && Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg60pct = Prediction[2] < Prediction[3] && Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg80pct = Prediction[1] < Prediction[2] && Prediction < Prediction[1];

def exposureneg100pct = xover && Prediction < Prediction[1];



AddLabel(yes, Concat( "", Concat(" ", 
if exposure20pct then "Long 20% " else 
if exposure40pct then "Long 40% " else
if exposure60pct then "Long 60%" else
if exposure80pct then "Long 60%" else
if exposure100pct then "Long 100%" else

if exposureneg20pct then "Short 20% " else 
if exposureneg40pct then "Short 40% " else
if exposureneg60pct then "Short 60%" else
if exposureneg80pct then "Short 60%" else
if exposureneg100pct then "Short 100%" else
"Go Flat")), 

if exposure20pct then Color.CYAN else 
if exposure40pct then  Color.CYAN else 
if exposure60pct then  Color.CYAN else
if exposure80pct then Color.CYAN else 
if exposure100pct then  Color.CYAN else
if exposureneg20pct then Color.ORANGE else 
if exposureneg40pct then Color.ORANGE else
if exposureneg60pct then Color.ORANGE else
if exposureneg80pct then Color.ORANGE else
if exposureneg100pct then Color.ORANGE else
Color.WHITE);

AddChartBubble(BubblesOn && xover && Prediction > Prediction[1] , low, Concat("Buy Baby", HL2), Color.CYAN, yes);

AddChartBubble(BubblesOn && xover && Prediction < Prediction[1] , high, Concat("Sell That Bad Boy", HL2), Color.ORANGE, yes);

# ************************************************************


# LeastSquaresMethodForecast_R1V2
    #
    # http://finance.groups.yahoo.com/group/TOS_thinkscript/message/6671
    # R1V0   2010.12.24          TradeScripter
    #
    # R1V1   2010.12.26:06:22    ebtrader
    # R1V2   2010.12.27:08:00    KumoBob
    #        Added Price Color and Arrows
    #        Option to remove any plot
    #        Added Chart Labels
    #         
# R1V3   2010.12.27:16:10    KumoBob
#        Added plot Today Only
#        Added Hints
# R2V1   2010.12.28:15:42 EBTrader
#        Added Regression Channel
#        Added Money Management
#        Added Bubble Option

# R2V2   2010.01.07: 16:06 KumoBob
#         Alternated Arrows

# R2V3   2010.01.11: 18:30 EBTrader
#         Made colors more consistent and added sell bubble


    #This program created by tradescripter
    #Dec. 24, 2010 Merry Christmas
    #It uses the Least-Squares Method to forecast a new price
    #For an explanation of Least-Squares method see:
    #http://en.wikiversity.org/wiki/Least-Squares_Method

#Hint PriceColorOn: Affect the color of the Price bars according to the signals of this indicator\n<b>Default is Yes
#Hint ArrowsOn: Plot Arrows at signal changes \n<b>Default is Yes
#Hint PredictionLineOn: Plot the indicator \n<b>Default is Yes
#Hint ShowTodayOnly: Plot only the most recent arrows in order not to clutter the Price history\nMakes it easier to see prior prices \n<b>Default is no
#Hint ShowExtraDays: Further adjustment to Show Only Today \n<b>Default is 0
#Hint space: Allows you to move the arrows, and avoid stepping on other arrows when multiple studies are shown \n<b>Default is 0.333
#Hint length: Touch this only if you are an expert! Just kidding. \n<b>Default is 9
#Hint price: For the experimenter \n<b>Default is HL2


AssignBackgroundColor(CreateColor(8, 0, 15));
input length2 = 14;
input price2 = HL2;
input PriceColorOn2 = Yes;
input ArrowsOn2 = Yes;
input PredictionLineOn2 = Yes;
input BubblesOn2 = No;
input ShowTodayOnly2 = no;
input ShowExtraDays2 = 0;
input space2 = 0.333;
def Today2 = if !ShowTodayOnly2 then 1 else if GetDay() + ShowExtraDays2 >= GetLastDay() then 1 else 0;

def AvgPrice2 = Average(price2, length2);
def SumTime2 = fold i2 = 1 to length2 + 1 with x2 = 0 do x2 + i2;
def AvgTime2 = SumTime2 / length2;
    #sx is the sum of all the deviations from time for the last x bars
def sx2 = fold j2 = 1 to length2 + 1 with y2 = 0 do y2 + ((j2 - AvgTime2) * (GetValue(price2, length2 - j2, length2 + 1) - AvgPrice2));
        #sy is the sum of all the deviations from price for the last x bars
def sy2 = fold k2 = 1 to length2 + 1 with z2 = 0 do z2 + (Power(k2 - AvgTime2, 2));
        #m is the slope of the line, b is the slope intercept of the line in the equation y = mx + b
def m2 = sx2 / sy2;
def b2 = AvgPrice2 - m2 * AvgTime2;
def Prediction2 = (m2 * (length2 + 1)) + b2;
plot FuturePrediction2 = if !PredictionLineOn2 then Double.NaN else Prediction2;
FuturePrediction2.AssignValueColor(if FuturePrediction2 > FuturePrediction2[1] then Color.GREEN else Color.RED);
FuturePrediction2.SetLineWeight(3);
FuturePrediction2.HideBubble();


AssignPriceColor(if !PriceColorOn2 then Color.CURRENT else if Prediction2 > Prediction2[1] then (CreateColor(000, 255, 255)) else if Prediction2 < Prediction2[1] then (CreateColor(255, 160, 000)) else Color.GRAY);
AddLabel(PriceColorOn2 , "Price Color On", if Prediction2 > Prediction2[1] then Color.GREEN else if Prediction2 < Prediction2[1] then Color.RED else Color.GRAY);

AddLabel(ArrowsOn2, "MagentaArrows=LeastSquares",  if Prediction2 > Prediction2[1] then Color.GREEN else if Prediction2 < Prediction2[1] then Color.RED else Color.GRAY);

def spaceman2 = Average(high - low) * space2;
def xover2 = Crosses(Prediction2, Prediction2[1], CrossingDirection.ANY);
plot UpArrow2 = if Today2 && xover2 && Prediction2 > Prediction2[1] then low - spaceman2 else Double.NaN;
UpArrow2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
UpArrow2.SetDefaultColor(Color.MAGENTA);
plot DnArrow2 = if Today2 && xover2 && Prediction2 < Prediction2[1] then high + spaceman2 else Double.NaN;
DnArrow2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
DnArrow2.SetDefaultColor(Color.MAGENTA);

    #    plot firstup = crosses((Prediction > low), yes) && Prediction < Prediction[1];
    #    firstup.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

    #    plot firstdown = crosses((Prediction < high), yes) && Prediction > Prediction[1];
    #    firstdown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

rec LastArrow2 = if low > Prediction2 && LastArrow2[1] <> 1 && Prediction2 < Prediction2[1] then 1 else
    if high < Prediction2 && LastArrow2[1] <> -1 && Prediction2 > Prediction2[1] then -1 else LastArrow2[1];

plot firstup2 = if Today2 && LastArrow2 > 0 && LastArrow2 <> LastArrow2[1]  then low else Double.NaN;
firstup2.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
firstup2.SetLineWeight(5);
firstup2.SetDefaultColor(Color.YELLOW);
plot firstdown2 = if Today2 && LastArrow2 < 0 && LastArrow2 <> LastArrow2[1]  then high else Double.NaN;
firstdown2.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
firstdown2.SetLineWeight(5);
firstdown2.SetDefaultColor(Color.YELLOW);








#    Money Management

# Longs

def exposure20pct2 = LastArrow2 < 0 && LastArrow2 <> LastArrow2[1];

def exposure40pct2 = Prediction2[3] > Prediction2[4] && Prediction2[2] > Prediction2[3] && Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1] ;

def exposure60pct22 = Prediction2[2] > Prediction2[3] && Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1];

def exposure80pct2 = Prediction2[1] > Prediction2[2] && Prediction2 > Prediction2[1];

def exposure100pct2 = xover2 && Prediction2 > Prediction2[1];

# Shorts

def exposureneg20pct2 = LastArrow2 > 0 && LastArrow2 <> LastArrow2[1];

def exposureneg40pct2 = Prediction2[3] < Prediction2[4] && Prediction2[2] < Prediction2[3] && Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];

def exposureneg60pct2 = Prediction2[2] < Prediction2[3] && Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];

def exposureneg80pct2 = Prediction2[1] < Prediction2[2] && Prediction2 < Prediction2[1];

def exposureneg100pct2 = xover2 && Prediction2 < Prediction2[1];



AddLabel(yes, Concat( "", Concat(" ", 
if exposure20pct2 then "Long 20% " else 
if exposure40pct2 then "Long 40% " else

if exposure80pct2 then "Long 80%" else
if exposure100pct2 then "Long 100%" else

if exposureneg20pct2 then "Short 20% " else 
if exposureneg40pct2 then "Short 40% " else
if exposureneg60pct2 then "Short 60%" else
if exposureneg80pct2 then "Short 80%" else
if exposureneg100pct2 then "Short 100%" else
"Go Flat")), 

if exposure20pct2 then Color.CYAN else 
if exposure40pct2 then  Color.CYAN else 

if exposure80pct2 then Color.CYAN else 
if exposure100pct2 then  Color.CYAN else
if exposureneg20pct2 then Color.ORANGE else 
if exposureneg40pct2 then Color.ORANGE else
if exposureneg60pct2 then Color.ORANGE else
if exposureneg80pct2 then Color.ORANGE else
if exposureneg100pct2 then Color.ORANGE else
Color.WHITE);

AddChartBubble(BubblesOn2 && xover2 && Prediction2 > Prediction2[1] , low, Concat("Buy Baby", HL2), Color.CYAN, yes);

AddChartBubble(BubblesOn2 && xover2 && Prediction2 < Prediction2[1] , high, Concat("Sell That Bad Boy", HL2), Color.ORANGE, yes);

# ************************************************************
 

Attachments

  • yfhXTsF.jpg
    yfhXTsF.jpg
    144.1 KB · Views: 116
Last edited by a moderator:
LR and Least Square methods work best for inter and longer time frames, my experience and in discussions with other CMTs, they were never designed for short term/intraday trading. This indicator will work for longer holdings and timeframes.

if anyone has experience using this successfully for short term time frames or have a pairing that works let us know.
 
someone please explain the LS Lower study when you get a chance and what the settings mean too thanks
 
Hguru Just 2 LS studis with different length inputs. The idea saw both turning red or green was a safer signal. Sometimes th short length will change color while the longer length does not. So then you could look at whether that was a false or true change. If the short then bounces good chance it is not a change in direction.
 
It is a linear regression slope using least squares method. It is curve fitting as far as I understand. Can an explaination be given why it is not suitable for any time frame. Just curious , I do not use it.
 
What are the differences between the yellow and the purple arrows? I also see the magneta arrows label going green and red, and the short and long % changing. I really like this indicator and i'd like to test it out. Any assistance will be greatly appreciated. thanks
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
472 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