Stock Option Screener in Python
Skills:
AI Pair Programming80%
Key Takeaways
The video demonstrates building a simple Python script for screening stock options, focusing on programming with financial data using tools like Python.
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to learn how to build a basic option screener in python so let's get right into it all right so this video is not going to be a very long one because there's just not too much to talk about from a python perspective when it comes to option screening because we basically have a library this library allows us to get the option chains uh to get the values like asks and bits and expiration dates and all the different things that we might be interested in we can learn how to filter but that's it so the code is not going to be too much today which is why i want to start with a little bit of financial knowledge in the beginning even though this channel is not a financial channel i don't want to become a stock investing channel here at least not yet so we're just going to cover the basics of options for those of you who don't know what options are but know what stocks are for example because i don't want to explain the whole stock market but before we get into that i want to mention as always since this is a financial video this video is not investing advice it's not financial advice i'm not a financial professional when you work with options or stocks your capital is at risk and you uh you are responsible for any gains or losses that you make so i'm not responsible for anything that you do in the markets i'm just showing you my python education maybe one or two things about options in general i'm not responsible and this video is not financial advice just to make that clear as always so let's just cover the very basic terms uh of what a call option is and what a put option is and i'm not going to pretend that i'm super educated here but a basic uh a basic definition i should be able to give you a basic definition here now a call option is basically a bet that the price of a stock is going to go up so an option gives you the right but not the obligation to buy a stock in the future or not only a stock any asset like a bond a commodity there is an asset and this option allows you to buy um but it only gives you the right it doesn't give you the obligation to buy this uh security this asset at a specific price so basically if i say okay i buy a option an option that allows me to buy facebook stocks for ten dollars per share at i don't know today we have the 21st of july let's say it allows me to do that up until the 28th of july i could exercise that option and say okay i want to buy the facebook shares because because they're way um way more expensive than what my strike price is this is what the strike price is the the price that i am allowed to buy these stocks for if i have the option if i want to exercise the option now of course i have to pay a premium for that option to get this right so this right costs money someone sells that contract to me but a call option basically is just uh i have the right let's let's put it down right but not obligation to buy to buy a certain asset uh up to a certain date for example um so this is what a call option is a put option is basically the same thing but not to buy but to sell so i have the right but not the obligation to sell a certain asset up to a certain date up to a certain date there you go so those are the two types of options that we have now a call option is more bullish i say okay this company is going to succeed and this is why i buy some call options a put option is more bearish i say okay i don't believe in that company so i want to have the right to sell at the current price because i predict that the price or i bet that the price is going to be lower uh in a couple of weeks months or years from now so that is the basic definition now let us get into the coding because i don't want to talk too much about options here this is still a programming channel uh the library that we're going to need here is called yahoo fin which refers obviously to the yahoo finance so you're going to start cmd and you're going to install yahoo underscore finn this is what we need and besides that we're also going to make use of pandas so make sure you have pandas installed pip install pandas like that uh those are the two packages that we're going to need and first of all we need to specify where actually first of all we need to import them we're going to say from yahoo underscore fin import options and we're going to say import pandas spd there you go so what we're going to do now is we're going to decide which stock we're looking for so for example what kind of options are we looking for let's say we're looking for options on the apple stock so let's say stock is apple and i want to know what options are out there for apple and in order to see that i can get the option chain which is basically just uh which i can basically get by saying options dot get options chain and then the stock so the ticker symbol of the stock uh and if i want to do that i can just store this in a chain chain equals options don't get option chain and then i can print that chain and you're going to see that there is a problem if we run it like that it's not really a problem but we cannot see all the columns because pandas by default says okay i'm not going to show everything so we have a bunch of dots here we can change that by just saying pd dot set underscore option uh display uh what is a display dot max underscore columns is going to be none so we don't have a maximum here and if we do that we're going to see all the values so you can see that we have what do we have here we have uh the contract name which is this year we have the last trade so when it happened uh last trade date we have the strike price which is uh what we have negotiated so the strike price of 75 for a call option means i have the right to buy this security for 75 then we have the last price that this option was sold for we have the bid which is basically what people are willing to pay for it we have the ask which is what the seller is asking for so of course either the seller has to lower the price or the buyer has to increase what they're willing to pay for then a transaction is going to happen we have the change we have um the change in percentage we have the volume we have open interest and volatility and so on the important thing is basically the strike price maybe the ask bid the last price and of course the expiration date or yeah the expiration date basically uh where is the expiration date do we have it somewhere here or do we have to query it separately now let's let's just go ahead and say that we only want to see the call options so that we make it a little bit a little bit more readable here so we're going to say chain and calls and by default we're going to get all the call options now if we are interested in call options that expire before a cert or on a certain day we can do that by saying get option chains uh get options chain and here we specify also the date so i want to say okay give me all the apple options that that expire on a certain day for example july 23 2021 which is in two days and if i do that i will get only the options that expire in two days and i'm not gonna get any other options here so as you can see in the contract name you can see the expiration date here um and i can see what they're trading for what the strike prices and so on if you don't know what a date is for example not every date works let's see if 29 works maybe it's going to work but maybe it's not going to work because not every expiration date is available then we're going to get this error here if we want to know what expiration dates are possible we can call the function um print and the function is called options dot get expiration dates for a security for a stock ticker apple and then we're going to get a list of the expiration dates that we can look for and you can see the next one is july 30 august 6 and august 13 august 20 and so on so i could go with august 20 here august 20 there you go and if i run this i will see the options that expire on august 20th which you can also see in the contract name here um so this is a very basic way to just show all the call options we can do the same thing with puts and i'm only going to get the put options here and this is not the only way in which we can do that we can also just go right right ahead and say not option chain options chain but give me the calls give me only the calls then of course i cannot use this as a dictionary here but then i would get all the calls and i can do the same thing with the puts as well so i can say get puts and i'm only going to get the the put options here now this is actually all you can do with that library but the important thing is that you can now go ahead and screen for certain criteria this is what a screener does now those functions get expiration dates get uh get spots get calls and get option options chain are the only functions that are actually important here but what we can do is we can use pandas to filter out certain things so for example i can say okay i'm interested in apple options in call options so let's change this to calls again and what i get here is a pandas data frame so what i can do is i can query based on certain criteria for example i can say okay i'm interested in those elements of the chain where the chain strike price is below i don't know let's say 200 for example uh does this make sense do we have anything above 200 oh let's go a little bit lower let's say below 100 [Music] outside of those square brackets of course so this basically says give me all the rows where the strike price is less than and if i run this as the last print statement i'm going i'm going to get only the options that expire on this date and have a strike price less than 100 as you can see here so 95 is the last the last one maybe if i say less or equal to 100 i'm going to get one more because i think 100 should be an available strike price let's see there you go you can see an option here um and i can add all sorts of criteria whatever i want to add for example i can say okay uh i want to have a volatility that is low or whatever i don't know let's say and chain volatility has to be less than now i'm not sure if this is a string or a number but let's just say it has to be less than 100 i'm not sure if this is going to work just because of the format no the true value of a series is ambiguous okay but but i can do the same thing with a bit or with the ask price and i can also get the date and format it and say okay i want this to be the last trade to to have happened or is this is the problem that i'm using and here let's just see let's say i don't have strike but i have volatility and get rid of all that here maybe this works there you go and maybe this is going to work maybe not no because because i don't know how to write volatility or do i know volatility doesn't work okay let's just see what it's written like but basically what you can do is you can just specify criteria like it has to be between this and that let's just see what it's called implied volatility okay sorry so i need to say chain [Music] implied or actually chain chain implied volatility is less than a hundred now again i'm not sure if this if this is going to work just because it has a percentage sign in it um yeah as you can see the one is a string and the one is integer we can do string formatting it's it's not a big deal we just uh remove the percentage and uh we then turn the whole thing into a float actually i think this should be quite easily which is quite uh easy we just say int off chain implied volatility dot replace um replace percentage with nothing if this doesn't work we're not going to play around with this anymore but this shouldn't be too difficult oh integer is probably a bad thing we should go with float let's see if that works if it doesn't work we're just going to go with bid or ask but you can filter for cannot convert the series to okay just forget about this uh but we can go and say the chain [Music] ask has to be below 100 and that should at least work there you go so now we only have um rows where the ask is less than 100 for whatever reason and then i can go ahead and say again chain um chain strike has to be less than 100 as well and so i can introduce multiple um what do we have here okay it's just a warning but so we can we can chain we can just combine multiple things so for example now the price at the strike price has to be less than 100 and here it also has uh the ask price also has to be less than 100. so this is how this works you can just screen your options you can also change this to tesla for example of course you need to to care about the expiration dates here um but with tesla we'll get different results here but i can also use this for stock screening or maybe i'm not going to get anything here because uh because i don't have any values that fit this criteria obviously but i can remove this because tesla is a little bit more expensive but i can print the whole chain if i want to and then use different uh different filters for example not less than 100 but less than 600 for example because the um the asks here are very expensive for tesla but this is how you build a simple option screener in python so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Original Description
Today we build a simple Python script for screening stock options.
DISCLAIMER: This is not investing advice. I am not a professional who is qualified in giving any financial advice. This is a video purely about programming using financial data.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Day 4 of 100 Days of GenAI for DevOps🚀
Dev.to AI
Local LLMs for agentic coding: a real-world viability report
Dev.to AI
Beyond Replication: Theo Browne on AI's 'Skeuomorphic Phase' and the Future of Development
Dev.to AI
Rust and Artificial Intelligence: The Rust Foundation's Position
Hacker News
🎓
Tutor Explanation
DeepCamp AI