Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# Project 1
This is a thoughtful write-up about my project...
This is a thoughtful write-up about my project...
## IBKR to OpenAI Operator integration

A sample Python script is provided under `scripts/ibkr_to_operator.py`. It pulls historical data from Interactive Brokers and sends it to an OpenAI Operator endpoint.

### Setup

1. Install Python 3.
2. Install dependencies:
```bash
pip install ib_insync requests
```
3. Edit `scripts/ibkr_to_operator.py` and replace the placeholder values:
- `IB_HOST`, `IB_PORT`, `IB_CLIENT_ID` – connection details for TWS or IB Gateway.
- `STOCK_SYMBOL`, `STOCK_EXCHANGE`, `STOCK_CURRENCY` – the contract to query.
- `OPERATOR_ENDPOINT` – your OpenAI Operator URL.
- `OPERATOR_API_KEY` – API key for the operator (if required).

### Running the script

Start TWS or IB Gateway and then run:
```bash
python scripts/ibkr_to_operator.py
```
The script prints the number of retrieved bars and the operator's response code.
57 changes: 57 additions & 0 deletions scripts/ibkr_to_operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""Fetch IBKR data and send to an OpenAI Operator."""

from ib_insync import IB, Stock
import requests
import json

# === Configuration Parameters ===
# Replace the values below with your own settings.

IB_HOST = "127.0.0.1" # TODO: Host where TWS or IB Gateway runs
IB_PORT = 7497 # TODO: API port for TWS or IB Gateway
IB_CLIENT_ID = 1 # TODO: Any integer; must be unique per client

STOCK_SYMBOL = "AAPL" # TODO: Ticker symbol
STOCK_EXCHANGE = "SMART" # TODO: Exchange code
STOCK_CURRENCY = "USD" # TODO: Currency code

OPERATOR_ENDPOINT = "http://YOUR_OPERATOR_ENDPOINT/ingest" # TODO: Operator URL
OPERATOR_API_KEY = None # TODO: Operator API key if required


def fetch_data():
"""Connect to IBKR and retrieve historical data."""
ib = IB()
ib.connect(IB_HOST, IB_PORT, clientId=IB_CLIENT_ID)
contract = Stock(STOCK_SYMBOL, STOCK_EXCHANGE, STOCK_CURRENCY)
bars = ib.reqHistoricalData(
contract,
endDateTime="",
durationStr="1 D",
barSizeSetting="1 min",
whatToShow="TRADES",
useRTH=True,
formatDate=1,
)
data = [bar.__dict__ for bar in bars]
ib.disconnect()
return data


def send_to_operator(data):
"""Send the fetched data to the OpenAI Operator."""
headers = {"Content-Type": "application/json"}
if OPERATOR_API_KEY:
headers["Authorization"] = f"Bearer {OPERATOR_API_KEY}"
response = requests.post(OPERATOR_ENDPOINT, json=data, headers=headers, timeout=30)
print(f"Operator response: {response.status_code} {response.text}")


def main():
data = fetch_data()
print(f"Retrieved {len(data)} bars from IBKR")
send_to_operator(data)


if __name__ == "__main__":
main()