# chewy_chgtoscan_00
#https://usethinkscript.com/threads/need-help-getting-scan-to-work.13651/#post-115972
#Need Help getting scan to work
#chewie76 Dec 9, 2022 #1
#I am trying to scan for when "Orange" is true. Or "OrangeScan" is true. T
#http://tos.mx/nV3vh6u
#ThinkScript_study_20221209_2
#DYNO Intraday
#LinearRegCh100 RegressionDivergence - Trigger Lines - Trend Cross
# From Lizard Indicators Link: https://www.lizardindicators.com/trigger-lines-cross-vs-thrust/
# Line #1 - Fast = LinReg (80)
# Line #2 - Slow = EXPEMA[LinReg (80)]
input displace = 0;
input LinRegLength = 1000;
input EMAlength = 180;
input EMAlength2 = 230;
input ColorOn = no;
input bandsON = yes;
def price = close;
#Definitions
def LinReg = Inertia(price[-displace], LinRegLength);
def EMA_LR = ExpAverage(LinReg[-displace], EMAlength);
def EMA_LR2 = ExpAverage(LinReg[-displace], EMAlength2);
def Body = (open + close)/2;
# Defining Long/Short Filters (these instructions determine entries / exits)
# Entry Requirements
def Long_Entry = close > LinReg and close > EMA_LR and body > LinReg and body > EMA_LR and close > high[1] and body > body[1];
# LinReg > LinReg[1] and
def Long_Stay_In = close > LinReg and close > EMA_LR;
def Long_Exit = (close < LinReg or close < EMA_LR) or Long_Stay_In == 0;
def Long_State = If Long_Entry then 1 else if Long_Exit then 0 else Long_State[1];
def Long = Long_State;
# Exit Requirements
def Short_Entry = close < LinReg and close < EMA_LR and body < LinReg and body < EMA_LR and close < low[1] and body < body[1];
# LinReg < LinReg[1] and
def Short_Stay_In = close < LinReg and close < EMA_LR;
def Short_Exit = (close > LinReg or close > EMA_LR) or Short_Stay_In == 0;
def Short_State = If Short_Entry then 1 else if Short_Exit then 0 else Short_State[1];
def Short = Short_State;
#Adding Linear Regression Plots
plot EMA_LinReg = EMA_LR;
EMA_LinReg.SetDefaultColor(CreateColor(255, 215,0));
EMA_LinReg.setlineweight(2);
plot LR = LinReg;
LR.setlineweight(1);
#LR.SetDefaultColor(CreateColor(0, 130, 255));
LR.DefineColor("OverBought", color.red);
LR.DefineColor("OverSold", color.green);
LR.DefineColor("Normal", color.white);
LR.AssignValueColor(if LR < EMA_LinReg then LR.Color("OverBought") else if LR > EMA_LINREG then LR.Color("OverSold") else LR.Color("Normal"));
# Coloring Bars
AssignPriceColor(if ColorON and Long_State then Color.GREEN else if ColorON and Short_State then Color.RED else Color.current);
DefineGlobalColor("Bullish", Color.Green);
DefineGlobalColor("Bearish", Color.Red);
DefineGlobalColor("Bullish2", Color.dark_Green);
DefineGlobalColor("Bearish2", Color.dark_Red);
AddCloud(EMA_LR, LinReg, GlobalColor("Bearish"), GlobalColor("Bullish"));
AddCloud(EMA_LR2,EMA_LR, GlobalColor("Bearish2"), GlobalColor("Bullish2"));
# End
#Regression Bands
input deviations = 2.600; #set your deviation units here.
input length = 1000; #set your channel lookback period here.
def stdDeviation = StDevAll(price, length);
plot UpCloud = if bandson then EMA_LinReg + deviations * stdDeviation else double.nan;
UpCloud.SetDefaultColor(Color.red);
plot DnCloud = if bandson then EMA_LinReg - deviations * stdDeviation else double.nan;
DnCloud.SetDefaultColor(Color.green);
plot MiddleLine = if bandson then EMA_LinReg else double.nan;
Middleline.SetDefaultColor(Color.yellow);
DefineGlobalColor("Bullish", Color.green);
DefineGlobalColor("Bearish", Color.RED);
#BB
input Num_Dev_Dn = -1.68;
input Num_Dev_up = 1.68;
input length1 = 10;
input BB = yes;
input OrangeLevel = 9;
def expDev = ExpAverage(AbsValue(EMA_LinReg - close), length1);
plot UpperBand2 = if BB then EMA_LinReg + Num_Dev_up * expDev else double.nan;
UpperBand2.SetDefaultColor(Color.dark_orange);
UpperBand2.setlineweight(2);
plot lowerBand2 = if BB then EMA_LinReg + Num_Dev_dn * expDev else double.nan;
lowerBand2.SetDefaultColor(Color.dark_orange);
lowerBand2.setlineweight(2);
def BBDistance = if BB and UpperBand2 - LowerBand2 < OrangeLevel then Upperband2 else double.nan;
def BBDistanceL = if BB and UpperBand2 - LowerBand2 < OrangeLevel then Lowerband2 else double.nan;
plot Orange = BBDistance;
plot OrangeL = BBDistanceL;
plot OrangeScan = if BBDistance then 1 else 0;
OrangeScan.Hide();
Orange.SetDefaultColor(Color.white);
Orange.SetPaintingStrategy(PaintingSTrategy.points);
Orange.setlineweight(5);
OrangeL.SetDefaultColor(Color.white);
OrangeL.SetPaintingStrategy(PaintingSTrategy.points);
OrangeL.setlineweight(5);