Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ibpy互动经纪人&x27;s python api不适用于下单_Python_Api_Interactive Brokers - Fatal编程技术网

ibpy互动经纪人&x27;s python api不适用于下单

ibpy互动经纪人&x27;s python api不适用于下单,python,api,interactive-brokers,Python,Api,Interactive Brokers,我有以下示例代码,当我第一次尝试运行它时,它起了作用: 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

我有以下示例代码,当我第一次尝试运行它时,它起了作用:

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

当我第一次运行脚本时,IB的界面会弹出一个窗口,并显示API中纸质交易的配置信息。然而,当我第二次、第三次运行它时,弹出信息再也不会出现,这让我很困惑。这里有什么问题吗?

如前所述,如果name==“main”:

您在那里所做的是运行一个无限循环,该循环连接到ibapi,下订单,断开连接并重复相同的过程

弹出窗口可能是一个API警告,一旦被接受,就不会再次出现,这就解释了为什么您不会再次看到它

您的订单很可能已下好,并且应该显示在TWS中,除非它导致您在TWS中看不到的错误

正如其他人提到的,你需要做的是首先不要使用iPython笔记本,因为它不会让你很好地了解正在发生的事情。将代码更改为如下所示,您将能够看到发生了什么:

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)

如果uuu name uuu==“uuuu main uuuuuu”:,它不应该是
。我想知道如果使用while循环会发生什么?我不知道足够的python来回答这个问题,但您应该始终注册一个错误处理程序。我会使用一个只打印消息的处理程序来进行注册。然后你就可以看到问题了。以其他ibpy问题为例。如果我不得不猜测,您可能需要在循环中使用
全局cid
声明。您必须跟踪一生中使用的最后一个cid,并且始终递增1,因此100也需要更改。我使用的是iPython,这意味着我可以一次一行地运行每一行代码,实际上我不是在终端中编译它,而是使用iPython并一次运行一行,因此问题不在于if name==“main”我确信这一点,我正在尝试你的代码,它是有效的,但只适用于第一批订单。在TWS上,它显示了AAPL购买200股股票的记录,但当我将AAPL更改为MSFT并再次执行它时,它没有显示第二个记录。我确实将cid从100更改为500,它显示服务器版本:76 TWS连接时间:20170117 08:55:14 EST好的,我想我解决了问题,因为我没有给时间。sleep()在这些代码行之间。记住它在本地/TWS服务器之间的请求/响应,在任何东西连接之前,请务必给予一些缓解时间。您只需获取下一个验证消息并使用它,而不是一直更改cid。而且,一旦你收到这个信息,你就知道你已经接通了,不需要睡觉了。