Skip to main content

Calculate estimated margin requirements for equities options, based on CBOE margining.

Project description

margin-estimator

Calculate estimated margin requirements for equities options, based on CBOE margining.

[!NOTE] Box spreads and some complex multi-legged positions may be calculated too conservatively.

Installation

$ pip install margin_estimator

Usage

Simply pass an arbitrary list of legs to the calculate_margin function along with an Underlying object containing information on the underlying, and you'll get back margin requirement estimates for cash and margin accounts!

from datetime import date
from decimal import Decimal
from margin_estimator import (
    ETFType,
    Option,
    OptionType,
    Shares,
    Underlying,
    calculate_margin,
)

# a SPY iron condor
# make sure to pass `ETFType.BROAD` for broad-based indices
underlying = Underlying(price=Decimal("587.88"), etf_type=ETFType.BROAD)
expiration = date(2024, 12, 20)
long_put = Option(
    expiration=expiration,
    price=Decimal("4.78"),
    quantity=1,
    strike=Decimal(567),
    type=OptionType.PUT,
)
short_put = Option(
    expiration=expiration,
    price=Decimal("5.61"),
    quantity=-1,
    strike=Decimal(572),
    type=OptionType.PUT,
)
short_call = Option(
    expiration=expiration,
    price=Decimal("5.23"),
    quantity=-1,
    strike=Decimal(602),
    type=OptionType.CALL,
)
long_call = Option(
    expiration=expiration,
    price=Decimal("3.68"),
    quantity=1,
    strike=Decimal(607),
    type=OptionType.CALL,
)
margin = calculate_margin(
    [long_put, short_put, long_call, short_call], underlying
)
print(margin)
>>> cash_requirement=Decimal('262.00') margin_requirement=Decimal('262.00')

For normal equities you can omit the etf_type parameter:

# a short F put
underlying = Underlying(price=Decimal("11.03"))
expiration = date(2024, 12, 20)
put = Option(
    expiration=expiration,
    price=Decimal("0.45"),
    quantity=-1,
    strike=Decimal(11),
    type=OptionType.PUT,
)
margin = calculate_margin([put], underlying)
print(margin)
>>> cash_requirement=Decimal('1055.00') margin_requirement=Decimal('263.00')

And for leveraged products, you'll need to pass in the leverage_factor:

# a naked TQQQ call
underlying = Underlying(
    price=Decimal("77.35"),
    etf_type=ETFType.BROAD,
    leverage_factor=Decimal(3),
)
expiration = date(2024, 12, 20)
call = Option(
    expiration=expiration,
    price=Decimal("4.45"),
    quantity=-1,
    strike=Decimal(80),
    type=OptionType.CALL,
)
margin = calculate_margin([call], underlying)
print(margin)
>>> cash_requirement=Decimal('7555.00') margin_requirement=Decimal('3661.00')

You can even add shares of an underlying to calculate requirements for covered legs:

# a covered SPY call
underlying = Underlying(price=Decimal("740"))
expiration = date(2026, 5, 15)
call = Option(
    expiration=expiration,
    price=Decimal("4.45"),
    quantity=-1,
    strike=Decimal(780),
    type=OptionType.CALL,
)
shares = Shares(price=Decimal("700.41"), quantity=100)
margin = calculate_margin([call, shares], underlying)
print(margin)
>>> cash_requirement=Decimal('70041.00') margin_requirement=Decimal('35020.00')

Please note that all numbers are baseline minimums from CBOE guidelines and individual broker margins will likely vary significantly.

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

margin_estimator-0.4.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

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

margin_estimator-0.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file margin_estimator-0.4.tar.gz.

File metadata

  • Download URL: margin_estimator-0.4.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for margin_estimator-0.4.tar.gz
Algorithm Hash digest
SHA256 dc6ea99b5b48e124f421dc33fb4df9c03c4affa119945be21c8299f06a209e8b
MD5 c4d3a423c711c8c6bdffc043b00109c4
BLAKE2b-256 af8af4c8632116a8da603ae5c739e1adb2b6cdb4f2642214e59d8841a997abf4

See more details on using hashes here.

Provenance

The following attestation bundles were made for margin_estimator-0.4.tar.gz:

Publisher: publish.yml on tastyware/margin-estimator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file margin_estimator-0.4-py3-none-any.whl.

File metadata

  • Download URL: margin_estimator-0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for margin_estimator-0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 05e9f3f33fff68b2889c6a6427b4b2d3db85d18acdedc36c14f16cbbda1c8248
MD5 0433c3ad7eccd44fc7a8610250cea746
BLAKE2b-256 a00072dc8c213976288aef7ef616ebe85353af6e5e02e63c03158579143a7e22

See more details on using hashes here.

Provenance

The following attestation bundles were made for margin_estimator-0.4-py3-none-any.whl:

Publisher: publish.yml on tastyware/margin-estimator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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