Download TWS
https://www.interactivebrokers.com/en/trading/tws.php#tws-software
Download API
Download from the github directly.
https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/#find-the-api
API_Version=10.33.02
setup API
PS C:\TWS API\source\pythonclient> python setup.py install
python -m pip show ibapi
“Enable ActiveX and Socket Clients”. The other settings which affect the initial connection are:
The setting ‘Read-Only’ is enabled by default and will block all API orders. It must be unchecked to allow any orders from the API.
TWS uses socket port 7496 for live sessions and 7497 for paper sessions. IB Gateway by contrast uses 4001 for live sessions and 4002 for paper sessions.
Test
PS C:\TWS API\samples\Python\Testbed> python Program.py --port 7496
第一个函数是 ReqAccountUpdates。此函数会导致返回指定账户的头寸和账户信息。
Here is a short sample program using ReqAccountUpdates.
下面是一个使用 ReqAccountUpdates 的简短示例程序。
Notice it looks very similar to the previous programs. The only difference is the function we call here in the start function, ReqAccountUpdates, and then the callback functions we’ve overridden. The overridden functions handle return data or update portfolio, update account value, update account in time and account download end.
请注意,它看起来与前面的程序非常相似。唯一的区别是我们在 start 函数中调用的函数 ReqAccountUpdates,然后是我们覆盖的回调函数。覆盖的函数处理返回数据或更新投资组合、更新帐户值、及时更新帐户和帐户下载结束。
If you want to start a subscription ?, you invoke ReqAccountUpdates with true. If you want to cancel it or stop it you can call ReqAccountUpdates with false, which is what I’ll do in the stop function.
如果要启动订阅,请使用 true 调用 ReqAccountUpdates。如果要取消或停止订阅,可以使用 false 调用 ReqAccountUpdates,这就是我将在 stop 函数中执行的动作。
Since I’m already logged in the TWS, and listening on socket port 7497, I can just run this program and it should connect and then call ReqAccountUpdates, wait five seconds and the. print out all the results and then call the stop function.
由于我已经登录了 TWS,并侦听套接字端口 7497,因此我可以运行此程序,它应该连接,然后调用 ReqAccountUpdates,等待 5 秒钟,然后。打印出所有结果,然后调用 Stop 函数。
twsapi-doc
https://www.interactivebrokers.com/campus/ibkr-api-page/twsapi-doc/
PS C:\TWS_API\samples\Python\Testbed> pip install lightweight-charts
安装应在有无科学上网分别尝试,有时科学上网指定代理时可能安装失败,出现SSL问题。
Essential components of TWS API programs
https://www.interactivebrokers.com/campus/trading-lessons/essential-components-of-tws-api-programs/
C:\TWS_API\samples\Python\Testbed> & C:/Users/john/AppData/Local/Programs/Python/Python310/pythoon.eonoonoon.exe c:/TWS_API/samplon.exe c:/TWS_API/samples/Python/Testbed/essential_components.py
reqId: -1, errorCode: 1738479515810, errorString: 502, orderReject: Couldn’t connect to TWS. Confirm that “Enable ActiveX and Socket EClients”
is enabled and connection port is the same as “Socket Port” on the
TWS “Edit->Global Configuration…->API->Settings” menu. Live Trading ports:
TWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations
of version 954.1 or newer: TWS: 7497; IB Gateway: 4002
reqId: -1, errorCode: 1738479516824, errorString: 504, orderReject: Not connected
“Send instrument-specific attributes for dual-mode API Client in”. Specifying Operator Timezone returns your TWS time zone, Instrument time zone returns the time zone for the contract, and UTC is obviously the UTC time zone.