python - IB Python API - 下订单
问题描述
我想使用本地 IB Python API 下订单。
我为此使用的代码如下:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import *
from threading import Timer
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)
def nextValidId(self, orderId):
self.nextOrderId = orderId
self.start()
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId,
whyHeld, mktCapPrice):
print('OrderStatus. Id: ', orderId, 'Status: ', status, 'Filled: ', filled, 'Remaining: ', remaining,
'LastFillPrice: ', lastFillPrice)
def openOrder(self, orderId, contract, order, orderState):
print('openOrder id:', orderId, contract.symbol, contract.secType, '@', contract.exchange, ':', order.action,
order.orderType, order.totalQuantity, orderState.status)
def execDetails(self, reqId, contract, execution):
print('Order Executed: ', reqId, contract.symbol, contract.secType, contract.currency, execution.execId,
execution.orderId, execution.shares, execution.lastLiquidity)
def start(self):
contract = Contract()
contract.symbol = "DAI"
contract.secType = 'STK'
contract.exchange = 'SMART'
contract.currency = "EUR"
contract.primaryExchange = "SMART"
order = Order()
order.action = "Buy"
order.totalQuantity = 1
order.orderType = "MKT"
order.lmtPrice = 99
def stop(self):
self.done = True
self.disconnect()
def main():
app = TestApp()
app.nextOrderId = 1
app.connect('127.0.0.1', 7497, 123)
Timer(3, app.stop).start()
app.run()
if __name__ == "__main__":
main()
我得到的输出是:
Error: -1 2104 Market data farm connection is OK:usfarm.nj
Error: -1 2104 Market data farm connection is OK:eufarm
Error: -1 2106 HMDS data farm connection is OK:euhmds
Error: -1 2106 HMDS data farm connection is OK:fundfarm
Error: -1 2106 HMDS data farm connection is OK:ushmds
Error: -1 2158 Sec-def data farm connection is OK:secdefeu
但是没有下订单。
我已将 ibapi 文件夹放在我的工作目录中并登录到我的 TWS 模拟交易账户。
为什么没有下订单?
解决方案
你不下订单。 https://interactivebrokers.github.io/tws-api/order_submission.html
下单后添加 self.placeOrder(self.nextOrderId, contract, order)。
不要忘记 nextOrderId += 1。
推荐阅读
- apache-spark - Spark 加入性能问题
- c++ - 模棱两可的函数调用:将模板参数传递给许多覆盖函数之一
- r - 将 for 循环中的多个数据帧保存为单独的 CSV
- python - 如何在类中包含函数属性?
- postgresql - Sequelize:如何对包含的第 3 级模型进行 COUNT
- jquery - 在使用 Django i18n 模式时,在将 POST 方法的 AJAX 调用转换为 GET 方法期间
- python-3.x - 如何在熊猫数据框中将“一个热编码”列名从字节解码为字符串
- javascript - JSON中有重复的内容,我想找到更好的方法来编辑JSON
- c# - 如果元素位于页面的用户控件中,如何访问元素并输入键?
- python - 如何在气流中使用 s3 挂钩