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.

Scan for looking back at daily close for past few days

Hi: Would appreciate if someone can help me with a scan that finds stocks where 'Daily close for today > daily close for y days ago' or ''Daily close for x days ago > daily close for y days ago''. Thanks


Best Answers

  • markdmarkd mod
    Answer ✓
    What you have for both is logically correct, but there is one syntax error:

    you have "MAX (9, SMA(50),Daily Close)"

    s/b "max(9, sma(50, close))"

    Alternatively, you could also say, in the first instance:

    and [sma(50, close) = max(10, sma(50, close))]

    or in the second instance:

    and [close = min(5, close)]

    But your way is fine if it's clearer to you.
  • markdmarkd mod
    Answer ✓
    The way you have it compares today's OBV to today's OBV, so you are asking it to be greater than itself.

    You could say

    and [OBV = max(60, OBV)]

    That allows for two possibilities - today's OBV has crossed a previous max value from sometime in the past for the first time, or, today is a consecutive max 60 value - in other words, yesterday was also a max 60, and maybe the day before, and the day before, and so on.


  • What you have is logically correct, so it's a matter of learning the rules of syntax.

    Here's a link to the documentation:

    Here's a quick and dirty summary:

    Basic rules for the scan engine
    Every statement begins with “and” except the first one
    Every statement, except the word “and”, goes between square brackets [ ]
    NOTE: Parentheses are reserved for indicators and functions, like RSI(14) or max(10,close). If you need to group arithmetic expressions, use more square brackets, not parentheses: For instance:
    And [ [high – close]/max(10, close) > 10]
    Every statement contains an operator. Valid operator are: is, is not, >, <, =, !=, >=, <=, x, contains, not contains.” !=” means “not equal to”. “x” means “crosses above”.
    Every statement has one value preceding the operator and one value after the operator. Values can be indicators, overlays, numbers, reserved words like group, market cap, close, etc. (see drop downs on Advanced Scan page) or expressions using these values. Some examples:
    [group is SP500] // note: this would be a first scan statement because it doesn’t begin with “and”
    And [MACD Line(12,26,9] > 20]
    And [MACD Line(12,26,9) x MACD Signal(12,26,9)]
    You can also write “or” statements. “Or” statements need to be isolated with an extra set of brackets
    // begin scan
    [group is sp500]
    // get bullish and bearish crossovers
    [ MACD Signal (12,26,9) x 20]
    [ 80 x MACD Signal(12,26,9)]
    // end scan
    The double slashes "//" tell the scan engine to ignore everything on that same line that comes after the "//". So you can use that space to explain to yourself (for later) what you thought you were trying to do in the scan code.
    NOTES on scan logic with “and” and “or”:
    If a scan has ONLY “and” statements, all the “and” statements must be true for the scan to return a symbol.
    If a scan has ONLY “or” statements, then only one condition must be true to return a symbol.
    If a scan has a mix of “and” and “or” statements, the results depend on whether you isolated the “or” statement with extra brackets as shown above:
    If you don’t use the brackets, then only one condition in the entire scan has to be true to return a symbol.
    If you do use brackets, then every “and” condition must be true AND at least one “or” condition must be true to return a symbol.
    Start simple.

    It's actually harder to figure out WHAT to code than HOW to code.

    For instance, how would you code for a rising SMA 200?

    You need to think about what would be true if the SMA 200 really is rising. First thought - the value today would be different from the value yesterday, or maybe 10 days ago or 100 days ago.
    And it would be greater because it is rising. So the scan would compare today's SMA 200 to the past SMA 200 and it would be greater, so:

    and [sma(200,close) > 10 days ago sma(200, close)]
  • Hi Mark: Thank you for the very detailed explanation.

    Going by what you said, if I wanted to scan for a condition where todays SMA50 close is greater than than the SMA(50) close of ANY of the past 10 days, would that be [SMA(50,Daily Close) > Yesterdays MAX (9, SMA(50),Daily Close)]

    Similarly, if I wanted to scan if todays daily close is less than the daily close within any the past 5 days, would that be [daily close < yesterday's Min(4, Daily close)]

  • Thank you again, Mark!
  • Hi Mark: In a similar way to the above, I am trying to write a quick scan to find stocks where the On Balance Volume is greater than the OBV within the past 3 months. Trying this [OBV > DAILY MAX (60, OBV)] yields me '0' scan results.

    Any suggestions would be appreciated?

  • Thank you very much for the solution, Mark!
Sign In or Register to comment.