• LIMITED TIME ONLY: Take $40 off VIP with code: BTD40.

Working on an moving average type indicator and need help putting in a scaled format

tibby42

New member
Hi,

I'm working on putting together an indicator for the second derivative of ADR% deviation from ADR% 20 day ma. I know that sounds convoluted. It's my first attempt to scale an indicator, I'm not sure of the best method of how to find highest and lowest for moving averages. I put lines on it that work pretty good for low adr stocks, but if I put a stock like tesla in it just totally doesn't work. I'm sure there's something easy I can do right? Help, please! Thanks in advance for any input.
Ruby:
declare lower;

plot zero = 0;
zero.setDefaultColor(color.dark_orange);
zero.SetStyle(curve.long_dash);
zero.setlineWeight(5);

plot OBpos = .75;
OBpos.setDefaultColor(color.dark_green);
OBpos.SetStyle(curve.firm);
OBpos.setlineWeight(2);

plot lowpos = .5;
lowpos.setDefaultColor(color.green);
lowpos.SetStyle(curve.MEDIUM_DASH);
lowpos.setlineWeight(1);

plot lowestpos = .25;
lowestpos.setDefaultColor(color.black);
lowestpos.SetStyle(curve.points);
lowestpos.setlineWeight(1);

plot OSneg = -.75;
OSneg.setDefaultColor(color.dark_red);
OSneg.SetStyle(curve.firm);
OSneg.setlineWeight(2);

plot lowneg = -.5;
lowneg.setDefaultColor(color.red);
lowneg.SetStyle(curve.medium_DASH);
lowneg.setlineWeight(1);

plot lowestneg = -.25;
lowestneg.setDefaultColor(color.black);
lowestneg.SetStyle(curve.points);
lowestneg.setlineWeight(1);

#Find way to AddCloud to Top and bottom after normalizing scale
#AddCloud(0, OS_line, color.Red, Color.Red);
#AddCloud(100, OB_line, color.Green, color.Green);
        ###AddCloud(30, Middle_line, color.Yellow, color.Yellow);
        ###middle line cloud inactive

input length = 20;
input ShowATR = no;
input ShowADR = no;
input ShowADRpercent = yes;
input ShowADRpercentDev = yes;
input ShowADRover = no;
input ShowADRunder = no;

def agg = AggregationPeriod.DAY;
def Pd = close(period = agg);
def hi = high(period = AggregationPeriod.DAY);
def lo = low(period = AggregationPeriod.DAY);

plot ATR = MovingAverage(AverageType.wilders, TrueRange(hi, Pd, lo), length);
ATR.SetDefaultColor(color.gray);
ATR.setHiding(!ShowATR);

##AddLabel (yes, "ATR:" + Round((ATR / close) * 100, 3) + "%", Color.LIGHT_GRAY);


##AddLabel (yes, "ATR:" + round((ATR) , 3), Color.LIGHT_GRAY);


plot ADR = MovingAverage(AverageType.wilders, hi-lo, length);
ADR.SetDefaultColor(color.black);
ADR.setHiding(!ShowADR);

plot ADRpercent = Round((ADR / Pd) * 100, 2);
ADRpercent.setdefaultcolor(color.blue);
ADRpercent.setlineweight(1);
ADRpercent.setHiding(!ShowADRpercent);


###"today %" higher than the " Avg %" DOES IT WORK??

plot ADRover = if (((Pd[0]-Pd[1])/Pd[1])*100) > adr then 5 else -5;
ADRover.setpaintingstrategy(paintingStrategy.line);
ADRover.setdefaultColor(getcolor(9));
ADRover.setlineWeight(1);
ADRover.setHiding(!ShowADRover);


plot ADRunder = if ((Pd-Pd[1])/Pd[1]*100) < adr then 5 else -5;
ADRunder.setpaintingstrategy(paintingStrategy.line);
ADRunder.setdefaultColor(getcolor(5));
ADRunder.setlineWeight(1);
ADRunder.setHiding(!ShowADRunder);

#AddCloud(ADRover, ADRunder, Color.GREEN, Color.RED);

plot ADRpercentDev = ((Pd-Pd[1])/Pd[1]*100);
ADRpercentDev.setstyle(curve.SHORT_DASH);
ADRpercentDev.setdefaultColor(getcolor(5));
ADRpercentDev.AssignValueColor(
    if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev >= OBpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= OBpos and ADRpercentDev >= lowpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowpos and ADRpercentDev >= lowestpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowestpos and ADRpercentDev >= lowestneg then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowestneg and ADRpercentDev >= lowneg then color.dark_green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowneg and ADRpercentDev >= OSneg then color.dark_green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= OSneg then color.cyan
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev >= OBpos then color.dark_orange
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= OBpos and ADRpercentDev >= lowpos then color.orange
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowpos and ADRpercentDev >= lowestpos then color.dark_red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowestpos and ADRpercentDev >= lowestneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowestneg and ADRpercentDev >= lowneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowneg and ADRpercentDev >= OSneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= OSneg then color.dark_orange
       else color.gray);
    
ADRpercentDev.setlineWeight(5);
ADRpercentDev.setHiding(!ShowADRpercentDev);


##############################################################################################
# def average of %ADR Deviation from %ADR 20 day Average

def ADRpD = ADRpercentDev;
input ba_length = 2;
input ba_type =  AverageType.exponential;
def ba = MovingAverage(ba_type, ADRpD, ba_length);


def BaseAvg = ba;

##############################################################################################
####### Tema 2 period EMA of %ADR Deviation from %ADR 20 Day Avg ##################################

input Display_ADRdevTema = yes;
input ADRdevTema_length = 10;
plot ADRdevTema = TEMA(BaseAvg, ADRdevTema_length);
ADRdevTema.SetPaintingStrategy(PaintingStrategy.LINE);
ADRdevTema.AssignValueColor(
    if ADRdevTema >= ADRdevTema[1] and ADRdevTema >= OBpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= OBpos and ADRdevTema >= lowpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowpos and ADRdevTema >= lowestpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowestpos and ADRdevTema >= lowestneg then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowestneg and ADRdevTema >= lowneg then color.dark_green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowneg and ADRdevTema >= OSneg then color.dark_green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= OSneg then color.cyan
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema >= OBpos then color.dark_orange
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= OBpos and ADRdevTema >= lowpos then color.orange
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowpos and ADRdevTema >= lowestpos then color.dark_red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowestpos and ADRdevTema >= lowestneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowestneg and ADRdevTema >= lowneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowneg and ADRdevTema >= OSneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= OSneg then color.dark_orange
       else color.gray);
ADRdevTema.SetLineWeight(3);
ADRdevTema.SetHiding(!Display_ADRdevTema);

####################################################################################
#Create an smoothed MA on Buying and Selling TEMAs for signal

input TemaSignal_length = 2;
plot TemaSignal = Average(ADRdevTema, TemaSignal_length);
TemaSignal.SetPaintingStrategy(PaintingStrategy.LINE);
TemaSignal.SetDefaultColor(Color.DARK_GREEN);
TemaSignal.SetLineWeight(1);
TemaSignal.Hide();

AddCloud(ADRdevTema, TemaSignal, Color.GREEN, Color.RED);
#AddCloud(TEMA_BV, 0, Color.GREEN, Color.Black);
#AddCloud(TEMA_SV, 0, Color.red, Color.Black);
####################################################################################

#Labels

AddLabel (yes, "ADR:" + round((ADR) , 1), (if (ADR > 5 AND hi-lo < ADR) then Color.GREEN  else if (ADR > 5) then Color.DARK_GREEN else if (ADR < 2) then Color.RED else Color.LIGHT_GRAY));

AddLabel (yes, "ADR:" + round((ADRpercent), 1) + "%", (if ((ADR / Pd) * 100 < 3) then Color.RED else if ((ADR / Pd) * 100 > 5) then Color.GREEN else Color.LIGHT_ORANGE));

AddLabel(ADRover, "OverAvg", if (ADRover > 0) then Color.DARK_GREEN else Color.LIGHT_GRAY);

AddLabel(ADRunder, "UnderAvg", if (ADRunder > 0) then Color.DARK_RED else Color.LIGHT_GRAY);

AddLabel(ADRpercentDev, "ADR% Diff from Avg: " + Round(ADRpercentDev, 1) + "%", if (ADRpercentDev > 0) then Color.Dark_Green else Color.Dark_Red);




################################################################################################

###Archive Versions of AddLabel

##AddLabel (yes, "ADR:" + round((ADR) , 2), (if ADR < 3 then Color.RED else if ADR > 5 then Color.GREEN else Color.LIGHT_ORANGE));

##AddLabel (yes, "ADR:" + Round((ADR / close) * 100, 1) + "%", (if ADR < 3 then Color.RED else if ADR > 5 then Color.GREEN else Color.LIGHT_ORANGE));

###"today %" higher than the " Avg %"
###plot ADRround = round((close-close[1])/close[1]*100) > adr;

###########
# Labels

#AddLabel(Show30DayAvg, "Avg 30 Days: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);

#AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

#AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then #Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.BLACK) );

#AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);
 
Solution
Hi,

I'm working on putting together an indicator for the second derivative of ADR% deviation from ADR% 20 day ma. I know that sounds convoluted. It's my first attempt to scale an indicator, I'm not sure of the best method of how to find highest and lowest for moving averages. I put lines on it that work pretty good for low adr stocks, but if I put a stock like tesla in it just totally doesn't work. I'm sure there's something easy I can do right? Help, please! Thanks in advance for any input.
Ruby:
declare lower;

plot zero = 0;
zero.setDefaultColor(color.dark_orange);
zero.SetStyle(curve.long_dash);
zero.setlineWeight(5);

plot OBpos = .75;
OBpos.setDefaultColor(color.dark_green);
OBpos.SetStyle(curve.firm);
OBpos.setlineWeight(2)...

halcyonguy

Well-known member
VIP
Lifetime
Hi,

I'm working on putting together an indicator for the second derivative of ADR% deviation from ADR% 20 day ma. I know that sounds convoluted. It's my first attempt to scale an indicator, I'm not sure of the best method of how to find highest and lowest for moving averages. I put lines on it that work pretty good for low adr stocks, but if I put a stock like tesla in it just totally doesn't work. I'm sure there's something easy I can do right? Help, please! Thanks in advance for any input.
Ruby:
declare lower;

plot zero = 0;
zero.setDefaultColor(color.dark_orange);
zero.SetStyle(curve.long_dash);
zero.setlineWeight(5);

plot OBpos = .75;
OBpos.setDefaultColor(color.dark_green);
OBpos.SetStyle(curve.firm);
OBpos.setlineWeight(2);

plot lowpos = .5;
lowpos.setDefaultColor(color.green);
lowpos.SetStyle(curve.MEDIUM_DASH);
lowpos.setlineWeight(1);

plot lowestpos = .25;
lowestpos.setDefaultColor(color.black);
lowestpos.SetStyle(curve.points);
lowestpos.setlineWeight(1);

plot OSneg = -.75;
OSneg.setDefaultColor(color.dark_red);
OSneg.SetStyle(curve.firm);
OSneg.setlineWeight(2);

plot lowneg = -.5;
lowneg.setDefaultColor(color.red);
lowneg.SetStyle(curve.medium_DASH);
lowneg.setlineWeight(1);

plot lowestneg = -.25;
lowestneg.setDefaultColor(color.black);
lowestneg.SetStyle(curve.points);
lowestneg.setlineWeight(1);

#Find way to AddCloud to Top and bottom after normalizing scale
#AddCloud(0, OS_line, color.Red, Color.Red);
#AddCloud(100, OB_line, color.Green, color.Green);
        ###AddCloud(30, Middle_line, color.Yellow, color.Yellow);
        ###middle line cloud inactive

input length = 20;
input ShowATR = no;
input ShowADR = no;
input ShowADRpercent = yes;
input ShowADRpercentDev = yes;
input ShowADRover = no;
input ShowADRunder = no;

def agg = AggregationPeriod.DAY;
def Pd = close(period = agg);
def hi = high(period = AggregationPeriod.DAY);
def lo = low(period = AggregationPeriod.DAY);

plot ATR = MovingAverage(AverageType.wilders, TrueRange(hi, Pd, lo), length);
ATR.SetDefaultColor(color.gray);
ATR.setHiding(!ShowATR);

##AddLabel (yes, "ATR:" + Round((ATR / close) * 100, 3) + "%", Color.LIGHT_GRAY);


##AddLabel (yes, "ATR:" + round((ATR) , 3), Color.LIGHT_GRAY);


plot ADR = MovingAverage(AverageType.wilders, hi-lo, length);
ADR.SetDefaultColor(color.black);
ADR.setHiding(!ShowADR);

plot ADRpercent = Round((ADR / Pd) * 100, 2);
ADRpercent.setdefaultcolor(color.blue);
ADRpercent.setlineweight(1);
ADRpercent.setHiding(!ShowADRpercent);


###"today %" higher than the " Avg %" DOES IT WORK??

plot ADRover = if (((Pd[0]-Pd[1])/Pd[1])*100) > adr then 5 else -5;
ADRover.setpaintingstrategy(paintingStrategy.line);
ADRover.setdefaultColor(getcolor(9));
ADRover.setlineWeight(1);
ADRover.setHiding(!ShowADRover);


plot ADRunder = if ((Pd-Pd[1])/Pd[1]*100) < adr then 5 else -5;
ADRunder.setpaintingstrategy(paintingStrategy.line);
ADRunder.setdefaultColor(getcolor(5));
ADRunder.setlineWeight(1);
ADRunder.setHiding(!ShowADRunder);

#AddCloud(ADRover, ADRunder, Color.GREEN, Color.RED);

plot ADRpercentDev = ((Pd-Pd[1])/Pd[1]*100);
ADRpercentDev.setstyle(curve.SHORT_DASH);
ADRpercentDev.setdefaultColor(getcolor(5));
ADRpercentDev.AssignValueColor(
    if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev >= OBpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= OBpos and ADRpercentDev >= lowpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowpos and ADRpercentDev >= lowestpos then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowestpos and ADRpercentDev >= lowestneg then color.green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowestneg and ADRpercentDev >= lowneg then color.dark_green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= lowneg and ADRpercentDev >= OSneg then color.dark_green
    else if ADRpercentDev >= ADRpercentDev[1] and ADRpercentDev <= OSneg then color.cyan
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev >= OBpos then color.dark_orange
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= OBpos and ADRpercentDev >= lowpos then color.orange
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowpos and ADRpercentDev >= lowestpos then color.dark_red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowestpos and ADRpercentDev >= lowestneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowestneg and ADRpercentDev >= lowneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= lowneg and ADRpercentDev >= OSneg then color.red
    else if ADRpercentDev <= ADRpercentDev[1] and ADRpercentDev <= OSneg then color.dark_orange
       else color.gray);
    
ADRpercentDev.setlineWeight(5);
ADRpercentDev.setHiding(!ShowADRpercentDev);


##############################################################################################
# def average of %ADR Deviation from %ADR 20 day Average

def ADRpD = ADRpercentDev;
input ba_length = 2;
input ba_type =  AverageType.exponential;
def ba = MovingAverage(ba_type, ADRpD, ba_length);


def BaseAvg = ba;

##############################################################################################
####### Tema 2 period EMA of %ADR Deviation from %ADR 20 Day Avg ##################################

input Display_ADRdevTema = yes;
input ADRdevTema_length = 10;
plot ADRdevTema = TEMA(BaseAvg, ADRdevTema_length);
ADRdevTema.SetPaintingStrategy(PaintingStrategy.LINE);
ADRdevTema.AssignValueColor(
    if ADRdevTema >= ADRdevTema[1] and ADRdevTema >= OBpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= OBpos and ADRdevTema >= lowpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowpos and ADRdevTema >= lowestpos then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowestpos and ADRdevTema >= lowestneg then color.green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowestneg and ADRdevTema >= lowneg then color.dark_green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= lowneg and ADRdevTema >= OSneg then color.dark_green
    else if ADRdevTema >= ADRdevTema[1] and ADRdevTema <= OSneg then color.cyan
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema >= OBpos then color.dark_orange
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= OBpos and ADRdevTema >= lowpos then color.orange
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowpos and ADRdevTema >= lowestpos then color.dark_red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowestpos and ADRdevTema >= lowestneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowestneg and ADRdevTema >= lowneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= lowneg and ADRdevTema >= OSneg then color.red
    else if ADRdevTema <= ADRdevTema[1] and ADRdevTema <= OSneg then color.dark_orange
       else color.gray);
ADRdevTema.SetLineWeight(3);
ADRdevTema.SetHiding(!Display_ADRdevTema);

####################################################################################
#Create an smoothed MA on Buying and Selling TEMAs for signal

input TemaSignal_length = 2;
plot TemaSignal = Average(ADRdevTema, TemaSignal_length);
TemaSignal.SetPaintingStrategy(PaintingStrategy.LINE);
TemaSignal.SetDefaultColor(Color.DARK_GREEN);
TemaSignal.SetLineWeight(1);
TemaSignal.Hide();

AddCloud(ADRdevTema, TemaSignal, Color.GREEN, Color.RED);
#AddCloud(TEMA_BV, 0, Color.GREEN, Color.Black);
#AddCloud(TEMA_SV, 0, Color.red, Color.Black);
####################################################################################

#Labels

AddLabel (yes, "ADR:" + round((ADR) , 1), (if (ADR > 5 AND hi-lo < ADR) then Color.GREEN  else if (ADR > 5) then Color.DARK_GREEN else if (ADR < 2) then Color.RED else Color.LIGHT_GRAY));

AddLabel (yes, "ADR:" + round((ADRpercent), 1) + "%", (if ((ADR / Pd) * 100 < 3) then Color.RED else if ((ADR / Pd) * 100 > 5) then Color.GREEN else Color.LIGHT_ORANGE));

AddLabel(ADRover, "OverAvg", if (ADRover > 0) then Color.DARK_GREEN else Color.LIGHT_GRAY);

AddLabel(ADRunder, "UnderAvg", if (ADRunder > 0) then Color.DARK_RED else Color.LIGHT_GRAY);

AddLabel(ADRpercentDev, "ADR% Diff from Avg: " + Round(ADRpercentDev, 1) + "%", if (ADRpercentDev > 0) then Color.Dark_Green else Color.Dark_Red);




################################################################################################

###Archive Versions of AddLabel

##AddLabel (yes, "ADR:" + round((ADR) , 2), (if ADR < 3 then Color.RED else if ADR > 5 then Color.GREEN else Color.LIGHT_ORANGE));

##AddLabel (yes, "ADR:" + Round((ADR / close) * 100, 1) + "%", (if ADR < 3 then Color.RED else if ADR > 5 then Color.GREEN else Color.LIGHT_ORANGE));

###"today %" higher than the " Avg %"
###plot ADRround = round((close-close[1])/close[1]*100) > adr;

###########
# Labels

#AddLabel(Show30DayAvg, "Avg 30 Days: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);

#AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));

#AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then #Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.BLACK) );

#AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);

here is a thread with some links and studies for notmalizing data

https://usethinkscript.com/threads/alternate-normalization-study-not-complex-for-thinkorswim.9067/
 
Solution

Similar threads

Top