-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9562a62
Showing
19 changed files
with
1,105 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
""" | ||
Copyright (C) 2019 Interactive Brokers LLC. All rights reserved. This code is subject to the terms | ||
and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. | ||
""" | ||
|
||
|
||
""" | ||
Just a thin wrapper around a socket. | ||
It allows us to keep some other info along with it. | ||
""" | ||
|
||
|
||
import socket | ||
import threading | ||
import logging | ||
|
||
from ibapi.common import * # @UnusedWildImport | ||
from ibapi.errors import * # @UnusedWildImport | ||
|
||
|
||
#TODO: support SSL !! | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Connection: | ||
def __init__(self, host, port): | ||
self.host = host | ||
self.port = port | ||
self.socket = None | ||
self.wrapper = None | ||
self.lock = threading.Lock() | ||
|
||
def connect(self): | ||
try: | ||
self.socket = socket.socket() | ||
#TODO: list the exceptions you want to catch | ||
except socket.error: | ||
if self.wrapper: | ||
self.wrapper.error(NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg()) | ||
|
||
try: | ||
self.socket.connect((self.host, self.port)) | ||
except socket.error: | ||
if self.wrapper: | ||
self.wrapper.error(NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg()) | ||
|
||
self.socket.settimeout(1) #non-blocking | ||
|
||
def disconnect(self): | ||
self.lock.acquire() | ||
try: | ||
if self.socket is not None: | ||
logger.debug("disconnecting") | ||
self.socket.close() | ||
self.socket = None | ||
logger.debug("disconnected") | ||
if self.wrapper: | ||
self.wrapper.connectionClosed() | ||
finally: | ||
self.lock.release() | ||
|
||
def isConnected(self): | ||
return self.socket is not None | ||
|
||
def sendMsg(self, msg): | ||
logger.debug("acquiring lock") | ||
self.lock.acquire() | ||
logger.debug("acquired lock") | ||
if not self.isConnected(): | ||
logger.debug("sendMsg attempted while not connected, releasing lock") | ||
self.lock.release() | ||
return 0 | ||
try: | ||
nSent = self.socket.send(msg) | ||
except socket.error: | ||
logger.debug("exception from sendMsg %s", sys.exc_info()) | ||
raise | ||
finally: | ||
logger.debug("releasing lock") | ||
self.lock.release() | ||
logger.debug("release lock") | ||
|
||
logger.debug("sendMsg: sent: %d", nSent) | ||
|
||
return nSent | ||
|
||
def recvMsg(self): | ||
if not self.isConnected(): | ||
logger.debug("recvMsg attempted while not connected, releasing lock") | ||
return b"" | ||
try: | ||
buf = self._recvAllMsg() | ||
# receiving 0 bytes outside a timeout means the connection is either | ||
# closed or broken | ||
if len(buf) == 0: | ||
logger.debug("socket either closed or broken, disconnecting") | ||
self.disconnect() | ||
except socket.timeout: | ||
logger.debug("socket timeout from recvMsg %s", sys.exc_info()) | ||
buf = b"" | ||
except socket.error: | ||
logger.debug("socket broken, disconnecting") | ||
self.disconnect() | ||
buf = b"" | ||
|
||
return buf | ||
|
||
def _recvAllMsg(self): | ||
cont = True | ||
allbuf = b"" | ||
|
||
while cont and self.isConnected(): | ||
buf = self.socket.recv(4096) | ||
allbuf += buf | ||
logger.debug("len %d raw:%s|", len(buf), buf) | ||
|
||
if len(buf) < 4096: | ||
cont = False | ||
|
||
return allbuf | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
""" | ||
Copyright (C) 2019 Interactive Brokers LLC. All rights reserved. This code is subject to the terms | ||
and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. | ||
""" | ||
|
||
|
||
""" | ||
This code fix is by Thane Brooker. Link: https://idalpha-devops.blogspot.com/2019/11/interactive-brokers-tws-api-python.html | ||
The EReader runs in a separate threads and is responsible for receiving the | ||
incoming messages. | ||
It will read the packets from the wire, use the low level IB messaging to | ||
remove the size prefix and put the rest in a Queue. | ||
""" | ||
import time | ||
import logging | ||
from threading import Thread | ||
|
||
from ibapi import comm | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class EReader(Thread): | ||
def __init__(self, conn, msg_queue): | ||
super().__init__() | ||
self.conn = conn | ||
self.msg_queue = msg_queue | ||
|
||
def run(self): | ||
try: | ||
buf = b"" | ||
while self.conn.isConnected(): | ||
|
||
try: | ||
data = self.conn.recvMsg() | ||
logger.debug("reader loop, recvd size %d", len(data)) | ||
buf += data | ||
|
||
except OSError as err: | ||
#If connection is disconnected, Windows will generate error 10038 | ||
if err.errno == 10038: | ||
|
||
#Wait up to 1 second for disconnect confirmation | ||
waitUntil = time.time() + 1 | ||
while time.time() < waitUntil: | ||
if not self.conn.isConnected(): | ||
break | ||
time.sleep(.1) | ||
|
||
if not self.conn.isConnected(): | ||
logger.debug("Ignoring OSError: {0}".format(err)) | ||
break | ||
|
||
#Disconnect wasn't received or error != 10038 | ||
raise | ||
|
||
while len(buf) > 0: | ||
(size, msg, buf) = comm.read_msg(buf) | ||
#logger.debug("resp %s", buf.decode('ascii')) | ||
logger.debug("size:%d msg.size:%d msg:|%s| buf:%s|", size, | ||
len(msg), buf, "|") | ||
|
||
if msg: | ||
self.msg_queue.put(msg) | ||
else: | ||
logger.debug("more incoming packet(s) are needed ") | ||
break | ||
|
||
logger.debug("EReader thread finished") | ||
except: | ||
logger.exception('unhandled exception in EReader thread') | ||
|
Oops, something went wrong.