Use alpaca instead of ibkr.

This commit is contained in:
2026-08-02 22:18:48 +03:00
parent a699bfc0fb
commit fddcc9190a
10 changed files with 692 additions and 11 deletions
+107
View File
@@ -0,0 +1,107 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "cd849b8e",
"metadata": {},
"source": [
"# IBKR scratch notebook\n",
"\n",
"This notebook is for quick manual testing of the IBKR gateway connection, portfolio lookup, and a simple SPY order flow.\n",
"\n",
"> Use this only with a paper-trading or test setup unless you explicitly intend to submit a live order.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "543426a2",
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"from pathlib import Path\n",
"\n",
"from ib_insync import IB, Stock\n",
"\n",
"# Allow the notebook to import local source code from the repository.\n",
"repo_root = Path.cwd().resolve()\n",
"if (repo_root / \"src\").exists():\n",
" repo_root = repo_root\n",
"else:\n",
" repo_root = repo_root.parent\n",
"\n",
"src_path = repo_root / \"src\"\n",
"if str(src_path) not in sys.path:\n",
" sys.path.insert(0, str(src_path))\n",
"\n",
"from trading_bot.data.fetch_ibkr_daily import (\n",
" IBKR_CLIENT_ID,\n",
" IBKR_CONNECT_TIMEOUT_SECONDS,\n",
" IBKR_HOST,\n",
" IBKR_PORT,\n",
")\n",
"\n",
"ib = IB()\n",
"print(f\"Connecting to IBKR Gateway at {IBKR_HOST}:{IBKR_PORT} with client id {IBKR_CLIENT_ID}...\")\n",
"ib.connect(IBKR_HOST, IBKR_PORT, clientId=IBKR_CLIENT_ID, timeout=IBKR_CONNECT_TIMEOUT_SECONDS)\n",
"print(\"Connection established.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecdc721f",
"metadata": {},
"outputs": [],
"source": [
"print(f\"Connected: {ib.isConnected()}\")\n",
"print(f\"Client ID: {ib.clientId}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10ebc240",
"metadata": {},
"outputs": [],
"source": [
"portfolio = ib.portfolio()\n",
"if not portfolio:\n",
" print(\"No open portfolio positions were returned.\")\n",
"else:\n",
" for item in portfolio:\n",
" print(\n",
" f\"{item.contract.symbol}: position={item.position}, \"\n",
" f\"market_value={item.marketValue}, unrealized_pnl={item.unrealizedPNL}\"\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fed654ec",
"metadata": {},
"outputs": [],
"source": [
"from ib_insync import MarketOrder\n",
"\n",
"contract = Stock(\"SPY\", \"SMART\", \"USD\")\n",
"ib.qualifyContracts(contract)\n",
"\n",
"# Adjust the quantity as needed before running this cell.\n",
"order = MarketOrder(\"BUY\", 1)\n",
"trade = ib.placeOrder(contract, order)\n",
"\n",
"print(f\"Submitted order for {contract.symbol}: {trade}\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}