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.

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

  • 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.

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.0.tar.gz (4.1 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.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: optstrat-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 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.0.tar.gz
Algorithm Hash digest
SHA256 cacb64981f5ffb93fe69b91074511ba5630ea8bc5fa50a347939e174b7878725
MD5 f88df867d7f6a36de98e2593c89abdca
BLAKE2b-256 bab5724b48c3c860624695c30c031e1aeaec4b15ac717ad6e9a1fe731da9510b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: optstrat-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6fec4c3f6fc45a82e711c2e82eb4fe7642ad0b1b7afe5afb1cc7416a9f1a30d
MD5 bb9316448b4bd361a8ff78c0677a73e2
BLAKE2b-256 4e8ec72178ee1a680b31172757012c3606fefca24554a3073b171c6dacaf5790

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