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.

Doing simple math in a scan?

Is there a way to do basic math in a scan?

Lets say I want to eliminate stocks with big wicks and tails.
This is how I think I would do it in pseudo-code

Wick = High - Close
Tail = Close - Low
Body = Close – Open
Body = (abs) Body [Absolute Value]

Good-Stock = (($Wick + $Tail) <= (Body x 0.20))

Can this, or anything like it, be done in a scan?
Thank you.

Comments

  • I guess that should be ($Body x 0.20)
  • The scan engine is a purpose written language, limited to the needs and abilities of the subscriber base looking for specific stock and indicator characteristics. It strives to be close to a "natural" language, as much as possible. So, it does not support custom defined variables like "Wick" and "Tail" in your example. It does have just a few functions, but they are very useful. That said, the language is remarkably flexible. It can do most things you can think of, BUT if you have a programming background, you will have to step back and re-think how you formulate the conditions you are looking for.

    If you take a little time with the documentation and/or tutorials, you will pick it up quickly. There really aren't many rules to master.

    Intro is here, with links to topics:

    https://stockcharts.com/docs/doku.php?id=scans

    Syntax is here (good place to start if you already program)

    https://stockcharts.com/docs/doku.php?id=scans:reference

    In your example, if you want the body to be more than 20 % of the range, you can write

    and [absval(open - close) > range * .8]

    You could also write

    and [absval(open - close) > [high - low] * .8]
  • markdmarkd mod
    edited October 2017
    Here's an old answer to a similar question about how to use the scan engine - sort of the Cliff Notes version - but don't skip reading the documentation, too:

    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 a comparison operator. Valid operators 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
    And
    [
    [ MACD Signal (12,26,9) x 20]
    Or
    [ 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 for than HOW to actually write the 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)]
  • Thank you. That will be very helpful. I actually don't have that much of a programing background beyond a little bash and perl. That just seemed the easiest way to convey what I thought I needed to do. Cheers
Sign In or Register to comment.