简体   繁体   English

ibpy 交互式经纪人的 python api 不适用于下订单

[英]ibpy interactive broker's python api not working for placing order

I have following sample code, when I try to run it for the first time, it worked:我有以下示例代码,当我第一次尝试运行它时,它起作用了:

from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order

def make_contract(symbol, sec_type, exch, prim_exch, curr):

    Contract.m_symbol = symbol
    Contract.m_secType = sec_type
    Contract.m_exchange = exch
    Contract.m_primaryExch = prim_exch
    Contract.m_currency = curr
    return Contract



def make_order(action,quantity, price = None):

    if price is not None:
        order = Order()
        order.m_orderType = 'LMT'
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_lmtPrice = price

    else:
        order = Order()
        order.m_orderType = 'MKT'
        order.m_totalQuantity = quantity
        order.m_action = action


    return order


cid = 100

while __name__ == "__main__":

    conn = Connection.create(port=7496, clientId=999)
    conn.connect()
    oid = cid
    cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
    offer = make_order('BUY', 1, 200)
    conn.placeOrder(oid, cont, offer)
    conn.disconnect()
    x = raw_input('enter to resend')
    cid += 1

as i run the script for the first time, the interface of IB pops up a window and says configuration information for paper trading from API.当我第一次运行脚本时,IB 的界面会弹出一个窗口,上面写着 API 模拟交易的配置信息。 However the second, third time I run it, the pop up information never shows up again which confuse me.然而,第二次,第三次我运行它时,弹出信息再也没有出现,这让我感到困惑。 Is there anything wrong here?这里有什么问题吗?

As already mentioned it should be if name == " main ":如前所述,它应该是 if name == " main ":

What you are doing there is running an infinite loop that connects to the IB API, places an order, disconnects and repeats the same process.你在那里做的是运行一个无限循环,连接到 IB API,下订单,断开连接并重复相同的过程。

The popup is probably one of the API warnings that once accepted doesn't show up again so that explains why you don't see it again.弹出窗口可能是 API 警告之一,一旦接受就不会再次出现,这就解释了为什么您再也看不到它。

Your order is quite likely placed and should show up in TWS unless it's resulting in an error which you won't see in TWS.您的订单很可能已下达并应显示在 TWS 中,除非它导致您在 TWS 中看不到的错误。

As others have alluded to what you need to do is firstly not use iPython notebook as it's not going to give you a good view of what's going on.正如其他人所提到的,您首先需要做的是不要使用 iPython notebook,因为它不会让您很好地了解正在发生的事情。 Change your code to look like this and you'll be able to see what's going on:将您的代码更改为如下所示,您将能够看到发生了什么:

from ib.opt import Connection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
import time

def make_contract(symbol, sec_type, exch, prim_exch, curr):

    Contract.m_symbol = symbol
    Contract.m_secType = sec_type
    Contract.m_exchange = exch
    Contract.m_primaryExch = prim_exch
    Contract.m_currency = curr
    return Contract


def make_order(action,quantity, price = None):

    if price is not None:
        order = Order()
        order.m_orderType = 'LMT'
        order.m_totalQuantity = quantity
        order.m_action = action
        order.m_lmtPrice = price

    else:
        order = Order()
        order.m_orderType = 'MKT'
        order.m_totalQuantity = quantity
        order.m_action = action


    return order


cid = 100

def handleAll(msg):
    print msg

if __name__ == "__main__":

    conn = Connection.create(port=7496, clientId=999)
    conn.connect()
    conn.registerAll(handleAll)
    oid = cid
    cont = make_contract('AAPL', 'STK', 'SMART', 'SMART', 'USD')
    offer = make_order('BUY', 1, 200)
    conn.placeOrder(oid, cont, offer)
    while 1:
        time.sleep(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM