Wednesday, August 31, 2011

sdi_openrange - display the open range on an intraday chart

The range exhibited during the cash-market opening is important to some intraday traders. Very often the high and low of the opening range are reversal levels. The sdi_openrange study will help you stay vigilant about these levels by displaying the opening range overlaying the main chart or on the sidebar. Also, this intraday study is smart enough to know when to get out of the way - if you reset your aggregation size (the duration of each chart bar) larger than the opening period sdi_openrange will hide so you don't have to fiddle with the chart settings to get a clean chart. Enabling the sideline parameter will throw the range cloud over to the right-extension area where it won't interfere with your chart drawings.

Here's the image and code:

SPX with sdi_openrange.
#########################
# sdi_openrange
#hint: Show the high and low range of the opening period. Rev:1.0.1
#rev 1.0.1 8sep2011 - refactor sidebar to sideline
# source: smalldoginvestor.blogspot.com
# author: allen everhart
# date: 31Aug2011
# 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 upper ;
#hint openTime: Time of the cash-market open in your locality. Format:HHMM
input openTime = 0930 ;
#hint openMinutes: The duration of the opening period.
input openMinutes = 30 ;
#hint displayMinutes: Determines the number of bars to extend the open range cloud when displayed overlaying the main chart.
input displayMinutes = 120 ;
#hint sideline: Yes to display the open range on the right-extension area.
input sideline = no;

def minPerAg = getAggregationPeriod() / aggregationPeriod.MIN;
def elapseSecs = secondsFromTime(openTime);
rec startHi =
  if elapseSecs == 0 then
    high
  else if between(elapseSecs, 0, (openMinutes * 60) - 1) then
    Max(high, startHi[1])
  else
    startHi[1]
;
rec startLo =
  if elapseSecs == 0 then
    low
  else if between(elapseSecs, 1, (openMinutes * 60) - 1) && startLo[1] == 0 then #rev2
    low  #rev2
  else if between(elapseSecs, 0, (openMinutes * 60) - 1) then
    Min(low, startLo[1])
  else
    startLo[1]
;
plot sh =
  if !sideline && between(elapseSecs, 0, (openMinutes + displayMinutes) * 60)  then
    startHi
  else if sideline && isnaN(close) then
    startHi
  else
    double.NaN
;
plot sl =
  if !sideline && between(elapseSecs, 0, (openMinutes + displayMinutes) * 60)  then
    startLo
  else if sideline && isnaN(close) then
    startLo
  else
    double.NaN
;
sh.SetDefaultColor(color.YELLOW);
sl.SetDefaultColor(color.YELLOW);
def aghide =
  if getAggregationPeriod() > openMinutes * aggregationPeriod.MIN then
    yes
  else
    no
;
sl.setHiding(aghide);
sh.setHiding(aghide);
AddCloud( sl, sh, color.YELLOW,color.YELLOW);
#################################

Monday, August 29, 2011

sdi_hivol - decorate volume moving average when volume exceeds threshold percent

One of the indicators that is used in the InvesTools method is volume moving average. In particular, a significant trading opportunity may be indicated when volume exceeds the volume moving average by 50%. Sometimes this is easy to eyeball, other times it is a close call for which you will need to do the math. In the spirit of keeping things simple I have created a study that decorates the volume moving average with triangles when the volume exceeds the percentage threshold. Here's the image and code:


SPY with sdi_hivol indicating volume days exceeding 50% above volume moving average.
#############################
# sdi_hivol
#hint: Decorates the volume moving average with triangles when the volume exceeds a specified threshold percent.
# source: smallDogInvestor.blogspot.com
# author: allen everhart
# date: 29Aug2011
# Copyleft! 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 upper ;

#hint type: Type of the volume moving average: simple or exponential
input type = { default SMP, EXP } ;
#hint length: Number of bars to use for volume moving average.
input length = 20 ;
#hint hotPct: Percent over volume moving average to trigger decorations.
input hotPct = 50.0 ;


def ma =
  if type == type.SMP then
    simpleMovingAvg(Volume, length)
  else
    movAvgExponential(vOlume, length)
;


plot hv =
  if 100 * ((volume / ma) - 1) >= hotPct then
    ma
  else
    double.NaN
;


hv.SetDefaultColor( color.CYAN);
hv.SetLineWeight(1) ;
hv.setPaintingStrategy( PaintingStrategy.LINE_VS_TRIANGLES);


###############################


Friday, August 26, 2011

sdi_open - display open price level of cash market

The sdi_open study displays the open price level of the "cash market" on an intraday chart.  For those of you who do not trade products like futures and currency spots, this distinction may seem a little odd. Futures and spots typically have an official opening time in the early evening hours, US Eastern time, which is not usually a time that creates a price level that you want to watch intraday. Rather, you are more interested in the print created when the US or European stock (or cash) markets open. In any case, sdi_open allows you to specify the open time that is of interest to you, in your locality, but it defaults to 9:30, the open of the US cash market, Eastern US time. As with many of these intraday price level studies that I write, sdi_open can be configured to show the level in the right-extension area by enabling the sideline parameter. Also, the sdi_open level will hide when the aggregation period of the chart is set to DAY or larger. Here is the image and code:


SPX with sdi_open price for 9:30 bar.
#########################
# sdi_open
#hint: Shows the opening price as a dotted price level on an intraday chart. rev:1.0.1
#rev 1.0.1 8sep2011 refactored sidebar to sideline
# source: smallDogInvestor.blogspot.com
# author: allen everhart
# date: 27Aug2011
# 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 upper ;
#hint openTime: Time of the cash market open in your locality. Format:HHMM.
input openTime = 0930 ;
#hint sideline: Yes to show open price level in right extension area. No to show open price level overlaying main chart. 
input sideline = no;

def elapseSecs = secondsFromTime(openTime);
def aggSecs = getAggregationPeriod()/1000;

rec openPrice =
  if elapseSecs<0 && -elapseSecs < aggsecs  then
    open
  else if elapseSecs == 0 then
    open
  else
    openPrice[1]
;
#plot es = elapseSecs ;

def agHide =  if getAggregationPeriod() >= aggregationPeriod.DAY then
    yes
 else
   no
;

plot op =
  if sideline && !IsNaN(close) then
    double.NaN
  else if !sideline && isnaN(close) then
    double.NaN
  else if openprice == 0 then
    double.NaN
  else
    openPrice ;


Op.SetStyle(curve.POINTS);
op.setDefaultColor(color.dARK_ORANGE);
op.setHiding(aghide);

Monday, August 22, 2011

sdi_yearClose - Display Last Year's Closing Price

This study is an off-shoot of my study sdi_yesterClose. It started as an enhancement to yesterClose but, due to limitations in ThinkScript (no support for the aggregation period: YEAR), it was just cleaner and easier to put this out as its own study. Like many of my scripts you can choose whether you want to see the level overlaying the main chart or on the right-extension area via the sideline parameter. Here's the image and code:

SPX with sdi_yearClose study.
############################################
# sdi_yearClose: Display Last Year's Closing Price
#hint: Displays last year's closing price. rev 1.0.2 http://www.smallDogInvestor.com
# author: allen everhart
# date: 21AUG2011
# rev:1.0.2 15july2013 corrected incompatibility with a tos update that produced a java stack overflow.
# rev:1.0.1 8sep2011 refactored sidebar to sideline
# 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.

#hint sideline: Where to show the yesterClose. If yes then yesterClose only displays in the right extension area of the chart otherwise yesterClose displays in the regular chart area.
input sideline = no;

plot yr =
  if isNaN(close) && !sideline then
    double.NaN
  else if !isNaN(close) && sideline then
    double.Nan
  else
    getvalue( close( period=aggregationPeriod.MONTH), getMonth())
  ;

yr.setDefaultColor(color.BLUE);
yr.setStyle(curve.POINTS);
##########


Sunday, August 21, 2011

sdi_yesterClose - Display Yesterday's Closing Price On Today's Intraday Chart

If you are an intraday trader then you know it is vitally important to be vigilent about yesterday's closing price because it is often the case that this is where price movement is challenged. The sdi_yesterClose study will help you stay vigilent by plotting yesterday's closing price on your intraday chart. Also, it has the hooks to display the previous close of a variety of aggregation periods such as last week or month. As an added convenience, you may set it to display only in the right extension area (sideline.)  Moreover, this study will hide if you increase your chart's aggregation period up to a level where it does not make sense to display (e.g. you change from intraday to daily.)  Here's an image of the study followed by source code:


SPX intraday plot with yesterday's closing price.

############################################
# sdi_yesterClose: Display A Previous Closing Price
#hint: Displays yesterday's closing price. rev 1.2.2 http://www.smallDogInvestor.com
# author: allen everhart
# date: 21AUG2011
# rev:
#   1.2.2 13jul2013- corrected incompatibility with new tos.
#   1.2.1 8sep2011- refactor sidebar to sideline
#   1.2 21aug2011- was not extending value to right on sideline
#   1.1 21AUG2011- fixed bug with 2 DAYS, 3 DAYS selections
# 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 upper ;
#hint yesterPeriod: The aggregation period from which to select the previous closing price. When you adjust the chart aggregation period equal to or greater than yesterPeriod then the yesterClose will automatically hide.  
input yesterPeriod = aggregationPeriod.DAY;
#hint sideline: Where to show the yesterClose. If yes then yesterClose only displays in the right extension area of the chart otherwise yesterClose displays in the regular chart area.
input sideline = no;

#plot prox = proximity;
plot yc =
  if !sideline && IsNaN(close) then
    double.NaN
  else if sideline && !isnaN(close) then
    double.NaN
  else
    close(period = yesterPeriod)[1] ;

def agHide =  if getAggregationPeriod() >= yesterPeriod then
   yes
 else
   no
;

def hidePc =   if agHide then
    yes
  else
    no
;
yc.setHiding( hidePc );
yc.SetStyle(curve.POINTS);
yc.setDefaultColor(color.RED);

#

Thursday, August 18, 2011

sdi_atr - Average True Range In Chart Label

ATR (Average True Range) is a calculation that shows how much you might expect the given equity to move during the course of the next chart bar. Most chart packages will display ATR as a subgraph study. Now, to my mind, that's a lot of screen real estate to invest in a calculation for which I am mostly interested in the current value. In other words, I have little need to do technical analysis on ATR. So my ATR simply plots as a chart label in the upper left hand corner. Sometimes it is interesting to know if ATR is rising, flat or falling so I color code the label green, orange or red, respectively. Here is an image:


SPY with ATR in a label on upper left
 ##########################################################
# sdi_atr: Display Average True Range as a label
#hint: Displays the AverageTrueRange as a color-coded chart label. Red=decreasing, Orange=level, Green=increasing. Version:1.0
# source:http://www.smallDogInvestor.com
# author: allen everhart
# date: 10Mar2011
# 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 UPPER ;
input length = 14 ;
def bar = avgTrueRange(high,close,low,length);
AddChartLabel(1,concat(concat(concat("ATR(",length), "):"), bar),
  if bar>bar[1] then
    color.GREEN
  else if bar== bar[1] then
    color.ORANGE
  else
    color.RED
);

#################




Sunday, August 7, 2011

sdi_ma - moving average with my own kung-fu.

One might not think that there is much innovation left in the basic moving average study but I, hereby,  challenge you to think again. In the moving average study presented below I introduce several innovations that I have not seen elsewhere:



CAT with sdi_ma


 Firstly, I introduce the idea of a linear extension. I take the change between the last two bars of the moving average study and project that as a straight line into the future. This is the dashed red line that eminates from the 30 day exponential moving average like a laser-beam from the head of a snake.

Secondly, I introduce the idea of color coding to help illuminate trending vs. flat behavior. Where the moving average is orange is where the moving average is within 5% of the moving average value 30 (configurable) bars prior. Where the moving average is red is where the moving average value has moved more than 5% away from the moving average value 30 bars prior. Because of the distortions introduced by modern charting techniques it is not always readily apparent when the squiggles in the moving average really amount to much percentage-wise. Also, if you are a follower of the InvesTools method this will help you discern when to apply the volume rule to a trading signal.

An alternative criteria is used for the color-coding switch on less volatile stocks. If the moving average has moved up or down 30 (config.) consecutive bars without moving more than 5% then the color also changes to red to indicate a quietly trending stock.

Lastly, I introduce the concept of decorating the moving average to show when the underlying price has moved significantly far away, percentage-wise, from the average. In the above chart above of CAT we see that the last two bars of the moving average are decorated with pink triangles. These are configured to display when the price is more than 10% away from the moving average. CAT has been beat up by the anticipated downgrade of US debt which has resulted in a rapid price decline and CAT might be oversold here.


Here is the code for the study:

#########################
# sdi_ma: Small Dog Investor Moving Average
#
#hint: Displays a moving average with some additional signals. In the default configuration, when the MA is trending, as defined by 30 consecutive up/down moves, OR the MA is more than 5% away from the MA 30 bars prior then the MA appears in red. When the moving average is nontrending and within 5% of the value 30 bars prior then the MA appears in orange. When price is more than 10% away from the MA, the MA line is decorated with hot pink triangles. In the right extension we show a straight line projection taken from the last two bars. Version: 1.1
#
# source: smalldoginvestor.blogspot.com
# author: allen everhart 8/4/2011
# revised: 8/21/2011 added price data series selection.
#
# 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. 


#hint price: Choose data series to average over.
input price = close;
#def price = close;
#hint type: Choose between Simple and Exponential Moving Average.
input type = { default EXP, SMP } ;
#hint length: Number of bars in moving average.
input length = 30 ;
#hint flatPct: Main criteria for color-coded trend changes.
input flatPct = 5.0 ;
#hint compLength: Number of consecutive up/down bars to use as the secondary criteria for color-coded trend changes.
input compLength = 30 ;
#hint hotPct: Criteria for the hot decoration.
input hotPct = 10.0 ;

def ma =
  if type == type.SMP then
    simpleMovingAvg(price, length)
  else
    movAvgExponential(price, length)
;
rec maUpBars =
  if ma > ma[1] then
    maUpBars[1] + 1
  else if ma == ma[1] then
    maUpBars[1]
  else
    0
;
#plot up = maUpBars ; # diagnostics
rec maDnBars =
  if ma < ma[1] then
    maDnBars [1] + 1
  else if ma == ma[1] then
    maDnBars[1]
  else
    0
;
#plot dn = maDnBars ; # diagnostics
plot steep =
 if AbsValue(100 * ((ma / ma[compLength]) - 1)) >= flatPct then
    ma
 else if maUpBars >= compLength then
    ma
 else if maDnBars >= compLength then
    ma
 else
    double.NaN
;
steep.SetDefaultColor( color.RED);
steep.SetLineWeight(2) ;
plot flat =
 if AbsValue(100 * ((ma / ma[compLength]) - 1)) < flatPct then
    ma
 else if maUpBars < compLength then
    ma
 else if maDnBars < compLength then
    ma
 else
    double.NaN
;
flat.SetDefaultColor( color.ORANGE);
flat.SetLineWeight(2) ;

#plot pct =100 * ((ma / ma[compLength]) - 1);
#plot bkma =  ma[compLength] ;
plot hot =
 if AbsValue( 100 * ((price / ma) - 1) ) > hotPct then
    ma
 else
    double.NaN
;
hot.setDefaultColor( color.MAGENTA);
hot.setPaintingStrategy(paintingStrategy.LINE_VS_TRIANGLES);
rec slope =
  if isnaN(close) and !isnaN(close[1]) then
    ma[1]-ma[2]
  else if isnaN(close) and isnaN(close[1]) then
    slope[1]
  else
    double.NaN
;
rec extrec =
  if !isnaN(slope) and isnaN(slope[1]) then
    ma[1]+slope
  else if !isnaN(slope) then
    extrec[1]+ slope
  else
    double.NaN
;
plot ext = extrec ;
ext.setDefaultColor(color.RED);
ext.setStyle( curve.SHORT_DASH);



#