Skip to main content

Options Forecasting and Trading Framework

Project description

OpTrade

OpTrade is a complete toolkit for quantitative research and development of options trading strategies. By abstracting away the complexity of data handling and experimental setup, researchers and traders can focus on what matters most: developing and testing alpha-generating ideas.

OpTrade Framework

Installation

The recommended way to install OptTrade is via pip:

pip install optrade

Example (Single Contract)

# Step 1: Find and initialize the optimal contract
from optrade.data import Contract

contract = Contract.find_optimal(
    root="AAPL",
    start_date="20230103",  # First trading day of 2023
    target_tte=30,          # Desired expiration: 30 days
    tte_tolerance=(20, 40), # Min 20, max 40 days expiration
    interval_min=1,         # Data requested at 1-min level
    moneyness="ATM",        # At-the-money option
)

# Step 2: Load market data (NBBO quotes and OHLCV)
df = contract.load_data()

# Step 3: Transform raw data into ML-ready features
from optrade.data.features import transform_features

data = transform_features(
    df=df,
    core_feats=[
        "option_returns",     # Option price returns
        "stock_returns",      # Underlying stock returns
        "moneyness",          # Log(S/K)
        "option_lob_imbalance", # Order book imbalance
        "stock_quote_spread", # Bid-ask spread normalized
    ],
    tte_feats=["sqrt", "exp_decay"],  # Time-to-expiration features
    datetime_feats=["minute_of_day", "hour_of_week"],  # Time features
    strike=contract.strike,
    exp=contract.exp,
).values

# Step 4: Create dataset for time series forecasting
from optrade.data.forecasting import ForecastingDataset
from torch.utils.data import DataLoader

torch_dataset = ForecastingDataset(
    data=data,
    seq_len=100,        # 100-minute lookback window
    pred_len=10,        # 10-minute forecast horizon
    target_channels=[0],  # Forecast option returns (first column)
)

torch_loader = DataLoader(torch_dataset)

Overview

🔄 Data Pipeline OpTrade integrates with ThetaData's API for affordable options and security data access (down to 1-min resolution). The framework processes NBBO quotes and OHLCVC metrics through a contract selection system optimizing for moneyness, expiration windows, and volatility-scaled strikes.

🌐 Market Environments Built-in market environments enable precise universe selection through multifaceted filtering. OpTrade supports composition by major indices, fundamental-based screening (e.g., PE ratio, market cap), and Fama-French model categorization.

🧪 Experimental Pipeline The experimentation framework supports PyTorch and scikit-learn for options forecasting with online Neptune logging, hyperparameter tuning, and model version control, supporting both online and offline experiment tracking.

🧮 Featurization OpTrade provides option market features including mid-price derivations, order book imbalance metrics, quote spreads, and moneyness calculations. Time-to-expiration transformations capture theta decay effects, while datetime features extract cyclical market patterns for intraday seasonality.

🤖 Models OpTrade includes several off-the-shelf PyTorch and scikit-learn models, including state-of-the-art architectures for time series forecasting alongside tried and true machine learning methods.

Advanced Usage

Multi-contract Data

# Step 1: Find optimal contracts within date range
from optrade.data import ContractDataset

contract_dataset = ContractDataset(
    root="AMZN",
    total_start_date="20220101",
    total_end_date="20220301",
    contract_stride=1,
    interval_min=1,
    right="P",
    target_tte=3,
    tte_tolerance=(1,10),
    moneyness="ITM",
    target_band=0.05,
    volatility_scaled=True,
    volatility_scalar=0.1,
    hist_vol=0.1117,
)
contract_dataset.generate()

# Step 2: Transform market data into ML-ready dataset
from optrade.data.torch import get_forecasting_dataset
from torch.utils.data import DataLoader

torch_dataset = get_forecasting_dataset(
    contracts=contract_dataset,
    core_feats=["option_returns"],
    tte_feats=["sqrt"],
    datetime_feats=["sin_minute_of_day"],
    tte_tolerance=(25, 35),
    seq_len=100,
    pred_len=10,
    verbose=True
)
torch_loader = DataLoader(torch_dataset)

Documentation

This project includes extensive documentation that is essential for understanding the framework. Users are strongly encouraged to review these documents before usage.

Document Description
DATA.md Information on the comprehensive data pipeline
FEATURES.md Details on the selection of important predictors for option forecasting

Contact

For queries, please contact: xmootoo at gmail dot com.

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

optrade-0.0.2.tar.gz (6.5 MB view details)

Uploaded Source

Built Distribution

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

optrade-0.0.2-py3-none-any.whl (6.5 MB view details)

Uploaded Python 3

File details

Details for the file optrade-0.0.2.tar.gz.

File metadata

  • Download URL: optrade-0.0.2.tar.gz
  • Upload date:
  • Size: 6.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/23.2.0

File hashes

Hashes for optrade-0.0.2.tar.gz
Algorithm Hash digest
SHA256 fa98c448cdde5ead1986ea015dae8bbf056deef5c0f7e7af9b66b67c6443505b
MD5 ea573be8ef3256236a9df5f37544072b
BLAKE2b-256 214517190a989e5df4e044dfad076b4faa5b696e3303128a7c81057fbcfe82f1

See more details on using hashes here.

File details

Details for the file optrade-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: optrade-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/23.2.0

File hashes

Hashes for optrade-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 87562d634816a36755a7a5842b76f0d4f6407085eea71196c4a95f668bba47a2
MD5 92b0db414e396b10f92b930b6c32a436
BLAKE2b-256 d55fe1e46e081b55c6851fae7cd6bf2c002905fd0e1dfdbacde6f63360517858

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