Skip to content

bryanlear/options-trading-schwab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Options Trading Schwab - ThinkOrSwim

Fetch options data

python option_chain_fetch.py AAPL --strike-count 10 --from-date 2025-10-11 --to-date 2025-10-31 > greeks/chain.json
python option_contract_fetch.py AAPL 2025-10-17 150 --type CALL

python option_chain_fetch.py AAPL --strike-count 10 --from-date 2025-10-11 --to-date 2025-10-31 > greeks/chain.json

  • option_chain_fetch.py --> pulls full options chains with bid/ask/last/sizes/volume, open interest, implied vol, quote/trade timestamps, venue --> returns JSON payload
  • option_contract_fetch.pyu --> pulls 1 call or put contract

e.g., Apple Oct 11 - 31 contracts:

...
 },
    {
      "symbol": "AAPL  251017C00240000",
      "type": "CALL",
      "expirationDate": "2025-10-17T20:00:00.000+00:00",
      "strike": 240.0,
      "bid": 7.5,
      "ask": 7.95,
      "last": 7.66,
      "bidSize": 10,
      "askSize": 10,
      "lastSize": 1,
      "openInterest": 27160,
      "volume": 3018,
      "impliedVolatility": 32.167,
      "quoteTime": "2025-10-10T20:00:01.195000Z",
      "tradeTime": "2025-10-10T19:59:55.876000Z",
      "venue": "OPR"
    },
    {
    ...

Compute greeks for options data:

cd greeks
python compute_greeks.py chain.json --output-file greeks.json

e.g.,

...
 },
    {
      "symbol": "AAPL  251017C00240000",
      "type": "CALL",
      "expiration": "2025-10-17",
      "strike": 240.0,
      "price": 7.725,
      "timeToExpiry": 0.018600720118608513,
      "forward": 245.27,
      "discountFactor": 1.0,
      "impliedVol": 0.349978867708856,
      "smileVol": 0.3284473732023895,
      "quoteTime": "2025-10-10T20:00:01.195000+00:00",
      "tradeTime": "2025-10-10T19:59:55.876000+00:00",
      "venue": "OPR",
      "greeks": {
        "delta_forward": 0.6940236004099168,
        "delta_spot": 0.6940236004099168,
        "gamma": 0.03192670231559985,
        "vega": 11.733794448835665,
        "theta": -103.596364545083,
        "vanna": -0.4939333736457969,
        "vomma": 8.381702060019615,
        "rho": 3.0273164222160327,
        "phi": -3.1662735144604666
      }
    },
    ...

Realized-vol estimates: measures magnitude risk

  • Let underlying return be $r_t = \ln(S_t/S_{t-1})$
  • Directional risk dependent on sign of $r_t$
  • Magnitude risk depends on $|r_t|$ i.e. extent of movement
  • Magnitude risk = exposure to variance $\sigma^2 = \mathbb{E}[(r - \mathbb{E}[r])^2]$
  • Option portfolios’ gamma and vega scale with such variance: $dV \approx \tfrac12\Gamma,dS^2 + \text{Vega},d\sigma$
  • Thus $dS^2$ (price-movement magnitude) drives P&L even if mean change $=0$

Test

python greeks/fetch_realized_inputs.py AAPL --session-date 2025-10-10 --intraday-days 5 --daily-days 252 --output greeks/test_inputs.json

python3 greeks/test_realized_vol.py greeks/test_inputs.json --horizon 5

output: [INFO] Forecasted sigma for 5 days: 0.250155


Pipeline:

python greeks/fetch_realized_inputs.py AAPL --session-date 2025-10-10 --intraday-days 5 --daily-days 252 --output greeks/test_inputs.json
python greeks/run_realized_vol.py --inputs greeks/test_inputs.json --greeks greeks/greeks.json --horizon 5 --pretty

Driver writes payload to greeks/test_inputs_forecast.json (to override --output) and also prints it. The JSON contains the variance forecast --> decomposition of intraday/overnight contributions and gamma/vega scaling for each contract when greeks are provided

e.g., output:

{
  "sigma": 0.2501552855962114,
  "variance": 0.0012416203752325815,
  "horizonDays": 5,
  "intraday": {
    "timestamp": "2025-10-09T19:59:00+00:00",
    "nowcast": 6.781753552132297e-05,
    "partial": 6.781753552132297e-05,
    "remaining": 0.0
  },
  "dailyForecast": 0.0002455981741660474,
  "overnightToday": 9.773909119749454e-07,
  "pathVariance": 0.0009823926966641896,
  "overnightVariance": 0.00019043275213509393,
  "optionImpact": {
    "symbol": "AAPL",
    "spot": 245.27,
    "variance": 0.0012416203752325815,
    "sigma": 0.2501552855962114,
    "portfolio": {
      "gammaVariance": 53.06882710540773,
      "vegaVol": 230.36767267915624
    },
    "portfolioRaw": {
      "delta_forward": -1.1936202676903997,
      "delta_spot": -1.1936202676903997,
      "gamma": 1.4209925202020435,
      "vega": 920.8986815133885,
      "theta": -4307.109739806376,
      "vanna": 6.901689097678868,
      "vomma": 985.1108328654229,
      "rho": -29.151614247817207,
      "phi": 11.127852519531807
    },
    "contracts": [
      {
        "symbol": "AAPL  251017C00235000",
        "type": "CALL",
        "expiration": "2025-10-17",
        "strike": 235.0,
        "price": 11.675,
        "timeToExpiry": 0.018497897288767204,
        "forward": 245.27,
        "discountFactor": 1.0,
        "impliedVol": 0.37837788840613884,
        "smileVol": 0.32095808297865347,
        "quoteTime": "2025-10-10T20:00:01.183000+00:00",
        "tradeTime": "2025-10-10T19:57:02.404000+00:00",
        "venue": "OPR",
        "greeks": {
          "delta_forward": 0.8417566050561172,
          "delta_spot": 0.8417566050561172,
          "gamma": 0.022561523385068453,
          "vega": 8.05801299940402,
          "theta": -69.90752420482671,
          "vanna": -0.7210454969622722,
          "vomma": 24.09392179327854,
          "rho": 3.61228137781403,
          "phi": -3.819032265855079
        },
        "riskScaling": {
          "gammaVariance": 0.8425896454307664,
          "vegaVol": 2.015754543203897
        }
      },
      {

About

CLI utilities for options trading

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages