ranaroussi / ezibpy

ezIBpy, a Pythonic Client for Interactive Brokers API
Apache License 2.0
324 stars 116 forks source link

Account information example #14

Closed mcg88 closed 5 years ago

mcg88 commented 7 years ago

The example code outputs with the following error.

My account contains 1 futures contract, 1 share of AAPL. There is also a pending order for AAPL LMT at 10.00 however, the code snippet doesn't seem to be capturing that at all in the Orders (by TickId) or Orders (by Symbol)

18-Apr-17 12:41:35 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 12:41:35 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 550, in handleOrders
    contractString = self.contractString(msg.contract)
AttributeError: 'OrderStatus' object has no attribute 'contract'

Server Version: 76 TWS Time at connection:20170418 12:41:34 EST Market Data {0: ask asksize bid bidsize last lastsize datetime
0 0 0 0 0 0 0} Market Depth {0: ask asksize bid bidsize 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0} Account Information {'AvailableFunds': 996222.14, 'AvailableFunds-C': -635.85, 'AvailableFunds-S': 996857.99, 'BuyingPower': 3987830.64, 'CashBalance': 999794.0, 'DayTradesRemaining': -1.0, 'InitMarginReq': 3712.95, 'MaintMarginReq': 2977.43, 'NetLiquidation': 999935.09} Positions {'ESU2017_FUT': {'symbol': 'ESU2017_FUT', 'position': 1, 'avgCost': 116564.54, 'account': 'xxx'}, 'AAPL': {'symbol': 'AAPL', 'position': 1, 'avgCost': 141.71345725, 'account': 'xxx'}} Portfolio {'AAPL': {'symbol': 'AAPL', 'position': 1, 'marketPrice': 141.33599855, 'marketValue': 141.34, 'averageCost': 141.71345725, 'unrealizedPNL': -0.38, 'realizedPNL': 0.0, 'account': 'xxx'}, 'ESU2017_FUT': {'symbol': 'ESU2017_FUT', 'position': 1, 'marketPrice': 2330.00024415, 'marketValue': 116500.01, 'averageCost': 116564.54, 'unrealizedPNL': -64.53, 'realizedPNL': 0.0, 'account': 'xxx'}} Contracts {1: <ib.ext.Contract.Contract object at 0x0000000007F10A20>} Orders (by TickId) {} Orders (by Symbol) {}

mcg88 commented 7 years ago

Also getting this error:


18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 550, in handleOrders
    contractString = self.contractString(msg.contract)
AttributeError: 'OrderStatus' object has no attribute 'contract'
18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 550, in handleOrders
    contractString = self.contractString(msg.contract)
AttributeError: 'OrderStatus' object has no attribute 'contract'
18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 12:59:31 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 550, in handleOrders
    contractString = self.contractString(msg.contract)
AttributeError: 'OrderStatus' object has no attribute 'contract
```'
ranaroussi commented 7 years ago

Oops! This bug introduced in version 1.12.47. Please upgrade to 1.12.48 and let me know if you still getting these errors:

$ pip install ezibpy --upgrade --no-cache-dir
mcg88 commented 7 years ago

updated, getting this:

18-Apr-17 13:29:40 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 13:29:40 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 597, in handleOrders
    self.orders[msg.orderId]['status']       = msg.status.upper()
KeyError: 8
18-Apr-17 13:29:40 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
18-Apr-17 13:29:40 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 597, in handleOrders
    self.orders[msg.orderId]['status']       = msg.status.upper()
KeyError: 4
ranaroussi commented 7 years ago

Can you share your code here? I want to try and replicate this error on my dev machine...

mcg88 commented 7 years ago

I can't seem to replicate it now after restarting my kernel. I'll keep an eye on it and let you know if I notice it again.

That being said, two additional items: 1)

print("Contracts")
print(ibConn.contracts)

prints out:

Contracts
{1: <ib.ext.Contract.Contract object at 0x0000000007CD47B8>, 2: <ib.ext.Contract.Contract object at 0x0000000007CD4588>}

2) Is the following code supposed to print pending orders? or filled orders? It doesn't seem to print out anything currently despite me having pending orders out there

print("Orders (by TickId)")
print(ibConn.orders)

print("Orders (by Symbol)")
print(ibConn.symbol_orders)

Thanks for all your help!

mcg88 commented 7 years ago

Another issue I came across, when sending an order while I have a prior order pending,, the logging prints out this:

2017-04-18 15:17:05,795 [INFO] ezibpy: [ORDER]: <openOrder orderId=3, contract=CVE, order=<ib.ext.Order.Order object at 0x0000000007C204E0>, orderState=<ib.ext.OrderState.OrderState object at 0x0000000007C20710>>
18-Apr-17 15:17:05 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'openOrder'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 585, in handleOrders
    "time": datetime.fromtimestamp(int(self.time))
OSError: [Errno 22] Invalid argument
2017-04-18 15:17:05,803 [INFO] ezibpy: [ORDER]: <orderStatus orderId=3, status=Submitted, filled=0, remaining=100, avgFillPrice=0.0, permId=xxx, parentId=0, lastFillPrice=0.0, clientId=999, whyHeld=None>
18-Apr-17 15:17:05 ERROR     Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ib\opt\dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 267, in handleServerEvents
    self.handleOrders(msg)
  File "C:\ProgramData\Anaconda2\envs\py36\lib\site-packages\ezibpy\ezibpy.py", line 597, in handleOrders
    self.orders[msg.orderId]['status']       = msg.status.upper()
KeyError: 3
ranaroussi commented 7 years ago

From the log, the errors seems to be coming from IbPy.

Do you have TWS setup correctly? See here for setup instructions.

Lastly, please post the code you're running so I can try and replicate these errors on my machine.

mcg88 commented 7 years ago

Yes, TWS appears installed correctly. I didn't have log api messages enabled, but have it now.

import ezibpy
import time
import logging

# after ezibpy is imported, we can change the logging handler to file
logger = logging.getLogger('ezibpy')
logger.addHandler(logging.FileHandler('C:\iblog\ezibpy.log'))
logger.setLevel(logging.INFO)
logger.propagate = False # do not also log to stderr

# define custom callback
def ibCallback(caller, msg, **kwargs):
    if caller == "handleOrders":
        print(msg)
        order = ibConn.orders[msg.orderId]
        if order["status"] == "FILLED":
            print(">>> ORDER FILLED")

# initialize ezIBpy
ibConn = ezibpy.ezIBpy()
ibConn.connect(clientId=100, host="localhost", port=7497)

# assign the custom callback
ibConn.ibCallback = ibCallback

# create a contract
contract = ibConn.createStockContract("CVE", currency="CAD")

# create an order
#order = ibConn.createOrder(quantity=100, orderType="MKT") # use price=X for LMT orders

# submit an order (returns order id)
#orderId = ibConn.placeOrder(contract, order)

#print(orderId)

# let order fill
time.sleep(2)

print('EXIT')
order2 = ibConn.createOrder(quantity=-200, price=14.1, orderType="LMT")
orderId2 = ibConn.placeOrder(contract, order2)
print(orderId2)

# see the positions
print("Positions")
print(ibConn.positions)