Would anyone be able to add script which would colorize the AvgSlope line of:
Linear Regression Slope [UCSGEARS]
https://usethinkscript.com/threads/linear-regression-slope-ucsgears-for-thinkorswim.18802/
?
It is currently always grey. I'd be interested in script that colors the line one color if current state is higher than previous day and another if current state is lower than previous day.
Linear Regression Slope [UCSGEARS]
https://usethinkscript.com/threads/linear-regression-slope-ucsgears-for-thinkorswim.18802/
?
It is currently always grey. I'd be interested in script that colors the line one color if current state is higher than previous day and another if current state is lower than previous day.
Ruby:
#// Created by UCSgears -- Version 2
#// Simple linear regression slope - Good way see if the trend is accelarating or decelarating
#study(title="UCSGEARS - Linear Regression Slope", shorttitle="UCS-LRS", overlay=false)
# Converted by Sam4Cok@Samer800 - 06/2024 - request from UseThinkScript.compoundValue member
declare lower;
input barColor = yes;
input src = close;
input CurveLength = 50; #, minval = 1, title = "Curve Length")
input SlopeLength = 5; #, minval=1, title="Slope Length")
input SignalLength = 13; #, minval=1, title="Signal Length")
def na = Double.NaN;
def last = IsNaN(close);
#//Linear Regression Curve
def lrc = Inertia(src, CurveLength);
#//Linear Regression Slope
def lrs = (lrc - lrc[1]) / 1;
#//Smooth Linear Regression Slope
def slrs = ExpAverage(lrs, SlopeLength);
#//Signal Linear Regression Slope
def alrs = Average(slrs, SignalLength);
#//loalrs = sma(slrs, (glen*5))
def uacce = lrs > alrs and lrs > 0;
def dacce = lrs < alrs and lrs < 0;
def scolor = if uacce then 1 else if dacce then -1 else 0;
plot AvgSlope = alrs; #, color = gray, title = "Average Slope")
plot zero = if last then na else 0; #, title = "Zero Line", color = gray)
AvgSlope.SetDefaultColor(Color.WHITE);
zero.SetDefaultColor(Color.DARK_GRAY);
#----Div-----------
input ShowLastDivLines = yes;
input ShowLastHiddenDivLines = no;
input divSignalOptions = {default "Bubbles", "Vertical Lines"};
input LookBackRight = 5; # "Pivot Lookback Right"
input LookBackLeft = 5; # "Pivot Lookback Left"
input MaxLookback = 60; # "Max of Lookback Range"
input MinLookback = 5; # "Min of Lookback Range"
input DivBull = yes; # "Plot Bullish"
input DivBear = yes; # "Plot Bearish"
input DivHiddenBull = no; # "Plot Hidden Bullish"
input DivHiddenBear = no; # "Plot Hidden Bearish"
def bubble = divSignalOptions == divSignalOptions."Bubbles";
def divSrc = slrs;
def lo = low;
def hi = high;
script FindPivots {
input dat = close; # default data or study being evaluated
input HL = 0; # default high or low pivot designation, -1 low, +1 high
input lbL = 5; # default Pivot Lookback Left
input lbR = 1; # default Pivot Lookback Right
##############
def _nan; # used for non-number returns
def _BN; # the current barnumber
def _VStop; # confirms that the lookforward period continues the pivot trend
def _V; # the Value at the actual pivot point
##############
_BN = BarNumber();
_nan = Double.NaN;
_VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
fold a = 1 to lbR + 1 with b=1 while b do
if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
if (HL > 0) {
_V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
then dat else _nan;
} else {
_V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
then dat else _nan;
}
plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#_inRange(cond) =>
script _inRange {
input cond = yes;
input rangeUpper = 60;
input rangeLower = 5;
def bars = if cond then 0 else bars[1] + 1;
def inrange = (rangeLower <= bars) and (bars <= rangeUpper);
plot retrun = inrange;
}
def pl = findpivots(divSrc, -1, LookBackLeft, LookBackRight);
def ph = findpivots(divSrc, 1, LookBackLeft, LookBackRight);
def plFound = if !IsNaN(pl) then 1 else 0;
def phFound = if !IsNaN(ph) then 1 else 0;
def vlFound0 = if plFound then divSrc else vlFound0[1];
def vhFound0 = if phFound then divSrc else vhFound0[1];
def plPrice0 = if plFound then lo else plPrice0[1];
def phPrice0 = if phFound then hi else phPrice0[1];
def vlFound = if vlFound0 != vlFound0[1] then vlFound0[1] else vlFound[1];
def vhFound = if vhFound0 != vhFound0[1] then vhFound0[1] else vhFound[1];
def plPrice = if plPrice0 != plPrice0[1] then plPrice0[1] else plPrice[1];
def phPrice = if phPrice0 != phPrice0[1] then phPrice0[1] else phPrice[1];
#// Regular Bullish
def inRangePl = _inRange(plFound[1], MaxLookback, MinLookback);
def oscHL = divSrc > vlFound and inRangePl;
def priceLL = lo < plPrice;
def bullCond = DivBull and plFound and oscHL and priceLL;
#// Hidden Bullish
def oscLL = divSrc < vlFound and inRangePl;
def priceHL = lo > plPrice;
def hiddenBullCond = DivHiddenBull and plFound and oscLL and priceHL;
#// Regular Bearish
def inRangePh = _inRange(phFound[1], MaxLookback, MinLookback);
def oscLH = divSrc < vhFound and inRangePh;
def priceHH = hi > phPrice;
def bearCond = DivBear and phFound and oscLH and priceHH;
#// Hidden Bearish
def oscHH = divSrc > vhFound and inRangePh;
def priceLH = hi < phPrice;
def hiddenBearCond = DivHiddenBear and phFound and oscHH and priceLH;
#------ Bubbles
AddChartBubble(bubble and bullCond, divSrc, "R", Color.CYAN, no);
AddChartBubble(bubble and bearCond, divSrc, "R", Color.MAGENTA, yes);
AddChartBubble(bubble and hiddenBullCond, divSrc, "H", Color.DARK_GREEN, no);
AddChartBubble(bubble and hiddenBearCond, divSrc, "H", Color.DARK_RED, yes);
#-- Vertica lines
AddVerticalLine(!bubble and bullCond, "Regular", Color.CYAN);
AddVerticalLine(!bubble and bearCond, "Regular", Color.MAGENTA);
AddVerticalLine(!bubble and hiddenBullCond, "Hidden", Color.DARK_GREEN);
AddVerticalLine(!bubble and hiddenBearCond, "Hidden", Color.DARK_RED);
##### Lines
def bar = BarNumber();
#-- Bear Line
def lastPhBar = if phFound then bar else lastPhBar[1];
def prePhBar = if lastPhBar != lastPhBar[1] then lastPhBar[1] else prePhBar[1];
def priorPHBar = if bearCond then prePhBar else priorPHBar[1];
def lastBearBar = if bearCond then bar else lastBearBar[1];
#-- Bull Line
def lastPlBar = if plFound then bar else lastPlBar[1];
def prePlBar = if lastPlBar != lastPlBar[1] then lastPlBar[1] else prePlBar[1];
def priorPLBar = if bullCond then prePlBar else priorPLBar[1];
def lastBullBar = if bullCond then bar else lastBullBar[1];
def HighPivots = if bar == HighestAll(priorPHBar) then 1 else if bar == HighestAll(lastBearBar) then 1 else 0;
def LowPivots = if bar == HighestAll(priorPLBar) then 1 else if bar == HighestAll(lastBullBar) then 1 else 0;
def pivotHigh = if HighPivots then divSrc else na;
def pivotLow = if LowPivots then divSrc else na;
plot PlotHline = if ShowLastDivLines then pivotHigh else na;
PlotHline.EnableApproximation();
PlotHline.SetDefaultColor(Color.MAGENTA);
plot PlotLline = if ShowLastDivLines then pivotLow else na;
PlotLline.EnableApproximation();
PlotLline.SetDefaultColor(Color.CYAN);
#--- Hidden Lines
#-- Bear Line
def priorHPHBar = if hiddenBearCond then prePhBar else priorHPHBar[1];
def lastHBearBar = if hiddenBearCond then bar else lastHBearBar[1];
#-- Bull Line
def priorHPLBar = if hiddenBullCond then prePlBar else priorHPLBar[1];
def lastHBullBar = if hiddenBullCond then bar else lastHBullBar[1];
def HighHPivots = if bar == HighestAll(priorHPHBar) then 1 else if bar == HighestAll(lastHBearBar) then 1 else 0;
def LowHPivots = if bar == HighestAll(priorHPLBar) then 1 else if bar == HighestAll(lastHBullBar) then 1 else 0;
def pivotHidHigh = if HighHPivots then divSrc else na;
def pivotHidLow = if LowHPivots then divSrc else na;
plot PlotHBearline = if ShowLastHiddenDivLines then pivotHidHigh else na;
PlotHBearline.EnableApproximation();
PlotHBearline.SetDefaultColor(Color.DARK_RED);
plot PlotHBullline = if ShowLastHiddenDivLines then pivotHidLow else na;
PlotHBullline.EnableApproximation();
PlotHBullline.SetDefaultColor(Color.DARK_GREEN);
#-- plot lin Reg
plot LinReg = slrs; # "Linear Regression Slope"
LinReg.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
LinReg.AssignValueColor(if scolor > 0 then if slrs>slrs[1] then Color.GREEN else Color.DARK_GREEN else
if scolor < 0 then if slrs<slrs[1] then Color.RED else Color.DARK_RED else Color.GRAY);
#-- bar Color
AssignPriceColor(if !barColor then Color.CURRENT else
if scolor > 0 then if slrs>slrs[1] then Color.GREEN else Color.DARK_GREEN else
if scolor < 0 then if slrs<slrs[1] then Color.RED else Color.DARK_RED else Color.GRAY);
#-- ENd Code
Last edited by a moderator: