forked from Komnomnomnom/swigibpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswigify_ib.i
158 lines (136 loc) · 4.74 KB
/
swigify_ib.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
SWIG interface file for Interactive Brokers API v9.65.
*/
%module(directors="1",docstring="Python wrapper for Interactive Brokers TWS C++ API") swigibpy
/* Turn on auto-generated docstrings */
%feature("autodoc", "1");
/*Inclusions for generated cpp file*/
%{
#include "Shared/EClient.h"
#include "Shared/EClientSocketBase.h"
#include "PosixSocketClient/EPosixClientSocket.h"
#include "Shared/EWrapper.h"
#include "Shared/CommonDefs.h"
#include "Shared/Contract.h"
#include "Shared/Execution.h"
#include "Shared/Order.h"
#include "Shared/OrderState.h"
#include "Shared/ScannerSubscription.h"
%}
/* auto convert std::string and typedefs to Python strings */
%include "std_string.i"
typedef std::string IBString;
/*EWrapper will be subclassed in Python*/
%feature("director") EWrapper;
%feature("director:except") {
if ($error != NULL) {
throw Swig::DirectorMethodException();
}
}
// Exception handling
%include exception.i
%exception {
/*
most errors should be propagated through to EWrapper->error,
others should be added here as and when needed / encountered.
*/
try {
$action
} catch(Swig::DirectorPureVirtualException &e) {
/* Call to pure virtual method, raise not implemented error */
PyErr_SetString(PyExc_NotImplementedError, e.getMessage());
} catch(Swig::DirectorException &e) {
/* Fail if there is a problem in the director proxy transport */
SWIG_fail;
} catch(std::exception& e) {
/* Convert standard error to standard error */
PyErr_SetString(PyExc_StandardError, const_cast<char*>(e.what()));
} catch(...) {
/* Final catch all, results in runtime error */
PyErr_SetString(PyExc_RuntimeError, "Unknown error caught in Interactive Brokers SWIG wrapper...");
}
}
/* Grab the header files to be wrapped */
%include "Shared/CommonDefs.h"
%include "Shared/Contract.h"
%include "Shared/EClient.h"
%include "Shared/EClientSocketBase.h"
%include "Shared/Execution.h"
%include "Shared/Order.h"
%include "Shared/OrderState.h"
%include "Shared/ScannerSubscription.h"
/* Customise EPosixClientSocket so that TWS is automatically polled for messages when we are connected to it */
%pythoncode %{
import threading
import time
class TWSPoller(threading.Thread):
'''Polls TWS every second for any outstanding messages'''
def __init__(self, tws):
super(TWSPoller, self).__init__()
self.daemon = True
self._tws = tws
self.stop_polling = False
def run(self):
'''Continually poll TWS until the stop flag is set'''
while not self.stop_polling:
try:
self._tws.checkMessages()
except:
if self.stop_polling:
break
else:
raise
time.sleep(1)
%}
%pythonappend EClientSocketBase::eConnect(const char *host, unsigned int port, int clientId=0) %{
if val:
self.poller = TWSPoller(self)
self.poller.start()
%}
%pythonprepend EPosixClientSocket::eDisconnect() %{
if self.poller:
self.poller.stop_polling = True
self.poller = None
%}
%include "PosixSocketClient/EPosixClientSocket.h"
/* Override EWrapper's error methods with default implementations */
%pythoncode %{
class TWSError(Exception):
'''Exception raised during communication with Interactive Brokers TWS
application
'''
def __init__(self, code, msg):
self.code = code
self.msg = msg
def __str__(self):
return "%s: %s" % (self.code, repr(self.msg))
class TWSSystemError(TWSError):
'''System related exception raised during communication with Interactive
Brokers TWS application.
'''
pass
class TWSClientError(TWSError):
'''Exception raised on client (python) side by Interactive Brokers API'''
pass
%}
%feature("shadow") EWrapper::winError(const IBString &, int) %{
def winError(self, str, lastError):
'''Error in TWS API library'''
raise TWSClientError(lastError, str)
%}
%feature("shadow") EWrapper::error(const int, const int, const IBString) %{
def error(self, id, errorCode, errorString):
'''Error during communication with TWS'''
if errorCode == 165:
print "TWS Message %s: %s" % (errorCode, errorString)
elif errorCode >= 100 and errorCode < 1100:
raise TWSError(errorCode, errorString)
elif errorCode >= 1100 and errorCode < 2100:
raise TWSSystemError(errorCode, errorString)
elif errorCode >= 2100 and errorCode < 2110:
import sys
sys.stderr.write("TWS Warning %s: %s\n" % (errorCode, errorString))
else:
raise RuntimeError(errorCode, errorString)
%}
%include "Shared/EWrapper.h"