Cost Basis Dividend Yield
Just like with any trading/investing platform, thinkorswim allows you to find the current dividend yield (Annual $ of dividends divided by the current price) of any stock that pays dividends. But what about your dividend yield based on the purchase price of your shares? And how do we see this while on the chart?
We insert the following script into a new study:
# Hint: Works best on 1D candle charts
def div = if IsNaN(GetDividend()) then div[1] else GetDividend();
def div_Event = if !isnan(getdividend()) then 1 else 0;
def div_Frequency = sum(div_Event,252);
def annualDiv = if div <> 0 then div * div_frequency else Double.NaN;
def costBasis = if GetAveragePrice() == 0 then Double.NaN else GetAveragePrice();
def cost_Basis_Yield = (annualDiv/costBasis) * 100;
def yield = (annualDiv/close)*100;
def show_Condition_CB_Yield = if !isnan(annualDiv) and !isnan(costBasis) then yes else no;
def show_Condition_Div_Yield = if !isnan(annualDiv) then yes else no;
AddLabel(show_Condition_Div_Yield, "Div Yield: " + round(yield,2) + "%", if isnan(costBasis) then color.white else if close < costBasis then color.green else if close > costBasis then color.red else color.white);
AddLabel(show_Condition_CB_Yield, "Cost Basis Yield: " + Round(cost_Basis_Yield, 2) + "%",if close > costBasis then color.green else if close < costBasis then color.red else color.white);
Now at the top of a dividend paying stock, we see the current yield of the stock, as well as the yield based on what we paid for the stock. In this case, AT&T is lower than when we bought it, so the cost basis yield is red. The current yield is green in this case as if we were to buy more shares right now we would be getting a better div yield on those shares than what we are currently getting based on our purchase price.
For stocks that we haven't purchased yet, it will still display the current dividend yield at the top of the chart. It will also work whether the stock pays dividends quarterly or monthly, like the stock shown here.