-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
126 lines (102 loc) · 3.92 KB
/
Main.py
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
"""
Copyright (C) 2016 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.
"""
import sys
import argparse
import datetime
import collections
import inspect
import logging
import time
import os.path
from ibapi import wrapper
from ibapi.utils import iswrapper
# types
from ibapi.common import *
from ibapi.order_condition import *
from ibapi.contract import *
from ibapi.order import *
from ibapi.order_state import *
from ibapi.execution import Execution
from ibapi.execution import ExecutionFilter
from ibapi.commission_report import CommissionReport
from ibapi.scanner import ScannerSubscription
from ibapi.ticktype import *
from ibapi.account_summary_tags import *
from App import TestClient, TestWrapper, TestApp
from Util import Singleton, SetupLogger
def printinstance(inst:Object):
attrs = vars(inst)
print(', '.join("%s: %s" % item for item in attrs.items()))
class Activity(Object):
def __init__(self, reqMsgId, ansMsgId, ansEndMsgId, reqId):
self.reqMsdId = reqMsgId
self.ansMsgId = ansMsgId
self.ansEndMsgId = ansEndMsgId
self.reqId = reqId
class RequestMgr(Object):
def __init__(self):
# I will keep this simple even if slower for now: only one list of
# requests finding will be done by linear search
self.requests = []
def addReq(self, req):
self.requests.append(req)
def receivedMsg(self, msg):
pass
def main():
SetupLogger()
logging.debug("now is %s", datetime.datetime.now())
logging.getLogger().setLevel(logging.ERROR)
cmdLineParser = argparse.ArgumentParser("api tests")
# cmdLineParser.add_option("-c", action="store_True", dest="use_cache", default = False, help = "use the cache")
# cmdLineParser.add_option("-f", action="store", type="string", dest="file", default="", help="the input file")
cmdLineParser.add_argument("-p", "--port", action="store", type=int,
dest="port", default=7497, help="The TCP port to use")
cmdLineParser.add_argument("-C", "--global-cancel", action="store_true",
dest="global_cancel", default=False,
help="whether to trigger a globalCancel req")
args = cmdLineParser.parse_args()
print("Using args", args)
logging.debug("Using args %s", args)
# print(args)
# enable logging when member vars are assigned
from ibapi import utils
from ibapi.order import Order
Order.__setattr__ = utils.setattr_log
from ibapi.contract import Contract, UnderComp
Contract.__setattr__ = utils.setattr_log
UnderComp.__setattr__ = utils.setattr_log
from ibapi.tag_value import TagValue
TagValue.__setattr__ = utils.setattr_log
TimeCondition.__setattr__ = utils.setattr_log
ExecutionCondition.__setattr__ = utils.setattr_log
MarginCondition.__setattr__ = utils.setattr_log
PriceCondition.__setattr__ = utils.setattr_log
PercentChangeCondition.__setattr__ = utils.setattr_log
VolumeCondition.__setattr__ = utils.setattr_log
# from inspect import signature as sig
# import code code.interact(local=dict(globals(), **locals()))
# sys.exit(1)
# tc = TestClient(None)
# tc.reqMktData(1101, ContractSamples.USStockAtSmart(), "", False, None)
# print(tc.reqId2nReq)
# sys.exit(1)
try:
app = TestApp.Instance()
if args.global_cancel:
app.globalCancelOnly = True
# ! [connect]
app.connect("127.0.0.1", args.port, clientId=1)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(),
app.twsConnectionTime()))
# ! [connect]
app.run()
except:
raise
finally:
app.dumpTestCoverageSituation()
app.dumpReqAnsErrSituation()
if __name__ == "__main__":
main()