Initial release
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
# IBKR Futures Forward Curves
|
||||
|
||||
This tool creates PNG charts and CSV snapshots for listed commodity futures from Interactive Brokers (IBKR).
|
||||
|
||||
The tool reads contract details and market data from Trader Workstation (TWS) or IB Gateway.
|
||||
|
||||
It does not submit, modify, or cancel orders.
|
||||
|
||||
## Features
|
||||
|
||||
- Discovers listed futures contracts through a configurable horizon.
|
||||
- Supports energy, metal, grain, and soft-commodity roots.
|
||||
- Tries configured exchange alternatives when the primary exchange returns no contracts.
|
||||
- Prefers valid bid-and-ask midpoints over other snapshot prices.
|
||||
- Uses recent daily closes when snapshots contain no usable prices.
|
||||
- Writes one chart and an optional CSV file.
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.10 or later.
|
||||
- TWS or IB Gateway with API socket access enabled.
|
||||
- IBKR market-data permissions for each requested futures market.
|
||||
|
||||
The package requires `pandas`, `matplotlib`, and `ib_async`. The program does not read API keys or brokerage passwords.
|
||||
|
||||
## Install
|
||||
|
||||
Create a virtual environment:
|
||||
|
||||
```powershell
|
||||
py -3.10 -m venv .venv
|
||||
```
|
||||
|
||||
Install the dependencies on Windows:
|
||||
|
||||
```powershell
|
||||
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
|
||||
```
|
||||
|
||||
For macOS or Linux, use `.venv/bin/python` in the command.
|
||||
|
||||
## Prepare IBKR
|
||||
|
||||
1. Start TWS or IB Gateway.
|
||||
2. Sign in to the applicable paper or live account.
|
||||
3. Enable API socket access.
|
||||
4. Confirm that the selected API port matches the command options.
|
||||
|
||||
The default connection uses `127.0.0.1`, paper-trading port `7497`, and client ID `19`.
|
||||
|
||||
## Run
|
||||
|
||||
Create curves for the default commodity set:
|
||||
|
||||
```powershell
|
||||
.\.venv\Scripts\python.exe scripts\live\plot_ibkr_forward_curve.py
|
||||
```
|
||||
|
||||
Create curves for selected roots:
|
||||
|
||||
```powershell
|
||||
.\.venv\Scripts\python.exe scripts\live\plot_ibkr_forward_curve.py --symbols WTI GC ZC
|
||||
```
|
||||
|
||||
Request delayed data and limit each root to six contracts:
|
||||
|
||||
```powershell
|
||||
.\.venv\Scripts\python.exe scripts\live\plot_ibkr_forward_curve.py --market-data-type delayed --max-contracts-per-symbol 6
|
||||
```
|
||||
|
||||
Use `--help` to see all connection, market-data, horizon, and output options.
|
||||
|
||||
## Default markets
|
||||
|
||||
| Root | Commodity | Primary exchange | Exchange alternative |
|
||||
|---|---|---|---|
|
||||
| `WTI` | WTI light sweet crude oil | NYMEX | The program requests IBKR root `CL` |
|
||||
| `BZ` | Brent crude oil | NYMEX | None |
|
||||
| `NG` | Henry Hub natural gas | NYMEX | None |
|
||||
| `GC` | Gold | COMEX | None |
|
||||
| `SI` | Silver | COMEX | None |
|
||||
| `PL` | Platinum | NYMEX | None |
|
||||
| `PA` | Palladium | NYMEX | None |
|
||||
| `HG` | Copper | COMEX | None |
|
||||
| `ZW` | Chicago wheat | CBOT | ECBOT |
|
||||
| `ZC` | Corn | CBOT | ECBOT |
|
||||
| `ZS` | Soybeans | CBOT | ECBOT |
|
||||
| `SB` | Sugar No. 11 | NYBOT | ICEUS |
|
||||
|
||||
Use `--exchange-map ROOT=EXCHANGE` to replace a primary exchange.
|
||||
|
||||
## Price selection
|
||||
|
||||
The program selects the first usable price in this order:
|
||||
|
||||
1. The midpoint of a positive bid and ask, when the ask is not below the bid.
|
||||
2. The price that `ib_async` returns from `marketPrice()`.
|
||||
3. A positive last price.
|
||||
4. A positive previous close.
|
||||
|
||||
The default `auto` mode requests live data first and delayed data second. Sugar uses delayed data by default.
|
||||
|
||||
When all snapshots fail, the program requests recent daily trade bars. Use `--no-historical-fallback` to disable this request.
|
||||
|
||||
## Outputs
|
||||
|
||||
The default output directory is `data/processed/futures_forward_curves/`. The file names contain the local run date.
|
||||
|
||||
The CSV contains these fields:
|
||||
|
||||
- Root, commodity group, display label, and exchange.
|
||||
- Local contract symbol, contract month, expiry, and contract ID.
|
||||
- Selected price, source, date, and market-data type.
|
||||
- Available bid, ask, last, close, and volume values.
|
||||
|
||||
Use `--output` to select the PNG path. Use `--csv` to select the CSV path, or use `--no-csv`.
|
||||
|
||||
## Important limitations
|
||||
|
||||
Contract definitions, exchange identifiers, and entitlements can differ by account. Review skipped-market messages before you use an output.
|
||||
|
||||
The program treats a month-only IBKR expiry as the final calendar day of that month. IBKR can define an earlier last-trade date.
|
||||
|
||||
CAUTION: Verify each contract and price before you use an output. Market data can be delayed, stale, incomplete, or unavailable.
|
||||
|
||||
This software supports research and monitoring. It does not provide investment advice or trade execution.
|
||||
Reference in New Issue
Block a user