Posted via the thinkScript lounge by Nube.
Here's the code:
Ex-dividend Days Label
22:40 Nube: Price to Earnings lines have come up a few times without a good answer. This isn't a good answer either, but it shows one way to do it. This will draw lines for 5 input PE values at the 2 most recent earnings releases. If someone desires the lines going further back, they can expand the script using more of the same logic
Here's the code:
Code:
# Earnings, Dividend and P/E Labels
# Nube
# v01 7.3.18
# http://tos.mx/ieYc5f#
#hint: Displays the previous annual earnings for the 2 most recent quarters and the previous 4 dividends in labels as well as 5 user selectable P/E values for two most recent quarters. Method can be duplicated to draw PE lines for previous quarters.
declare hide_on_intraday;
input PEOne = 9;
input PETwo = 13;
input PEThree = 15;
input PEFour = 16;
input PEFive = 18;
def c = close;
def na = Double.NaN;
def bn = BarNumber();
def Earnings = GetActualEarnings();
def Dividend = GetDividend();
def currentBar = if !IsNaN(c) and IsNaN(c[-1])
then bn
else currentBar[1];
# section below identifies the bars at which earnings were released
def earningsBar = if !IsNaN(Earnings)
then bn
else earningsBar[1];
def prevEarningsBar1 = if earningsBar != earningsBar[1]
then earningsBar[1]
else prevEarningsBar1[1];
def prevEarningsBar2 = if prevEarningsBar1 != prevEarningsBar1[1]
then prevEarningsBar1[1]
else prevEarningsBar2[1];
def prevEarningsBar3 = if prevEarningsBar2 != prevEarningsBar2[1]
then prevEarningsBar2[1]
else prevEarningsBar3[1];
def prevEarningsBar4 = if prevEarningsBar3 != prevEarningsBar3[1]
then prevEarningsBar3[1]
else prevEarningsBar4[1];
# identifies the which bars are the previous 1,2,3 etc earnings bars as of the current time
def hEB = HighestAll(earningsBar);
def hpEB1 = HighestAll(prevEarningsBar1);
def hpEB2 = HighestAll(prevEarningsBar2);
def hpEB3 = HighestAll(prevEarningsBar3);
def hpEB4 = HighestAll(prevEarningsBar4);
# getting the four most recent earnings values
def currentEarnings = if bn == hEB
then Earnings
else currentEarnings[1];
def secondEarnings = if bn == hEB
then GetValue(Earnings, hEB - prevEarningsBar1)
else secondEarnings[1];
def thirdEarnings = if bn == hEB
then GetValue(Earnings, hEB - prevEarningsBar2)
else thirdEarnings[1];
def fourthEarnings = if bn == hEB
then GetValue(Earnings, hEB - prevEarningsBar3)
else fourthEarnings[1];
# getting 4 earnings values up until second most recent earnings bar
def lQfirstEarnings = if bn == hpEB1
then Earnings
else lQfirstEarnings[1];
def lQsecondEarnings = if bn == hpEB1
then GetValue(Earnings, hpEB1 - hpEB2)
else lQsecondEarnings[1];
def lQthirdEarnings = if bn == hpEB1
then GetValue(Earnings, hpEB1 - hpEB3)
else lQthirdEarnings[1];
def lQfourthEarnings = if bn == hpEB1
then GetValue(Earnings, hpEB1 - hpEB4)
else lQfourthEarnings[1];
def currentAnnualEarnings = currentEarnings + secondEarnings + thirdEarnings + fourthEarnings;
def lastQannualEarning = lQfirstEarnings + lQsecondEarnings + lQthirdEarnings + lQfourthEarnings;
def cAE = currentAnnualEarnings;
def lQaE = lastQannualEarning;
def annualEarnings = if bn == hpEB1
then lQaE
else if bn == hEB
then cAE
else annualEarnings[1];
def AE = annualEarnings;
def PE = c / AE;
# same process for getting the dividend bars
def dividendBar = if !IsNaN(Dividend)
then bn
else dividendBar[1];
def prevDividendBar1 = if DividendBar != DividendBar[1]
then DividendBar[1]
else prevDividendBar1[1];
def prevDividendBar2 = if prevDividendBar1 != prevDividendBar1[1]
then prevDividendBar1[1]
else prevDividendBar2[1];
def prevDividendBar3 = if prevDividendBar2 != prevDividendBar2[1]
then prevDividendBar2[1]
else prevDividendBar3[1];
# again, same process for getting the dividend values
def currentDividend = if bn == DividendBar
then Dividend
else currentDividend[1];
def secondDividend = GetValue(Dividend, currentBar - prevDividendBar1);
def thirdDividend = GetValue(Dividend, currentBar - prevDividendBar2);
def fourthDividend = GetValue(Dividend, currentBar - prevDividendBar3);
def annualDividend = currentDividend + secondDividend + thirdDividend + fourthDividend;
def AD = annualDividend;
def yield = AD / c;
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));
Addlabel(1, "Earnings 1: $" +currentEarnings+" Earnings 2: $" +secondEarnings+" Earnings 3: $" +thirdEarnings+" Earnings 4: $" +fourthEarnings, CreateColor(200,200,100));
Addlabel(1, " Last Q Earnings 1: $" +lQfirstEarnings+" Last Q Earnings 2: $" +lQsecondEarnings+" Last Q Earnings 3: $" +lQthirdEarnings+" Last Q Earnings 4: $" +lQfourthEarnings, CreateColor(225,175,75));
Addlabel(1, "Dividend 1: $" +currentDividend+" Dividend 2: $" +secondDividend+" Dividend 3: $" +thirdDividend+" Dividend 4: $" +fourthDividend, CreateColor(250,150,0));
AddLabel(1," Annual Earnings: $"+AE, CreateColor(100,225,100));
AddLabel(1," Annual Earnings Last Q: $"+lQaE, CreateColor(100,225,100));
AddLabel(1," Annual Dividend: $"+AD, CreateColor(100,225,100));
AddLabel(yes, "P/E Ratio: " + Round(PE,2),
CreateColor(200,200,200));
AddLabel(yes, "Yield: " + AsPercent(yield),
CreateColor(200,200,200));
# f/ Earnings, Dividend and P/E Labels
Ex-dividend Days Label
Code:
# WatchList of Label Days Till X Dividend
# If over 30 days away shows 30
# Mobius
input DaysTillXdiv = 30;
def LastDividendBar = AbsValue(GetEventOffset(Events.DIVIDEND, 0));
def NextDividend = if isNaN(LastDividendBar) then 0 else LastDividendBar;
AddLabel(NextDividend == DaysTillXdiv, "Dividend More than 30 days out", color.white);
AddLabel(NextDividend <= DaysTillXdiv-1 and NextDividend > 0, "Dividend in " + NextDividend + " days", Color.Red);
Last edited by a moderator: