Prediction and model training work

This commit is contained in:
2026-08-06 17:22:57 +03:00
parent fddcc9190a
commit 465e09fc82
13 changed files with 1689 additions and 403 deletions
+141 -20
View File
@@ -17,7 +17,26 @@
"execution_count": null,
"id": "543426a2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connecting to IBKR Gateway at 127.0.0.1:4002 with client id 101...\n",
"Failed to connect to IBKR Gateway: This event loop is already running\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Error 200, reqId 9: No security definition has been found for the request, contract: Stock(symbol='VUAA', exchange='SMART', currency='USD')\n",
"Error 200, reqId 10: No security definition has been found for the request\n",
"Canceled order: Trade(contract=Stock(symbol='VUAA', exchange='SMART', currency='USD'), order=MarketOrder(orderId=10, clientId=101, action='BUY', totalQuantity=0.7), orderStatus=OrderStatus(orderId=10, status='Cancelled', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2026, 7, 30, 19, 36, 57, 539380, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2026, 7, 30, 19, 36, 57, 743208, tzinfo=datetime.timezone.utc), status='Cancelled', message='Error 200, reqId 10: No security definition has been found for the request', errorCode=200)], advancedError='')\n",
"Peer closed connection.\n"
]
}
],
"source": [
"import sys\n",
"from pathlib import Path\n",
@@ -35,36 +54,55 @@
"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",
"IBKR_HOST = \"127.0.0.1\"\n",
"IBKR_PORT = 4002\n",
"IBKR_CLIENT_ID = 101\n",
"IBKR_CONNECT_TIMEOUT_SECONDS = 10\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"
"try:\n",
" ib.connect(IBKR_HOST, IBKR_PORT, clientId=IBKR_CLIENT_ID, timeout=IBKR_CONNECT_TIMEOUT_SECONDS)\n",
" print(\"Connection established.\")\n",
"except Exception as e:\n",
" print(f\"Failed to connect to IBKR Gateway: {e}\")\n",
" ib.disconnect()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "ecdc721f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connected: True\n",
"Client ID: 101\n"
]
}
],
"source": [
"print(f\"Connected: {ib.isConnected()}\")\n",
"print(f\"Client ID: {ib.clientId}\")\n"
"print(f\"Client ID: {ib.client.clientId}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "10ebc240",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No open portfolio positions were returned.\n"
]
}
],
"source": [
"portfolio = ib.portfolio()\n",
"if not portfolio:\n",
@@ -79,27 +117,110 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"id": "fed654ec",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Unknown contract: Stock(symbol='VUAA', exchange='SMART', currency='USD')\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Submitted order for VUAA: Trade(contract=Stock(symbol='VUAA', exchange='SMART', currency='USD'), order=MarketOrder(orderId=10, clientId=101, action='BUY', totalQuantity=0.7), orderStatus=OrderStatus(orderId=10, status='PendingSubmit', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2026, 7, 30, 19, 36, 57, 539380, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0)], advancedError='')\n"
]
}
],
"source": [
"from ib_insync import MarketOrder\n",
"\n",
"contract = Stock(\"SPY\", \"SMART\", \"USD\")\n",
"ib.qualifyContracts(contract)\n",
"contract = Stock(\"VUAA\", \"SMART\", \"USD\")\n",
"await ib.qualifyContractsAsync(contract)\n",
"\n",
"# Adjust the quantity as needed before running this cell.\n",
"order = MarketOrder(\"BUY\", 1)\n",
"order = MarketOrder(\"BUY\", 0.70)\n",
"trade = ib.placeOrder(contract, order)\n",
"\n",
"print(f\"Submitted order for {contract.symbol}: {trade}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "06881101",
"metadata": {},
"outputs": [],
"source": [
"trds = ib.trades()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "08663602",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['Cancelled', 'Cancelled', 'Cancelled']"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda x: x.orderStatus.status, trds))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "2c7abf23",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Trade(contract=Stock(conId=756733, symbol='SPY', right='?', exchange='SMART', currency='USD', localSymbol='SPY', tradingClass='SPY'), order=Order(permId=1578393268, action='BUY', totalQuantity=1.0, orderType='MKT', lmtPrice=0.0, auxPrice=0.0, tif='DAY', ocaType=3, displaySize=2147483647, rule80A='0', openClose='', volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DUR281921', clearingIntent='IB', cashQty=0.0, dontUseAutoPriceForHedge=True, filledQuantity=0.0, refFuturesConId=2147483647, shareholder='Not an insider or substantial shareholder'), orderStatus=OrderStatus(orderId=0, status='Cancelled', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[], advancedError=''),\n",
" Trade(contract=Stock(conId=756733, symbol='SPY', right='?', exchange='SMART', currency='USD', localSymbol='SPY', tradingClass='SPY'), order=Order(permId=24474350, action='BUY', totalQuantity=0.1345, orderType='MKT', lmtPrice=0.0, auxPrice=0.0, tif='DAY', ocaType=3, displaySize=2147483647, rule80A='0', openClose='', volatilityType=0, deltaNeutralOrderType='None', referencePriceType=0, account='DUR281921', clearingIntent='IB', cashQty=0.0, dontUseAutoPriceForHedge=True, filledQuantity=0.0, refFuturesConId=2147483647, shareholder='Not an insider or substantial shareholder'), orderStatus=OrderStatus(orderId=0, status='Cancelled', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[], advancedError=''),\n",
" Trade(contract=Stock(symbol='VUAA', exchange='SMART', currency='USD'), order=MarketOrder(orderId=10, clientId=101, action='BUY', totalQuantity=0.7), orderStatus=OrderStatus(orderId=10, status='Cancelled', filled=0.0, remaining=0.0, avgFillPrice=0.0, permId=0, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld='', mktCapPrice=0.0), fills=[], log=[TradeLogEntry(time=datetime.datetime(2026, 7, 30, 19, 36, 57, 539380, tzinfo=datetime.timezone.utc), status='PendingSubmit', message='', errorCode=0), TradeLogEntry(time=datetime.datetime(2026, 7, 30, 19, 36, 57, 743208, tzinfo=datetime.timezone.utc), status='Cancelled', message='Error 200, reqId 10: No security definition has been found for the request', errorCode=200)], advancedError='')]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trds"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.15"
}
},
"nbformat": 4,