# Best way to scan for stocks

**URL:** https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264
**Category:** Alpaca Market Data
**Created:** [September 13, 2019, 7:25pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264 "2019-09-13T19:25:31Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![grandpawithgreyhair](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@grandpawithgreyhair](https://forum.alpaca.markets/u/grandpawithgreyhair)
#### Post date: [September 13, 2019, 7:25pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/1 "2019-09-13T19:25:31Z")

</div>

It seems easy to fetch data for a particular stock with the API. In python: `df = api.get_barset('AAPL', 'day', limit=500).df['AAPL']`. However, how can I scan for stocks that I want to trade? Simple example: I’d like to get all stocks that gapped up premarket by \> 20%.

It seems like for this, Alpaca does not really have a good support yet. Hence, my question:  
**Is the best way to scan for stocks to directly query the IEX API to find the tickers I want to trade ([https://iexcloud.io/financial-data/](https://iexcloud.io/financial-data/))?**

---

<div class="post-metadata">

### Author: ![joshcogburn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joshcogburn/32/127_2.png) [@joshcogburn](https://forum.alpaca.markets/u/joshcogburn)
#### Post date: [September 14, 2019, 12:09am UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/2 "2019-09-14T00:09:59Z")

</div>

Alpaca’s purpose is to facilitate the trade, it’s our job as the programmer to come up with the logic to make a good trade. You’re going to need to define your own filters/scanners. As for Pandas DataFrames you can use Lambda functions.

> **[Apply and Lambda usage in pandas](https://towardsdatascience.com/apply-and-lambda-usage-in-pandas-b13a1ea037f7)**
>
> Learn these to master Pandas

---

<div class="post-metadata">

### Author: ![grandpawithgreyhair](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@grandpawithgreyhair](https://forum.alpaca.markets/u/grandpawithgreyhair)
#### Post date: [September 14, 2019, 7:05am UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/3 "2019-09-14T07:05:07Z")

</div>

@joshcogburn, thanks a lot for your answer. I’m already downloading all IEX intra-day data. The problem is: It’s a lot of tickers (several thousand). Even with asynchronous requests, it takes a while to get the tickers. So building a real time scanner would be tricky.

Hence, I am wondering: What’s the best practice for this? Do you use something like the inofficial finviz api ([https://pypi.org/project/finviz/](https://pypi.org/project/finviz/)). Or do you have your own data pipeline that fetches all, say IEX, tickers every hour or so to scan for new opportunities?

---

<div class="post-metadata">

### Author: ![joshcogburn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joshcogburn/32/127_2.png) [@joshcogburn](https://forum.alpaca.markets/u/joshcogburn)
#### Post date: [September 14, 2019, 5:01pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/4 "2019-09-14T17:01:38Z")

</div>

@grandpawithgreyhair No problem, Take a look at the Web Socket API. Web Sockets use a protocol different from http, they will allow you to stream data.

> **[Documentation | Alpaca](https://docs.alpaca.markets/api-documentation/web-api/streaming/)**
>
> Alpaca API lets you build and trade with real-time market data for free.

Polygon:

> **[Polygon.io - Real-time Stock APIs, Forex and Crypto](https://polygon.io/sockets)**
>
> Polygon.io offers streaming realtime data for stocks/equities, ETFs, Indecies and Forex/Currencies including crypto currencies. Our Real-Time Stock Data APIs help you build the future on fintech.

---

<div class="post-metadata">

### Author: ![joshcogburn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joshcogburn/32/127_2.png) [@joshcogburn](https://forum.alpaca.markets/u/joshcogburn)
#### Post date: [September 14, 2019, 5:03pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/5 "2019-09-14T17:03:09Z")

</div>

You can also use python filters to define a function that will return results that only match your filters.

---

<div class="post-metadata">

### Author: ![grandpawithgreyhair](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@grandpawithgreyhair](https://forum.alpaca.markets/u/grandpawithgreyhair)
#### Post date: [September 15, 2019, 11:36am UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/6 "2019-09-15T11:36:30Z")

</div>

Okay, but if I’m not mistaken the Alpaca `stream` endpoint is only for account and order updates. So you can’t listen to say all stocks on the market every minute. Instead, you can only stream/listen to your current trades, right?

Maybe you have an example of how you use python filters to keep scanning real time market data for several thousand tickers you’re currently not trading yet (without polygon if possible)? That would be really helpful!

---

<div class="post-metadata">

### Author: ![pbharrin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/pbharrin/32/59_2.png) [@pbharrin](https://forum.alpaca.markets/u/pbharrin)
#### Post date: [September 16, 2019, 4:27pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/7 "2019-09-16T16:27:15Z")

</div>

@grandpawithgreyhair Can you post your code for fetching the data from IEX. I’m guessing there may be room for improvement there. I have some code that pulled 1000 tickers from IEX and it only took ~1 minute. Two tricks you can use to reduce the amount of data coming back are:

1. send multiple requests at once, you can request 100 tickers at a time
2. just get the quote, don’t ask IEX to send you everything they know about the ticker. That’s just too much data.

With those improvements you should be able to scan several thousand in a few minutes.

---

<div class="post-metadata">

### Author: ![grandpawithgreyhair](https://avatars.discourse-cdn.com/v4/letter/g/da6949/32.png) [@grandpawithgreyhair](https://forum.alpaca.markets/u/grandpawithgreyhair)
#### Post date: [September 16, 2019, 5:06pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/8 "2019-09-16T17:06:37Z")

</div>

Good point! Using just the quotes will definitely speed things up.

I am fetching all IEX tickers just to get training data. It takes me ca. 7 minutes for 8.8K tickers (all data in 1-min resolution for each ticker for 1 day). I am using asyncio in Python for asynchronous requests (with 4 workers).

---

<div class="post-metadata">

### Author: ![joshcogburn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joshcogburn/32/127_2.png) [@joshcogburn](https://forum.alpaca.markets/u/joshcogburn)
#### Post date: [September 17, 2019, 1:17pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/9 "2019-09-17T13:17:52Z")

</div>

Your definitely heading in the right direction with asynchronous requests. Could you post the problematic code?

---

<div class="post-metadata">

### Author: ![Joe\_Ching](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joe_ching/32/1748_2.png) [@Joe\_Ching](https://forum.alpaca.markets/u/Joe_Ching)
#### Post date: [December 23, 2020, 1:51pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/10 "2020-12-23T13:51:15Z")

</div>

the following new information that i found from polygon websites seems to require a fee of $199/m to access real data. pls help me to resolve the discrepancy:  
from alpaca: “”"" [Documentation | Alpaca

Alpaca API lets you build and trade with real-time market data for free."""  
from polygon:[https://polygon.io/dashboard/stocks](https://polygon.io/dashboard/stocks)  
“”" Stocks  
Starter  
NEW  
Great for Historical Data  
$99/m  
Upgrade  
• Unlimited API Calls  
• 15-minute Delayed Data

Stocks  
Developer  
POPULAR  
Great for Real Time Data  
$199/m  
• Unlimited API Calls  
• Real-time Data

> **[Polygon.io - WebSocket Documentation](https://polygon.io/sockets)**

If you do not have access to real-time data, you will need to connect to wss://delayed.polygon.io to receive messages.

* * *

Stocks Cluster  
real-time: wss://socket.polygon.io/stocks  
delayed: wss://delayed.polygon.io/stocks  
Available Channels:  
T.\* Trades  
Q.\* Quotes  
A.\* Aggregate ( per second )"""

---

<div class="post-metadata">

### Author: ![Jason\_Bohne\_Alpaca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/jason_bohne_alpaca/32/1104_2.png) [@Jason\_Bohne\_Alpaca](https://forum.alpaca.markets/u/Jason_Bohne_Alpaca)
#### Post date: [December 23, 2020, 6:50pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/11 "2020-12-23T18:50:58Z")

</div>

> [@Joe\_Ching](#):
>
> sites seems to require a fee of $199/m to access real data. pls help me to resolve the discrepancy:

Hey @Joe_Ching

Perhaps this previous thread will help [here](https://forum.alpaca.markets/t/contiguous-and-consistent-minute-data/3586/3)

TLDR: If an Alpaca User has a funded live trading account they have access to Polygon Data

Hope this helps,  
Jason from Alpaca

---

<div class="post-metadata">

### Author: ![joshcogburn](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joshcogburn/32/127_2.png) [@joshcogburn](https://forum.alpaca.markets/u/joshcogburn)
#### Post date: [December 23, 2020, 8:16pm UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/12 "2020-12-23T20:16:47Z")

</div>

If you have an Alpaca brokerage account, you get access to the Polygon API for free. Your live Alpaca API key will allow you to access the Polygon API.

---

<div class="post-metadata">

### Author: ![Joe\_Ching](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joe_ching/32/1748_2.png) [@Joe\_Ching](https://forum.alpaca.markets/u/Joe_Ching)
#### Post date: [December 24, 2020, 6:28am UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/13 "2020-12-24T06:28:19Z")

</div>

thanks. that’s great news.

---

<div class="post-metadata">

### Author: ![Joe\_Ching](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/joe_ching/32/1748_2.png) [@Joe\_Ching](https://forum.alpaca.markets/u/Joe_Ching)
#### Post date: [December 24, 2020, 6:31am UTC](https://forum.alpaca.markets/t/best-way-to-scan-for-stocks/264/14 "2020-12-24T06:31:32Z")

</div>

thanks for the reconfirmation. but the final verdict is live trading, which will start next week. and i ll let u know then.
