Creating a simple stock screener

So I’m new to Alpaca and relatively new to coding in general. I run Linux and have written some very basic Python code before, but things like dictionaries and dataframes can quickly go straight over my head.

However, that being said, I feel like creating a simple stock screener is something within my grasp. I’ve been going through the momentum day-trading algo that’s linked in the ‘getting started with Alpaca’ page. Here’s the code that’s confusing me:

session = requests.session()

min_share_price = 2.0
max_share_price = 13.0
min_last_dv = 500000
default_stop = .99
risk = 0.05

def get_1000m_history_data(symbols):
print(‘Getting historical data…’)
minute_history = {}
c = 0
for symbol in symbols:
minute_history[symbol] = api.polygon.historic_agg(
size=“minute”, symbol=symbol, limit=1000
).df
c += 1
print(’{}/{}’.format(c, len(symbols)))
print(‘Success.’)
return minute_history

def get_tickers():
print(‘Getting current ticker data…’)
tickers = api.polygon.all_tickers()
print(‘Success.’)
assets = api.list_assets()
symbols = [asset.symbol for asset in assets if asset.tradable]
return [ticker for ticker in tickers if (
ticker.ticker in symbols and
ticker.lastTrade[‘p’] >= min_share_price and
ticker.lastTrade[‘p’] <= max_share_price and
ticker.prevDay[‘v’] * ticker.lastTrade[‘p’] > min_last_dv and
ticker.todaysChangePerc >= 2.5
)]

Please forgive my simplistic noobie mind, but here’s what’s confusing me:

  1. Why couldn’t I just run something like the get_tickers object more or less on its own? Sure I’d need to specify today’s date and all that stuff, but couldn’t I do without the get_1000m_history_data? In addition to being fairly confused by the code for that object, it doesn’t look absolutely necessary for the purpose of making a simple stock screener.

  2. When I ran this program, I didn’t get output that actually showed me the stocks and their respective stats that met the criteria. I think it’d help me to actually see it.

I envisioned the process going something along the lines of: request data, filter data, output filtered data. Doesn’t the get_tickers part take care of the first two steps? In a perfect world, running the screener would result in the following output: a few simple messages updating me as the program completes steps, and finishes with an array of data showing the ticker, price, MACD, and a positive percent change from yesterday.

From there, I think I could handle writing the code that would place the buy/sell orders. That could also be wishful thinking.

I know I just threw a lot at you (whoever you are), and please don’t feel obligated to respond to all the issues I’ve raised here. Like I said, just pointing me in the direction of a tutorial, article, or perhaps a simpler program on GitHub would be plenty helpful. Of course, if you’d like to connect a couple of the dots for me yourself, you’re more than welcome to do that as well.

1 Like

I’m not an expert, but here’s an attempt to help without rebuilding the whole algo (I may try this for my own education and post an update)…

‘get_tickers’ is defining a function, which will run and filter down the universe of tradable stocks according to the parameters defined in the return statement (ie. min & max share price). This will return a list of tickers.

Likewise, the ‘get_1000m_history_data’ function is defining a function to consume a list of tickers, and output the prior 1000 minute pricing.

Here’s the missing piece - defining a function in Python doesn’t automatically call and run the function. You still need to instantiate it and pass whatever arguments are required. Try running something like this, defining variables that call the functions (so the outputs are saved in memory and can be passed to the pricing function):

symbols = get_tickers()
get_1000m_history_data(symbols)

Output looks like:

(Note - I changed the min / max price parameters to return a shorter list of tickers)

1 Like