Learn how to scan stocks to buy with a python script
I have been searching for APIs for the past few years that are powerful enough to handle high-frequency trading. In the last year, Nasdaq came out with an API where individuals and businesses can signup and get stock data directly. I reached out to Nasdaq earlier this year and talk to a sales guy to begin pricing out how much it would cost to build out a couple of servers to make it easy for users to get free market data to feed their algorithmic trading system. When I was figuring out how to pay for everything, I came across Alpaca API market subscription, which users can subscribe to Nasdaq through them for $9 a month. Most people don’t know this, but this is a game-changer for self-made trading applications.
I will make a couple of videos and post them to my YouTube account to teach individuals to focus on high-frequency trading. In the first video, I show you how to set up a WebSocket to connect to Alpaca API.
THE CODE EASILY PASSED ALPACA 2050 SYMBOLS! WHICH IS VERY EXCITING AND HARD TO MATCH!
To get started, you will have to create an account with ALPACA here
Next, purchase the Market data subscription for $9
Technically you do not have to buy the $9 subscription to gather Market Data from Alpaca. You can get free data from IEX, but you can only make 200 requests per minute. You will have to update my code here.
CHANGE sip to iex
Alpaca premium subscription talks about how it provides unlimited data, which holds, but it is restricted. I tested how many symbols I can subscribe to Alpaca with one request. I gathered all Nasdaq symbols using the Nasdaq Screener tool. You can find the tool here.
After gathering the stock symbols, I converted all of them into a Tuple. In the end, my Tuple consisted of over 4000 stock symbols, and if you were to convert it to bytes, it equaled over 32,000 bytes. The message ended up being more significant than the 16,000 max bytes Alpaca allows. I had to cut the stock symbols I was sending up in half. If you wanted to scan the whole Nasdaq, you would have to create two micro python services, which is still better than the other APIs like Etrade. The problem with being limited when trading off of speed is each time you have to make a request, and you have to count how long it takes to request data from the server and how long it takes to get the requested data back to you. For example, if it takes 100ms to the server and 100ms back, each time will cost 200 ms. There are 4389 stocks listed on the Nasdaq, and if I were to build my scanner with Etrade, I am limited to 25 symbols each time, which means the minimum amount of requests I will have to make to scan the whole market is 175, which comes out to be 35 seconds spent on making a requests. The number will go up or down depending on how long it takes to complete the request. The other significant thing also to consider is it also 175 times, you have to write a function to handle the data, which adds more time, which is why one request to Alpaca Nasdaq Market has a significant advantage. I will show how much of an advantage in another video.
Download the code from my GitHub here
Watch the YouTube video above for a walkthrough. Don't forget to subscribe to my channel for more trading videos. Like this article, if it helped you out, don't forget to leave a tip and help fund future projects.