AttributeError: 'NoneType' object has no attribute 'conId'
AttributeError: 'NoneType' object has no attribute 'conId'
我正在使用 Interactive brokers python api 做 AMD 的限价订单。到目前为止,代码是:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId):
self.nextValidId = orderId
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=13)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)
def USstock(symbol, secType='STK', Currency='USD', exchnange = 'SMART'):
contract=Contract()
contract.symbol = symbol
contract.secType = secType
contract.currency = Currency
contract.exchange = exchnange
def limit_order(direction, quantity, price):
order=Order()
order.action = direction
order.orderType = 'LMT'
order.totalQuantity = quantity
order.lmtPrice = price
order_id = app.nextValidId
app.placeOrder(order_id,USstock('AMD'),limit_order('BUY',10,50))
尽管我是 运行,但我收到错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-49-407bad8e9645> in <module>
40 order.lmtPrice = price
41 order_id = app.nextValidId
---> 42 app.placeOrder(order_id,USstock('AMD'),limit_order('BUY',10,50))
~\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\client.py in placeOrder(self, orderId, contract, order)
1041 # send contract fields
1042 if self.serverVersion() >= MIN_SERVER_VER_PLACE_ORDER_CONID:
-> 1043 flds.append(make_field( contract.conId))
1044 flds += [make_field( contract.symbol),
1045 make_field( contract.secType),
AttributeError: 'NoneType' object has no attribute 'conId'
但我不知道我在哪里使用了 'conId' 或者为什么会显示此错误。有任何想法吗?谢谢!
问题是,在这两个函数的末尾,我应该添加到 return 合同和订单,因为如果不是的话,我所做的只是 deffing 函数中的组件。所以代码应该是:
def Contract_Order():
contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'ISLAND'
return contract
def Limit_Order():
order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 10
order.lmtPrice = 100
return order
只需添加 return 合约和 return 订单即可解决此问题
我正在使用 Interactive brokers python api 做 AMD 的限价订单。到目前为止,代码是:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
import threading
import time
class TradingApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def nextValidId(self, orderId):
self.nextValidId = orderId
def websocket_con():
app.run()
app = TradingApp()
app.connect("127.0.0.1", 7497, clientId=13)
con_thread = threading.Thread(target=websocket_con, daemon=True)
con_thread.start()
time.sleep(1)
def USstock(symbol, secType='STK', Currency='USD', exchnange = 'SMART'):
contract=Contract()
contract.symbol = symbol
contract.secType = secType
contract.currency = Currency
contract.exchange = exchnange
def limit_order(direction, quantity, price):
order=Order()
order.action = direction
order.orderType = 'LMT'
order.totalQuantity = quantity
order.lmtPrice = price
order_id = app.nextValidId
app.placeOrder(order_id,USstock('AMD'),limit_order('BUY',10,50))
尽管我是 运行,但我收到错误消息:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-49-407bad8e9645> in <module>
40 order.lmtPrice = price
41 order_id = app.nextValidId
---> 42 app.placeOrder(order_id,USstock('AMD'),limit_order('BUY',10,50))
~\AppData\Roaming\Python\Python37\site-packages\ibapi-9.76.1-py3.7.egg\ibapi\client.py in placeOrder(self, orderId, contract, order)
1041 # send contract fields
1042 if self.serverVersion() >= MIN_SERVER_VER_PLACE_ORDER_CONID:
-> 1043 flds.append(make_field( contract.conId))
1044 flds += [make_field( contract.symbol),
1045 make_field( contract.secType),
AttributeError: 'NoneType' object has no attribute 'conId'
但我不知道我在哪里使用了 'conId' 或者为什么会显示此错误。有任何想法吗?谢谢!
问题是,在这两个函数的末尾,我应该添加到 return 合同和订单,因为如果不是的话,我所做的只是 deffing 函数中的组件。所以代码应该是:
def Contract_Order():
contract=Contract()
contract.symbol = 'AMD'
contract.secType = 'STK'
contract.currency = 'USD'
contract.exchange = 'ISLAND'
return contract
def Limit_Order():
order=Order()
order.action = 'BUY'
order.orderType = 'LMT'
order.totalQuantity = 10
order.lmtPrice = 100
return order
只需添加 return 合约和 return 订单即可解决此问题