# plots regression line through PE ratio. Requires just over 5 years on the chart, in ToS that will mean a 10-year chart.
declare lower;
declare hide_on_intraday;
input priorYear = no;
input StDevLines = no;
input bubbles = no;
input cloud = no;
def c = close;
def na = Double.NaN;
def bn = BarNumber();
def Earnings = GetActualEarnings();
# Subcript{} for getting prior value of a BarNumber() defined variable
Script prior {
input of = close;
def priorOf = if of != of[1] then of[1] else priorOf[1];
plot
prior = priorOf;
}
def earningsBar = if !IsNaN(Earnings)
then bn
else earningsBar[1];
def priorEarningsBar1 = prior(of = earningsBar);
def priorEarningsBar2 = prior(priorEarningsBar1);
def priorEarningsBar3 = prior(priorEarningsBar2);
def priorEarningsBar4 = prior(priorEarningsBar3);
def priorEarningsBar5 = prior(priorEarningsBar4);
def priorEarningsBar6 = prior(priorEarningsBar5);
def priorEarningsBar7 = prior(priorEarningsBar6);
def priorEarningsBar8 = prior(priorEarningsBar7);
def priorEarningsBar9 = prior(priorEarningsBar8);
def priorEarningsBar10 = prior(priorEarningsBar9);
def priorEarningsBar11 = prior(priorEarningsBar10);
def priorEarningsBar12 = prior(priorEarningsBar11);
def priorEarningsBar13 = prior(priorEarningsBar12);
def priorEarningsBar14 = prior(priorEarningsBar13);
def priorEarningsBar15 = prior(priorEarningsBar14);
def priorEarningsBar16 = prior(priorEarningsBar15);
def priorEarningsBar17 = prior(priorEarningsBar16);
def priorEarningsBar18 = prior(priorEarningsBar17);
def priorEarningsBar19 = prior(priorEarningsBar18);
def priorEarningsBar20 = prior(priorEarningsBar19);
def priorEarningsBar21 = prior(priorEarningsBar20);
def priorEarningsBar22 = prior(priorEarningsBar21);
def priorEarningsBar23 = prior(priorEarningsBar22);
def priorEarningsBar24 = prior(priorEarningsBar23);
def hEB = HighestAll(earningsBar);
def hpEB1 = HighestAll(priorEarningsBar1);
def hpEB2 = HighestAll(priorEarningsBar2);
def hpEB3 = HighestAll(priorEarningsBar3);
def hpEB4 = HighestAll(priorEarningsBar4);
def hpEB5 = HighestAll(priorEarningsBar5);
def hpEB6 = HighestAll(priorEarningsBar6);
def hpEB7 = HighestAll(priorEarningsBar7);
def hpEB8 = HighestAll(priorEarningsBar8);
def hpEB9 = HighestAll(priorEarningsBar9);
def hpEB10 = HighestAll(priorEarningsBar10);
def hpEB11 = HighestAll(priorEarningsBar11);
def hpEB12 = HighestAll(priorEarningsBar12);
def hpEB13 = HighestAll(priorEarningsBar13);
def hpEB14 = HighestAll(priorEarningsBar14);
def hpEB15 = HighestAll(priorEarningsBar15);
def hpEB16 = HighestAll(priorEarningsBar16);
def hpEB17 = HighestAll(priorEarningsBar17);
def hpEB18 = HighestAll(priorEarningsBar18);
def hpEB19 = HighestAll(priorEarningsBar19);
def hpEB20 = HighestAll(priorEarningsBar20);
def hpEB21 = HighestAll(priorEarningsBar21);
def hpEB22 = HighestAll(priorEarningsBar22);
def hpEB23 = HighestAll(priorEarningsBar23);
def hpEB24 = HighestAll(priorEarningsBar24);
# defining which 4 earnings are in the running annual total for each quarter
def currentEarnings;
def earnings2;
def earnings3;
def earnings4;
def earnings5;
def earnings6;
def earnings7;
def earnings8;
def earnings9;
def earnings10;
def earnings11;
def earnings12;
def earnings13;
def earnings14;
def earnings15;
def earnings16;
def earnings17;
def earnings18;
def earnings19;
def earnings20;
def earnings21;
def earnings22;
def earnings23;
def earnings24;
def earnings25;
def earnings26;
def earnings27;
def earnings28;
def earnings29;
def earnings30;
def earnings31;
def earnings32;
def earnings33;
def earnings34;
def earnings35;
def earnings36;
def earnings37;
def earnings38;
def earnings39;
def earnings40;
def earnings41;
def earnings42;
def earnings43;
def earnings44;
def earnings45;
def earnings46;
def earnings47;
def earnings48;
def earnings49;
def earnings50;
def earnings51;
def earnings52;
def earnings53;
def earnings54;
def earnings55;
def earnings56;
def earnings57;
def earnings58;
def earnings59;
def earnings60;
def earnings61;
def earnings62;
def earnings63;
def earnings64;
def earnings65;
def earnings66;
def earnings67;
def earnings68;
def earnings69;
def earnings70;
def earnings71;
def earnings72;
def earnings73;
def earnings74;
def earnings75;
def earnings76;
def earnings77;
def earnings78;
def earnings79;
def earnings80;
def earnings81;
def earnings82;
def earnings83;
def earnings84;
def earnings85;
def earnings86;
def earnings87;
def earnings88;
if bn == hEB {
currentEarnings = earnings;
earnings2 = GetValue(earnings, bn - priorEarningsBar1);
earnings3 = GetValue(earnings, bn - priorEarningsBar2);
earnings4 = GetValue(earnings, bn - priorEarningsBar3);
}else{
currentEarnings = currentEarnings[1];
earnings2 = earnings2[1];
earnings3 = earnings3[1];
earnings4 = earnings4[1];
}
if bn == hpEB1 {
earnings5 = earnings;
earnings6 = HighestAll(earnings3);
earnings7 = HighestAll(earnings4);
earnings8 = GetValue(Earnings, hpEB1 - hpEB4);
}else{
earnings5 = earnings5[1];
earnings6 = earnings6[1];
earnings7 = earnings7[1];
earnings8 = earnings8[1];
}
if bn == hpEB2 {
earnings9 = earnings;
earnings10 = HighestAll(earnings7);
earnings11 = HighestAll(earnings8);
earnings12 = GetValue(earnings, hpEB2 - hpEB5);
}else{
earnings9 = earnings9[1];
earnings10 = earnings10[1];
earnings11 = earnings11[1];
earnings12 = earnings12[1];
}
if bn == hpEB3 {
earnings13 = earnings;
earnings14 = HighestAll(earnings11);
earnings15 = HighestAll(earnings12);
earnings16 = GetValue(earnings, hpEB3 - hpEB6);
}else{
earnings13 = earnings13[1];
earnings14 = earnings14[1];
earnings15 = earnings15[1];
earnings16 = earnings16[1];
}
if bn == hpEB4 {
earnings17 = earnings;
earnings18 = HighestAll(earnings15);
earnings19 = HighestAll(earnings16);
earnings20 = GetValue(earnings, hpEB4 - hpEB7);
}else{
earnings17 = earnings17[1];
earnings18 = earnings18[1];
earnings19 = earnings19[1];
earnings20 = earnings20[1];
}
if bn == hpEB5 {
earnings21 = earnings;
earnings22 = HighestAll(earnings19);
earnings23 = HighestAll(earnings20);
earnings24 = GetValue(earnings, hpEB5 - hpEB8);
}else{
earnings21 = earnings21[1];
earnings22 = earnings22[1];
earnings23 = earnings23[1];
earnings24 = earnings24[1];
}
if bn == hpEB6 {
earnings25 = earnings;
earnings26 = HighestAll(earnings23);
earnings27 = HighestAll(earnings24);
earnings28 = GetValue(earnings, hpEB6 - hpEB9);
}else{
earnings25 = earnings25[1];
earnings26 = earnings26[1];
earnings27 = earnings27[1];
earnings28 = earnings28[1];
}
if bn == hpEB7 {
earnings29 = earnings;
earnings30 = HighestAll(earnings27);
earnings31 = HighestAll(earnings28);
earnings32 = GetValue(earnings, hpEB7 - hpEB10);
}else{
earnings29 = earnings29[1];
earnings30 = earnings30[1];
earnings31 = earnings31[1];
earnings32 = earnings32[1];
}
if bn == hpEB8 {
earnings33 = earnings;
earnings34 = HighestAll(earnings31);
earnings35 = HighestAll(earnings32);
earnings36 = GetValue(earnings, hpEB8 - hpEB11);
}else{
earnings33 = earnings33[1];
earnings34 = earnings34[1];
earnings35 = earnings35[1];
earnings36 = earnings36[1];
}
if bn == hpEB9 {
earnings37 = earnings;
earnings38 = HighestAll(earnings35);
earnings39 = HighestAll(earnings36);
earnings40 = GetValue(earnings, hpEB9 - hpEB12);
}else{
earnings37 = earnings37[1];
earnings38 = earnings38[1];
earnings39 = earnings39[1];
earnings40 = earnings40[1];
}
if bn == hpEB10 {
earnings41 = earnings;
earnings42 = HighestAll(earnings39);
earnings43 = HighestAll(earnings40);
earnings44 = GetValue(earnings, hpEB10 - hpEB13);
}else{
earnings41 = earnings41[1];
earnings42 = earnings42[1];
earnings43 = earnings43[1];
earnings44 = earnings44[1];
}
if bn == hpEB11 {
earnings45 = earnings;
earnings46 = HighestAll(earnings43);
earnings47 = HighestAll(earnings44);
earnings48 = GetValue(earnings, hpEB11 - hpEB14);
}else{
earnings45 = earnings45[1];
earnings46 = earnings46[1];
earnings47 = earnings47[1];
earnings48 = earnings48[1];
}
if bn == hpEB12 {
earnings49 = earnings;
earnings50 = HighestAll(earnings47);
earnings51 = HighestAll(earnings48);
earnings52 = GetValue(earnings, hpEB12 - hpEB15);
}else{
earnings49 = earnings49[1];
earnings50 = earnings50[1];
earnings51 = earnings51[1];
earnings52 = earnings52[1];
}
if bn == hpEB13 {
earnings53 = earnings;
earnings54 = HighestAll(earnings51);
earnings55 = HighestAll(earnings52);
earnings56 = GetValue(earnings, hpEB13 - hpEB16);
}else{
earnings53 = earnings53[1];
earnings54 = earnings54[1];
earnings55 = earnings55[1];
earnings56 = earnings56[1];
}
if bn == hpEB14 {
earnings57 = earnings;
earnings58 = HighestAll(earnings55);
earnings59 = HighestAll(earnings56);
earnings60 = GetValue(earnings, hpEB14 - hpEB17);
}else{
earnings57 = earnings57[1];
earnings58 = earnings58[1];
earnings59 = earnings59[1];
earnings60 = earnings60[1];
}
if bn == hpEB15 {
earnings61 = earnings;
earnings62 = HighestAll(earnings59);
earnings63 = HighestAll(earnings60);
earnings64 = GetValue(earnings, hpEB15 - hpEB18);
}else{
earnings61 = earnings61[1];
earnings62 = earnings62[1];
earnings63 = earnings63[1];
earnings64 = earnings64[1];
}
if bn == hpEB16 {
earnings65 = earnings;
earnings66 = HighestAll(earnings63);
earnings67 = HighestAll(earnings64);
earnings68 = GetValue(earnings, hpEB16 - hpEB19);
}else{
earnings65 = earnings65[1];
earnings66 = earnings66[1];
earnings67 = earnings67[1];
earnings68 = earnings68[1];
}
if bn == hpEB17 {
earnings69 = earnings;
earnings70 = HighestAll(earnings67);
earnings71 = HighestAll(earnings68);
earnings72 = GetValue(earnings, hpEB17 - hpEB20);
}else{
earnings69 = earnings69[1];
earnings70 = earnings70[1];
earnings71 = earnings71[1];
earnings72 = earnings72[1];
}
if bn == hpEB18 {
earnings73 = earnings;
earnings74 = HighestAll(earnings71);
earnings75 = HighestAll(earnings72);
earnings76 = GetValue(earnings, hpEB18 - hpEB21);
}else{
earnings73 = earnings73[1];
earnings74 = earnings74[1];
earnings75 = earnings75[1];
earnings76 = earnings76[1];
}
if bn == hpEB19 {
earnings77 = earnings;
earnings78 = HighestAll(earnings75);
earnings79 = HighestAll(earnings76);
earnings80 = GetValue(earnings, hpEB19 - hpEB22);
}else{
earnings77 = earnings77[1];
earnings78 = earnings78[1];
earnings79 = earnings79[1];
earnings80 = earnings80[1];
}
if bn == hpEB20 {
earnings81 = earnings;
earnings82 = HighestAll(earnings79);
earnings83 = HighestAll(earnings80);
earnings84 = GetValue(earnings, hpEB20 - hpEB23);
}else{
earnings81 = earnings81[1];
earnings82 = earnings82[1];
earnings83 = earnings83[1];
earnings84 = earnings84[1];
}
if bn == hpEB21 {
earnings85 = earnings;
earnings86 = HighestAll(earnings83);
earnings87 = HighestAll(earnings84);
earnings88 = GetValue(earnings, hpEB21 - hpEB24);
}else{
earnings85 = earnings85[1];
earnings86 = earnings86[1];
earnings87 = earnings87[1];
earnings88 = earnings88[1];
}
def cAE = currentEarnings + earnings2 + earnings3 + earnings4;
def p1QaE = earnings5 + earnings6 + earnings7 + earnings8;
def p2QaE = earnings9 + earnings10 + earnings11 + earnings12;
def p3QaE = earnings13 + earnings14 + earnings15 + earnings16;
def p4QaE = earnings17 + earnings18 + earnings19 + earnings20;
def p5QaE = earnings21 + earnings22 + earnings23 + earnings24;
def p6QaE = earnings25 + earnings26 + earnings27 + earnings28;
def p7QaE = earnings29 + earnings30 + earnings31 + earnings32;
def p8QaE = earnings33 + earnings34 + earnings35 + earnings36;
def p9QaE = earnings37 + earnings38 + earnings39 + earnings40;
def p10QaE = earnings41 + earnings42 + earnings43 + earnings44;
def p11QaE = earnings45 + earnings46 + earnings47 + earnings48;
def p12QaE = earnings49 + earnings50 + earnings51 + earnings52;
def p13QaE = earnings53 + earnings54 + earnings55 + earnings56;
def p14QaE = earnings57 + earnings58 + earnings59 + earnings60;
def p15QaE = earnings61 + earnings62 + earnings63 + earnings64;
def p16QaE = earnings65 + earnings66 + earnings67 + earnings68;
def p17QaE = earnings69 + earnings70 + earnings71 + earnings72;
def p18QaE = earnings73 + earnings74 + earnings75 + earnings76;
def p19QaE = earnings77 + earnings78 + earnings79 + earnings80;
def p20QaE = earnings81 + earnings82 + earnings83 + earnings84;
def p21QaE = earnings85 + earnings86 + earnings87 + earnings88;
def annualEarnings =
if bn == hpEB21
then p21QaE
else if bn == hpEB20
then p20QaE
else
if bn == hpEB19
then p19QaE
else if bn == hpEB18
then p18QaE
else if bn == hpEB17
then p17QaE
else if bn == hpEB16
then p16QaE
else
if bn == hpEB15
then p15QaE
else if bn == hpEB14
then p14QaE
else if bn == hpEB13
then p13QaE
else if bn == hpEB12
then p12QaE
else if bn == hpEB11
then p11QaE
else if bn == hpEB10
then p10Qae
else
if bn == hpEB9
then p9QaE
else if bn == hpEB8
then p8QaE
else if bn == hpEB7
then p7QaE
else if bn == hpEB6
then p6QaE
else
if bn == hpEB5
then p5QaE
else if bn == hpEB4
then p4QaE
else if bn == hpEB3
then p3QaE
else if bn == hpEB2
then p2QaE
else if bn == hpEB1
then p1QaE
else if bn == hEB
then cAE
else annualEarnings[1];
def AE = annualEarnings;
# variable length regression line subscript
script Slope {
input y = close;
input l = 40;
def n = Round(l, 0);
def na = Double.NaN;
def bn = BarNumber();
# start and end bars
def developingBar = if !IsNaN
&& IsNaN(y[-1])
then bn else developingBar[1];
def currentBar = HighestAll(developingBar);
def trailingBar = if GetValue(bn, -n) == currentBar
then bn else trailingBar[1];
def startingBar = HighestAll(trailingBar);
def x = if GetValue(bn, -n) > currentBar
then bn - startingBar else x[1];
# arithmetic
def Ex = fold ix = 0 to n
with px do px + GetValue(x, ix);
def Ey = fold iy = 0 to n
with py do py + GetValue(y, iy);
def Exy = fold ixy = 0 to n
with pxy do pxy + GetValue(x * y, ixy);
def Ex2 = fold ix2 = 0 to n
with px2 do px2 + GetValue(x * x, ix2);
# math
def b = (n * Exy - (Ex * Ey)) /
(n * Ex2 - (Ex * Ex));
def getB = if bn > startingBar
then GetValue(b, bn - currentBar)
else na;
def a = (GetValue(Ey, bn - currentBar) - getB *
GetValue(Ex, bn - currentBar)) / n;
plot
line = if !IsNaN(getB) && !IsNaN
then a + (getB * x) else na;
plot
startBar = startingBar;
plot
formingBar = currentBar;
}
def newBar = Slope().formingBar;
def startingBar = Slope(l = newBar - hpEB21).startBar;
plot PE;
plot HiPE;
plot LoPE;
if bn >= startingBar {
PE = c / AE;
HiPE = HighestAll(if(bn >= startingBar,PE,na));
LoPE = LowestAll(if(bn >= startingBar,PE,na));
}else{
PE = na;
HiPE = na;
LoPE = na;
}
PE.SetDefaultColor(Color.White);
HiPE.SetDefaultColor(Color.DownTick);
LoPE.SetDefaultColor(Color.UpTick);
plot
MeanPE = (LoPE + HiPE) / 2;
MeanPE. SetDefaultColor(Color.Dark_Gray);
MeanPE. SetStyle(Curve.Short_Dash);
plot
Slope = Slope(c/AE, newBar - startingBar);
Slope. SetDefaultColor(Color.Cyan);
plot
YearSlope = if priorYear
then Slope(PE, newBar - hpEB4)
else na;
YearSlope. SetDefaultColor(Color.Yellow);
YearSlope. SetStyle(Curve.Long_Dash);
YearSlope. SetLineWeight(2);
def xLocate = bubbles && IsNaN(c[24]) && !IsNaN(c[25]);
AddChartBubble(xLocate,HiPE, "High P:E\n"+Round(HiPE,2), Color.DownTick,0);
AddChartBubble(xLocate,LoPE, "Low P:E\n"+Round(LoPE,2), Color.UpTick);
AddCloud(if(cloud,PE,na),Slope,Color.Green);
plot Upper1;
plot Upper2;
plot Lower1;
plot Lower2;
if StDevLines {
upper1 = Slope + HighestAll(StDev(c/AE,22));
upper2 = Slope + 2 * HighestAll(StDev(c/AE,22));
lower1 = Slope - HighestAll(StDev(c/AE,22));
lower2 = Slope - 2 * HighestAll(StDev(c/AE,22));
}else{
upper1 = na;
upper2 = na;
lower1 = na;
lower2 = na;
}
upper1.SetDefaultColor(Color.Gray);
upper2.SetDefaultColor(Color.Gray);
lower1.SetDefaultColor(Color.Gray);
lower2.SetDefaultColor(Color.Gray);
# f/ Price to Earnings Oscillator