Saturday, October 3, 2015

thinkscript included: sdi_bnh_label - display buy 'n hold p&l in a chart label

hi folks, i know it's been a while but i've been working on a bunch of things and i have a backlog of stuff to get out to you. here's the first:

the sdi_bnh_label displays the percentage gain/loss that one would have achieved had one bought the charted equity on the open of the first bar of the chart. here's what i'm talking about:
 
spy with bnh label
i find that this is particularly helpful when combined with the sdi_yield_label on a 1 year chart:

spy with bnh and yield labels
you see, some data providers treat dividends as little splits of the underlying equity but tos charts do not, which is a positive in my book. here we can see that the gain in spy in the past 52 weeks has been entirely from dividends.

here's the code: (how to install)


################################
# sdi_bnh_label: Display Buy N' Hold Percentage P&L
#hint: Displays the P&L in percent of a trade bought on the open of the first candle of the chart and sold at the close of the most recent candle. rev: 1.0.0 http://www.smalldoginvestor.com
# author: allen everhart
# date: 3oct2015
# revision 1.0.0
# copylefts reserved. This is free software. That means you are free
# to use or modify it for your own usage but not for resale. 
# Help me get the word out about my blog by keeping this header
# in place. 

input labelText="bnh:";
#hint labelText: text that appears before the p&l number. rev: 1.0.0 http://www.smallDogInvestor.com
def firstOpen=compoundValue(1,firstOpen[1],open);
def bnh=close/firstOpen-1;
plot labelColor=double.NaN;
labelColor.hideTitle();
labelColor.defineColor("up",color.UPTICK);
labelColor.defineColor("down", color.DOWNTICK);
addlabel(1, 
    concat( labelText,
    concat( if bnh>=0 then "+" else "(",
    concat( AsPercent(bnh),
         if bnh<0 then ")" else ""))),
# copylefts reserved. This is free software. That means you are free
# to use or modify it for your own usage but not for resale. 
# Help me get the word out about my blog by keeping this header
# in place. 
    if bnh>=0then labelColor.color("up") else labelColor.color("down")

);
############################

No comments:

Post a Comment