Skip to main content

A small Python library for building options strategies

Project description

optstrat

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.

Installation

pip install optstrat

For plotting examples:

pip install matplotlib

Usage example

Plot 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()

Notes

  • spot_range controls the simulated range around S0 as a decimal percentage.
  • underlying_prices represents possible underlying prices at expiration.
  • quantity is the number of contracts for option legs and the number of shares for underlying legs.
  • contract_size applies 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

optstrat-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optstrat-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file optstrat-0.1.1.tar.gz.

File metadata

  • Download URL: optstrat-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for optstrat-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c97c714f83a49bc3a9b04286b5d42d9e82127f80949f7a763783afa43c791050
MD5 ffa58807201edef611aebeb689f76df5
BLAKE2b-256 10dba0b52e9098d2d46168838b974ef5492916de81f7814ab215d3627146bf9e

See more details on using hashes here.

File details

Details for the file optstrat-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: optstrat-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for optstrat-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 202db503b790db825e59a9a18cd692c5b576232501e55f3701b5cd5473d2fc77
MD5 74ec8786f748d25f0a0f049fdb7a3ca4
BLAKE2b-256 a37aa8cb93cdd4dd77e21007a11f727c1b271812b4314ec67eb1bd1dee760f54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page