OptionLab: A Python library for evaluating option trading strategies

Roberto Gomes, PhD
3 min readJul 13, 2023

--

Photo by cottonbro studio.

EDIT: The library has undergone significant changes, rendering the code from the previous version of this article incompatible. As a result, this article has been updated to reflect the latest version of OptionLab.

This is my first public Python library focused on computational finance. As the name suggests, OptionLab aims to be a tool used to evaluate option trading strategies for their profit potential and associated risks. The world of options trading can be intricate and challenging, requiring a deep understanding of mathematical models and quantitative analysis. By leveraging Python’s ease of use and flexibility, OptionLab simplifies the process of modeling even the most complex option strategies with just a few lines of code.

The library can be downloaded from the project’s GitHub page or easily installed using pip:

pip install optionlab

An OptionLab run can produce several interesting outputs, such as:

  • Profit/loss profile of the strategy on a user-defined target date,
  • Range of stock prices for which the strategy is profitable,
  • The Greeks associated with each leg of the strategy,
  • The resulting debit or credit on the trading account,
  • The maximum and minimum returns within a specified lower and higher price range of the underlying asset, and
  • An estimate of the strategy’s probability of profit.

The probability of profit (PoP) for the strategy is calculated based on the distribution of estimated prices of the underlying asset on the user-defined target date. Specifically, for the price range in the payoff where the strategy generates profit, the PoP represents the probability that the stock price will fall within that range. The distribution of underlying asset prices on the target date can be derived from the Black-Scholes model, obtained through simulations (e.g., Monte Carlo), or generated using machine learning models.

Basic documentation (I know it has to be improved!) is also available on the project’s GitHub page, along with examples of modeling common option trading strategies provided as Jupyter notebooks. I plan to add more examples in the near future (if I have enough time).

Despite the code having been developed with option strategies in mind, it can also be used for strategies that combine options with stocks, such as the strategy known as covered call.

In the code snippet below, I present an example of using OptionLab to calculate and display the probability of profit for this strategy. The data used in the calculation refers to Nvidia stock (ticker: NVDA) on
January 16, 2023. The maturity of the call option was February 17, 2023. The covered call setup consists of buying 100 Nvidia shares at $168.99 and
selling the same number of $185.00 strike calls for $4.10 each.

# Import run_strategy()
from optionlab import run_strategy

# Input data (Nvidia, NVDA)
stock_price = 168.99
volatility = 0.483
interest_rate = 0.045
min_stock = stock_price - 100.0
max_stock = stock_price + 100.0
start_date = "2023-01-16"
target_date = "2023-02-17"

# Strategy definition
strategy = [
{"type": "stock", "n": 100, "action": "buy"},
{
"type": "call",
"strike": 185.0,
"premium": 4.1,
"n": 100,
"action": "sell",
},
]

# Run a calculation
out = run_strategy(
{
"stock_price": stock_price,
"volatility": volatility,
"start_date": start_date,
"target_date": target_date,
"interest_rate": interest_rate,
"min_stock": min_stock,
"max_stock": max_stock,
"strategy": strategy,
}
)

# Print the output
for key, value in out.dict(exclude={"data", "inputs"}, exclude_none=True).items():
print(f"{key.capitalize().replace('_',' ')}: {value}")

The input data is passed as a Python dictionary to the run_strategy() function, imported from OptionLab. This function performs the calculations and generates a series of outputs for the covered call strategy, as shown below.

Probability of profit: 0.5472008423945269
Profit ranges: [(164.9, inf)]
Per leg cost: [-16899.0, 409.99999999999994]
Strategy cost: -16489.0
Minimum return in the domain: -9590.000000000002
Maximum return in the domain: 2011.0
Implied volatility: [0.0, 0.456]
In the money probability: [1.0, 0.256866624586934]
Delta: [1.0, -0.30713817729665704]
Gamma: [0.0, 0.013948977387090415]
Theta: [0.0, 0.19283555235589467]
Vega: [0.0, 0.1832408146218486]

OptionLab is provided as is, with no guarantee that its results are accurate. As such, I am not responsible for any losses caused by the use of the code. Bugs can be reported as issues on the project’s GitHub page.

Options are very risky derivatives and, like any other type of financial vehicle, trading options requires due diligence. I am not a financial advisor and the content of this article is not a recommendation.

I am open to hearing any questions, corrections, comments or suggestions. You can also reach me on Linkedin or X.

--

--

Roberto Gomes, PhD

I am a Professor of Materials Engineering specialized in computational research in materials science, physics, and engineering. Also interested in finance.