问题
I'm using IbPy to read current orders. The response messages which come back to be processed with EWrapper methods have some attributes which appear to be of the wrong type.
To start, here is my handler for Order-related messages. It is intended to catch all messages due to having called reqAllOpenOrders()
.
from ib.opt import ibConnection, message
from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.ext.OrderState import OrderState
_order_resp = dict(openOrderEnd=False, openOrder=[], openStatus=[])
def order_handler(msg):
""" Update our global Order data response dict
"""
global _order_resp
if msg.typeName in ['openStatus', 'openOrder']:
d = dict()
for i in msg.items():
if isinstance(i[1], (Contract, Order, OrderState)):
d[i[0]] = i[1].__dict__
else:
d[i[0]] = i[1]
_order_resp[msg.typeName].append(d.copy())
elif msg.typeName == 'openOrderEnd':
_order_resp['openOrderEnd'] = True
log.info('ORDER: {})'.format(msg))
In the above code, I'm loading all the objects and their attributes to a dict which is then appended to lists within _order_resp
.
The log output lines show healthy interaction with IB:
25-Jan-16 14:57:04 INFO ORDER: <openOrder orderId=1, contract=<ib.ext.Contract.Contract object at 0x102a98150>, order=<ib.ext.Order.Order object at 0x102a98210>, orderState=<ib.ext.OrderState.OrderState object at 0x102a98350>>)
25-Jan-16 14:57:04 INFO ORDER: <orderStatus orderId=1, status=PreSubmitted, filled=0, remaining=100, avgFillPrice=0.0, permId=1114012437, parentId=0, lastFillPrice=0.0, clientId=0, whyHeld=None>)
25-Jan-16 14:57:04 INFO ORDER: <openOrderEnd>)
But when looking at the data put into the _order_resp
dict, it looks like some numbers are off:
{
"contract": {
"m_comboLegsDescrip": null,
"m_conId": 265598,
"m_currency": "USD",
"m_exchange": "SMART",
...
},
"order": {
"m_account": "DU12345",
"m_action": "SELL",
"m_activeStartTime": "",
"m_activeStopTime": "",
"m_algoStrategy": null,
"m_allOrNone": false,
"m_auctionStrategy": 0,
"m_auxPrice": 0.0,
"m_basisPoints": 9223372036854775807,
"m_basisPointsType": 9223372036854775807,
...
},
"orderId": 1,
"orderState": {
"m_commission": 9223372036854775807,
"m_commissionCurrency": null,
"m_equityWithLoan": "1.7976931348623157E308",
"m_initMargin": "1.7976931348623157E308",
"m_maintMargin": "1.7976931348623157E308",
"m_maxCommission": 9223372036854775807,
"m_minCommission": 9223372036854775807,
...
}
}
],
"openOrderEnd": true,
In the source code, we see that m_maxCommission is a float()
, yet the value looks like an int, and is much larger than most commissions people like paying.
Some other keys like m_equityWithLoan
have string type values, but the source code says that's correct.
How do I fix the case where I'm getting large ints instead of floats? Is it possible to read the value from memory and reinterpret it as a float? Is this an Interactive Brokers API problem?
来源:https://stackoverflow.com/questions/35004621/how-to-fix-mis-cast-floats-in-ibpy-messages