#study(title="Humble LinReg Candles", shorttitle="LinReg Candles", format=format.price, precision=4, overlay=true)
# Converted and mod by Sam4COK@Samer800 - 12/2022 - Update - option to change plot Line/Candle
input BarColor = no;
input ShowSignal = yes;
input HidePricePlot = no;
input LinRegStyle = {Default Line, Candle, None};
input signal_length = 11; # "Signal Smoothing"
input signalMovAvg = AverageType.SIMPLE; # "Simple MA (Signal Line)"
input lin_reg = yes; # "Lin Reg"
input linreg_length = 11; # "Linear Regression Length"
HidePricePlot(HidePricePlot);
def na = Double.NaN;
def Style = if LinRegStyle==LinRegStyle.Line then 1 else
if LinRegStyle==LinRegStyle.Candle then -1 else 0;
def bopen = if lin_reg then Inertia(open, linreg_length) else open;
def bhigh = if lin_reg then Inertia(high, linreg_length) else high;
def blow = if lin_reg then Inertia(low, linreg_length) else low;
def bclose = if lin_reg then Inertia(close,linreg_length) else close;
def ohlc = (bopen + bhigh + blow + bclose) / 4;
def raising = bclose > bopen;
def candleUp = if Style>=0 then na else bopen < bclose;
def signal = MovingAverage(signalMovAvg, bclose, signal_length);
plot candleLine = if Style==1 then ohlc else na;
candleLine.AssignValueColor(if raising then Color.CYAN else Color.MAGENTA);
#// Plot Candles
AddChart(high = if candleUp then bhigh else na,
low = if candleUp then blow else na,
open = if candleUp then bclose else na,
close = if candleUp then bopen else na,
type = ChartType.CANDLE, growcolor = CreateColor(0,153,153));
AddChart(high = if candleUp then na else bhigh,
low = if candleUp then na else blow ,
open = if candleUp then na else bopen,
close = if candleUp then na else bclose,
type = ChartType.CANDLE, growcolor = CreateColor(153,0,153));
# --- Signal Line
def sigUp = signal>signal[1];
plot SigLine = signal;
SigLine.AssignValueColor(if sigUp then Color.GREEN else Color.RED);
#--Bar Color
def ExtHi = raising and sigUp;
def Hi = !ExtHi and (raising or sigUp);
def ExtLo = !raising and !sigUp;
def Lo = !ExtLo and (!raising or !sigUp);
AssignPriceColor(if !BarColor then Color.CURRENT else
if ExtHi then Color.GREEN else
if Hi then Color.DARK_GREEN else
if ExtLo then Color.RED else
if Lo then Color.DARK_RED else Color.GRAY);
# ---- Signal
def up = signal>signal[1] and ohlc>signal and ohlc>ohlc[1];
def countUp = if up then CountUp[1] + 1 else 0;
def Dn = signal<signal[1] and ohlc<signal and ohlc<ohlc[1];
def countDn = if Dn then CountDn[1] + 1 else 0;
plot PtUp = if countUp==1 and ShowSignal then signal else na;
PtUp.SetLineWeight(3);
PtUp.SetDefaultColor(Color.GREEN);
PtUp.SetStyle(Curve.POINTS);
plot PtDn = if countDn==1 and ShowSignal then signal else na;
PtDn.SetLineWeight(3);
PtDn.SetDefaultColor(Color.RED);
PtDn.SetStyle(Curve.POINTS);
#--- END CODE