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 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 opstratInstalling 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 opPlotting single option
The payoff diagram for a single option can be plotted using the single_plotter() function.
Default plot:
op.single_plotter()
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)
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])
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)
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)
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])
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:






