ajs_thinkscripter
New member
Hello, I wrote a script to calculate the dividend yield divided by the original cost basis(I.E "Cost Basis Yield") and show it as a label at the top of the chart(since portfolio data doesn't work as a column as far as I understand.
Here's the script for that:
def QuarterlyDiv = if IsNaN(GetDividend()) then QuarterlyDiv[1] else GetDividend();
def AnnualDiv = if QuarterlyDiv <> 0 then QuarterlyDiv * 4 else Double.NaN;
def costbasis = if GetAveragePrice() == 0 then Double.NaN else GetAveragePrice();
def yield = AnnualDiv / costbasis * 100;
AddLabel(1, "Cost Basis Yield: " + Round(yield, 1) + "%", Color.CYAN);
It works fine, but I want to code it so if a stock doesn't pay dividends that the whole label doesn't show(instead of N/A or -----).
I don't understand why something like this:
AddLabel(if isNan(quarterlydiv) then 0 else 1 , "Cost Basis Yield: " + Round(yield, 1) + "%", Color.CYAN);
doesn't work. It accepts the script like it's going to work, but still shows N/A on non div paying stocks
Here's the script for that:
def QuarterlyDiv = if IsNaN(GetDividend()) then QuarterlyDiv[1] else GetDividend();
def AnnualDiv = if QuarterlyDiv <> 0 then QuarterlyDiv * 4 else Double.NaN;
def costbasis = if GetAveragePrice() == 0 then Double.NaN else GetAveragePrice();
def yield = AnnualDiv / costbasis * 100;
AddLabel(1, "Cost Basis Yield: " + Round(yield, 1) + "%", Color.CYAN);
It works fine, but I want to code it so if a stock doesn't pay dividends that the whole label doesn't show(instead of N/A or -----).
I don't understand why something like this:
AddLabel(if isNan(quarterlydiv) then 0 else 1 , "Cost Basis Yield: " + Round(yield, 1) + "%", Color.CYAN);
doesn't work. It accepts the script like it's going to work, but still shows N/A on non div paying stocks