Skip to main content

Calculate estimated margin requirements for equities, options, futures, and futures options. Based on CBOE and CME margining.

Project description

margin-estimator

Calculate estimated margin requirements for equities, options, futures, and futures options. Based on CBOE and CME margining.

[!NOTE] Not all features are available yet, pending further development. Currently, equity/ETF/index options are supported, for any trade type other than ratio spreads, box spreads, and jaze lizards. Contributions welcome!

Installation

$ pip install margin_estimator

Usage

Simply pass a 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,
    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')

Please note that all numbers are baseline estimates based on CBOE/CME guidelines and individual broker margins will likely vary significantly.

Project details


Release history Release notifications | RSS feed

This version

0.1

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.1.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

margin_estimator-0.1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: margin_estimator-0.1.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for margin_estimator-0.1.tar.gz
Algorithm Hash digest
SHA256 72b1f585f7f157b128b17215e5d882b3c4fd9bc2ae25b356057ca7843dfef148
MD5 5f555659d6110a373c9bf41bd9fd7566
BLAKE2b-256 406beb132dbc3002720e44aff750baadf8e70ad70b3ab1ab119ff6527a6eb6f6

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on tastyware/margin-estimator

Attestations:

File details

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

File metadata

File hashes

Hashes for margin_estimator-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2e2fcd7796de77b9c90acf2360080e4fb6987adb6eaa4250c8b0f8b367a997a8
MD5 665f791149c45d5a7ce17f8994a74812
BLAKE2b-256 65c29235b18e31d34d9c0d5aaca004f81d21f0ae931d282b047d93f125198f8b

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on tastyware/margin-estimator

Attestations:

Supported by

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