-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathuse_async.py
More file actions
82 lines (62 loc) · 2.87 KB
/
Copy pathuse_async.py
File metadata and controls
82 lines (62 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Example usage of the async REST session with httpx."""
import asyncio
from configparser import ConfigParser
from pprint import pprint
from ibc.async_session import AsyncInteractiveBrokersSession
from ibc.client import InteractiveBrokersClient
config = ConfigParser()
config.read('config/config.ini')
account_number = config.get('interactive_brokers_paper', 'paper_account')
ibc_client = InteractiveBrokersClient(
account_number=account_number,
)
async def main():
"""Demonstrate async REST requests to the IB Client Portal API."""
# -----------------------------------------------------------------------
# Initialize the async session as a context manager.
# -----------------------------------------------------------------------
async with AsyncInteractiveBrokersSession(ib_client=ibc_client) as session:
# -------------------------------------------------------------------
# Fetch portfolio accounts.
# -------------------------------------------------------------------
accounts = await session.make_request(
method='get',
endpoint='/api/portfolio/accounts'
)
pprint(accounts)
# Output: [{'accountId': 'U1234567', 'type': 'INDIVIDUAL', ...}]
# -------------------------------------------------------------------
# Get a market data snapshot for AAPL (conid 265598).
# -------------------------------------------------------------------
snapshot = await session.make_request(
method='get',
endpoint='/api/iserver/marketdata/snapshot',
params={'conids': '265598', 'fields': '31,84,86'}
)
pprint(snapshot)
# Output: [{'conid': 265598, '31': '150.25', '84': '151.00', ...}]
# -------------------------------------------------------------------
# Search for a contract by symbol.
# -------------------------------------------------------------------
search = await session.make_request(
method='post',
endpoint='/api/iserver/secdef/search',
json_payload={'symbol': 'AAPL', 'name': True}
)
pprint(search)
# Output: [{'conid': 265598, 'companyName': 'APPLE INC', ...}]
# -------------------------------------------------------------------
# Multiple concurrent requests using asyncio.gather.
# -------------------------------------------------------------------
news_task = session.make_request(method='get', endpoint='/api/iserver/news/top')
pnl_task = session.make_request(
method='get',
endpoint='/api/iserver/account/pnl/partitioned'
)
news, pnl = await asyncio.gather(news_task, pnl_task)
print("\n--- Top News ---")
pprint(news)
print("\n--- PnL ---")
pprint(pnl)
if __name__ == '__main__':
asyncio.run(main())