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.

Consecutive true days - SMA, EMA, etc -

How to scan for consecutive days of a true statement.

1) 20 days of the close being over the 30 day EMA
2) 30 days where the 20 SMA > 50 SMA > 200 SMA

I have been searching for multiple hours and cant seem to find this.


  • markdmarkd mod
    edited August 2020
    I don't know how to do it for SMAs, but for EMAs you can try this (I'm hoping I remember this right):

    [group is sp500]
    and [min(20, MACD Hist(1,30,0)) > 0]

    For MACD Hist, the first parameter is the shorter EMA and the second parameter is longer EMA. The "1" for the first parameter above represents the close (EMA of length 1 is the same as the close). The third parameter is the Signal line - which is an EMA of the difference between the first two EMAs. But we don't want that - we just want the difference between the first two EMAs - so the third parameter is "0".

    We want to min(... ) > 0 because a value less than zero would indicate the close went below the EMA, and that would interrupt the count of consecutive closes above the EMA.

    For the second part, if EMAs are OK, this seems to work:

    [group is sp500]
    and [min(30, MACD Hist(20,50,0)) > 0]
    and [min(30, MACD Hist(50,200,0) > 0]

    Same explanation applies, just the parameters are different.

    Make sure you check the results with a chart style showing the EMAs on the chart and MACD Hist(s) below with the corresponding parameters.

    P.S. - I learned this trick years ago on this site from another user - it's an "off label" use for an indicator that you wouldn't come up with on your own without a lot of experience - and creativity.
  • Great tip @markd .

    "Old school" would have been a lengthy nested statement like

    close > 30 day ema
    and 1 day ago close > 1 day ago 30 day ema
    and 2 day ago close > 2 day ago 30 day ema
    and 3 day ago close > 3 day ago 30 day ema
    and 4 day ago close > 4 day ago 30 day ema
    and 5 day ago close > 5 day ago 30 day ema

  • Thank you both.

    @markd For the line "and [min(30, MACD Hist(50,200,0) > 0]" I received the error "Could not parse "(502000" located in the clause "MIN(30, MACD HIST(50,200,0)" (an integer was expected)"
  • lmkwinlmkwin ✭✭
    edited August 2020
    You need to add a second ) before the >

    [group is sp500]
    and [min(30, MACD Hist(20,50,0)) > 0]
    and [min(30, MACD Hist(50,200,0)) > 0]
  • Think of the ( and the [ as being part of a team. There must be a corresponding ) and ] for every ( or [.

    Parsing is usually a ( ) issue but also can just be that the system can't do what you are asking.

    [ ] issues usually result with an error message about mis-matched brackets.
  • lmkwin is right on the money - you should always have an even number of parentheses (and brackets).

    I should have added a warning about watching parentheses when using nested functions.
  • Great, I didn't catch the missing item. Thanks all!
Sign In or Register to comment.