From 9ae68c684341924dfb6967d45ee25293105f7714 Mon Sep 17 00:00:00 2001 From: Kelvin FH Loh Date: Sat, 7 Jun 2025 14:23:05 -0700 Subject: [PATCH] Add IBKR to OpenAI Operator script --- README.md | 26 ++++++++++++++++- scripts/ibkr_to_operator.py | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 scripts/ibkr_to_operator.py diff --git a/README.md b/README.md index 9fc6b2f..51a11ba 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # Project 1 -This is a thoughtful write-up about my project... \ No newline at end of file +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. diff --git a/scripts/ibkr_to_operator.py b/scripts/ibkr_to_operator.py new file mode 100644 index 0000000..3291be9 --- /dev/null +++ b/scripts/ibkr_to_operator.py @@ -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()