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
View File
+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
}
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -1,5 +1,5 @@
{
"p_base": 0.5830346475507766,
"p_base": 0.5833333333333334,
"feature_cols": [
"VIX_rank_20",
"TLT_ret_10",
@@ -9,5 +9,5 @@
"SPY_ret_20",
"SPY_dist_sma50"
],
"last_trained_date": "2026-07-17 00:00:00"
"last_trained_date": "2026-07-24 00:00:00"
}
+5 -4
View File
@@ -20,7 +20,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
@@ -52,7 +52,8 @@
"\n",
"\n",
"PROJECT_ROOT = find_project_root()\n",
"RAW_DATA_DIR = PROJECT_ROOT / \"data\" / \"ibkr\" / \"daily\"\n",
"#RAW_DATA_DIR = PROJECT_ROOT / \"data\" / \"ibkr\" / \"daily\"\n",
"RAW_DATA_DIR = PROJECT_ROOT / \"data\" / \"alpaca\" / \"daily\"\n",
"OUTPUT_PATH = PROJECT_ROOT / \"data\" / \"training\" / \"spy_direction_5d.parquet\"\n",
"\n",
"SPY_SYMBOL = \"SPY\"\n",
@@ -233,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [
{
@@ -383,7 +384,7 @@
"df[\"TLT_ret_10\"] = df[\"TLT_close\"].pct_change(10)\n",
"df[\"USO_ret_5\"] = df[\"USO_close\"].pct_change(5)\n",
"df[\"SPY_TLT_ratio_ret\"] = (df[\"SPY_close\"] / df[\"TLT_close\"]).pct_change(5)\n",
"\n",
"df_model\n",
"spy_forward_close = df[\"SPY_close\"].shift(-5)\n",
"df[\"spy_up_5d\"] = np.nan\n",
"df.loc[spy_forward_close > df[\"SPY_close\"], \"spy_up_5d\"] = 1.0\n",