错误定义的对象。从库中导入对象时出现问题
Incorrectly defined object. Problem with importing an object from the library
这是使用 Python 在 IB 上下订单的代码。此代码有效,但出现一个错误。最后尝试下单,却报错:
Traceback (most recent call last):
Getting the time from the server...
File "C:/Users/B/PycharmProject/1/api1.py", line 117, in <module>
order1 = order.Order()
AttributeError: type object 'Order' has no attribute 'Order'
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfarm.nj
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfuture
IB error id -1 errorcode 2104 string Market data farm connection is OK:cashfarm
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfarm
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:ushmds.us
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:ilhmds
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:njhmds
1544354853
我猜问题出在第5行和第6行。当我删除它们时,我得到 "name 'order' is not defined"。我想我只是错误地定义了它。也许有人遇到过类似的 problem/error?
from ibapi.wrapper import EWrapper
from ibapi.client import EClient
from threading import Thread
import queue
from ibapi.contract import Contract as contract
from ibapi.order import Order as order
class TestWrapper(EWrapper):
"""
The wrapper deals with the action coming back from the IB gateway or TWS instance
We override methods in EWrapper that will get called when this action happens, like currentTime
"""
## error handling code
def init_error(self):
error_queue=queue.Queue()
self._my_errors = error_queue
def get_error(self, timeout=5):
if self.is_error():
try:
return self._my_errors.get(timeout=timeout)
except queue.Empty:
return None
return None
def is_error(self):
an_error_if=not self._my_errors.empty()
return an_error_if
def error(self, id, errorCode, errorString):
## Overriden method
errormsg = "IB error id %d errorcode %d string %s" % (id, errorCode, errorString)
self._my_errors.put(errormsg)
## Time telling code
def init_time(self):
time_queue=queue.Queue()
self._time_queue = time_queue
return time_queue
def currentTime(self, time_from_server):
## Overriden method
self._time_queue.put(time_from_server)
class TestClient(EClient):
"""
The client method
We don't override native methods, but instead call them from our own wrappers
"""
def __init__(self, wrapper):
## Set up with a wrapper inside
EClient.__init__(self, wrapper)
def speaking_clock(self):
"""
Basic example to tell the time
:return: unix time, as an int
"""
print("Getting the time from the server... ")
## Make a place to store the time we're going to return
## This is a queue
time_storage=self.wrapper.init_time()
## This is the native method in EClient, asks the server to send us the time please
self.reqCurrentTime()
## Try and get a valid time
MAX_WAIT_SECONDS = 10
try:
current_time = time_storage.get(timeout=MAX_WAIT_SECONDS)
except queue.Empty:
print("Exceeded maximum wait for wrapper to respond")
current_time = None
while self.wrapper.is_error():
print(self.get_error())
return current_time
class TestApp(TestWrapper, TestClient):
def __init__(self, ipaddress, portid, clientid):
TestWrapper.__init__(self)
TestClient.__init__(self, wrapper=self)
self.init_error()
self.connect(ipaddress, portid, clientid)
thread = Thread(target = self.run)
thread.start()
setattr(self, "_thread", thread)
if __name__ == '__main__':
##
## Check that the port is the same as on the Gateway
## ipaddress is 127.0.0.1 if one same machine, clientid is arbitrary
app = TestApp("127.0.0.1", 4001, 10)
current_time = app.speaking_clock()
print(current_time)
order1 = order.Order()
order1.action = "BUY"
order1.orderType = "MKT"
order1.totalQuantity = 1
contract1 = contract.Contract()
contract1.symbol = "AMZN"
contract1.secType = "FUT"
contract1.exchange = "GLOBEX"
contract1.currency = "USD"
contract1.lastTradeDateOrContractMonth = "201903"
app.placeOrder(6566, contract1, order1)
app.disconnect()
问题是您的导入:
from ibapi.order import Order as order
您将 class Order
重命名为 order
。
没有尝试,正确的方法应该是:
order1 = order()
错误告诉您您有一个 class、order
,它没有属性 Order
。那是因为这一行:
from ibapi.order import Order as order
在其中导入 class 订单,但将其重命名为 order
。我不知道你为什么这样做,但不要这样做。导入模块:
from ibapi import order
并保留您现有的实例化代码:
order1 = order.Order()
或者,导入 class 而不重命名:
from ibapi.order import Order
并做
order1 = Order()
这是使用 Python 在 IB 上下订单的代码。此代码有效,但出现一个错误。最后尝试下单,却报错:
Traceback (most recent call last):
Getting the time from the server...
File "C:/Users/B/PycharmProject/1/api1.py", line 117, in <module>
order1 = order.Order()
AttributeError: type object 'Order' has no attribute 'Order'
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfarm.nj
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfuture
IB error id -1 errorcode 2104 string Market data farm connection is OK:cashfarm
IB error id -1 errorcode 2104 string Market data farm connection is OK:usfarm
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:ushmds.us
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:ilhmds
IB error id -1 errorcode 2106 string HMDS data farm connection is OK:njhmds
1544354853
我猜问题出在第5行和第6行。当我删除它们时,我得到 "name 'order' is not defined"。我想我只是错误地定义了它。也许有人遇到过类似的 problem/error?
from ibapi.wrapper import EWrapper
from ibapi.client import EClient
from threading import Thread
import queue
from ibapi.contract import Contract as contract
from ibapi.order import Order as order
class TestWrapper(EWrapper):
"""
The wrapper deals with the action coming back from the IB gateway or TWS instance
We override methods in EWrapper that will get called when this action happens, like currentTime
"""
## error handling code
def init_error(self):
error_queue=queue.Queue()
self._my_errors = error_queue
def get_error(self, timeout=5):
if self.is_error():
try:
return self._my_errors.get(timeout=timeout)
except queue.Empty:
return None
return None
def is_error(self):
an_error_if=not self._my_errors.empty()
return an_error_if
def error(self, id, errorCode, errorString):
## Overriden method
errormsg = "IB error id %d errorcode %d string %s" % (id, errorCode, errorString)
self._my_errors.put(errormsg)
## Time telling code
def init_time(self):
time_queue=queue.Queue()
self._time_queue = time_queue
return time_queue
def currentTime(self, time_from_server):
## Overriden method
self._time_queue.put(time_from_server)
class TestClient(EClient):
"""
The client method
We don't override native methods, but instead call them from our own wrappers
"""
def __init__(self, wrapper):
## Set up with a wrapper inside
EClient.__init__(self, wrapper)
def speaking_clock(self):
"""
Basic example to tell the time
:return: unix time, as an int
"""
print("Getting the time from the server... ")
## Make a place to store the time we're going to return
## This is a queue
time_storage=self.wrapper.init_time()
## This is the native method in EClient, asks the server to send us the time please
self.reqCurrentTime()
## Try and get a valid time
MAX_WAIT_SECONDS = 10
try:
current_time = time_storage.get(timeout=MAX_WAIT_SECONDS)
except queue.Empty:
print("Exceeded maximum wait for wrapper to respond")
current_time = None
while self.wrapper.is_error():
print(self.get_error())
return current_time
class TestApp(TestWrapper, TestClient):
def __init__(self, ipaddress, portid, clientid):
TestWrapper.__init__(self)
TestClient.__init__(self, wrapper=self)
self.init_error()
self.connect(ipaddress, portid, clientid)
thread = Thread(target = self.run)
thread.start()
setattr(self, "_thread", thread)
if __name__ == '__main__':
##
## Check that the port is the same as on the Gateway
## ipaddress is 127.0.0.1 if one same machine, clientid is arbitrary
app = TestApp("127.0.0.1", 4001, 10)
current_time = app.speaking_clock()
print(current_time)
order1 = order.Order()
order1.action = "BUY"
order1.orderType = "MKT"
order1.totalQuantity = 1
contract1 = contract.Contract()
contract1.symbol = "AMZN"
contract1.secType = "FUT"
contract1.exchange = "GLOBEX"
contract1.currency = "USD"
contract1.lastTradeDateOrContractMonth = "201903"
app.placeOrder(6566, contract1, order1)
app.disconnect()
问题是您的导入:
from ibapi.order import Order as order
您将 class Order
重命名为 order
。
没有尝试,正确的方法应该是:
order1 = order()
错误告诉您您有一个 class、order
,它没有属性 Order
。那是因为这一行:
from ibapi.order import Order as order
在其中导入 class 订单,但将其重命名为 order
。我不知道你为什么这样做,但不要这样做。导入模块:
from ibapi import order
并保留您现有的实例化代码:
order1 = order.Order()
或者,导入 class 而不重命名:
from ibapi.order import Order
并做
order1 = Order()