mariostoev/finviz

Enhancement request: combine pattern filters

Opened this issue · 2 comments

First of all, great package!!

I have a request if its possible to combine pattern filters. For example, I want to find out all the stocks in sp500 which either have a wedge pattern or downward channel.

Thanks
mwahal

d3an commented

As of now, I think (not sure) this is only available in the Elite version. To implement this feature, the function would require at least 2 requests and some filtering functionality to implement. If you're interested, you could add it as a feature.

Here is my crude attempt to implement it.
It basically takes sp500 stocks which are above 50DMA.
Then find the stocks which are either in ta_pattern_channeldown or ta_pattern_wedge.
Then it finds a union and intersection of these two sets.

#!/usr/bin/env python
from finviz.screener import Screener
import sys

debug = False
debug_list = False
tickers = []
def print_all():
for stock in stock_list: # Loop
print(stock['Ticker'], stock['Price']) # Print symbol and price

def add_to_tickers():
for stock in stock_list:
tickers.append(stock['Ticker'])

def print_tickers():
for t in tickers:
print ("%s," % t, end='')
print("")

def print_list(msg, this_list):
if len(this_list) > 0:
print ("%s " % msg, end='')
for t in this_list:
print ("%s," % t, end='')
print("")

filters = ['idx_sp500', 'ta_sma50_pa'] # Shows sp500 and above 50day SMA
if len(sys.argv) > 1 and sys.argv[1] != '':
arg_tickers = sys.argv[1].split(',')
else:
arg_tickers = None
#filters = ['idx_ndx', 'ta_sma50_pa'] # Shows sp500 and above 50day SMA
stock_list = Screener(tickers=arg_tickers, filters=filters, table='Technical', order='ticker') # Get the performance table and sort it by price ascending
#print (stock_list.get_ticker_details())

sp500_50dma = [stocks['Ticker'] for stocks in stock_list]

if debug_list:
print ("sp500_50dma")
print (sp500_50dma)

sp500_50dma_ta_pattern_channeldown = []
#print_all()
try:
stock_list.add(filters=['ta_pattern_channeldown']) # Show stocks with high dividend yield
add_to_tickers()
sp500_50dma_ta_pattern_channeldown = [stocks['Ticker'] for stocks in stock_list]
sp500_50dma_ta_pattern_channeldown.sort()
except:
pass

if debug_list:
print ("sp500_50dma_ta_pattern_channeldown")
print (sp500_50dma_ta_pattern_channeldown)

if debug:
print("After ta_pattern_channeldown")
print_all()

sp500_50dma_ta_pattern_wedge = []

try:
stock_list.add(filters=['ta_pattern_wedge']) # Show stocks with high dividend yield
add_to_tickers()

sp500_50dma_ta_pattern_wedge = [stocks['Ticker'] for stocks in stock_list]
sp500_50dma_ta_pattern_wedge.sort()

except:
pass

if debug_list:
print ("sp500_50dma_ta_pattern_wedge")
print (sp500_50dma_ta_pattern_wedge)

if debug:
print("After ta_pattern_wedge")
print_all()

tickers.sort()
#print_tickers()

sp500_filtered_stocks = list(set(sp500_50dma_ta_pattern_channeldown + sp500_50dma_ta_pattern_wedge))
sp500_intersection_filter = list(set(sp500_50dma_ta_pattern_channeldown).intersection(sp500_50dma_ta_pattern_wedge))
sp500_filtered_stocks.sort()
#print ("sp500_filtered_stocks")
sp500_intersection_filter.sort()

print_list ("OR", sp500_filtered_stocks)
print_list ("AND", sp500_intersection_filter)

#stock_list.get_ticker_details()