get_historical_data
Retrieve historical market data for financial instruments by specifying contract details, time periods, and bar sizes to analyze past performance.
Instructions
Get historical bar data.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contract_type | Yes | ||
| symbol | Yes | ||
| exchange | No | SMART | |
| currency | No | USD | |
| duration | No | Duration (e.g., '1 D', '1 W', '1 M', '1 Y') | 1 D |
| bar_size | No | Bar size (e.g., '1 min', '5 mins', '1 hour', '1 day') | 1 hour |
| what_to_show | No | Data type: TRADES, MIDPOINT, BID, ASK | TRADES |
| use_rth | No | Regular trading hours only |
Implementation Reference
- src/ib_async_mcp/server.py:579-602 (handler)The handler implementation for the "get_historical_data" tool, which creates a contract, qualifies it, and requests historical bar data from the IB gateway.
if name == "get_historical_data": contract = create_contract( args["contract_type"], symbol=args["symbol"], exchange=args.get("exchange", "SMART"), currency=args.get("currency", "USD"), ) await ib.qualifyContractsAsync(contract) bars = await ib.reqHistoricalDataAsync( contract, endDateTime="", durationStr=args.get("duration", "1 D"), barSizeSetting=args.get("bar_size", "1 hour"), whatToShow=args.get("what_to_show", "TRADES"), useRTH=args.get("use_rth", True), ) return [{ "date": b.date.isoformat() if hasattr(b.date, 'isoformat') else str(b.date), "open": b.open, "high": b.high, "low": b.low, "close": b.close, "volume": b.volume, } for b in bars] - src/ib_async_mcp/server.py:220-236 (registration)Registration of the "get_historical_data" tool in the list_tools() function.
name="get_historical_data", description="Get historical bar data.", inputSchema={ "type": "object", "properties": { "contract_type": {"type": "string"}, "symbol": {"type": "string"}, "exchange": {"type": "string", "default": "SMART"}, "currency": {"type": "string", "default": "USD"}, "duration": {"type": "string", "default": "1 D", "description": "Duration (e.g., '1 D', '1 W', '1 M', '1 Y')"}, "bar_size": {"type": "string", "default": "1 hour", "description": "Bar size (e.g., '1 min', '5 mins', '1 hour', '1 day')"}, "what_to_show": {"type": "string", "default": "TRADES", "description": "Data type: TRADES, MIDPOINT, BID, ASK"}, "use_rth": {"type": "boolean", "default": True, "description": "Regular trading hours only"}, }, "required": ["contract_type", "symbol"], }, ),