Sunday, February 15, 2015

my comp - display labeled comparison plots (thinkscript included)

thinkorswim has a native comparison study that you can apply to a chart. this plots the performance of an comparison equity to the charted equity. the method employed is to re-origin the comparison product to the closing price on the first bar of the charted equity and then apply the percentage moves of the comparison product to the comparison plot. when you get a lot of comparison equities on a chart it looks like this:
SPY with sector etf comparisons.
spaghetti! and it takes some forensics to figure out which product is which colored line. 

sdi_comp plots the same comparison line but adds a bubble at the endpoint of each comparison plot to identify the ticker. also, a chart label is displayed at the top in the same color as the comparison line. here's how this looks:
SPY compared to sector etf performance using sdi_comp
ok, still spaghetti but it should be a little easier to figure out what's what. you should at least be able to see what the two extreme sector etfs are: xly the leader and xle the laggard. 

in the theory of sector rotation xly, the consumer descretionary sector etf, is typically a beneficiary of the late stages of an economic contraction cycle while xle, the energy sector etf, is typically prominent in the late stages of an economic expansion. iyt, the transportation etf, is a beneficiary of the early stages of economic expansion and it is clearly lagging a little bit. so this array of sector performance would seem to indicate the economy is in the late stages of an economic contraction soon to begin an expansion phase ... in theory.

here's the code:




##########################
# sdi_comp: Plot performance of a comparison equity.
#hint: Displays the performance of a comparison equity. Price is re-origined to the first close of the primary charted equity and then the daily percentage moves of the comparison equity are applied to the comparison plot. At the endpoint of the comparison line a chart bubble is displayed identifying the comparison ticker symbol. Also, a color coded chart label is displayed in the same color as the comparison plot and showing the overall percentage move. It is recommended to use percentage price scale with comparison plots. rev: 1.0 http://www.smallDogInvestor.com 
# author: allen everhart
# date: 15feb2015
# 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 symbol="IYT";
#hint symbol: The symbol to compare. rev: 1.0 http://www.smallDogInvestor.com
input compBubble={default SHOW,HIDE};
#hint compBubble: Show or hide the bubble at the endpoint of the comparison plot.
input compBubblePosition={default CHOOSE, UP, DOWN};
#hint compBubblePosition: CHOOSE will position the comparison bubble above the plot when the comparison equity is higher than the primary chart equity and below when the comparison equity is lower. UP forces the bubble position above the comparison line. DOWN forces the bubble position below the comparison line.
input colorCode={default SHOW,HIDE};
#hint colorCode: Show or hide the color code label.

def symOrg=compoundValue(1, symOrg[1], close(symbol));
def underlyingOrg=compoundValue(1, underlyingOrg[1],close);
plot cmp=underlyingOrg*close(symbol)/symOrg;
def barn=if !isnan(close) then barNumber() else double.NEGATIVE_INFINITY;
def bubbleUp;
switch(compBubblePosition){
    case UP: bubbleUp=1;
    case DOWN: bubbleUp=0;
    case CHOOSE: bubbleUp= cmp>close;
}
addChartBubble(
   barn==highestAll(barn) && compBubble==compBubble.SHOW,
    cmp, symbol, cmp.takeValueColor()
    , bubbleUp
);
addlabel(
    colorCode==colorCode.SHOW,
    concat(symbol,
    concat(":",asPercent(close(symbol)/symOrg-1))),
    cmp.takeValueColor()

);

3 comments:

  1. Hey Allen, How are you getting this to work with multiple symbols? "IYT" is the only symbol in the code, and when I try to add more, separated by commas, nothing is plotted. Really interesting study! Thanks or sharing.

    ReplyDelete
    Replies
    1. hi brandon, you have to add the study to the chart multiple times. each one sets up for one comparison. that way you can have as many as you like.
      best.
      -allen

      Delete
    2. Thank you! That makes so much sense, but was alluding me. Also, I really appreciate you sharing all this work. It's quite generous. SuperTRIN has lived on my charts since I discovered it on your blog.

      Delete