Last active
February 26, 2019 16:40
-
-
Save audubon/b14eeda1c2cad704b333 to your computer and use it in GitHub Desktop.
order status and accountUpdates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv | |
from time import sleep, strftime | |
from ib.ext.Contract import Contract | |
from ib.ext.Order import Order | |
from ib.ext.EWrapper import EWrapper | |
from ib.ext.EClientSocket import EClientSocket | |
from ib.ext.ExecutionFilter import ExecutionFilter | |
def showmessage(message, mapping): | |
try: | |
del(mapping['self']) | |
except (KeyError, ): | |
pass | |
items = mapping.items() | |
items.sort() | |
print '### %s' % (message, ) | |
for k, v in items: | |
print ' %s:%s' % (k, v) | |
class ReferenceWrapper(EWrapper): | |
def __init__ (self,subs={}): | |
super(ReferenceWrapper, self).__init__() | |
self.orderID = None | |
self.subscriptions = subs | |
def orderStatus(self, orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeId): | |
showmessage('orderStatus', vars()) | |
def openOrder(self, orderId, contract, order, state): | |
showmessage('openOrder', vars()) | |
def connectionClosed(self): | |
showmessage('connectionClosed', {}) | |
def execDetails(self, orderId, contract, execution): | |
showmessage('execDetails', vars()) | |
def error(self, id=None, errorCode=None, errorMsg=None): | |
showmessage('error', vars()) | |
def managedAccounts(self, accountsList): | |
showmessage('managedAccounts', vars()) | |
def nextValidId(self, orderId): | |
self.orderID = orderId | |
showmessage('nextValidId', vars()) | |
def updateAccountValue(self, key, value, currency, accountName): | |
showmessage('updateAccountValue', vars()) | |
def updatePortfolio(self, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName): | |
showmessage('updatePortfolio', vars()) | |
def updateAccountTime(self, timestamp): | |
showmessage('updatePortfolio', vars()) | |
class BaseApp(): | |
def __init__(self, host='localhost', port=7496, clientId=0,name=None,**kwargs): | |
self.host = host | |
self.port = port | |
self.clientId = clientId | |
self.subscriptions = {} | |
if kwargs.has_key('wrapper'): | |
self.wrapper = kwargs['wrapper'] | |
self.wrapper.setSubscriptions(self.subscriptions) | |
else: | |
self.wrapper = ReferenceWrapper(self.subscriptions) | |
self.connection = EClientSocket(self.wrapper) | |
self.conid = 1 | |
def eConnect(self): | |
self.connection.eConnect(self.host, self.port, self.clientId) | |
def reqAutoOpenOrders(self): | |
self.connection.reqAutoOpenOrders(1) | |
def reqAllOpenOrders(self): | |
self.connection.reqAllOpenOrders() | |
def eDisconnect(self): | |
sleep(5) | |
self.connection.eDisconnect() | |
def reqAccountUpdates(self): | |
self.connection.reqAccountUpdates(True,"DU666") | |
if __name__ == '__main__': | |
""" run from command prompt and type 'm' hit enter | |
manually place / change orders in TWS | |
you should see order statuses and executions | |
""" | |
import msvcrt,sys | |
app = BaseApp() | |
app.eConnect() | |
sleep(5) | |
while True: | |
action = msvcrt.getch() | |
print action | |
if action=='m': | |
app.reqAutoOpenOrders() | |
app.reqAccountUpdates() | |
elif action=='c': | |
sys.exit(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow. thanks. this is the best