-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrtOption2Mongo.py
More file actions
324 lines (272 loc) · 11.7 KB
/
Copy pathrtOption2Mongo.py
File metadata and controls
324 lines (272 loc) · 11.7 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python
"""Collects 5 second RT bars from IB and sends to mongo.
Author: Jim Strieter
Date: 06/01/2018
Where: Scottsdale, AZ
Copyright 2017 James J. Strieter"""
ENABLE_REDUNDANCY_PREVENTION = False
REDUNDANCY_PREVENTION_TERMS = ['ticker', 'epoch'] # Either None or a dictionary to prevent adding the same record twice
import numpy as np
import sys
import argparse
from datetime import datetime
import inspect # Use this to get traceback. Give it a try some time!
import logging
import time
from ibapi import wrapper
from ibapi.client import EClient
from ibapi.utils import iswrapper
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.ticktype import *
from ibapi.account_summary_tags import *
from historicalFetcher import durationDay
from historicalFetcher import sizeMin
from tradingEngine import states
from barTools import human2epoch
from barTools import epoch2human
from techInd import trendDecision
import copy as cp
from contractDump import *
from contracts_jim_uses import spy
from rtWrapper import CustomWrapper
from cmdLineParser import cmdLineParseObj
from threading import Thread
from pymongo import MongoClient
import datetime
from makeContract import dict2Contract
client = MongoClient('localhost', 27017)
db = client.test_database
# def dic2cont(someDic):
# """Converts a contract dic to an object contract"""
# cont = Contract()
# cont.lastTradeDateOrContractMonth = someDic["expiration"]
# cont.symbol = someDic["ticker"]
# cont.exchange = "SMART",
# cont.right = "CALL",
# cont.strike = someDic["strike"]
# cont.currency = "USD"
# return cont
class newFifteenOnly(CustomWrapper):
def __init__(self, parseObj=None):
super().__init__()
self.dataIds = []
# Keyed by reqId:
self.tickers = {}
self.historicalDone = {}
self.rights = {}
# Keyed by ticker:
self.tick2req = {}
# Initialize all the dictionaries:
self.chooseStocks()
# For interacting w/ Mongo:
self.posts = db.posts
self.optContracts = {} # Keyed by ticker
for m in self.posts.find({"right" : {"$exists" : 1}}):
self.optContracts[m["ticker"]] = m
# print(m["ticker"], " ", m["strike"])
def chooseStocks(self):
# self.chooseStocksHelper("SPY", spy())
# self.chooseStocksHelper('AAPL', usStock_aapl())
self.chooseStocksHelper('ABX', usStock_abx())
self.chooseStocksHelper('AMD', usStock_amd())
self.chooseStocksHelper('AXP', usStock_axp())
self.chooseStocksHelper('BA', usStock_ba())
# self.chooseStocksHelper('BABA', usStock_baba())
# self.chooseStocksHelper('BAC', usStock_bac())
# self.chooseStocksHelper('BB', usStock_bb())
# self.chooseStocksHelper('CAT', usStock_cat())
# self.chooseStocksHelper('CHK', usStock_chk())
# self.chooseStocksHelper('CSCO', usStock_csco())
# self.chooseStocksHelper('CVX', usStock_cvx())
# self.chooseStocksHelper('DIS', usStock_dis())
# self.chooseStocksHelper('DWDP', usStock_dwdp())
# self.chooseStocksHelper('FB', usStock_fb())
# self.chooseStocksHelper('JNJ', usStock_jnj())
# self.chooseStocksHelper('MU', usStock_mu())
# self.chooseStocksHelper('NFLX', usStock_nflx())
# self.chooseStocksHelper('NKE', usStock_nke())
# self.chooseStocksHelper('NVDA', usStock_nvda())
# self.chooseStocksHelper('PFE', usStock_pfe())
# self.chooseStocksHelper('PG', usStock_pg())
# self.chooseStocksHelper('FCX', usStock_fcx())
# self.chooseStocksHelper('GE', usStock_ge())
# self.chooseStocksHelper('GLW', usStock_glw())
# self.chooseStocksHelper('GS', usStock_gs())
# self.chooseStocksHelper('HD', usStock_hd())
# self.chooseStocksHelper('HPQ', usStock_hpq())
# self.chooseStocksHelper('IBM', usStock_ibm())
# self.chooseStocksHelper('INTC', usStock_intc())
# self.chooseStocksHelper('JPM', usStock_jpm())
# self.chooseStocksHelper('KO', usStock_ko())
# self.chooseStocksHelper('LOW', usStock_low())
# self.chooseStocksHelper('MCD', usStock_mcd())
# self.chooseStocksHelper('MMM', usStock_mmm())
# self.chooseStocksHelper('MRK', usStock_mrk())
# self.chooseStocksHelper('MSFT', usStock_msft())
# self.chooseStocksHelper('SBUX', usStock_sbux())
# self.chooseStocksHelper('SLB', usStock_slb())
# self.chooseStocksHelper('SNAP', usStock_snap())
# self.chooseStocksHelper('TSLA', usStock_tsla())
# self.chooseStocksHelper('TWTR', usStock_twtr())
# self.chooseStocksHelper('TXN', usStock_txn())
# self.chooseStocksHelper('UNH', usStock_unh())
# self.chooseStocksHelper('UTX', usStock_utx())
# self.chooseStocksHelper('V', usStock_v())
# self.chooseStocksHelper('VZ', usStock_vz())
# self.chooseStocksHelper('WMT', usStock_wmt())
# self.chooseStocksHelper('XOM', usStock_xom())
def chooseStocksHelper(self, t, undContract):
"""undContract is not used in this file. It is only an input so that chooseStocks()
can be copied & pasted from other files.
Since reqIds start at 3001, odd reqIds are calls and even reqIds are puts.
Since self.tick2req[someTicker] is a scalar, not a list, it is only populated
for calls. If you want the reqId for a put, use this:
self.tick2req[someTicker]+1 """
# Handle Calls:
reqId = self.newReqId()
self.tick2req[t] = reqId
self.tickers[reqId] = t
self.historicalDone[reqId] = False
self.rights[reqId] = "CALL"
# Handle Puts:
reqId = self.newReqId()
self.tickers[reqId] = t
self.historicalDone[reqId] = False
self.rights[reqId] = "PUT"
def newReqId(self):
if len(self.dataIds) == 0:
self.dataIds.append(3001)
else:
self.dataIds.append(self.dataIds[-1] + 1)
return self.dataIds[-1]
def nextValidId(self, orderId: int):
super().nextValidId(orderId)
self.start()
def start(self):
"""Try moving this to rtWrapper.py"""
print("*****************************************************************************************************")
print("******************************************* Running start *******************************************")
print("*****************************************************************************************************")
print("self.started: ", self.started)
if self.started:
return
# Normal stuff goes here:
self.started = True
if self.globalCancelOnly:
print("Executing GlobalCancel only")
self.reqGlobalCancel()
else:
print("Executing requests")
self.realTimeBars_req()
print("Executing requests ... finished")
def realTimeBars_req(self):
"""Iterates through all the reqId's we created earlier and sends the contract associated with each
to TWS."""
print(self.dataIds)
for reqId in self.dataIds:
ticker = self.tickers[reqId]
print("Requesting Data Id: ", reqId, " ", ticker)
cont = dict2Contract(ticker, self.optContracts[ticker]["strike"], self.rights[reqId], self.optContracts[ticker], acct=None)
cont.lastTradeDateOrContractMonth = "20180720"
self.reqRealTimeBars(reqId, cont, 5, "MIDPOINT", False, [] )
def realtimeBar(self, reqId: TickerId, eTime: int, open: float, high: float, low: float, close: float, volume: int,
wap: float, count: int, testMode=False):
super().realtimeBar(reqId, time, open, high, low, close, volume, wap, count)
ticker = self.tickers[reqId]
print("RT Data: ", ticker, " ", close, " ", self.rights[reqId])
dateObj = epoch2human(eTime)
dateStr = str(dateObj)
oneBar = {
"secType": "OPT",
"open": open,
"close": close,
"high": high,
"low": low,
"epoch": eTime,
"dateStr": dateStr[:8],
"year": dateObj.year,
"month": dateObj.month,
"day": dateObj.day,
"hour": dateObj.hour,
"minute": dateObj.minute,
"second": dateObj.second,
"barLen": "15 mins",
"ticker": ticker,
"strike": self.optContracts[ticker]["strike"],
"expiration": self.optContracts[ticker]["expiration"],
"right": self.rights[reqId],
"exchange": self.optContracts[ticker]["exchange"]
}
self.posts.insert_one(oneBar)
def historicalData(self, reqId:int, bar: BarData):
"""Q: Does tEng[] want a bar class or a bar dict?
A: When he goes to the bar, he's looking for dict. Get it?!? HAHAHAHAHAHAHA"""
ticker = self.tickers[reqId]
print("Historical Data: ", ticker, " ", bar.close)
eTime = human2epoch(bar.date)
dateObj = epoch2human(eTime)
dateStr = str(dateObj)
oneBar = {
"secType" : "OPT",
"open" : bar.open,
"close" : bar.close,
"high" : bar.high,
"low" : bar.low,
"epoch" : eTime,
"dateStr" : dateStr[:8],
"year" : dateObj.year,
"month" : dateObj.month,
"day" : dateObj.day,
"hour" : dateObj.hour,
"minute" : dateObj.minute,
"second" : dateObj.second,
"barLen" : "15 mins",
"ticker" : ticker,
"strike" : self.optContracts[ticker]["strike"],
"expiration": self.optContracts[ticker]["expiration"],
"right" : self.optContracts[ticker]["right"],
"exchange" : self.optContracts[ticker]["exchange"]
}
self.posts.insert_one(oneBar)
def main():
cmdLineParser = cmdLineParseObj()
args = cmdLineParser.parse_args()
print("Using args", args)
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
try:
#app = MyTradingApp(cmdLineParser.parse_args())
app = newFifteenOnly(cmdLineParser.parse_args())
if args.global_cancel:
app.globalCancelOnly = True
# ! [connect]
app.connect("127.0.0.1", args.port, clientId=0)
print("serverVersion:%s connectionTime:%s" % (app.serverVersion(),
app.twsConnectionTime()))
# ! [connect]
app.run()
app.nextValidId(1) # nextValidId
except:
raise
finally:
app.dumpTestCoverageSituation()
app.dumpReqAnsErrSituation()
if __name__ == "__main__":
main()