def _ensure_index(self): """ Ensure indices for all databases and collections. first access self._dbs config to get index column names; then get collection names from self._collNames and loop over all collections. """ if self._collNames and self._dbs: try: for dbName in self._dbs: # Iterate over database configurations. db = self._dbs[dbName] dbSelf = db['self'] index = db['index'] collNames = self._collNames[db['collNames']] # db['self'] is the pymongo.Database object. for name in collNames: coll = dbSelf[name] coll.ensure_index([(index, pymongo.DESCENDING)], unique=True) print '[MONGOD]: MongoDB index set.' return 1 except KeyError: msg = '[MONGOD]: Unable to set collection indices; ' + \ 'infomation in Config.body["dbs"] is incomplete.' raise VNPAST_DatabaseError(msg) except Exception, e: msg = '[MONGOD]: Unable to set collection indices; ' + str(e) raise VNPAST_DatabaseError(msg)
def download_equity_M1(self, tasks, startYr=2012, endYr=2015): """ """ try: # map equity tickers to security IDs. if self._mapTickersToSecIDs: maps = self._mapTickersToSecIDs else: assert os.isfile('./names/secID.json') jsonFile = open(dName, 'r') allSecIds = json.loads(jsonFile.read()) jsonFile.close() allTickers = [s.split('.')[0] for s in allSecIds] maps = dict(zip(allTickers, allSecIds)) self._mapTickersToSecIDs = maps tasks_ = [maps[task] for task in tasks] db = self._dbs['EQU_M1']['self'] self._api.get_equity_M1_interMonth(db, id=1, startYr=startYr, endYr=endYr, tasks=tasks_) except AssertionError: msg = '[MONGOD]: Cannot map tickers to secIDs; ' + \ 'secID.json does not exist.' raise VNPAST_DatabaseError(msg) except Exception as e: msg = '[MONGOD]: Unable to download data; ' + str(e) raise VNPAST_DatabaseError(msg)
def _get_coll_names(self): """ get all instruments'names and store them in self._collNames. """ try: if not os.path.exists('names'): os.makedirs('names') self._collNames['equTicker'] = self._allEquTickers() self._collNames['fudTicker'] = self._allFudTickers() self._collNames['secID'] = self._allSecIds() self._collNames['futTicker'] = self._allFutTickers() self._collNames['optTicker'] = self._allOptTickers() self._collNames['idxTicker'] = self._allIdxTickers() print('[MONGOD]: Collection names gotten.') return 1 except AssertionError: warning = '[MONGOD]: Warning, collection names ' + \ 'is an empty list.' print(warning) except Exception as e: msg = '[MONGOD]: Unable to set collection names; ' + \ str(e) raise VNPAST_DatabaseError(msg)
def download_future_D1(self, start, end, sessionNum=30): """ """ try: db = self._dbs['FUT_D1']['self'] self._api.get_future_D1_mongod(db, start, end, sessionNum) except Exception, e: msg = '[MONGOD]: Unable to download data; ' + str(e) raise VNPAST_DatabaseError(msg)
def download_index_D1(self, start, end, sessionNum=30): """ """ try: db = self._dbs['IDX_D1']['self'] self._api.get_index_D1_overlord(db, 'mongod', start, end, sessionNum) except Exception, e: msg = '[MONGOD]: Unable to download data; ' + str(e) raise VNPAST_DatabaseError(msg)
def update_equity_D1_(self, sessionNum=30): """ """ try: # set databases and tickers db = self._dbs['EQU_D1']['self'] index = self._dbs['EQU_D1']['index'] allEquTickers = self._allEquTickers() coll = db[allEquTickers[0]] # find the latest timestamp in collection. latest = coll.find_one(sort=[(index, pymongo.DESCENDING)])[index] start = datetime.strftime(latest + timedelta(days=1), '%Y%m%d') end = datetime.strftime(datetime.now(), '%Y%m%d') # then download. self._api.get_equity_D1_mongod(db, start, end, sessionNum) except Exception as e: msg = '[MONGOD]: Unable to update data; ' + str(e) raise VNPAST_DatabaseError(msg)
def __update(self, key, target1, target2, sessionNum): """ Basic update method. Looks into the database specified by 'key', find the latest record in the collection of it. Then update the collections till last trading date. parameters ---------- * key: string; a database alias (refer to the database config) e.g., 'EQU_D1'. * target1: method; pointer to the function with which controller obtain all tickers in the database. Concretely, target1 are self._all#Tickers methods. * target2: method; pointer to the api overlord requesting functions i.e. self._api.get_###_mongod methods. * sessionNum: integer; the number of threads. """ try: # get databases and tickers db = self._dbs[key]['self'] index = self._dbs[key]['index'] allTickers = target1() coll = db[allTickers[0]] # find the latest timestamp in collection. latest = coll.find_one(sort=[(index, pymongo.DESCENDING)])[index] start = datetime.strftime(latest + timedelta(days=1), '%Y%m%d') end = datetime.strftime(datetime.now(), '%Y%m%d') # then download. target2(db, start, end, sessionNum) return db except Exception, e: msg = '[MONGOD]: Unable to update data; ' + str(e) raise VNPAST_DatabaseError(msg)