StockFetcher Forums · General Discussion · How to count appearances in a filter with ranking? xarlor?<< >>Post Follow-up
fotchstecker
304 posts
msg #156075
Ignore fotchstecker
modified
3/9/2021 1:32:01 PM

xarlor helped me with a filter with counts of the symbols appearance on that filter for the past 40 days:
https://www.stockfetcher.com/sfforums/?mid=155878

I'm trying to adapt the same concept to the filter below, but I'm struggling. xarlor or anyone else who can give a hand, please let me know.

Where I'm trying to go with this: This is a rotation filter, so the "top" symbols tend to stick around a while. However, it's valuable to know two count-related things:
1. When a symbol has suddenly entered, say, the top 10 or top 5 and has a comparatively low count of appearances. This often means that a significant change of momentum is occurring.
2. When a symbol has a high absolute count in general, but doesn't appear near the top, it often means a steady longer-term change.

One problem with the filter below is that it needs a selection criteria, not just a column-sort ranking like it currently uses because you need to count some kind of condition, not just sort the data. not sure of how to solve this, since the stocks are just bought as they rotate-in to the top 5.

Maybe the selection criteria is that count > 0 over the last 40 days? That way it includes everything that has appeared (but still needs a count solution).

Any help would be greatly appreciated.

Thank you.


Fetcher[
chart-time is 1 year
set{20Return, ROC(20,1)}
set{ROCrank1, 20Return * .5}
set{60Crank, ROC(60,1)}
set{ROCrank2, 60Crank * .5}
set{ROCrotate, ROCrank1 + ROCrank2}

set{roc3, ROCrotate 3 months ago }
set{roc1, ROCrotate 1 months ago }
set{rocmomo, roc1 - roc3}
ADD column rocmomo

ADD COLUMN SEPARATOR
ADD column ROCrotate 3 months ago
ADD column ROCrotate 1 month ago
ADD column ROCrotate 1 week ago
ADD column ROCrotate

sort by column 10 descending


ADD COLUMN SEPARATOR
and draw MA(100)
/* and close > MA(100) */


and add column corr(SPY,30,Close)
and add column corr(SPY,60,Close)
and add column corr(SPY,90,Close)
and add column corr(SPY,150,Close)

add column Historical Volatility(30,1)
add column Historical Volatility(100,1)

add column relative strength(spy,30) > -1

set{ma100a,count(close > ma(100),1)}
ADD COLUMN ma100a {above ma100}

set{ma20a,count(close > ma(20),1)}
ADD COLUMN ma20a {above ma20}

set{20x, count (close crossing above ma(20) within the last week,1)}
add column 20x

and add column industry
and add column sector

draw ma(20)
]



SAFeTRADE
645 posts
msg #156078
Ignore SAFeTRADE
3/9/2021 10:39:15 PM

8/16/2011 3:06:37 PM

Hi, adapt this filter to your needs. This is from one of Kevin's masterpieces. May give you ideas
of how to implement what you are trying to do.

The issue is related to how custom variables and counts handle weekly data. Once you create a custom variable, the variable is established as a "daily" variable.

While not an ideal solution, the example below may do what you are looking for.

Fetcher[
set{var1,WEEKLY ROC(14,1)}
set{var1a, IND(SPY, var1)}
set{var1b, IND(IWM, var1)}
set{var1c, IND(EFA, var1)}
set{var1d, IND(BND, var1)}

SET{RANK1A, COUNT(var1 is above var1a,1)}
SET{RANK1B, COUNT(var1 is above var1b,1)}
SET{RANK1C, COUNT(var1 is above var1c,1)}
SET{RANK1D, COUNT(var1 is above var1d,1)}
SET{RANK2, RANK1A + RANK1B}
SET{RANK3, RANK2 + RANK1C}
SET{RANK4, RANK3 + RANK1D}
SET{RANK, 4 - RANK4}
ADD COLUMN RANK1A {ABOVE SPY}
ADD COLUMN RANK1B {ABOVE IWM}
ADD COLUMN RANK1C {ABOVE EFA}
ADD COLUMN RANK1D {ABOVE BND}
ADD COLUMN RANK

SYMLIST(IWM,EFA,SPY,BND)
ADD COLUMN SEPARATOR
ADD COLUMN WEEKLY ROC(14,1) {14 week performance}
SORT ON COLUMN 11 DESCENDING
chart-display is weekly
CHART-TIME IS 26 WEEKS
]



SAFeTRADE
645 posts
msg #156079
Ignore SAFeTRADE
3/9/2021 10:54:30 PM

Try this one if you have more than 4 markets. This is another one of Kevin's work at Stockfetcher.

Fetcher[
/* Basic Filters */
symlist(spxl,spxs,udow,sdow,sqqq,tqqq,tlt,gld)
chart-display is weekly
chart-time is 13 weeks
sort on column 7 ascending

/* 14 week performance as rank variable */
set{perf, weekly roc(14, 1)}
set{perf2, perf/100}
set{rfr, ind(^irx, close)}
set{riskfreereturn, rfr/100}
set{perf14, perf - riskfreereturn}
set{std14, cstddev(weekly close, 14)}
set{vol14, std14 / weekly ma(14)}

/* calculation of sharpe ratio - annualized */
set{sharpe14a, perf2 / vol14}
set{sharpe14, sharpe14a * 0.5189}
add column sharpe14 {sharpe ratio}

set{var1, weekly roc(14,1)}
set{var1a, ind(spxl, var1)}
set{var1b, ind(spxs, var1)}
set{var1c, ind(tqqq, var1)}
set{var1d, ind(tlt, var1)}
set{var1e, ind(gld, var1)}
set{var1f, ind(sqqq, var1)}
set{var1g, ind(udow, var1)}
set{var1h, ind(sdow, var1)}

set{rank1a, count(var1 is above var1a,1)}
set{rank1b, count(var1 is above var1b,1)}
set{rank1c, count(var1 is above var1c,1)}
set{rank1d, count(var1 is above var1d,1)}
set{rank1e, count(var1 is above var1e,1)}
set{rank1f, count(var1 is above var1f,1)}
set{rank1g, count(var1 is above var1g,1)}
set{rank1h, count(var1 is above var1h,1)}

set{rank1ee, rank1a + rank1b}
set{rank1ff, rank1c + rank1d}
set{rank1gg, rank1e + rank1f}
set{rank1hh, rank1g + rank1h}
set{rank1ii, rank1ee + rank1ff}
set{rank1jj, rank1gg + rank1hh}
set{rank1kk, rank1ii + rank1jj}
set{rank, 8 - rank1kk}
add column separator
add column rank {current rank}
add column separator

add column weekly roc(14,1) {14 week performance}
add column corr(^spx,70, close) {correlation to s&p 500}
]



Cheese
1,374 posts
msg #156092
Ignore Cheese
3/10/2021 2:18:50 PM

fotch,

TRO's approach to "quantify" uptrends and downtrends strings of 1 and 0 may be of use to you.

https://www.stockfetcher.com/forums/Filter-Exchange/bollinger-bands/26146/210

https://stockfetcher.com/forums/Filter-Exchange/How-To-Search-for-MA-20-MA-50-MA-100-MA-200-Cross-Over/35908/0

Best wishes with your invention.

fotchstecker
304 posts
msg #156100
Ignore fotchstecker
3/10/2021 9:26:20 PM

Hi, Safetrade and Cheese -- thank you both for your suggestions. I'm going to give a try to both methods tomorrow to see what I come up with. Thanks again!


fotchstecker
304 posts
msg #156130
Ignore fotchstecker
modified
3/12/2021 7:13:23 PM

I want to share the solution I came up with for counting hits on a filter where there aren't conditions. It's based on what xarlor shared. And my version very simple so it probably could be enhanced.

This is a rotation filter, so it is important for me to know how long a ticker has been appearing, or if it is newly among the highest-ranked symbols.

The way I solved it is to simply count the ranking/sorting column if it has value greater than 1. You can change the target value to identify tickers that are popping quickly, or to see those that have been around a lot. The base code is commented in the "hit machine" comment line.

I'll leave the filter as I use it. I tend to copy/paste from the commented-out criteria rather than create separate filters.


Fetcher[
/*
apply to watchlist(vanguardETFs)
apply to watchlist(ETF20215k)
apply to watchlist(indexes2021)
market is ETF
market is not OTCBB
Average Volume(30) > 1234567
apply to symlist(bnd, dbc, veu, vnq, vti)
close crossing above ma(20) within the last week


*/
apply to watchlist( )


/*
https://www.stockfetcher.com/forums/General-Discussion/simple-rotation-filter-dos-the-code-match-the-rules/151129
simple rotation filter -- dos the code match the rules?
fotchstecker 3/8/2020 1:15:53 PM
*/
/*
System -- Buy the Rank Score leader
1. Overall Rank Score = ( 20-Day Return ) *.5 + ( 3-Month Return ) *.5
2. Close > Average( Close, 100 days);
--------------------
*/


set{20Return, ROC(20,1)}
set{ROCrank1, 20Return * .25}
set{60Crank, ROC(60,1)}
set{ROCrank2, 60Crank * .75}
set{ROCrotate, ROCrank1 + ROCrank2}
sort on column 9 descending

ADD COLUMN SEPARATOR
ADD column ROCrotate 3 months ago
ADD column ROCrotate 1 month ago
ADD column ROCrotate 1 week ago
ADD column ROCrotate

/* hit machine -- for filters that use only sorted columns and no conditions, count by the sorted column hits */
set{x1,count(ROCrotate > 0,1)}
set{long1,x1 * 1}
long1 > 0
set{hits,count(long1 > 0,100)}
add column hits {hits/100days}

ADD COLUMN SEPARATOR
and draw MA(100)
/* and close > MA(100) */

add column Historical Volatility(30,1)
add column Historical Volatility(100,1)

add column relative strength(spy,30) > -1

set{ma100a,count(close > ma(100),1)}
ADD COLUMN ma100a {above ma100}

set{ma20a,count(close > ma(20),1)}
ADD COLUMN ma20a {above ma20}

set{20x, count (close crossing above ma(20) within the last week,1)}
add column 20x

and add column industry
and add column sector

draw ma(20)

]



fotchstecker
304 posts
msg #156131
Ignore fotchstecker
modified
3/12/2021 7:19:59 PM

This further explains why this count is needed. See how VDE suddenly appears among the "mainstays". Soon thereafter, a few days, VDE was among the top 3. And it has been hovering there since.



The main problem with my solution is that the "count" rolls-forward with the counting lookback window, in this case 100 days. So, "31" will not necessarily increment to "32" on the next day, since the end of the window moves forward one day and the new window might not contain a hit. Therefore, you might not see the numbers increment until several days later -- at least this is why I think it happens this way.

Still, progress kind of. Thanks to all who shared help along the way.

Cheese
1,374 posts
msg #156133
Ignore Cheese
modified
3/13/2021 12:26:54 AM

Wow, lots of work.
Thank you for sharing your labor of love.
I hope your hard work will pay off handsomely
Trade well !

StockFetcher Forums · General Discussion · How to count appearances in a filter with ranking? xarlor?<< >>Post Follow-up

*** Disclaimer *** StockFetcher.com does not endorse or suggest any of the securities which are returned in any of the searches or filters. They are provided purely for informational and research purposes. StockFetcher.com does not recommend particular securities. StockFetcher.com, Vestyl Software, L.L.C. and involved content providers shall not be liable for any errors or delays in the content, or for any actions taken based on the content.


Copyright 2022 - Vestyl Software L.L.C.Terms of Service | License | Questions or comments? Contact Us
EOD Data sources: DDFPlus & CSI Data Quotes delayed during active market hours. Delay times are at least 15 mins for NASDAQ, 20 mins for NYSE and Amex. Delayed intraday data provided by DDFPlus


This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.