Scanning Technically

« July 2011 | December 2011 »

Scan coding sample blocks

Hi all from the 3rd floor of the Red Lion in Seatle, ChartCon has been wonderful and educational.  I hope all who are in attendance are enjoying it as much as I am, many many thanks to Chip and the whole team for putting on this exceptional event.  I'm sure the next one will be even better, but I'm not sure how you're going to do that Chip.

This mornings sessions covered the scanning engine and there were many questions on how to set up scans and whether there were any libraries of standard scan coding.  There are many predefined scans on the website which you can look at and learn from, I recommend you study them closely.

However in order to help get you started here are a few Advanced scan coding sections, these are provided just to get you thinking of different things you can do and how the general format of the coding is set up.

These sections of coding can be modified and grouped into an advanced scan as you see fit, these are only examples they are not necessarily scans which would be applicable to your investment / trading style.  Just cut and paste into the scan engine and start creating your own library.  (all info provided is subject to change without notice, use at your own risk)

Happy scanning
Gord Greer


Advanced Scan Code Sections,
Just a Few Examples
( // indicates comment lines)


// Sample scan layout, generic
[type = stock]
and [1st criteria]
and [2nd criteria]
and [ [3rd criteria] or [4th criteria] ]


[country = us]
[country = canada]
[country = uk]


// Type
[type = stock]
[type = index]
[type = fund]

// Exchange
and [exchange = NASDAQ]
and [exchange = NYSE]
and [exchange = AMEX]
and [exchange = PINK]
and [exchange = TSE]
and [exchange = CDNX]
and [exchange = LSE]


and [group is SP500]
// or for S&P 1500, large, mid and small cap
and [ [group is SP500] or [group is SP400] or [group is SP600] ]


// Fav lists, numbers will be different for each user, note extra brackets grouping the "OR" clauses together
//
[[favorites list = 13]
or [favorites list = 24]
or [favorites list = 63]
or [favorites list = 64]
or [favorites list = 67]
or [favorites list = 68]
or [favorites list = 70]
or [favorites list = 71]]


// Liquidity  SMA of average dollars traded per day > $500,000
[type = stock]
and [SMA(50,close) * SMA(50,volume) > 500000]


// Price bracket, used to restrict the output when developing scans, also min close used to eliminate stocks which are not marginable and not likely to have options, or stocks which have a price which would require a board lot trade to be above your position size. (ie if your max position size is say $10K then 100 shares of a $100 stock would be at your limit of $10K,  100 shares of Google would be $60K,  this is important if you broker requires you to trade in board lots when inputting conditional orders)
//


and [Close >=10] and [Close <=20]


// Uptrend: for 75 days the min RSI(14) value >= 40
and [Min(75,RSI(14)) >= 40.0]


// Downtrend:  for 75 days max RSI(14) value <= 60
and [Max(75,RSI(14)) <= 60.0]


// Uptrend: MACD positive for 130 days (~ 6 months)
and [Min (130, MACD Line (12,26,9)) > 0.0]


// MA Price convergence long term,
// SMA-20, 100 & 200 all within +/-1% of the SMA-50
and [daily sma(20,daily close) >= daily sma(50,daily close) * 0.99]
and [daily sma(20,daily close) <= daily sma(50,daily close) * 1.01]
and [daily sma(100,daily close) >= daily sma(50,daily close) * 0.99]
and [daily sma(100,daily close) <= daily sma(50,daily close) * 1.01]
and [daily sma(200,daily close) >= daily sma(50,daily close) * 0.99]
and [daily sma(200,daily close) <= daily sma(50,daily close) * 1.01]


// MA Price convergence short term,
// SMA-3, 10 & 40 all within +/-1% of the SMA-20
and [daily sma(3,daily close) >= daily sma(20,daily close) * 0.99]
and [daily sma(3,daily close) <= daily sma(20,daily close) * 1.01]
and [daily sma(10,daily close) >= daily sma(20,daily close) * 0.99]
and [daily sma(10,daily close) <= daily sma(20,daily close) * 1.01]
and [daily sma(40,daily close) >= daily sma(20,daily close) * 0.99]
and [daily sma(40,daily close) <= daily sma(20,daily close) * 1.01]


// Price convergence consolidation mid term,
// Max / Min daily close for last 20 days,
// contained within +/-2% of the current SMA-20
and [max (20, daily close) <= daily sma(20,daily close) * 1.02]
and [min  (20, daily close) >= daily sma(20,daily close) * 0.98]


// Price convergence consolidation short term,
// Max high / Min low, for last 10 days,
// contained within +/-2% of the current SMA-10
and [max (10, daily high) <= daily sma(10,daily close) * 1.02]
and [min  (10, daily low) >= daily sma(10,daily close) * 0.98]


// Price channel convergence,
// Max / Min daily close for last 20 days,
// contained within +/-3% of the current close
and [min(20, daily close) > close * 0.97]
and [max(20, daily close) < close * 1.03]


// Consolidation then breakout,
// 30 days prior to yesterday Max/Min close within +/-3% of yesterdays SMA-20,
// then breakout, close today 5% above yesterdays SMA-20
and [1 day ago max (30,  close ) <= 1 day ago sma(20, close) * 1.03]
and [1 day ago min (30,  close ) >= 1 day ago sma(20, close) * .97]
and [close > 1 day ago sma(20, close) *1.05]

//Note last line could be modified to look for a break in either direction,
and [[close > 1 day ago sma(20, close) *1.05] or [close < 1 day ago sma(20, close) *0.95]]


// Fav lists scan for unusual activity, select Fav lists grouped with "OR" clause; watch, hold and sector lists. 
//Then define several criteria for today, yesterday, day before etc, based on close, high, low, volume etc etc,
// and then group them with "OR" so any individual criteria will select a stock.
//
[ [favorites list = 1]
or [favorites list = 3]
or [favorites list = 5]
or [favorites list = 6]
or [favorites list = 10]
or [favorites list = 13]
or [favorites list = 61]
or [favorites list = 70] ]
and [[0 days ago volume > 1 day ago sma (60, volume)*1.25]
or [1 days ago volume > 2 days ago sma(60, volume)*1.25]
or [2 days ago volume > 3 days ago sma(60, volume)*1.25]
or [0 days ago close > 1 day ago close * 1.05]
or [1 days ago close > 2 day ago close * 1.05]
or [0 days ago high > 1 days ago close * 1.05]
or [1 days ago high > 2 days ago close * 1.05]]

end

Gord Greer

Scanning for an overlay of an indicator

Question:

Is it possible to use an EMA of an indicator in a scan? For example, If I wanted to know when RSI(5) is above a 3-period EMA of RSI(5)?
I know you can use sma(60,volume), for example, but volume doesn't have a parameter. When I try to introduce an indicator parameter to similar phrasing, I get syntax errors. It doesn't "get" RSI(5,5) or EMA(5,RSI(5)).
Any help would be appreciated.

 

Answer:

Yes it is possible and its one of the scan coding items that many people have trouble with.  I assume the following chart explains the type of thing you are looking for.  Note for this example I've just choosen a stock and adjusted the EMA and RSI time frames to get a crossover in the last few days in order to check my scan coding, you can change the parameters to your own needs.


Scan ema of rsi

Now here is the coding I set up to look for this cross.

[type = stock] and [country = us]
and [Close >=48] and [Close <=49]
and [RSI(7) X EMA(14,RSI(7))]

Note in order to check the scan I know this cross happened 3 days ago for GG (Goldcorp), so when I run the scan with a start date of 3 days I should get GG which will verify that the scan is working. (note I narrowed the price range specifically to home in on GG)

Scan gg

Note we did pick up GG, you might notice that the price range for GG on the above list is outside the price range in the scan, however the list above is todays price range, the scan was for 3 days ago.

I hope this helps, happy scanning.

 

 

Building a Scan, Q&A

Question

"...I'm hoping someone can help me with some criteria for setting up a scan.... I came up with a description in words, but other than item 1 and 2, not sure how to input things into stockcharts.com.

1. Stock must trade over 500,000 shares average for last 20 days
2. Greater than $5
3. Stock must have traded above the 200 DEMA for 20 days or more
4. Stock must be in decline
5. Stock must trade at less than 105% of the 200 DEMA

I define the pivot as the following, using candlesticks:
A down bar, followed by a bar that opens lower and closes higher, and the final bar opens down or equal to the previous bar’s close, and closes higher on the day.

I'm probably asking a lot for the pivot part and I'm cool with eye balling that part if necessary...."

Answer

Great question,
I've been away for a few weeks unplugged, (ie without internet).  Back now,  currently at the Chartcon conference out in Seattle.  The short answer is yes you can scan for all of what you are looking for.

Let’s break it up into sections and review each in a little more detail.

- 1. Stock must trade over 500,000 shares average for last 20 days

and [daily sma(20, daily volume) > 500000]

- 2. Greater than $5

and [Close > 5]

This requires today’s close to be greater than $5, but we could also expand this to require that the average close for the last 20 days be greater than $5.

and [daily sma(20, daily close) > 5]

We could also require the min close for the last 20 days to be greater than $5.

and [Min(20, daily close) > 5]

Or we could require that the min daily low be greater than $5.


and [Min(20, daily low) > 5]


- 3. Stock must have traded above the 200 DEMA for 20 days or more

This one is a little tricky, lets look first at some of the things which won't work.

and [close > ema(20, close)]
This requires only today’s close to be above today’s value of the ema20.

and [min(20,close) > ema(20,close)]
This requires the min close over the last 20 days to be above today’s value of the ema20.

and [min(20,close) > max(20, ema(20,close))]
This requires the min close over the last 20 days to be above the max value over the last 20 days of the ema20, however not necessarily on the same day.

In summary the above criteria are close to what we are looking for but none of them require the close on every individual day to be above the ema20 for that same individual day.

One way to accurately check for a few days is to just write the criteria for each day, as follows.

and [close > ema(20,close)]
and [1 day ago close > 1 day ago ema(20, close)]
and [2 day ago close > 2 day ago ema(20, close)]
and [3 day ago close > 3 day ago ema(20, close)]
etc,
etc,
etc,
and [20 day ago close > 20 day ago ema(20, close)]

While the above method is OK for a few days it can become lengthy for longer time frames and luckily there is another method.  We need to start by becoming familiar with the formulas for all the indicators we use on our daily charts.  In this case the MACD indicator is of interest, Moving Average Convergence Divergence.  The MACD line which is plotted is the raw difference between two exp moving averages, the typical default values are 12, 26.  Normally there is also a signal line plotted which is typically a 9 ema of the MACD line.

In our case we are looking at the close being above the 200ema, the trick to remember is that a 1 day ema or sma is actually the closing price for each day.  Thus if we plot the Macd 1,200 we are actually plotting the difference between the daily close and the daily value of the ema200 for each individual day.  The value is positive when the close is above the ema200 and negative when the close is below the ema200.  Thus we only need to scan for the min  value to be positive, (greater than 0), for the last 20 days, as follows.

and [Min (20, MACD Line (1,200)) > 0.0]


-4. Stock must be in decline

Lots of ways to this so lets just say in the medium term the 20SMA is below the 50SMA.

and [sma(20, close) < sma(50, close)]



- 5. Stock must trade at less than 105% of the 200 DEMA
Here we simply have to require the close to be less than the ema200 times 1.05

and [Close < ema(200, close)*1.05]

- 6. "I define the pivot as the following, using candlesticks:
A down bar, followed by a bar that opens lower and closes higher, and the final bar opens down or equal to the previous bar’s close, and closes higher on the day".

In this case we just need to look at each day and define the criteria for the open and close each day, note as far as the scan engine is concerned today is day zero "0" and counts back in time.  See the following chart which identifies the criteria for each day.

Pivot


and [2 days ago close < 2 days ago open]
and [1 day ago open < 2 days ago close]
and [1 day ago close > 1 day ago open]
and [open <= 1 day ago close]
and [close > 1 day ago close]

 

hope this gives you some new scanning ideas

good luck

Other StockCharts Blogs