Saturday, July 7, 2012

thinkscript included: the trouble with trin

a popular measure of market breadth is an indicator called trin (and also trin/q on the thinkdesktop platform.) these are also known as the arms indices. the problem i encounter with trin stems from my desire to plot it as a lower study indicator on a normal linear chart of the related futures products, /es and /nq, resp.  you see, trin, being a ratio of several other breadth measures ((advancing stocks/declining stocks)/(advancing volume/declining volume)) presents a neutral reading when it is at one, a bullish reading for values less than 1 and a bearish reading for values greater than 1.

the first trouble i have with trin is that it is an inverse indicator. it indicates sell when it is up and buy when it is down. just as a matter of sanity i always try to rejigger such indicators so that up is a buy signal and down is a sell signal. this is not hard to do just plot the negated (-trin) value.

the second trouble i have with trin is that it is a nonlinear indicator. values in the bearish area are linear, with a value of 4 being twice as bearish as a value of 2, but values in the bullish area are logarithmic with bullish readings of 1/4 being twice as bullish as 1/2 (and which correspond to the bearish 4 and 2, respectively.) a plot of bullish trin readings look kind of squashed on a normal, linear-grid chart, even if one is plotting the negated value. what i want is to plot bullish readings as their inverse value, 1/trin.

finally, the third trouble i have with trin is stitching together these two disparate views into a single indicator. the solution i use is to re-origin the neutral trin reading from one to zero. thus, if trin is greater than or equal to 1 then i will plot 1-trin otherwise i plot (1/trin)-1. this is the view of trin that i present in my study called sdi_breadth. here's a picture of what i'm talking about:

/ES with sdi_breadth

a bonus of this view of trin is that we can plot a legitimate moving average of the values. this is true because bullish and bearish readings are on the same scale - a one point change in the bearish area is equal to a one point change in the bullish area (not so for the raw trin plot.) so the green/red line is a 6 bar moving average of the histogram plot, brc (breadth corrected.)

also, values of raw trin that are close to one are more-or-less neutral. likewise values of brc that are close to zero are neutral. only now with brc we can equitably declare values within say, .2, from zero to be more-or-less neutral which i show in grey.

so here's the code:

# sdi_breadth - TRIN-based market breadth indicator
#hint: a view of the TRIN and TRIN/Q ratio's that is inverted and reorigined from the neutral reading of 1 to 0. this unpancakes the bullish readings to create an indicator that is evenly represented on an linear scale, can be averaged and analyzed technically. plots bright red for increasingly bearish bars, bright green for increasingly bullish bars, darkened bars represent fading and grey bars represent weak readings below the threshold. rev: 1.1 source:
# rev: 1.1 - get the color right on the first bar
# author: allen everhart
# date: Jul 17, 2010
# 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.
declare lower;
input source = { default "$TRIN", "$TRIN/Q"};
#hint source: ratio selection
input threshold = 0.2;
def trclose = close(source);

plot brc =
  if trclose >= 1 then
    1 - trclose
    (1 / trclose) - 1

  if absValue(brc) < threshold then
  else if isNaN(brc[1]) && brc >= 0 then
  else if isnaN(brc[1]) && brc < 0 then
  else if brc >= 0 && brc >= brc[1] then
  else if brc >= 0 && brc < brc[1] then
  else if brc < 0 && brc <= brc[1] then

input mvgAvgLen = 6 ;
plot bma = average(brc,mvgAvgLen);
  if bma[1] > bma then color.RED
  else if bma[1] < bma then color.GREEN
  else color.LIGHT_GRAY
#plot bn = barNumber();

No comments:

Post a Comment