Ford Equity Research Bands In ThinkScript

SiCPH

New member
Hey everyone, I've been trying to recreate the Ford Equity Research bands in thinkscript, thought I would ask the professionals here! Been asking around, but to no success have I been able to recreate it one for one. Would love your guy's help!

I can't seem to get it quite right, they overlay the bands onto a monthly chart. I've tried coding it onto a daily chart, with no success so far. Here's what I currently have (http://tos.mx/K7vE49E) Any help to recreate this would be appreciated!

Overview/Info Here: http://www.fordequity.com/docs/default-source/previoussite/ss1202.pdf?sfvrsn=2
IMAGE: Valuation Bands Preview
Ford's Explanation of the Valuation bands
1)
The current price/earnings ratio was compared with the average P/E plus or minus two standard deviations (current 12 month reported earnings per share (ECU) was used in the computation of P/E)
2) Plotting a plus and minus two standard deviations band around the mean price/earnings ratio. The standard deviation bands measure the amount of historical variation from the average P/E. The average P/E plus or minus two standard deviations represents the range that P/E ratios will fall 95% of the time
 
@SiCPH Welcome to the forums...

Your imgur link is broken... Just select the BBCode option for your imgur image and you can post the code directly...

Personally, I've never heard of this Study... It's speculative whether anyone will generate the code you've requested, especially considering how you just signed up to ask... We don't often just bang out code upon request unless it is something that there is considerable interest in... But maybe you'll get lucky...

Also, please post your code here, using the </> icon to encapsulate it, rather than simply posting a shared link... We shouldn't be required to import the code into out TOS install just to review the code...
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

@rad14733

Code:
def length = 252;
def AE = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_TTM = Sum(AE, 252);
def pe = close / EPS_TTM;

#######SD calculation/PLOTTING
def PEratio = pe;
plot avgpe = average(peratio, 252);

plot twosd = avgpe + 2*stdev(avgpe,252);
plot twosddown = avgpe + -2*stdev(avgpe,252);

Here's the code I've written, no worries. Thought it could be useful for everyone else if it does eventually get coded
 
@rad14733

Code:
def length = 252;
def AE = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def EPS_TTM = Sum(AE, 252);
def pe = close / EPS_TTM;

#######SD calculation/PLOTTING
def PEratio = pe;
plot avgpe = average(peratio, 252);

plot twosd = avgpe + 2*stdev(avgpe,252);
plot twosddown = avgpe + -2*stdev(avgpe,252);

Here's the code I've written, no worries. Thought it could be useful for everyone else if it does eventually get coded
I like this idea, i have a code that's something like that, maybe we could use this to build it out.

Code:
declare hide_on_intraday;

    # inputs

input PEOne   = 10;

input PETwo   = 12;

input PEThree = 15;

input PEFour  = 16;

input PEFive  = 18;

    # universals

def c  = close;

def bn = BarNumber();

def Earnings = GetActualEarnings();

    # subscript for 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;

}

    # identifies the which bars are the previous 1,2,3 etc earnings bars

def earningsBar = if   !IsNaN(Earnings)

                  then bn

                  else earningsBar[1];

def priorEarningsBar1 = prior(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 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);

    # defining which 4 earnings are in the running annual total for each quarter

def earnings1;

def earnings2;

def earnings3;

def earnings4;

if  bn == hEB {

    earnings1 = earnings;

    earnings2 = GetValue(earnings, bn - priorEarningsBar1);

    earnings3 = GetValue(earnings, bn - priorEarningsBar2);

    earnings4 = GetValue(earnings, bn - priorEarningsBar3);

}else{

    earnings1 = earnings1[1];

    earnings2 = earnings2[1];

    earnings3 = earnings3[1];

    earnings4 = earnings4[1];

}



def earnings5 = if(bn == hpEB1, earnings, earnings5[1]);

def earnings6 = if(bn == hpEB1, HighestAll(earnings3), earnings6[1]);

def earnings7 = if(bn == hpEB1, HighestAll(earnings4), earnings7[1]);

def earnings8 = if(bn == hpEB1, GetValue(Earnings, hpEB1 - hpEB4),

                   earnings8[1]);



def earnings9  = if(bn == hpEB2, earnings, earnings9[1]);

def earnings10 = if(bn == hpEB2, HighestAll(earnings7),earnings10[1]);

def earnings11 = if(bn == hpEB2, HighestAll(earnings8),earnings11[1]);

def earnings12 = if(bn == hpEB2, GetValue(earnings, hpEB2 - hpEB5), 

                    earnings12[1]);



def earnings13 = if(bn == hpEB3, earnings, earnings13[1]);

def earnings14 = if(bn == hpEB3,HighestAll(earnings11),earnings14[1]);

def earnings15 = if(bn == hpEB3,HighestAll(earnings12),earnings15[1]);

def earnings16 = if(bn == hpEB3,GetValue(earnings, hpEB3 - hpEB6), 

                    earnings16[1]);



def earnings17 = if(bn == hpEB4, earnings, earnings17[1]);

def earnings18 = if(bn == hpEB4,HighestAll(earnings15),earnings18[1]);

def earnings19 = if(bn == hpEB4,HighestAll(earnings16),earnings19[1]);;

def earnings20 = if(bn == hpEB4,GetValue(earnings, hpEB4 - hpEB7),

                    earnings20[1]);



def earnings21 = if(bn == hpEB5, earnings, earnings21[1]);

def earnings22 = if(bn == hpEB5, HighestAll(earnings19), earnings22[1]);

def earnings23 = if(bn == hpEB5, HighestAll(earnings20), earnings23[1]);;

def earnings24 = if(bn == hpEB5, GetValue(earnings, hpEB5 - hpEB8),

                    earnings24[1]);



def aE =   

if    bn == hpEB5 

then  earnings21 + earnings22 + earnings23 + earnings24

else  if    bn == hpEB4 

      then  earnings17 + earnings18 + earnings19 + earnings20

      else  if    bn == hpEB3 

            then  earnings13 + earnings14 + earnings15 + earnings16

            else  if    bn == hpEB2 

                  then  earnings9 + earnings10 + earnings11 + earnings12

                  else 

if    bn == hpEB1 

then  earnings5 + earnings6 + earnings7 + earnings8

else  if   bn == hEB

      then earnings1 + earnings2 + earnings3 + earnings4

      else aE[1];  



plot

"P/E 1" = AE * PEOne;

"P/E 1".  SetDefaultColor(CreateColor(100,225,100));

"P/E 1".  SetPaintingStrategy(PaintingStrategy.DASHES);

plot 

"P/E 2" = AE * PETwo;

"P/E 2".  SetDefaultColor(CreateColor(75,175,225));

"P/E 2".  SetPaintingStrategy(PaintingStrategy.DASHES);

plot 

"P/E 3" = AE * PEThree;

"P/E 3".  SetDefaultColor(CreateColor(250,175,50));

"P/E 3".  SetPaintingStrategy(PaintingStrategy.DASHES);

plot 

"P/E 4" = AE * PEFour;

"P/E 4".  SetDefaultColor(CreateColor(200,100,200));

"P/E 4".  SetPaintingStrategy(PaintingStrategy.DASHES);

plot 

"P/E 5" = AE * PEFive;

"P/E 5".  SetDefaultColor(CreateColor(225,100,100));

"P/E 5".  SetPaintingStrategy(PaintingStrategy.DASHES);



def locate = !IsNaN(c[3]) && IsNaN(c[2]);

AddChartBubble(locate, "P/E 1", "P/E: "+PEOne+ "\n$"+"P/E 1", 

CreateColor(100,225,100));

AddChartBubble(locate, "P/E 2", "P/E: "+PETwo+ "\n$"+"P/E 2",

CreateColor(75,175,225)); 

AddChartBubble(locate, "P/E 3", "P/E: "+PEThree+ "\n$"+"P/E 3", 

CreateColor(250,175,50));

AddChartBubble(locate, "P/E 4", "P/E: "+PEFour+ "\n$"+"P/E 4",

CreateColor(200,100,200)); 

AddChartBubble(locate, "P/E 5", "P/E: "+PEFive+ "\n$"+"P/E 5",

CreateColor(225,100,100)); 

# f/ Price to Earnings Lines
 
I like this idea, i have a code that's something like that, maybe we could use this to build it out.








declare hide_on_intraday;

# inputs

input PEOne = 10;

input PETwo = 12;

input PEThree = 15;

input PEFour = 16;

input PEFive = 18;

# universals

def c = close;

def bn = BarNumber();

def Earnings = GetActualEarnings();

# subscript for 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;

}

# identifies the which bars are the previous 1,2,3 etc earnings bars

def earningsBar = if !IsNaN(Earnings)

then bn

else earningsBar[1];

def priorEarningsBar1 = prior(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 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);

# defining which 4 earnings are in the running annual total for each quarter

def earnings1;

def earnings2;

def earnings3;

def earnings4;

if bn == hEB {

earnings1 = earnings;

earnings2 = GetValue(earnings, bn - priorEarningsBar1);

earnings3 = GetValue(earnings, bn - priorEarningsBar2);

earnings4 = GetValue(earnings, bn - priorEarningsBar3);

}else{

earnings1 = earnings1[1];

earnings2 = earnings2[1];

earnings3 = earnings3[1];

earnings4 = earnings4[1];

}



def earnings5 = if(bn == hpEB1, earnings, earnings5[1]);

def earnings6 = if(bn == hpEB1, HighestAll(earnings3), earnings6[1]);

def earnings7 = if(bn == hpEB1, HighestAll(earnings4), earnings7[1]);

def earnings8 = if(bn == hpEB1, GetValue(Earnings, hpEB1 - hpEB4),

earnings8[1]);



def earnings9 = if(bn == hpEB2, earnings, earnings9[1]);

def earnings10 = if(bn == hpEB2, HighestAll(earnings7),earnings10[1]);

def earnings11 = if(bn == hpEB2, HighestAll(earnings8),earnings11[1]);

def earnings12 = if(bn == hpEB2, GetValue(earnings, hpEB2 - hpEB5),

earnings12[1]);



def earnings13 = if(bn == hpEB3, earnings, earnings13[1]);

def earnings14 = if(bn == hpEB3,HighestAll(earnings11),earnings14[1]);

def earnings15 = if(bn == hpEB3,HighestAll(earnings12),earnings15[1]);

def earnings16 = if(bn == hpEB3,GetValue(earnings, hpEB3 - hpEB6),

earnings16[1]);



def earnings17 = if(bn == hpEB4, earnings, earnings17[1]);

def earnings18 = if(bn == hpEB4,HighestAll(earnings15),earnings18[1]);

def earnings19 = if(bn == hpEB4,HighestAll(earnings16),earnings19[1]);;

def earnings20 = if(bn == hpEB4,GetValue(earnings, hpEB4 - hpEB7),

earnings20[1]);



def earnings21 = if(bn == hpEB5, earnings, earnings21[1]);

def earnings22 = if(bn == hpEB5, HighestAll(earnings19), earnings22[1]);

def earnings23 = if(bn == hpEB5, HighestAll(earnings20), earnings23[1]);;

def earnings24 = if(bn == hpEB5, GetValue(earnings, hpEB5 - hpEB8),

earnings24[1]);



def aE =

if bn == hpEB5

then earnings21 + earnings22 + earnings23 + earnings24

else if bn == hpEB4

then earnings17 + earnings18 + earnings19 + earnings20

else if bn == hpEB3

then earnings13 + earnings14 + earnings15 + earnings16

else if bn == hpEB2

then earnings9 + earnings10 + earnings11 + earnings12

else

if bn == hpEB1

then earnings5 + earnings6 + earnings7 + earnings8

else if bn == hEB

then earnings1 + earnings2 + earnings3 + earnings4

else aE[1];



plot

"P/E 1" = AE * PEOne;

"P/E 1". SetDefaultColor(CreateColor(100,225,100));

"P/E 1". SetPaintingStrategy(PaintingStrategy.DASHES);

plot

"P/E 2" = AE * PETwo;

"P/E 2". SetDefaultColor(CreateColor(75,175,225));

"P/E 2". SetPaintingStrategy(PaintingStrategy.DASHES);

plot

"P/E 3" = AE * PEThree;

"P/E 3". SetDefaultColor(CreateColor(250,175,50));

"P/E 3". SetPaintingStrategy(PaintingStrategy.DASHES);

plot

"P/E 4" = AE * PEFour;

"P/E 4". SetDefaultColor(CreateColor(200,100,200));

"P/E 4". SetPaintingStrategy(PaintingStrategy.DASHES);

plot

"P/E 5" = AE * PEFive;

"P/E 5". SetDefaultColor(CreateColor(225,100,100));

"P/E 5". SetPaintingStrategy(PaintingStrategy.DASHES);



def locate = !IsNaN(c[3]) && IsNaN(c[2]);

AddChartBubble(locate, "P/E 1", "P/E: "+PEOne+ "\n$"+"P/E 1",

CreateColor(100,225,100));

AddChartBubble(locate, "P/E 2", "P/E: "+PETwo+ "\n$"+"P/E 2",

CreateColor(75,175,225));

AddChartBubble(locate, "P/E 3", "P/E: "+PEThree+ "\n$"+"P/E 3",

CreateColor(250,175,50));

AddChartBubble(locate, "P/E 4", "P/E: "+PEFour+ "\n$"+"P/E 4",

CreateColor(200,100,200));

AddChartBubble(locate, "P/E 5", "P/E: "+PEFive+ "\n$"+"P/E 5",

CreateColor(225,100,100));

# f/ Price to Earnings Lines
Yeah could use this, I don't know how to apply it fully though. We need to plot a "plus and minus two standard deviations band around the mean price/earnings ratio"
My theory behind this is something like this

def MeanPE average(pe, 252);
plot UpperBand = 2*StDev(MeanPE)
plot LowerBand = -2*StDev(MeanPE)
 
Yeah could use this, I don't know how to apply it fully though. We need to plot a "plus and minus two standard deviations band around the mean price/earnings ratio"
My theory behind this is something like this

def MeanPE average(pe, 252);
plot UpperBand = 2*StDev(MeanPE)
plot LowerBand = -2*StDev(MeanPE)
I like the PE data and the stDev for the growth stocks now that Yields are moving higher and bond futures are moving lower making new monthly lower lows, lots of high growth valuations out there and they will all cycle lower as valuations become more important in the face of higher interest rates, Fed Kaplan said today he is on board for higher rates next year and the Fed is already normalizing by reducing the current facilities. We will need some more help to get this code completed.
 
I like the PE data and the stDev for the growth stocks now that Yields are moving higher and bond futures are moving lower making new monthly lower lows, lots of high growth valuations out there and they will all cycle lower as valuations become more important in the face of higher interest rates, Fed Kaplan said today he is on board for higher rates next year and the Fed is already normalizing by reducing the current facilities. We will need some more help to get this code completed.
Yeah the theory behind it is just finding stocks that are outside the two PE bands. I'll try to work something out right now, otherwise would love help from anyone
 
Code:
declare lower;
declare hide_on_intraday;

input priorYear = no;
input StDevChannel = no;
input StDevMult    = 1;
input bubbles   = no;
input cloud     = no;

def c  = close;
def na = Double.NaN;
def bn = BarNumber();
def earnings = GetActualEarnings();

# subscript 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;
}

# modified version of Mobius fold subscript to handle bars at which there are no earnings
script E {
    input c = close;
    input n = 10;
    def y = if !IsNaN(c)
            then c else 0 ;
    def e = fold i = 0 to n+1
    with s do s + GetValue(y, i);
    plot sum = e;
}

    # identifies the which bars are the previous 1,2,3 etc earnings bars as of the current time
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 priorEarningsBar25 = prior(priorEarningsBar24);
def priorEarningsBar26 = prior(priorEarningsBar25);
def priorEarningsBar27 = prior(priorEarningsBar26);
def priorEarningsBar28 = prior(priorEarningsBar27);
def priorEarningsBar29 = prior(priorEarningsBar28);
def priorEarningsBar30 = prior(priorEarningsBar29);
def priorEarningsBar31 = prior(priorEarningsBar30);
def priorEarningsBar32 = prior(priorEarningsBar31);
def priorEarningsBar33 = prior(priorEarningsBar32);
def priorEarningsBar34 = prior(priorEarningsBar33);
def priorEarningsBar35 = prior(priorEarningsBar34);
def priorEarningsBar36 = prior(priorEarningsBar35);
def priorEarningsBar37 = prior(priorEarningsBar36);
def priorEarningsBar38 = prior(priorEarningsBar37);
def priorEarningsBar39 = prior(priorEarningsBar38);
def priorEarningsBar40 = prior(priorEarningsBar39);
def priorEarningsBar41 = prior(priorEarningsBar40);
def priorEarningsBar42 = prior(priorEarningsBar41);
def priorEarningsBar43 = prior(priorEarningsBar42);
def priorEarningsBar44 = prior(priorEarningsBar43);

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);
def hpEB25 = HighestAll(priorEarningsBar25);
def hpEB26 = HighestAll(priorEarningsBar26);
def hpEB27 = HighestAll(priorEarningsBar27);
def hpEB28 = HighestAll(priorEarningsBar28);
def hpEB29 = HighestAll(priorEarningsBar29);
def hpEB30 = HighestAll(priorEarningsBar30);
def hpEB31 = HighestAll(priorEarningsBar31);
def hpEB32 = HighestAll(priorEarningsBar32);
def hpEB33 = HighestAll(priorEarningsBar33);
def hpEB34 = HighestAll(priorEarningsBar34);
def hpEB35 = HighestAll(priorEarningsBar35);
def hpEB36 = HighestAll(priorEarningsBar36);
def hpEB37 = HighestAll(priorEarningsBar37);
def hpEB38 = HighestAll(priorEarningsBar38);
def hpEB39 = HighestAll(priorEarningsBar39);
def hpEB40 = HighestAll(priorEarningsBar40);
def hpEB41 = HighestAll(priorEarningsBar41);
def hpEB42 = HighestAll(priorEarningsBar42);
def hpEB43 = HighestAll(priorEarningsBar43);
def hpEB44 = HighestAll(priorEarningsBar44);

def Year  = E(earnings, hEB   - hpEB3);
def pY1Q  = E(earnings, hpEB1 - hpEB4);
def pY2Q  = E(earnings, hpEB2 - hpEB5);
def pY3Q  = E(earnings, hpEB3 - hpEB6);
def pY4Q  = E(earnings, hpEB4 - hpEB7);
def pY5Q  = E(earnings, hpEB5 - hpEB8);
def pY6Q  = E(earnings, hpEB6 - hpEB9);
def pY7Q  = E(earnings, hpEB7 - hpEB10);
def pY8Q  = E(earnings, hpEB8 - hpEB11);
def pY9Q  = E(earnings, hpEB9 - hpEB12);
def pY10Q = E(earnings, hpEB10 - hpEB13);
def pY11Q = E(earnings, hpEB11 - hpEB14);
def pY12Q = E(earnings, hpEB12 - hpEB15);
def pY13Q = E(earnings, hpEB13 - hpEB16);
def pY14Q = E(earnings, hpEB14 - hpEB17);
def pY15Q = E(earnings, hpEB15 - hpEB18);
def pY16Q = E(earnings, hpEB16 - hpEB19);
def pY17Q = E(earnings, hpEB17 - hpEB20);
def pY18Q = E(earnings, hpEB18 - hpEB21);
def pY19Q = E(earnings, hpEB19 - hpEB22);
def pY20Q = E(earnings, hpEB20 - hpEB23);
def pY21Q = E(earnings, hpEB21 - hpEB24);
def pY22Q = E(earnings, hpEB22 - hpEB25);
def pY23Q = E(earnings, hpEB23 - hpEB26);
def pY24Q = E(earnings, hpEB24 - hpEB27);
def pY25Q = E(earnings, hpEB25 - hpEB28);
def pY26Q = E(earnings, hpEB26 - hpEB29);
def pY27Q = E(earnings, hpEB27 - hpEB30);
def pY28Q = E(earnings, hpEB28 - hpEB31);
def pY29Q = E(earnings, hpEB29 - hpEB32);
def pY30Q = E(earnings, hpEB30 - hpEB33);
def pY31Q = E(earnings, hpEB31 - hpEB34);
def pY32Q = E(earnings, hpEB32 - hpEB35);
def pY33Q = E(earnings, hpEB33 - hpEB36);
def pY34Q = E(earnings, hpEB34 - hpEB37);
def pY35Q = E(earnings, hpEB35 - hpEB38);
def pY36Q = E(earnings, hpEB36 - hpEB39);
def pY37Q = E(earnings, hpEB37 - hpEB40);
def pY38Q = E(earnings, hpEB38 - hpEB41);
def pY39Q = E(earnings, hpEB39 - hpEB42);
def pY40Q = E(earnings, hpEB40 - hpEB43);


def annualEarnings =
if bn == hEB    then Year  else
if bn == hpEB1  then pY1Q  else
if bn == hpEB2  then pY2Q  else
if bn == hpEB3  then pY3Q  else
if bn == hpEB4  then pY4Q  else
if bn == hpEB5  then pY5Q  else
if bn == hpEB6  then pY6Q  else
if bn == hpEB7  then pY7Q  else
if bn == hpEB8  then pY8Q  else
if bn == hpEB9  then pY9Q  else
if bn == hpEB10 then pY10Q else
if bn == hpEB11 then pY11Q else
if bn == hpEB12 then pY12Q else
if bn == hpEB13 then pY13Q else
if bn == hpEB14 then pY14Q else
if bn == hpEB15 then pY15Q else
if bn == hpEB16 then pY16Q else
if bn == hpEB17 then pY17Q else
if bn == hpEB18 then pY18Q else
if bn == hpEB19 then pY19Q else
if bn == hpEB20 then pY20Q else
if bn == hpEB21 then pY21Q else
if bn == hpEB22 then pY22Q else
if bn == hpEB23 then pY23Q else
if bn == hpEB24 then pY24Q else
if bn == hpEB25 then pY25Q else
if bn == hpEB26 then pY26Q else
if bn == hpEB27 then pY27Q else
if bn == hpEB28 then pY28Q else
if bn == hpEB29 then pY29Q else
if bn == hpEB30 then pY30Q else
if bn == hpEB31 then pY31Q else
if bn == hpEB32 then pY32Q else
if bn == hpEB33 then pY33Q else
if bn == hpEB34 then pY34Q else
if bn == hpEB35 then pY35Q else
if bn == hpEB36 then pY36Q else
if bn == hpEB37 then pY37Q else
if bn == hpEB38 then pY38Q else
if bn == hpEB39 then pY39Q else
if bn == hpEB40 then pY40Q 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 - hpEB39).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);

plot
Upper = if   StDevChannel
        then Slope + StDevMult * HighestAll(StDev(c/AE,22))
        else na;
plot
Lower = if   StDevChannel
        then Slope - StDevMult * HighestAll(StDev(c/AE,22))
        else na;
Upper.SetDefaultColor(Color.Gray);
Lower.SetDefaultColor(Color.Gray);

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.WHITE, color2 = Color.WHITE);

# f/ Price to Earnings Oscillator
 
Code:
# 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
 
If you can get the Standard Dev coded this could be a good code for the coming volatility in growth stocks, we would watch the cycle lower into a good valuation and then look for a reversal and confirmation in the cycle and have swing longs for stocks ahead of next quarter EPS
 
Code:
declare hide_on_intraday;

script Scale {
    input c = close;
    input Min = .01;
    input Max =   1;
    def hh = HighestAll(c);
    def ll   = LowestAll(c);
    plot Range = (((Max - Min) * (c - ll)) /  (hh - ll)) + Min;
}
def Earnings = if IsNaN(GetActualEarnings()) then 0 else GetActualEarnings();
def YearsEarnings = Sum(Earnings, 252);
def PE = close / YearsEarnings;

AddLabel(yes, "P/E Ratio: " + PE, color.white);
plot Ratio = Average(Scale(c = PE, Min = Lowest(close, 252), Max = Highest(close, 252)), 252);
     Ratio.SetDefaultColor(Color.White);
def SD = StDev(Ratio, 252);
plot Upper = Ratio + (3 * SD);
Upper.SetDefaultColor(color.cyan);
plot Lower = Ratio + (-3 * SD);
Lower.SetDefaultColor(color.cyan);
# End Code PE Study
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
187 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top