A small Python library for building options strategies.
It lets you define a strategy leg by leg, calculate the payoff at expiration, and compute max profit and max loss. All legs are assumed to share the same expiration.
pip install optstratFor plotting examples:
pip install matplotlibPlot payoff using matplotlib:
import matplotlib.pyplot as plt
from optstrat import OptionStrategy
strategy = OptionStrategy("Covered Call", 100, contract_size=100)
# 100 shares + short 1 call contract
strategy.long_underlying(100, quantity=100)
strategy.short_call(110, 2.5, quantity=1)
max_profit, max_loss = strategy.max_profit_and_loss()
net_cost = strategy.net_cost()
print(f"Max Profit: {max_profit}")
print(f"Max Loss: {max_loss}")
print(f"Cost of entering position ${net_cost}")
for leg in strategy.legs:
print(leg)
plt.plot(strategy.underlying_prices, strategy.payoffs)
plt.title(strategy.name)
plt.fill_between(
strategy.underlying_prices,
strategy.payoffs,
where=(strategy.payoffs > 0),
facecolor='g',
alpha=0.4,
)
plt.fill_between(
strategy.underlying_prices,
strategy.payoffs,
where=(strategy.payoffs < 0),
facecolor='r',
alpha=0.4,
)
plt.xlabel(r'$S_T$')
plt.ylabel('P&L')
plt.show()spot_rangecontrols the simulated range aroundS0as a decimal percentage.underlying_pricesrepresents possible underlying prices at expiration.quantityis the number of contracts for option legs and the number of shares for underlying legs.contract_sizeapplies only to option legs.strategy.net_cost()computes the net cost of entering the position.strategy.max_profit_and_loss()computes max profit and max loss at expiration.- All legs are assumed to share the same expiration.