Esempio n. 1
0
def getAllStockMst() :
    import time

    from cxCybosPlus import constants
    from cxCybosPlus import gCybosPlusClassDic
    from cxStockMgr  import stockMgr
    cpCybos = gCybosPlusClassDic['cxCpCybos']
    stockMst = gCybosPlusClassDic['cxCpStockMst']
    stockList = stockMgr.getStockList()

    sep = u'\t'
    newLine = u'\n'
    
    fileName = u'StockMst.des'
    if common.checkFileExist(fileName) == True :
        os.remove(fileName)
    stockMstFile = cxFile(fileName)
    strLine = u''
    
    for key in stockMst.headerIndexDic.keys() :
        strLine += stockMst.headerIndexDic[key][1] + sep 
    strLine += newLine
    print strLine
    stockMstFile.write(strLine)
   
    #stockList = [[u'A000660',u'SK하이닉스']]
    for stockItem in stockList :
        print stockItem[0]
        stockMst.SetInputValue(u'0',stockItem[0])
        stockMst.BlockRequest()
        result = stockMst.getResult()
        
        print 'getResult (',result[0],')'
        
        if result[0] != 0 :
            print stockItem[0], ': BlockRequest is failed (%d).'%(result[0])
            continue
        
        strLine = u''
        for key in stockMst.headerIndexDic.keys() :
            strLine += unicode(result[5][0][key][2]) + sep
        strLine += newLine
        print strLine
        stockMstFile.write(strLine)
        
        remainCount = cpCybos.GetLimitRemainCount(constants.LT_NONTRADE_REQUEST)
        remainTime = cpCybos.LimitRequestRemainTime()
        print remainCount, remainTime
        if remainCount <= 0 :
            time.sleep((remainTime/1000)+1)

    stockMstFile.close()
Esempio n. 2
0
    def loadLogData(self, stockCode, chartType ) :

        try :
            ct = self.chartTypeDic[chartType]
        except KeyError :
            print 'ERROR: cxEmulator.loadLogData : param chartType : "%s" is not valid.\n'%\
                    (chartType) 
            return None

        cpStockCode = self.cpClsDic['cxCpStockCode']

        stockName = cpStockCode.CodeToName(stockCode)
        #print '"%s"'%(stockName)
        if stockName == u'' :
            print 'Can not find stock name for stock code "%s".'%(stockCode)
            return

        path = 'log\\%s\\'%(chartType.lower())

        fileName = u'%s%s_%s.log'%(path,stockCode,stockName)

        print fileName 

        if checkFileExist(fileName) == False : return None

        dataFile = cxFile(fileName)

        tmpList = []
        dataList = []
        i = 0
        for lines in dataFile.readlines() :
            if i < 5 : 
                i += 1
                continue
            tmpList = []
            for item in lines[:-1].split() :
                tmpList.append(item)
            dataList.append(tmpList)
        dataFile.close()
        del dataFile

        #print 'len of dataList', len(dataList) 
        #print dataList[0]
        #print dataList[len(dataList)-1]
        return dataList
Esempio n. 3
0
    def updateData(self,
                    argStockCode,
                    argChartType,
                    argFileName = u'') :
        import common
        import os
        cpStockCode = self.cpClsDic['cxCpStockCode']
        stockName = cpStockCode.CodeToName(argStockCode)
        if stockName == u'' :
            return E_FIND_STOCK_NAME
        chartType = self.getChartTypeParam(argChartType)
        if chartType == u'' :
            return E_FIND_CHART_TYPE
        if argFileName == u'' :
            path = u'log\\%s\\'%(chartType.lower())
            if os.path.exists(path) == False :
                os.makedirs(path)
            fileName = u'%s%s_%s.log' %\
                        ( path,
                          argStockCode,
                          stockName )
        else :
            fileName = argFileName
        if common.checkFileExist(fileName) == True :
            os.remove(fileName)
            #return W_FILE_EXIST
        
        fieldList = [
            0,  # 날짜
            1,  # 시간
            3,  # 고가
            4,  # 저가
            5,  # 종가
            8,  # 거래량
            9,  # 거래대금
        ]   # refer to CybosPlus manual
        
        paramList = [
            [   0,  argStockCode    ],
            [   1,  ord(u'1')       ],  # 기간요청
            [   3,  19500101        ],  # 1950.01.01 ~
#            [   3,  20121124        ],  # 1950.01.01 ~
            [   4,  len(fieldList)  ],
            [5] + fieldList          ,  # Field List
            [   6,  ord(chartType)  ],  # Chart Type
            [   9,  ord(u'1')       ],  # 수정주가
            [   10, ord(u'3')       ],  # 시간외 거래량 모두 제외
        ]
        stockChart = self.cpClsDic[u'cxStockChart']
        resultList = common.templateBlockRequest( stockChart,
                                                  paramList )
        if resultList == None :
            return E_BLOCKREQUEST_RESULT_NONE
        if len(resultList) == 0 :
            return E_BLOCKREQUEST_RESULT_BLANK
        
        
        #print common.dumpList(resultList)
        bFirstLoop = True
        dataNum = 0
        fieldNameList = []
        fileHeader = []
        fileData = []
        for result in resultList :
            #print common.getResultStringLandscape( result,
            #                                      statusOption=1,
            #                                      headerValue=1,
            #                                      dataValue=1,
            #                                      titleOption=1 )
            dibStatus = common.getResultDibStatus(result)
            if dibStatus != 0 :
                return E_DIB_STATUS
            dataList = common.getResultDataList(result)
            
            for dataDic in dataList :
                tempList = []
                for key in dataDic.keys() :
                    tempList.append(dataDic[key][2])
                    if bFirstLoop == True :
                        fieldNameList.append(dataDic[key][1])
                fileData.append(tempList)
                bFirstLoop = False
            fileHeader.append(fieldNameList)
        #end of 'for result in resultList :'
        #print common.dumpList(fieldNameList)
        #print common.dumpList(fileData)
        """
        [0] : field list
        [1] ~ : data
        """

        from cxFile import cxFile
        dataLogFile = cxFile(fileName)
        
        fieldNameString = u''
        for fieldName in fieldNameList :
            fieldNameString += fieldName + u' '
        fieldNameString += u'\n'
        dataLogFile.write(fieldNameString)
        #print fieldNameString
        
        for i in range(len(fileData)-1,-1,-1):
            itemString = u''
            itemList = fileData[i]
            for item in itemList :
                itemString += common.UNI(item) + u' '
            itemString += u'\n'
            dataLogFile.write(itemString)
            #print itemString
        dataLogFile.close()
        return 0 
Esempio n. 4
0
    def makeLogData(self, stockCode, chartType ) :

        cpStockCode = self.cpClsDic['cxCpStockCode']
        stockName = cpStockCode.CodeToName(stockCode)
        #print '"%s"'%(stockName)
        if stockName == u'' :
            print 'Can not find stock name for stock code "%s".'%(stockCode)
            return False
        path = 'log\\%s\\'%(chartType.lower())
        fileName = u'%s%s_%s.log'%(path,stockCode,stockName)
        if checkFileExist(fileName) == True :
            return True
        try :
            ct = self.chartTypeDic[chartType]
        except KeyError :
            print 'ERROR: cxStockLogData.makeLogData : param chartType : "%s" is not valid.\n'%\
                    (chartType) 
            return False

        fieldList = [ 
            0, # 날짜
            1, # 시간
            3, # 고가
            4, # 저가
            5, # 종가
            8, # 거래량
            9, # 거래대금
            25, # 주식회전율
        ]

        #if ct == 'T' or ct == 'm' :
        #    fieldList += [1]  # 시간 - hhmm

        paramList = [
            [ 0,    stockCode       ],
            [ 1,    ord(u'1')       ],  # 기간요청
            [ 3,    19500101        ],
            [ 4,    len(fieldList)  ],
            [ 5 ] + fieldList,
            [ 6,    ord(ct)         ],  # 차트종류
            [ 9,    ord(u'1')       ],  # 수정주가
            [ 10,   ord(u'3')       ]   # 시간외거래량 모두 제외
        ]

        stockChart = getCybosPlusClassDic()[u'cxStockChart']

        resultList = templateBlockRequest( stockChart, paramList )

        if resultList == None :
            print 'templateBlockRequest("cxStockChart"): result is none.\n'
            return False

        if len(resultList) == 0 :
            print 'templateBlockRequest("cxStockChart") : result length is zero.\n'
            return False

        bFirst = 1
        fieldNum = 0
        storeList = []
        dataNum = 0

        for result in resultList :
            if getResultDibStatus(result) != 0 :
                break
            headerList = result[5]
            if len(headerList) == 0 :
                print 'header result is empty.'
                continue
            dataNum += headerList[0][3][2]
            print 'dataNum', dataNum, type(dataNum)
            if bFirst == 1 :
                fieldNum = headerList[0][1][2]
                fieldNameList = headerList[0][2][2]
                print 'fieldNum', fieldNum, type(fieldNum)
                print 'fieldNameList'
                for fieldName in fieldNameList :
                    print fieldName,
                print
                
                storeList.insert(3,fieldNum)
                storeList.insert(4,fieldNameList)

                bFirst = 0

            dataList = result[6]
            print 'len of dataList', len(dataList)

            if len(dataList) == 0 : 
                print 'failed to make data list for %s'%(stockCode)
                self.failedList.append([stockCode,chartType])
                return True
                #return False

            tmpList = []
            for dataDic in dataList :
                tmpList = [] 
                for fieldType in range( 0, fieldNum ) :
                    key = stockChart.fieldNameDic[fieldNameList[fieldType]]
                    tmpList.append( dataDic[key][2] )
                storeList.append(tmpList)


        storeList.insert(0,dataNum)
        storeList.insert(1,storeList[len(storeList)-1][0])
        storeList.insert(2,storeList[4][0])
        print 'len of storeList',len(storeList)
        #print storeList

        """
        [0] : dataNum
        [1] : start date
        [2] : end date
        [3] : fieldNum
        [4] : field Name List
        [5] ~ : data
        """

        print fileName 

        dataFile = cxFile(fileName)

        dataFile.write('%s\n'%(storeList[0]))
        dataFile.write('%s\n'%(storeList[1]))
        dataFile.write('%s\n'%(storeList[2]))
        dataFile.write('%s\n'%(storeList[3]))

        for fieldName in storeList[4] :
            dataFile.write('%s\t'%(fieldName))
        dataFile.write('\n')
        tmpList = storeList[5:]
        tmpList.reverse()
        for itemList in tmpList :
            for item in itemList :
                dataFile.write('%s\t'%(item))
            dataFile.write('\n')

        dataFile.close()
        del dataFile

        return True
Esempio n. 5
0
    def makeLogData(self, stockCode, chartType ) :

        cpStockCode = self.cpClsDic['cxCpStockCode']

        stockName = cpStockCode.CodeToName(stockCode)
        #print '"%s"'%(stockName)
        if stockName == u'' :
            print 'Can not find stock name for stock code "%s".'%(stockCode)
            return False

        path = 'log\\%s\\'%(chartType.lower())

        fileName = u'%s%s_%s.log'%(path,stockCode,stockName)

        if checkFileExist(fileName) == True :
            return True

        try :
            ct = self.chartTypeDic[chartType]
        except KeyError :
            print 'ERROR: cxEmulator.makeLogData : param chartType : "%s" is not valid.\n'%\
                    (chartType) 
            return False

        fieldList = [ 
            0, # 날짜
            1, # 시간
            3, # 고가
            4, # 저가
            5, # 종가
            8, # 거래량
            9, # 거래대금
            25, # 주식회전율
        ]

        #if ct == 'T' or ct == 'm' :
        #    fieldList += [1]  # 시간 - hhmm

        paramList = [
            [ 0,    stockCode       ],
            [ 1,    ord(u'1')       ],  # 기간요청
            [ 3,    19500101        ],
            [ 4,    len(fieldList)  ],
            [ 5 ] + fieldList,
            [ 6,    ord(ct)         ],  # 차트종류
            [ 9,    ord(u'1')       ],  # 수정주가
            [ 10,   ord(u'3')       ]   # 시간외거래량 모두 제외
        ]

        stockChart = getCybosPlusClassDic()[u'cxStockChart']

        resultList = templateBlockRequest( stockChart, paramList )

        if resultList == None :
            print 'templateBlockRequest("cxStockChart"): result is none.\n'
            return False

        if len(resultList) == 0 :
            print 'templateBlockRequest("cxStockChart") : result length is zero.\n'
            return False

        bFirst = 1
        fieldNum = 0
        storeList = []
        dataNum = 0

        for result in resultList :
            if getResultDibStatus(result) != 0 :
                break
            headerList = result[5]
            if len(headerList) == 0 :
                print 'header result is empty.'
                continue
            dataNum += headerList[0][3][2]
            print 'dataNum', dataNum, type(dataNum)
            if bFirst == 1 :
                fieldNum = headerList[0][1][2]
                fieldNameList = headerList[0][2][2]
                print 'fieldNum', fieldNum, type(fieldNum)
                print 'fieldNameList'
                for fieldName in fieldNameList :
                    print fieldName,
                print
                
                storeList.insert(3,fieldNum)
                storeList.insert(4,fieldNameList)

                bFirst = 0

            dataList = result[6]
            print 'len of dataList', len(dataList)

            if len(dataList) == 0 : 
                print 'failed to make data list for %s'%(stockCode)
                self.failedList.append([stockCode,chartType])
                return True
                #return False

            tmpList = []
            for dataDic in dataList :
                tmpList = [] 
                for fieldType in range( 0, fieldNum ) :
                    key = stockChart.fieldNameDic[fieldNameList[fieldType]]
                    tmpList.append( dataDic[key][2] )
                storeList.append(tmpList)


        storeList.insert(0,dataNum)
        storeList.insert(1,storeList[len(storeList)-1][0])
        storeList.insert(2,storeList[4][0])
        print 'len of storeList',len(storeList)
        #print storeList

        """
        [0] : dataNum
        [1] : start date
        [2] : end date
        [3] : fieldNum
        [4] : field Name List
        [5] ~ : data
        """


        print fileName 

        dataFile = cxFile(fileName)

        dataFile.write('%s\n'%(storeList[0]))
        dataFile.write('%s\n'%(storeList[1]))
        dataFile.write('%s\n'%(storeList[2]))
        dataFile.write('%s\n'%(storeList[3]))

        for fieldName in storeList[4] :
            dataFile.write('%s\t'%(fieldName))
        dataFile.write('\n')
        tmpList = storeList[5:]
        tmpList.reverse()
        for itemList in tmpList :
            for item in itemList :
                dataFile.write('%s\t'%(item))
            dataFile.write('\n')

        dataFile.close()
        del dataFile

        return True