Repository navigation
Expand file tree
/
Copy pathcontractMiner.py
More file actions
executable file
·365 lines (312 loc) · 13.2 KB
/
Copy pathcontractMiner.py
File metadata and controls
executable file
·365 lines (312 loc) · 13.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
"""Acquires option contract data and dumps it to mongo.
Author: Jim Strieter
Date: 06/10/2018
Where: Scottsdale, AZ
Copyright 2018 James J. Strieter
ACTION: Flesh out this file.
- Make a version that ONLY does options
- Make a version that ONLY does underlying
- This version should mine conId's and generate contractDump.py.
- They both dump data to mongo
- Modify to cancel data request after receiving enough stuff
- Use default params, and start modifying if returned data is too small,
doesn't cover all cancelConditions, etc.
"""
defaultParams = {
"expiration" : "20180720"
}
MAX_ATTEMPTS = 3
# Do this for all possible keys:
# if "numExchanges" in cancelConditions.keys():
# numExchanges = getExchanges()
# if numExchanges >= cancelConditions["numExchanges"]:
# cancelRequest()
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 sizeDay
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 dailyDataAnalysis import connect2Mongo
from getLatestClose import getLatestClose
from pymongo import MongoClient
import datetime
from dailyDataAnalysis import connect2Mongo
from dailyDataAnalysis import sortListOfDictionaries
from daily2mongo import rightJustify
class contractMiner(CustomWrapper):
def __init__(self, parseObj=None, maxAttempts=MAX_ATTEMPTS):
super().__init__()
# Store all our reqIds in 1 place:
self.dataIds = []
# Keyed by reqId:
self.tickers = {}
self.contracts = {}
self.requestedContracts = {} # Can be underlying or derivative
self.receivedContracts = {} # Handy for calculating Greeks later.
self.closes = {}
self.epochs = {}
self.tickerDone = {}
self.startingStrike = {}
self.strikeDeviationMag = {}
self.strikeDeviationSgn = {}
self.numAttempts = {}
# Keyed by ticker:
self.tick2req = {}
# Initialize all the dictionaries:
self.chooseStocks()
# For interacting w/ Mongo:
self.posts = connect2Mongo().posts
# Terminate stuff that is going poorly:
self.maxAttempts = maxAttempts
self.reqMgrCount = 0
self.getClosingPrices()
def getClosingPrices(self):
"""Retrieves closing prices from mongo and maps the latest one to reqId."""
x = self.posts.find({"barLen":"close only"})
for m in x:
print(m)
reqId = self.tick2req[m['ticker']]
if m['epoch'] is not None and m['epoch'] > self.epochs[reqId]:
self.epochs[reqId] = m['epoch']
self.closes[reqId] = m['close']
self.requestedContracts[reqId].strike = np.floor(m['close'])
self.startingStrike[reqId] = np.floor(m['close'])
for m in self.tick2req.keys():
reqId = self.tick2req[m]
print(m, " ", self.closes[reqId], " ", self.epochs[reqId])
def chooseStocksHelper(self, t, contract, conId=None):
reqId = self.newReqId()
self.tick2req[t] = reqId
self.tickers[reqId] = t
self.contracts[reqId] = contract
self.closes[reqId] = None
self.epochs[reqId] = -1 # DO NOT initialize to None!!!
self.successfulStrikes = []
self.tickerDone[reqId] = False
c = Contract()
c.symbol = t
c.secType = "OPT"
c.exchange = "SMART"
c.lastTradeDateOrContractMonth = defaultParams["expiration"]
c.right = "CALL"
c.currency = "USD"
c.strike = None
self.requestedContracts[reqId] = c
self.receivedContracts[reqId] = []
self.startingStrike[reqId] = None
self.strikeDeviationMag[reqId] = None
self.strikeDeviationSgn[reqId] = 1
self.numAttempts[reqId] = 0
def error(self, reqId: TickerId, errorCode: int, errorString: str):
super().error(reqId, errorCode, errorString)
if "No security definition" in errorString:
# PICK UP HERE: try another strike
self.requestedContracts[reqId].strike += 1
self.reqContractDetails(reqId, self.requestedContracts[reqId])
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 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)
print("Got to Next Valid Id")
self.start()
def start(self):
"""Try moving this to rtWrapper.py"""
print("*****************************************************************************************************")
print("******************************************* Running start #44 *******************************************")
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.requestMgr()
for m in self.dataIds:
self.reqContractDetails(m, self.requestedContracts[m])
print("Executing requests ... finished")
def allTickersDone(self):
for m in self.tickerDone.keys():
if not self.tickerDone[m]:
return False
return True
def getNextTicker(self):
for m in self.tickerDone.keys():
if not self.tickerDone[m]:
return m
return None
def requestMgr(self, reqId=None):
"""This is the logical hub of the whole program"""
return
# print("Request Manager", self.reqMgrCount)
# self.reqMgrCount += 1
# if self.allTickersDone():
# self.done = True
# return
#
# if reqId is not None:
# if self.numAttempts[reqId] >= self.maxAttempts:
# self.done = True
# return
#
# nextTicker = self.getNextTicker()
# if nextTicker in self.tick2req.keys():
# reqId = self.tick2req[nextTicker]
# if reqId in self.requestedContracts.keys():
# self.reqContractDetails(reqId, self.requestedContracts[reqId])
def tryAnotherStrike(self, reqId):
self.requestedContracts[reqId].strike = str()
return
def contractDetails(self, reqId:int, contractDetails:ContractDetails):
oneContr = {
"ticker" : self.tickers[reqId],
"expiration" : contractDetails.summary.lastTradeDateOrContractMonth,
"strike" : contractDetails.summary.strike,
"right" : "CALL",
"exchange" : contractDetails.summary.exchange,
"validExchanges": contractDetails.validExchanges,
"underConId" : contractDetails.underConId,
"underSymbol" : contractDetails.underSymbol,
"underSecType" : contractDetails.underSecType,
"contractMonth" : contractDetails.contractMonth,
"tradingHours" : contractDetails.tradingHours,
"liquidHours" : contractDetails.liquidHours,
"currency" : contractDetails.summary.currency,
"epochAdded" : '',
"dateAdded" : '',
"notes" : contractDetails.notes
}
self.posts.insert_one(oneContr)
# print("Contract Details: ", contractDetails)
# with open("./contract-details.txt", 'a+') as f:
# f.write(str(contractDetails)+"\n")
def contractDetailsEnd(self, reqId:int):
print("Finished with: ", reqId)
self.tickerDone[reqId] = True
if self.allTickersDone():
for m in self.posts.find({"right":"CALL"}):
print(m)
self.done = True
#self.requestMgr(reqId)
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 = contractMiner(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()
# Print Trades to Screen:
print("\n"*20)
if __name__ == "__main__":
main()