Thank you for this example. can you please complete it by using this screener as input to a strategy. for instance to be long under and short the long list and close the trades if they go out of the list. thx a lot

@backtrader Hi, I've tryed to test your code but unfortunatly it didn't work properly as the compiler do not reconignize "pos.data" from this line "{pos: pos.data for pos in self.getpositions() if pos}" and also i suspect this line too "self.order_target_percent(data, target=self.perctarget)" the self.order_target percent part. Can you please share the full code that works for you?

def __init__(self):
# The highest ranked: mkt_cap
self.ranks = [bt.ind.movav(d.mkt_cap,period = 1) for d in self.datas]
# allocation perc pro stock
# reserve kept to make sure orders are not rejected due to
# margin. Prices are calculated when known (close), but orders can only
# be executed next day (opening price). Price can gap upwards
self.perctarget = (100.0 - self.p.reserve) % self.p.torank
def next(self):
# get the ranks sorted for each iteration in a list
ranks = sorted(
((self.datas[i], rank[0]) for i, rank in enumerate(self.ranks)),
key=lambda x: x[1], # use rank (elem 1 in the tuple) to sort
reverse=True, # highest ranked 1st ... please
)
# Put top 100 in ordereddict with data as key to test for presence
r100 = collections.OrderedDict(ranks[:self.p.torank])
# Remove those no longer Top 100, prepare quick lookup dict
positions = {pos: pos.data for pos in self.getpositions().values() if pos}
for pos, data in positions.items():
if data not in r100: # open and no rank100 ... close
self.order_target_percent(data, target=0.0)
# Add the newcomers to the Top 100, prepare easy reverse lookup
revpositions = {data: pos for pos, data in positions.items()}
for data in (d for d in r100 if d not in revpositions):
self.order_target_percent(data, target=self.perctarget)

Hi, I've tryed to test your code but unfortunatly it didn't work properly as the compiler do not reconignize "pos.data" from this line "{pos: pos.data for pos in self.getpositions() if pos}" and also i suspect this line too "self.order_target_percent(data, target=self.perctarget)" the self.order_target percent part. Can you please share the full code that works for you?

It is a hand-crafted snippet, giving an indication and orientation as to how a proposed algorithm could be implemented.