0% found this document useful (0 votes)
237 views11 pages

Visualizing Option Trading Strategies in Python - by Abhijith Chandradas - DataDrivenInvestor

This document discusses visualizing option trading strategies in Python using the opstrat package. The package allows generating option payoff diagrams for single options and complex multi-option strategies. It demonstrates plotting for strategies like short strangle, iron condor, and call/put options. The diagrams provide insight into the risk/reward profile of different option positions. It also shows how to generate diagrams from real market data by fetching option prices from Yahoo Finance.

Uploaded by

FriendlyarmMini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views11 pages

Visualizing Option Trading Strategies in Python - by Abhijith Chandradas - DataDrivenInvestor

This document discusses visualizing option trading strategies in Python using the opstrat package. The package allows generating option payoff diagrams for single options and complex multi-option strategies. It demonstrates plotting for strategies like short strangle, iron condor, and call/put options. The diagrams provide insight into the risk/reward profile of different option positions. It also shows how to generate diagrams from real market data by fetching option prices from Yahoo Finance.

Uploaded by

FriendlyarmMini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Published in DataDrivenInvestor

You have 2 free member-only stories left this month.


Sign up for Medium and get an extra one

Abhijith Chandradas Follow

Mar 23, 2021 · 5 min read · · Listen

Save

Visualizing Option Trading Strategies in


Python
A walk-through of how to plot option payoff diagrams using opstrat
package in python

Introduction
An option is a derivative, a contract that gives the buyer the right, but not the
obligation, to buy or sell the underlying asset by a certain date (expiration date) at a
specified price (strike price).

There are two types of options: calls and puts. Traders can construct option
strategies ranging from buying or selling a single option to very complex ones that
involve multiple simultaneous option positions.

Option payoff diagrams are profit and loss charts that show the risk/reward profile
of an option or combination of options. As option probability can be complex to
understand, payoff diagrams gives an insight into the risk/reward for the trading
strategy.

Opstrat Package
Opstrat is a python package which can be used for visualizing options, without the
need of complex coding. The package provides functions for constructing payoff

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 1/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

diagrams for single options as well as complex strategies involving multiple


positions.
The package is also compatible with yahoo finance package and can be used to
generate option payoff diagrams fetching data from yahoo finance API.

Installing the package

The package can be installed by using pip install command.

pip install opstrat

Installing the package also installs the dependencies- pandas, matplotlib, seaborn
and yfinance packages.

Time to Code
Once the package is installed successfully, it can be imported as below:

import opstrat as op

Plotting single option


The payoff diagram for a single option can be plotted using the single_plotter()
function.

Default plot:

op.single_plotter()

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 2/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Image by Author

If no arguments are provided, payoff diagram for a long call option will be
generated with strike price as $102 and spot price $100.

Note that the trader’s profit is shown in green shade and loss is shown in red. The
call option buyer’s loss is limited to $2 regardless of how low the share price falls.
The trader’s profit increases if the stock price increase beyond $104(break-even
price)

Customizing single plot


The plot can be modified by providing the details of the option as arguments.
Example:
The following code will generate the payoff diagram for an option seller who
receives option premium of $12.50 selling a put option at a strike price of $460 when
the stock is also trading at $460(spot price).

op.single_plotter(spot=460, strike=460, op_type='p',


tr_type='s', op_pr=12.5)

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 3/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Image by author

Plotting for Multiple Options strategy


The payoff diagram for a single option can be plotted using the multi_plotter()
function. This function will plot each individual payoff diagrams and the resultant
payoff diagram.
The particulars of each option has to be provided as a list of dictionaries.

Example 1: Short Strangle


A short strangle is an options trading strategy that involve:
(a)selling of a slightly out-of-the-money put and
(b)a slightly out-of-the-money call of the same underlying stock and expiration date

op_1 = {'op_type':'c','strike':110,'tr_type':'s','op_pr':2}
op_2 = {'op_type':'p','strike':95,'tr_type':'s','op_pr':6}
op.multi_plotter(spot=100, op_list=[op_1,op_2])

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 4/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Image by Author

Example 2 : Iron Condor (Option strategy with 4 options)


An iron condor is an options strategy consisting of two puts (one long and one
short) and two calls (one long and one short), and four strike prices, all with the
same expiration date.

The stock currently trading at $212.26 (Spot Price)


Option 1: Sell a call with a $215 strike, which gives $7.63 in premium
Option 2: Buy a call with a strike of $220, which costs $5.35.
Option 3: Sell a put with a strike of $210 with premium received $7.20
Option 4: Buy a put with a strike of $205 costing $5.52.

op1={'op_type': 'c', 'strike': 215, 'tr_type': 's', 'op_pr': 7.63}


op2={'op_type': 'c', 'strike': 220, 'tr_type': 'b', 'op_pr': 5.35}
op3={'op_type': 'p', 'strike': 210, 'tr_type': 's', 'op_pr': 7.20}
op4={'op_type': 'p', 'strike': 205, 'tr_type': 'b', 'op_pr': 5.52}

op_list=[op1, op2, op3, op4]


op.multi_plotter(spot=212.26,spot_range=10, op_list=op_list)

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 5/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Image by Author

The optional argument, spot range limits the range of spot values covered in the
plot. The default spot range in +/-20%. If the underlying asset is less volatile and the
strike price of options are within a small range, smaller spot range like 5% can be
considered. For highly volatile underlying asset, higher spot range can be used.

Plotting Real Options using Yahoo Finance API


We can plot the option-payoff by providing the option ticker and other
parameters(option type, transaction type and strike price) into the yf_plotter
function.
Example 1 : Call Option Buyer Payoff Diagram of Microsoft Inc.
The following code will generate the payoff diagram for Microsoft Inc. call option
buyer, who buys call option at strike price $235.
MSFT is the stock ticker for Microsoft Inc.

op_list=[{'tr_type':'b', 'op_type':'c', 'strike':235}]


op.yf_plotter('msft', spot_range=10, op_list=op_list)

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 6/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Image by Author

Example 2: Strangle on Amazon


Strangle is a strategy which involves simultaneous purchase of call option and put
option near the spot price allowing the purchaser to make a profit whether the
price of the stock goes up or down.

Stock ticker : AMZN(Amazon Inc.)


Amazon stock is currently trading around $3070. A straddle can be constructed by
purchasing the following options:
Option 1: Buy Call at Strike Price 3070
Option 2: Buy Put option at Strike price 3070
Option expiry date can be specified as parameter ‘exp’ in the format ‘YYYY-MM-DD’.

op_1={'op_type': 'c', 'strike':3070, 'tr_type': 'b'}


op_2={'op_type': 'p', 'strike':3070, 'tr_type': 'b'}
op.yf_plotter(ticker='amzn', exp='2021-03-26',
op_list=[op_1, op_2])

Open in app Sign up Sign In


https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 7/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor
Open in app Sign up Sign In

Image by Author

If you have any questions please feel free to leave a comment.


You can also reach out to me in twitter.

You can refer Jupyter Notebook for the article here.

If you prefer the tutorial in video format, you can check out the below video:

How to plot option payoff charts using opstrat package in python

946 8

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 8/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Tutorial in video Format (AB Analytica)

Become a Member
I hope you like the article, I would highly recommend signing up for Medium
Membership to read more articles by me or stories by thousands of other authors on
variety of topics.
Your membership fee directly supports me and other writers you read. You’ll also
get full access to every story on Medium.

Here are a few related articles which you may find interesting

Stock Market Data Visualization Using Mplfinance


How to obtain stock market data and create
visualizations(candlestick, OHLC, PnF etc.) in Python using…
medium.com

All you need to know about yfinance : Yahoo! Finance Library


A complete step-by-step tutorial on how to use Yahoo Finance
Package for python- yfinance, to obtain share price and…
medium.com

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 9/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Photo by Patrick Weissenberger on Unsplash

Python Finance Trading Options Visualization

Get an email whenever Abhijith Chandradas publishes.


By signing up, you will create a Medium account if you don’t already have one. Review
our Privacy Policy for more information about our privacy practices.

Subscribe

About Help Terms Privacy

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 10/11
1/4/23, 11:47 PM Visualizing Option Trading Strategies in Python | by Abhijith Chandradas | DataDrivenInvestor

Get the Medium app

https://medium.datadriveninvestor.com/visualizing-option-trading-strategies-in-python-35bfa61151d9 11/11

You might also like