New Members: Be sure to confirm your email address by clicking on the link that was sent to your email inbox. You will not be able to post messages until you click that link.

Using SPY as a filter in scans.

I had an idea that I would use SPY as a filter to easily decide if I should buy stocks. Yes, I know I can simply look at SPY on a daily chart and look at its ema in an overlay. But for the sake of learning if I can include it in a scan, I'm asking the question.

So, for example, if I make a trading rule that I will not buy stocks when SPY is below its daily ema(12), then the scan results should give me a return of 0 on days when that conditional filter is met. I tried the syntax " AND [ SPY > EMA(12) ] ", but that doesn't work, as there is no relationship in that syntax that connects SPY to its own ema. I'm currently stumped as to how to proceed.

All feedback will be appreciated.

Best Answer

  • markdmarkd mod
    Answer ✓
    I have no connection to Stockcharts whatsoever, except that, like you, I am a subscriber. I've never been there and don't know anyone associated with it. I do have a background in programming. My knowledge of the the scan engine is solely as a user for several years, my own background in programming and from the documentation and email exchanges with support on various issues.

    Regarding your questions:

    1. No, your code is not correct in the environment it is running in. You and I can make sense of it, we know what you want it to do, but the scan engine does not. As I said, the syntax checker is not robust enough to deal with the particular non-conformities of your code. For instance, "close" is not a function in that environment, therefore it cannot take arguments like "symbol is "SPY". But the format of close(...) does not violate any rules, so the syntax checker lets it run. Probably it keeps looking for the close function until it times out.

    2. I doubt very much it would be economical to develop the scan engine into a more complete language and still charge a very low fee (which Stockcharts does, considering its usefulness). The support costs would be enormous. If you want that kind of flexibility you have to pay much more for a package like Tradestation. As it is, the scan engine is incredibly flexible if you understand its purpose (find symbols that conform to your data state requirements) and can tune your thinking to state your requirements in its terms.

Answers

  • If you were scanning for SPY alone, you would write

    [symbol is "SPY"]

    and [close < ema(12,close) ]

    If it's true, the results will return SPY, if not, no results.

    But, the scan language does not support "if...then" statements, so I don't think you can use the code above as a switch to determine whether to run the rest of the code.

    Also, you couldn't write this and get what you want:

    [symbol is "SPY"]

    and [close < ema(12,close) ]

    and [favorites list is ??]

    and [MACD Hist(12,26,9) x 0]

    SPY would have to meet both conditions and symbols in the favorites list that weren't "SPY" would not be selected because "symbol is SPY" is required. A symbol must meet all conditions to be a hit. You can't isolate them, except through "or" statements. But in your case, I don't think there is an "or" combination that will work.
  • Hi markd,

    Thanks for trying to help!

    I tried " and [ CLOSE (symbol is SPY) > [ EMA (12), ( Symbol is SPY) ] ] "
    This line tells me that SPY is greater than its own ema(12).

    The syntax is correct, according to the "Check Syntax" function. However, the Scan Results came back with "The Scan Engine is taking too long to return your results. Please try again later."

    Any thoughts?

  • Ok, I realized that I needed to put single quotes like this: 'SPY'

    So the syntax now looks this way: and [ CLOSE (symbol is 'SPY') > [ EMA (12), ( Symbol is 'SPY') ] ]
    But I'm still getting "The Scan Engine is taking too long to return your results. Please try again later."

    I thought I had it that last time...bummed!
  • Once again, I realized further changes to the syntax were needed. So now the line code looks like this: and [ CLOSE (Symbol is 'SPY') > Daily EMA (144, Symbol is 'SPY' ) ]
    This syntax is also correct...

    ...but I'm still getting "The Scan Engine is taking too long to return your results. Please try again later."

    Ideas, thoughts?
  • The syntax checker is not exhaustive. There has to be a trade off between efficiency and error checking, so it mainly finds the most and likely common errors. More unusual or obscure errors can get by.

    The conventional way to set up a scan is to specify the groups of symbols you want to look at at the top of the scan and then the conditions you want those symbols to satisfy in the body of the scan.

    You have broad discretion in how to specify the groups you want to look at. You can specify a single symbol, as I showed you above, or you can specify a group like

    [group is sp500]

    or you can specify some kind of characteristic(s)

    [exchange is NASD]
    and [market cap > 100]
    and [volume > 10000]

    and so on.

    But you couldn't write

    and [close (exchange is NASD and market cap > 100 and volume > 10000) > [ema(12, (exchange is NASD and market cap > 100) and volume > 10000] ]

    The scan engine isn't designed to interpret that format. It may not violate any syntax rules but that doesn't always mean it will work. When it "takes too long", that's a good sign the interpreter is having a problem with the code as written.

    Your method of specifying the close of a particular symbol inside a condition like close > ema(12, close) is not legal. You have to do it the way I showed in the previous post - the symbol or group of symbols is one condition, the indicator states are a separate condition. If all statements are true - that is, the indicator condition is true for one or more symbols in the specified symbol or group of symbols, then you get results - the symbol or a list of symbols belonging to the group which satisfy the indicator state specified.



  • edited February 2018
    Hi markd,

    I really appreciate your quick reply and thoughts on my query.

    So this is what I'm thinking now:

    1. If the scan engine is saying that [ CLOSE (Symbol is 'SPY') > Daily EMA (144, Symbol is 'SPY' ) ]
    is correct, would you also agree to that?

    2. Even if the scan engine isn't able to make the interpretation, my next thought would be to wonder if the software developers are willing to give the scan engine the robustness to allow for "...a switch to determine whether to run the rest of the code." I take it--from your expertly demonstrated knowledge--that you are one of the software developers, and would be able to authoritatively answer my wondering thought.


Sign In or Register to comment.