Exemple #1
0
def Mom(priceColName, momPrefix, momDaysList, endDate=None):
    if endDate is None:
        dtNow = dt.datetime.now() - dt.timedelta(days=1)
        endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
    else:
        endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
        if endDate.date() >= dt.datetime.now().date():
            endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
                      dt.timedelta(days=1)
    mongoConn = Public.GetPara('mongoConn')
    mc = pm.MongoClient(mongoConn)
    db = mc['factor']

    for days in momDaysList:
        facName = momPrefix + '_' + str(days)
        lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
        tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
        currentDate = lastUpdateDate - dt.timedelta(days=days)
        savedDate = lastUpdateDate
        priceDict = {}

        while currentDate + dt.timedelta(days=1) <= endDate:
            currentDate += dt.timedelta(days=1)
            # if currentDate not in tradingDateSet:
            #     continue
            # get data
            record = db[priceColName].find_one({'_id': currentDate})
            if record is None:
                continue
            for symbol, priceList in priceDict.items():
                priceList.append(np.nan)
            for symbol, price in record.items():
                if symbol[0] == '_':
                    continue
                if symbol not in priceDict.keys():
                    priceDict[symbol] = [np.nan]
                priceDict[symbol][-1] = price
            if currentDate <= lastUpdateDate:
                continue

            # evaluate momentum and save to db
            mongoDoc = {
                '_id': currentDate,
                '_updateTime': dt.datetime.now(),
                '_isTrade': (currentDate in tradingDateSet)
            }
            for symbol, priceList in priceDict.items():
                arr = np.array(priceList[max(0, len(priceList) - days):])
                if np.sum(
                        np.isfinite(arr)) < days / 3:  # data is not sufficient
                    continue
                mom = arr[-1] / np.nanmean(arr[0:len(arr) - 1])
                mongoDoc[symbol] = mom
            db[facName].save(mongoDoc)

            savedDate = currentDate
            print(facName + ' ' + str(currentDate))

        db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': savedDate})
Exemple #2
0
def Day(facNameList, colList, sql, endDate=None):
    if endDate is None:
        dtNow = dt.datetime.now() - dt.timedelta(days=1)
        endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
    else:
        endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
        if endDate.date() >= dt.datetime.now().date():
            endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
                      dt.timedelta(days=1)

    connStr = Public.GetPara('connStr')
    conn = co.connect(connStr)
    cursor = conn.cursor()
    mongoConn = Public.GetPara('mongoConn')
    mc = pm.MongoClient(mongoConn)
    db = mc['factor']

    # for each factor
    for f in range(len(facNameList)):
        facName = facNameList[f]
        col = colList[f]
        lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
        tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
        mongoDoc = db[facName].find_one({'_id': lastUpdateDate})
        mongoDoc = {} if mongoDoc is None else mongoDoc
        # save data day by day.
        currentDate = lastUpdateDate + dt.timedelta(days=1)
        while currentDate <= endDate:
            cursor.execute(
                sql.replace('{TRADE_DATE}', currentDate.strftime('%Y-%m-%d')))
            dataList = cursor.fetchall()
            isTrade = (currentDate in tradingDateSet)
            mongoDoc = {
            }  # if this statement is executed, invalid data will not be filled by previous value
            mongoDoc['_id'] = currentDate
            mongoDoc['_isTrade'] = isTrade
            mongoDoc['_updateTime'] = dt.datetime.now()
            for record in dataList:
                symbol = record[1]
                data = record[col]
                if data is not None:  # and np.isfinite(data):
                    mongoDoc[symbol] = data
            db[facName].save(mongoDoc)
            print(facName + ' ' + str(currentDate))
            currentDate += dt.timedelta(days=1)
        db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': endDate})

    ddd = 0
Exemple #3
0
def FinDer(configDictList, endDate):
    if endDate is None:
        dtNow = dt.datetime.now() - dt.timedelta(days=1)
        endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
    else:
        endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
        if endDate.date() >= dt.datetime.now().date():
            endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
                      dt.timedelta(days=1)

    mongoConn = Public.GetPara('mongoConn')
    mc = pm.MongoClient(mongoConn)
    db = mc['factor']
    # for each factor
    for configDict in configDictList:
        facName = configDict['facName']
        depFacNameList = configDict['depFacNameList']
        EvalFun = configDict['EvalFun']

        lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
        tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
        currentDate = lastUpdateDate + dt.timedelta(days=1)
        savedDate = lastUpdateDate
        while currentDate <= endDate:
            deficientData = False
            # get all dependent factor
            symbolSet = set()
            depFacListDict = {}
            for d in range(len(depFacNameList)):
                depFacName = depFacNameList[d]
                record = db[depFacName].find_one({'_id': currentDate})
                if record is None:
                    deficientData = True
                    break
                for symbol, value in record.items():
                    if symbol[0] == '_':
                        continue
                    if symbol not in depFacListDict.keys():
                        depFacListDict[symbol] = np.nan * np.zeros(
                            [len(depFacNameList)])
                    depFacListDict[symbol][
                        d] = value if value is not None else np.nan
                symbolSet = symbolSet | record.keys()
            if deficientData:
                break
            # evaluate factor value and save to db
            mongoDoc = {
                '_id': currentDate,
                '_isTrade': (currentDate in tradingDateSet),
                '_updateTime': dt.datetime.now()
            }
            for symbol in symbolSet:
                if symbol[0] == '_':  # system field
                    continue
                facValue = EvalFun(depFacListDict[symbol])
                mongoDoc[symbol] = facValue
            db[facName].save(mongoDoc)
            savedDate = currentDate

            print(facName + ' ' + str(currentDate))
            currentDate += dt.timedelta(days=1)

        db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': savedDate})
Exemple #4
0
def Beta(betaPrefix,
         betaDaysList,
         indexSymbolDict,
         indexSql,
         stockSql,
         endDate=None):
    if endDate is None:
        dtNow = dt.datetime.now() - dt.timedelta(days=1)
        endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
    else:
        endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
        if endDate.date() >= dt.datetime.now().date():
            endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
                      dt.timedelta(days=1)

    connStr = Public.GetPara('connStr')
    conn = co.connect(connStr)
    cursor = conn.cursor()
    mongoConn = Public.GetPara('mongoConn')
    mc = pm.MongoClient(mongoConn)
    db = mc['factor']

    for days in betaDaysList:
        for indexCode, indexSymbol in indexSymbolDict.items():
            facName = betaPrefix + '_' + indexSymbol + '_' + str(days)
            lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
            tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
            currentDate = lastUpdateDate - dt.timedelta(days=days)
            savedDate = lastUpdateDate
            indexRtnDict = {}
            stockRtnDict = {}
            while currentDate + dt.timedelta(days=1) <= endDate:
                currentDate += dt.timedelta(days=1)
                # if currentDate not in tradingDateSet:
                #     continue

                # get data
                for stockSymbol, stockRtnList in stockRtnDict.items():
                    stockRtnList.append(np.nan)
                for indexSymbol, indexRtnList in indexRtnDict.items():
                    indexRtnList.append(np.nan)

                cursor.execute(
                    stockSql.replace('{TRADE_DATE}',
                                     currentDate.strftime('%Y-%m-%d')))
                stockRtnRecordSet = cursor.fetchall()
                for stockRtnRecord in stockRtnRecordSet:
                    symbol = stockRtnRecord[1]
                    rtn = stockRtnRecord[4]
                    if symbol not in stockRtnDict.keys():
                        stockRtnDict[symbol] = [np.nan]
                    stockRtnDict[symbol][
                        -1] = rtn if rtn is not None else np.nan

                cursor.execute(
                    indexSql.replace('{TRADE_DATE}',
                                     currentDate.strftime('%Y-%m-%d')).replace(
                                         '{INDEX_CODE}', str(indexCode)))
                indexRtnRecordSet = cursor.fetchall()
                for indexRtnRecord in indexRtnRecordSet:
                    symbol = indexRtnRecord[1]
                    if symbol != indexSymbol:
                        raise Exception(
                            'index code is not fit to index symbol!')
                    rtn = indexRtnRecord[4]
                    if symbol not in indexRtnDict.keys():
                        indexRtnDict[symbol] = [np.nan]
                    indexRtnDict[symbol][
                        -1] = rtn if rtn is not None else np.nan

                if currentDate <= lastUpdateDate:
                    continue

                # evaluate beta and save to db
                if indexSymbol not in indexRtnDict.keys() or len(
                        indexRtnDict[indexSymbol]) < days / 2:
                    continue
                mongoDoc = {
                    '_id': currentDate,
                    '_updateTime': dt.datetime.now(),
                    '_isTrade': (currentDate in tradingDateSet)
                }
                indexArr = np.array(indexRtnDict[indexSymbol]
                                    [max(0,
                                         len(indexRtnDict[indexSymbol]) -
                                         days):],
                                    dtype=float)
                for stockSymbol, stockRtnList in stockRtnDict.items():
                    stockArr = np.array(
                        stockRtnList[max(0,
                                         len(stockRtnList) - days):],
                        dtype=float)
                    if len(stockArr) != len(indexArr):
                        continue
                    vld = np.isfinite(stockArr) * np.isfinite((indexArr))
                    if np.sum(vld) < days / 3:  # data is not sufficient
                        continue
                    const = np.ones(len(stockArr))
                    X = np.vstack([indexArr[vld], const[vld]]).T
                    y = stockArr[vld]
                    c, res, rank, s = np.linalg.lstsq(X, y)
                    beta = c[0]
                    mongoDoc[stockSymbol] = beta
                db[facName].save(mongoDoc)

                savedDate = currentDate
                print(facName + ' ' + str(currentDate))

            db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': savedDate})
Exemple #5
0
def Beta(tovPrefix, tovDaysList, stockSql, endDate=None):
    if endDate is None:
        dtNow = dt.datetime.now() - dt.timedelta(days=1)
        endDate = dt.datetime(dtNow.year, dtNow.month, dtNow.day)
    else:
        endDate = dt.datetime(endDate.year, endDate.month, endDate.day)
        if endDate.date() >= dt.datetime.now().date():
            endDate = dt.datetime(dt.datetime.now().year, dt.datetime.now().month, dt.datetime.now().day) - \
                      dt.timedelta(days=1)

    connStr = Public.GetPara('connStr')
    conn = co.connect(connStr)
    cursor = conn.cursor()
    mongoConn = Public.GetPara('mongoConn')
    mc = pm.MongoClient(mongoConn)
    db = mc['factor']

    for days in tovDaysList:
        facName = tovPrefix + '_' + str(days)
        lastUpdateDate = Public.GetLastUpdateDate(facName, mc)
        tradingDateSet = Public.GetCalendar(lastUpdateDate, endDate)
        currentDate = lastUpdateDate - dt.timedelta(days=days)
        savedDate = lastUpdateDate
        stockRtnDict = {}
        while currentDate + dt.timedelta(days=1) <= endDate:
            currentDate += dt.timedelta(days=1)
            # if currentDate not in tradingDateSet:
            #     continue

            # get data
            for stockSymbol, stockRtnList in stockRtnDict.items():
                stockRtnList.append(np.nan)

            cursor.execute(
                stockSql.replace('{TRADE_DATE}',
                                 currentDate.strftime('%Y-%m-%d')))
            stockRtnRecordSet = cursor.fetchall()
            for stockRtnRecord in stockRtnRecordSet:
                symbol = stockRtnRecord[1]
                tov = stockRtnRecord[4]
                if symbol not in stockRtnDict.keys():
                    stockRtnDict[symbol] = [np.nan]
                stockRtnDict[symbol][-1] = tov if tov is not None else np.nan

            if currentDate <= lastUpdateDate:
                continue

            # evaluate beta and save to db
            mongoDoc = {
                '_id': currentDate,
                '_updateTime': dt.datetime.now(),
                '_isTrade': (currentDate in tradingDateSet)
            }
            for stockSymbol, stockRtnList in stockRtnDict.items():
                stockArr = np.array(stockRtnList[max(0,
                                                     len(stockRtnList) -
                                                     days):],
                                    dtype=float)
                tov = np.nanmean(stockArr)
                mongoDoc[stockSymbol] = tov
            db[facName].save(mongoDoc)

            savedDate = currentDate
            print(facName + ' ' + str(currentDate))

        db.cfgUpdate.save({'_id': facName, 'lastUpdateDate': savedDate})