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.

Code for sloping SMA

Would appreciate suggested code that finds "downward sloping 50 day sma for x days". Thanks

Best Answers

  • markdmarkd mod
    Answer ✓
    You can determine the general direction of an MA by comparing today's value to the sma value some number of days ago. So, for instance, if you want symbols with a rising sma 20, you might write

    and [sma(50, close) > 10 days ago sma(50, close)]

    That could get you a variety of results, from an sma that recently turned up to an sma that has been rising for months, or anything in between.

    But you can't be sure (without a more complicated scan) that the sma was continuously rising during the whole look back period (10 days in the example above). The longer the look back period, the more possible it is that the sma is NOT rising now. So if you write

    and [sma(50,close) > 42 days ago sma(50,close)]

    you could get results where the sma peaked maybe 15 days ago and has turned down, but not yet enough to be less than 42 days ago.

    If you want a continuously rising MA, you would have test several shorter overlapping segments - something like today's sma - 10 days ago, 5 days ago - 15 days ago, 10 days ago - 20 days ago, etc.

  • lmkwinlmkwin ✭✭
    Answer ✓
    Another option that was tossed out on a recent Wyckoff installment could be something like this.

    and [Slope(X,sma(50)) < 0

    Their guest scan expert used the following to create a pool of possible Wyckoff short candidates.

    and [Slope(63,sma(50)) < 0]
    and [PctDiff(close,sma(50)) < 0];list=PLyNJu-3PikrS22Ti9lUWcq7Jj2RyDctZC


  • Markd and Imkwin,
    Thanks much for your help with the code for a sloping sma. Before your suggestions, I tried to use the slope command but couldn't ever get the parameters right. I need it to run a bearish PSAR routine that uses a downward sloping 50 dsma. With your help, I was able to run a scan and got 4 issues. I'll see if the scan is useful in a few days. Bill
  • @blaniboy

    a couple of comments - you really don't need to wait to see if the scan is useful. You can run it today for dates in the past and look at the results. Also, you need to look at many, many hits (not just four) over a wide period of time in different markets to get a feel for when it works and when it doesn't. Many set ups will work in one market but not another. Markets trending up, trending down or moving sideways produce different degrees of success (since most stocks move with the market as a whole).
  • lmkwinlmkwin ✭✭
    edited September 2021
    I sometimes set up my preferred scans to allow me to capture things that happened recently or in the recent past. I use PnF for the most part but you can apply the "logic" anywhere.

    Dave Landry shared a scan thought a year or so ago. A stock makes a new high and then pulls back for 2 days.

    and [[3 days ago high = 3 days ago max(251,high)] and [Streak Down(high) = 2]]

    So I can run this today and get today's results.

    Or, I can nest in similar criteria looking back further than today by adding some additional lines.

    and [ [[3 days ago high = 3 days ago max(251,high)] and [Streak Down(high) = 2]]
    [[4 days ago high = 4 days ago max(251,high)] and [1 days ago Streak Down(high) = 2]]
    [[5 days ago high = 5 days ago max(251,high)] and [2 days ago Streak Down(high) = 2]] ]

    Of course this modified his idea to a certain extent but I could set it up as a scan or alert and capture the symbols that met the criteria for today and yesterday and the day before.

    I could set up a scan to run today and capture all the symbols that met a criteria in the last 5 days, or 5 weeks or whatever.

    I find this idea useful when looking at buying and selling climaxes. It's not important to know what is going on with a BC or SC the next day. And probably not the next week. I tend to start analysis of them a few weeks out from their occurrence to see there the Wyckoff activity analysis would start to take shape. So my scan would look for BC or SC that occurred a few weeks ago to create a list.

    You can also run the nested scan using back dates to cast the net over those that met the criteria then as well.

  • I would add to @lmkwin 's thoughts, that if you want to catch newly breaking out stocks, versus stocks already on a run after a breakout, you can test for a flat upper price channel over some period -

    and [high > 1 day ago max(251, high) ]

    and [ 1 day ago max(251, high) = 50 days ago max(251, high)]

    You could play with the "50 days ago" to see what happens.

    Note: 1 day ago max(251, high is the same as Upper Price Chan(251) - price channels lag by one day.

    Also note that new annual highs are sometime tops. Verify that other parts of the chart show strength, and the longer term weekly and monthly charts show some trendiness.
  • Thanks to both again-your help re being able to scan for past dates is very useful. I used code as you suggested and got several hits-helpful in evaluating the scan but it's looking like the scan isn't that good. That's OK-I learned a lot from your suggestions.
  • Remember to also think about what the market conditions are/were before dismissing a scan. Especially when using back dates.

    You may have a good scan for certain market conditions.

    Not saying that your scan was good or not. I don't know. Just adding some color to be aware of when evaluating results.
  • Thanks. I understand. Won't give up on the scan yet.
Sign In or Register to comment.