예제 #1
0
    def GetStockIndustry(self, inputTradingDay, strStock):
        dtTradingDay = dateTime.ToDateTime(inputTradingDay)
        strSi = '-'
        if strStock in self.__dfData[0].index:        
            dfStock = self.__dfData[0].ix[strStock]

            if isinstance(dfStock, pd.Series):
                dfLoop = dfStock
                dtIn = dateTime.ToDateTime(dfLoop[self.__colIn])
                if dtTradingDay >= dtIn:
                    if dfLoop[self.__colCurSign] == '1' or dtTradingDay <= dateTime.ToDateTime(dfLoop[self.__colOut]):
                        strSi = dfLoop[self.__colIndCode]
            else:
                nIdxLen = len(dfStock.index)
                nIndex = 0
                while nIndex < nIdxLen:
                    dfLoop = dfStock.ix[nIndex]
                    nIndex += 1
                    dtIn = dateTime.ToDateTime(dfLoop[self.__colIn])
                    if dtTradingDay < dtIn:
                        continue
                    else:
                        if dfLoop[self.__colCurSign] == '1' or dtTradingDay <= dateTime.ToDateTime(dfLoop[self.__colOut]):
                            strSi = dfLoop[self.__colIndCode]
                            break
        return strSi
예제 #2
0
    def GenerateTdIndex(self):
        print('CStockPoolManager.GenerateTdIndex ...')
        nLoopIndex = 0
        nShowPercent = 0
        for nStockId, listElems in self.__dictSpElems.items():
            nLoopPercent = int(nLoopIndex * 100 / len(self.__dictSpElems))
            if (nShowPercent < nLoopPercent and nLoopPercent % 10 == 0):
                nShowPercent = int(nLoopIndex * 100 / len(self.__dictSpElems))
                print(str(nShowPercent) + '%')
            # print(nLoopIndex, '/', len(self.__dictSpElems))
            nLoopIndex += 1

            for elem in listElems:
                nInDate = elem.GetInDate()
                nOutDate = elem.GetOutDate()

                nLoopDate = nInDate
                while (nLoopDate <= nOutDate):
                    if ((nLoopDate in self.__dictSpElemsByTd) == False):
                        self.__dictSpElemsByTd[nLoopDate] = list()
                    self.__dictSpElemsByTd[nLoopDate].append(nStockId)
                    # Next Date
                    dateLoopDate = dateTime.ToDateTime(nLoopDate)
                    dateTomorrow = dateLoopDate + datetime.timedelta(days=1)
                    nLoopDate = int(dateTime.ToIso(dateTomorrow))
예제 #3
0
 def GetTradingDayList(self, strExchange, dtFrom, dtTo, bDateTime=False):
     listRtn = []
     nTradingDay = int(dateTime.ToIso(dtFrom))
     nTdTo = int(dateTime.ToIso(dtTo))
     if self.IsTradingDay(strExchange, nTradingDay) == True:
         if bDateTime == True:
             listRtn.append(dateTime.ToDateTime(dtFrom))
         else:
             listRtn.append(nTradingDay)
     while True:
         bSuccess, nTradingDay = self.GetNextTradingDay(
             strExchange, nTradingDay)
         if bSuccess == False or nTradingDay > nTdTo:
             break
         if bDateTime == True:
             dtTradingDay = dateTime.ToDateTime(nTradingDay)
             listRtn.append(dtTradingDay)
         else:
             listRtn.append(nTradingDay)
     return listRtn
예제 #4
0
    def GetPreTradingDay(self, strExchange, tradingDay):
        dtTradingDay = dateTime.ToDateTime(tradingDay)
        nTradingDay = int(dateTime.ToIso(tradingDay))
        if nTradingday <= self.__dictTc[strExchange][0]:
            return False, 0

        while True:
            dtTradingDay = Yestoday(dtTradingDay)
            nTradingDay = int(dateTime.ToIso(dtTradingDay))
            if self.IsTradingDay(nTradingDay) == True:
                return True, nTradingDay
예제 #5
0
    def GetStocksByDatePeriod(self, dtFrom, dtTo):
        if (isinstance(self.__dfData, DataFrame) == False
                or self.__dfData.empty):
            return None
        dtFromFix = dateTime.ToDateTime(dtFrom)
        dtToFix = dateTime.ToDateTime(dtTo)
        if (dtFromFix == None or dtToFix == None):
            return None

        setRtn = set()
        # datePeriod = dateTime.CDatePeriod(dtFromFix, dtToFix)
        for index, row in self.__dfData.iterrows():
            dtSpBegin = index[0]
            dtSpEnd = index[1]

            if (dtSpEnd < dtFromFix or dtSpBegin > dtToFix):
                continue

            setRtn.add(row['STOCK_WINDCODE'])
        return setRtn
예제 #6
0
    def GetStocksByTd(self, inputDate):
        if (isinstance(self.__dfData, DataFrame) == False
                or self.__dfData.empty):
            return None
        dtTd = dateTime.ToDateTime(inputDate)
        if (dtTd == None):
            return None

        setRtn = set()
        for index, row in self.__dfData.iterrows():
            if (dtTd >= index[0] and dtTd <= index[1]):
                setRtn.add(row['STOCK_WINDCODE'])
        return setRtn
예제 #7
0
def DBReqStockEODPrice(strHost, strUser, strPwd, strDb, listStocks, strDateFrom = '', strDateTo = ''):
    dbInst = windDb.CWindDb(strHost, strUser, strPwd, strDb)
    listStockEODPrice = dbInst.DBReqStockEODPrice(listStocks, strDateFrom, strDateTo)
    # print('DBReqStockEODPrice() return [', len(listStockEODPrice), '] records')

    mdbarMgr = mdBar.CMdBarDataManager()
    for row in listStockEODPrice:
        if (len(row) != 9):
            continue
        # print(row)
        dtTd = dateTime.ToDateTime(row[1])
        if (dtTd == None):
            print('DBReqStockEODPrice.tradingDay error: ', row)
        mdbarData = mdBar.CMdBarData(mdBar.EU_MdBarInterval.mdbi_1d, float(row[2]), float(row[3]), float(row[4]), float(row[5]), float(row[6]), float(row[7]), float(row[8]), dtTd)
        mdbarMgr.Add(row[0], mdbarData)
    # mdbarMgr.Print()
    return True
예제 #8
0
    def Add(self, strStockWindCode, mdBarData):
        nStockId = stockCn.StockWindCode2Int(strStockWindCode)
        if (nStockId == None):
            return False
        if (isinstance(mdBarData, CMdBarData) == False):
            return False

        euMdBi = mdBarData.GetInterval()
        if (euMdBi not in self.__dictMdBarData.keys()):
            self.__dictMdBarData[euMdBi] = {}
        if (nStockId not in self.__dictMdBarData[euMdBi]):
            self.__dictMdBarData[euMdBi][nStockId] = {}

        dtDateTime = dateTime.ToDateTime(mdBarData.GetDateTime())
        if (dtDateTime == None):
            return False
        self.__dictMdBarData[euMdBi][nStockId][dtDateTime] = mdBarData
        return True
예제 #9
0
 def RemoveBefore(self, inputBefore):
     dtBefore = inputBefore
     print(1)
     if (isinstance(inputBefore, datetime.datetime) == False):
         print(2)
         dtBefore = dateTime.ToDateTime(inputBefore)
         if (dtBefore == None):
             print(3, dtBefore, inputBefore)
             return False
     print(4)
     for keyMdbi in list(self.__dictMdBarData.keys()):
         for keyStockId in list(self.__dictMdBarData[keyMdbi].keys()):
             for keyTd in list(
                     self.__dictMdBarData[keyMdbi][keyStockId].keys()):
                 if (keyTd < dtBefore):
                     del self.__dictMdBarData[keyMdbi][keyStockId][keyTd]
     print(5)
     # self.Print()
     pass
예제 #10
0
 def __IsIn(self, euMdbi, strStockWindCode, strTradingDay):
     nStockId = stockCn.StockWindCode2Int(strStockWindCode)
     if (nStockId == None):
         print('nStockId is None')
         return False
     dtDateTime = dateTime.ToDateTime(strTradingDay)
     if (dtDateTime == None):
         print('dtDateTime is None')
         return False
     if (euMdbi not in self.__dictMdBarData.keys()):
         print('euMdbi is not in: ', euMdi, self.__dictMdBarData.keys())
         return False
     if (nStockId not in self.__dictMdBarData[euMdbi].keys()):
         # print('nStockId is not in: ', nStockId, self.__dictMdBarData[euMdbi].keys())
         return False
     if (dtDateTime not in self.__dictMdBarData[euMdbi][nStockId].keys()):
         # print('dtDateTime is not in: ', dtDateTime, strStockWindCode, self.__dictMdBarData[euMdbi][nStockId].keys())
         return False
     return True
예제 #11
0
    def GetStockReportPeriod(inputTradingDay):
        dtTd = dateTime.ToDateTime(inputTradingDay)
        dtInput = dtTd.date()
        nYearInput = dtInput.year

        dt0 = datetime.date(nYearInput - 1, 11, 1)
        dt1 = datetime.date(nYearInput, 4, 1)
        dt2 = datetime.date(nYearInput, 5, 1)
        dt3 = datetime.date(nYearInput, 9, 1)
        dt4 = datetime.date(nYearInput, 11, 1)
        dt5 = datetime.date(nYearInput + 1, 4, 1)

        dtRtn = None
        if (dtInput >= dt0 and dtInput < dt1):
            dtRtn = datetime.date(nYearInput - 1, 9, 30)
        elif (dtInput >= dt1 and dtInput < dt2):
            dtRtn = datetime.date(nYearInput - 1, 12, 31)
        elif (dtInput >= dt2 and dtInput < dt3):
            dtRtn = datetime.date(nYearInput, 3, 31)
        elif (dtInput >= dt3 and dtInput < dt4):
            dtRtn = datetime.date(nYearInput, 6, 30)
        elif (dtInput >= dt4 and dtInput < dt5):
            dtRtn = datetime.date(nYearInput, 9, 30)
        return dtRtn
예제 #12
0
 def GetVolume(self, euMdbi, strStockWindCode, strTradingDay):
     dtDateTime = dateTime.ToDateTime(strTradingDay)
     nStockId = stockCn.StockWindCode2Int(strStockWindCode)
     if (self.__IsIn(euMdbi, nStockId, dtDateTime) == False):
         return None
     return self.__dictMdBarData[euMdbi][nStockId][dtDateTime].GetVolume()
예제 #13
0
def CalcIndex(strDbHost, strDbUserName, strDbPasswd, strDbDatabase,
              strDateFrom, strDateTo, strCsvFile, euCFI):
    # 初始化数据
    tcInst = tCal.CTradingCalendar()
    ## 资金总额,分配到不同的成分中
    dFundTotal = BaseIndexValue() * IndexMulti()

    ## 读取成分比重
    dfCFIndexWeight_NanHua = pd.read_csv(strCsvFile)
    nCFRowLen = len(dfCFIndexWeight_NanHua)
    nCFColLen = len(dfCFIndexWeight_NanHua.columns)
    nCFRowIndex = 0

    ## 行情数据;连续合约实际合约对应表
    ### 循环处理成分比重表示,第一循环从数据库读取数据
    dfCFPrice = pd.DataFrame()
    dfCFMapping = pd.DataFrame()

    dt1stDate = None
    dtLastDate = dateTime.ToDateTime(strDateTo)

    dictDailyIndexValue = {}
    dtDailyConstituent = pd.DataFrame()
    # 每次循环做一次一次指数成分调整
    while nCFRowIndex < nCFRowLen:
        dictFundByProduct = {}  # 各个品种分配的资金
        nColIndex = 0
        dtLoopFrom = None
        dtLoopTo = None

        # 处理起始日期 和 各个品种分配的初始资金
        while nColIndex < nCFColLen:
            strCol = dfCFIndexWeight_NanHua.columns[nColIndex]
            dValue = dfCFIndexWeight_NanHua[strCol][nCFRowIndex]
            if (strCol == 'TradingDay'):
                dtLoopFrom = dateTime.ToDateTime(
                    int(dfCFIndexWeight_NanHua[strCol][nCFRowIndex]))
            else:
                if dValue != 0:
                    dictFundByProduct[strCol] = dfCFIndexWeight_NanHua[strCol][
                        nCFRowIndex] * dFundTotal
            nColIndex += 1

        # 查询数据库, 初始化数据
        if (nCFRowIndex == 0):
            dt1stDate = dtLoopFrom
            dfCFPrice, dfCFMapping = InitData_Common(strDbHost, strDbUserName,
                                                     strDbPasswd,
                                                     strDbDatabase,
                                                     dateTime.ToIso(dt1stDate),
                                                     strDateTo)

        # 处理结束日期
        if nCFRowIndex + 1 == nCFRowLen:
            dtLoopTo = dtLastDate
        else:
            dtLoopTo = dateTime.ToDateTime(
                int(dfCFIndexWeight_NanHua['TradingDay'][nCFRowIndex + 1]))

        if tcInst.IsTradingDay('SHFE', dateTime.ToIso(dtLoopFrom)) == False:
            bSuccess, nRtn = tcInst.GetNextTradingDay('SHFE', dtLoopFrom)
            if bSuccess == True:
                dtLoopFrom = dateTime.ToDateTime(nRtn)
            else:
                print('tcInst.GetNextTradingDay failed', 'SHFE', dtLoopFrom)
                break
        if tcInst.IsTradingDay('SHFE', dateTime.ToIso(dtLoopTo)) == False:
            bSuccess, nRtn = tcInst.GetNextTradingDay('SHFE', dtLoopTo)
            if bSuccess == True:
                dtLoopTo = dateTime.ToDateTime(nRtn)
            else:
                print('tcInst.GetNextTradingDay failed', 'SHFE', dtLoopTo)
                break

        print(
            '--------------------------------------------------------------------'
        )
        print('Processing From ' + dateTime.ToIso(dtLoopFrom) + ' To ' +
              dateTime.ToIso(dtLoopTo))

        # 处理每个成分
        dfLoopAll = pd.DataFrame()
        for strProduct in dictFundByProduct.keys():
            # 连续合约与现实合约进行对应
            for idxProd in dfCFMapping.ix[strProduct].index:
                # 期货合约
                strInstrument = dfCFMapping['FS_MAPPING_WINDCODE'][strProduct][
                    idxProd]
                if strInstrument not in dfCFPrice.index.levels[0]:
                    continue
                strExchange = GetExchange(strProduct)
                if strExchange == '':
                    print('xxxxxx Exchange Is NULL: ', strProduct)
                    continue

                # 日期处理, 只处理本次循环的
                bIncludeTailRow = True
                dtProductFrom = idxProd[0]
                dtProductTo = idxProd[1]
                if dtProductFrom > dtLoopTo:
                    break
                if dtProductTo < dtLoopFrom:
                    continue
                if dtProductFrom < dtLoopFrom:
                    dtProductFrom = dtLoopFrom
                if dtProductTo >= dtLoopTo:
                    dtProductTo = dtLoopTo
                else:
                    bSuccess, nProductTo = tcInst.GetNextTradingDay(
                        strExchange, dtProductTo)
                    if bSuccess == False:
                        continue
                    dtProductTo = dateTime.ToDateTime(nProductTo)
                    bIncludeTailRow = False

                # 获取时间区间的行情数据
                listDates = tcInst.GetTradingDayList(strExchange,
                                                     dtProductFrom,
                                                     dtProductTo, True)
                dfLoopMd = dfCFPrice.ix[strInstrument].ix[listDates].dropna()
                # 处理缺失的行情数据,使用昨收盘和昨结算
                if len(list(set(listDates).difference(dfLoopMd.index))) >= 1:
                    dateLastTd = None
                    for keyDate in listDates:
                        if keyDate not in dfLoopMd.index:
                            if dateLastTd == None:
                                bSuccess, nLastTd = tcInst.GetPreTradingDay(
                                    strExchange, keyDate)
                                if (bSuccess == None):
                                    print(
                                        'xxxxxx Last TradingDay Md Is NULL: ' +
                                        strInstrument + ' ' + str(keyDate))
                                    continue
                                dateLastTd = dateTime.ToDateTime(nLastTd)

                            dClose = dfLoopMd['S_DQ_CLOSE'][dateLastTd]
                            dSettle = dfLoopMd['S_DQ_SETTLE'][dateLastTd]
                            dfToday = pd.DataFrame(
                                {
                                    'S_DQ_CLOSE': [dClose],
                                    'S_DQ_SETTLE': [dSettle]
                                },
                                index=[keyDate])
                            dfToday.index.names = ['TRADE_DT']
                            dfLoopMd = pd.concat([dfLoopMd, dfToday],
                                                 axis=0,
                                                 join='outer')
                        dateLastTd = keyDate

                dfLoopMd['InstrumentId'] = strInstrument
                dfLoopMd['DominantContract'] = strProduct
                dfLoopMd['Volume'] = np.nan
                dfLoopMd['Value'] = np.nan
                dfLoopMd.reset_index(inplace=True)
                dfLoopMd = dfLoopMd.set_index(['DominantContract', 'TRADE_DT'])
                dfLoopMd = dfLoopMd.sortlevel(1)

                dPrice = dfLoopMd['S_DQ_CLOSE'][strProduct][dtProductFrom]
                dVolume = dictFundByProduct[strProduct] / dPrice
                dfLoopMd['Volume'][strProduct][
                    dtProductFrom:dtProductTo] = dVolume
                dfLoopMd['Value'][strProduct][dtProductFrom:dtProductTo] = \
                    dfLoopMd['S_DQ_CLOSE'][strProduct][dtProductFrom:dtProductTo] * \
                    dfLoopMd['Volume'][strProduct][dtProductFrom:dtProductTo]

                dfLoopMd['Value'][strProduct][
                    dtProductFrom] = dictFundByProduct[strProduct]
                dictFundByProduct[strProduct] = dfLoopMd['Value'][strProduct][
                    dtProductTo]

                dfLoopAll = pd.concat([dfLoopAll, dfLoopMd[:-1]],
                                      axis=0,
                                      join='outer')

        dfSwap = dfLoopAll.swaplevel(0, 1)
        dfSwap['Weight'] = np.nan
        for dtKey in dfSwap['Value'].index.levels[0]:
            total = dfSwap['Value'][dtKey].sum()
            dictDailyIndexValue[dtKey] = total
            print(str(dtKey) + ' ' + str(dfSwap['Value'][dtKey].sum()))
            dfSwap['Weight'][dtKey] = dfSwap['Value'][dtKey] / total
            dFundTotal = total
        nCFRowIndex += 1
        dtDailyConstituent = pd.concat([dtDailyConstituent, dfSwap],
                                       axis=0,
                                       join='outer')

    # 处理每日每个品种的权重
    dtDailyConstituent = dtDailyConstituent.swaplevel(0, 1)
    print('Dump Index Value into index_daily.csv')
    fIndex = open('index_daily.csv', 'w')
    for key in sorted(dictDailyIndexValue.keys()):
        strOutput = dateTime.ToIso(key) + ',' + str(
            dictDailyIndexValue[key] / IndexMulti()) + ',' + str(
                euCFI.value) + '\n'
        fIndex.write(strOutput)
    fIndex.close()

    del dtDailyConstituent['Volume']
    del dtDailyConstituent['Value']

    print('Dump Index Daily Constituent into index_daily_constituent.csv')
    dtDailyConstituent['IndexType'] = euCFI.value
    dtDailyConstituent.to_csv('index_daily_constituent.csv', header=False)