Stock Option Screener in Python

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches viewers how to build a simple Python script for screening stock options, providing a hands-on introduction to programming with financial data. The script utilizes Python's capabilities for data analysis, making it a valuable resource for beginners in ai coding. By following the video, viewers can learn how to apply programming concepts to real-world financial applications.

Key Takeaways
  1. Install necessary Python libraries
  2. Import required libraries
  3. Define a function to fetch stock option data
  4. Implement a loop to iterate through stock options
  5. Apply filters to screen stock options
  6. Output the screened stock options
💡 The video highlights the importance of programming in financial data analysis, demonstrating how Python can be used to build a simple yet effective stock option screener.

Related Reads

📰
Day 4 of 100 Days of GenAI for DevOps🚀
Learn what ChatGPT stands for and its significance in conversational AI, enabling you to better understand AI-powered tools in DevOps
Dev.to AI
📰
Local LLMs for agentic coding: a real-world viability report
Learn how local LLMs perform in real-world agentic coding tasks, including file reads and writes, and understand their viability
Dev.to AI
📰
Beyond Replication: Theo Browne on AI's 'Skeuomorphic Phase' and the Future of Development
Learn about AI's 'Skeuomorphic Phase' and its implications on the future of development with Theo Browne's insights
Dev.to AI
📰
Rust and Artificial Intelligence: The Rust Foundation's Position
Learn about the Rust Foundation's stance on Rust and Artificial Intelligence and its potential applications
Hacker News
Up next
Free WooCommerce Product Carousel Using Elementor Angie AI | Product Marquee Widget
Quick Tips - Web Desiign & Ai Tools
Watch →