def update( self ) : self.reset() cpStockCode = cxCpStockCode() self.num = cpStockCode.GetCount() + 1 # add 0'th stock item for i in range( 0, self.num ) : stockItem = [] stockItem.append( cpStockCode.GetData( 0, i ) ) # stockCode stockItem.append( cpStockCode.GetData( 1, i ) ) # stockName stockItem.append( cpStockCode.GetData( 2, i ) ) # stockFullCode self.data.append( stockItem ) self.updateTime = unicode(time.strftime('%Y%m%d%H%M%S')) del cpStockCode
def update(self): self.reset() cpStockCode = cxCpStockCode() self.num = cpStockCode.GetCount() + 1 # add 0'th stock item for i in range(0, self.num): stockItem = [] stockItem.append(cpStockCode.GetData(0, i)) # stockCode stockItem.append(cpStockCode.GetData(1, i)) # stockName stockItem.append(cpStockCode.GetData(2, i)) # stockFullCode self.data.append(stockItem) self.updateTime = unicode(time.strftime("%Y%m%d%H%M%S")) del cpStockCode
def update(self) : del self.stockList self.stockList = [] try : cpStockCode = cxCpStockCode() except : return -1 stockNum = cpStockCode.GetCount() for i in range( 0, stockNum ) : stockItem = [] stockItem.append( cpStockCode.GetData( 0, i ) ) # stockCode stockItem.append( cpStockCode.GetData( 1, i ) ) # stockName stockItem.append( cpStockCode.GetData( 2, i ) ) # stockFullCode self.stockList.append( stockItem ) del cpStockCode return len( self.stockList)
def update(self): del self.stockList self.stockList = [] try: cpStockCode = cxCpStockCode() except: return -1 stockNum = cpStockCode.GetCount() for i in range(0, stockNum): stockItem = [] stockItem.append(cpStockCode.GetData(0, i)) # stockCode stockItem.append(cpStockCode.GetData(1, i)) # stockName stockItem.append(cpStockCode.GetData(2, i)) # stockFullCode self.stockList.append(stockItem) del cpStockCode return len(self.stockList)
def test_getStockDayData() : from cxCybosPlus import cxCpStockCode from cxFile import cxFile #stockCode = u'A000660' #하이닉스 #stockCode = u'A005930' #삼성전자 stockCode = u'A005380' #현대자동차 #stockCode = u'A004990' #롯데제과 cpStockCode = cxCpStockCode() stockName = cpStockCode.CodeToName(stockCode) chartType = u'D' fileName = u'%s_%s_%s.log'%(stockCode, stockName, chartType) refreshLog = 1 if refreshLog == 1 : resultFile = cxFile(fileName) fieldList = [ 0, # 날짜 3, # 고가 4, # 저가 5, # 종가 8, # 거래량 9, # 거래대금 25, # 주식회전율 ] paramList = [ [ 0, stockCode ], [ 1, ord(u'1') ], [ 3, 19920901 ], [ 4, len(fieldList) ], [ 5 ] + fieldList, [ 6, ord(chartType) ], [ 9, ord(u'1') ], # 수정주가 [ 10, ord(u'3') ] ] testBlockRequest(u'cxStockChart', paramList, 0, 0, 1, 1, resultFile ) resultFile.close() resultFile = cxFile(fileName) lines = resultFile.readlines() """ value = lines[1].split(u'\t') print value print value[0], value[3] """ dataLog = [] for i in range(0, len(lines)) : if i == 0 : continue value = lines[i].split(u'\t') #print value[0],value[3] dataLog.append( [ value[0], value[3] ] ) resultFile.close() dataLogLen = len(dataLog) print dataLogLen flagBuy = False earningMoney = 0 resultFile = cxFile() buyCount = 0 buyedMoney = 0 maxBuyedMoney = 0 for i in range(dataLogLen -1 -20, -1, -1 ) : currentValue = int(dataLog[i][1]) avr = 0 total = 0 maxValue = 0 minValue = 10000000000 #resultFile.write(u'%d\t'%(i)) resultFile.write(u'%s\t'%(dataLog[i][0])) for j in range(i+20,i,-1) : oldValue = int(dataLog[j][1]) #resultFile.write(u'%d(%s,%d),'%(j,dataLog[i][0],oldValue)) total += oldValue if maxValue < oldValue : maxValue = oldValue if minValue > oldValue : minValue = oldValue resultFile.write(u' ') avr = int(total/20) resultFile.write(u'current:%d,avr:%d,20s min:%d,20s max:%d\n'%( currentValue, avr, minValue, maxValue)) if (currentValue > maxValue) and (flagBuy == False) : resultFile.write(u'BUY at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue, maxValue, buyedMoney, earningMoney, buyCount)) flagBuy = True buyedMoney += currentValue buyCount += 1 maxBuyedMoney = max(maxBuyedMoney, buyedMoney) elif flagBuy == True and currentValue < minValue : earningMoney += (currentValue*buyCount)-buyedMoney resultFile.write(u'SELL at cv:%d for mv:%d in bm:%d (em:%d, bc:%d)\n'%( currentValue, minValue, buyedMoney, earningMoney, buyCount)) flagBuy = False buyCount = 0 buyedMoney = 0 resultFile.write(u'earned Money :%d, max buyed money : %d, (%f)\n'%(earningMoney, maxBuyedMoney, float(earningMoney)/float(maxBuyedMoney)*(100.0))) resultFile.write(u'%s~%s:%d~%d(%d)\n'%( dataLog[dataLogLen-1][0], dataLog[0][0], int(dataLog[dataLogLen-1][1]), int(dataLog[0][1]), int(int(dataLog[0][1])/int(dataLog[dataLogLen-1][1])))) print
def genStockList(): stockList = [] cpStockCode = cxCpStockCode() cpCodeMgr = cxCpCodeMgr() stockChart = cxStockChart() stockListNum = cpStockCode.GetCount() for i in range(0, stockListNum): stockInfo = [] stockInfo.append(cpStockCode.GetData(0, i)) # stock code stockInfo.append(cpStockCode.GetData(1, i)) # stock name stockInfo.append(cpStockCode.GetData(2, i)) # full code name if cpCodeMgr.GetStockKospi200Kind(stockInfo[0]) > 0: # KOSPI 200 if cpCodeMgr.GetStockStatusKind(stockInfo[0]) == 0: # 주식 상태 : 정상 if cpCodeMgr.GetStockSupervisionKind( stockInfo[0]) == 0: # 관리 종목 여부 : 일반 종목 if cpCodeMgr.GetStockControlKind(stockInfo[0]) == 0: stockInfo.append( cpCodeMgr.GetStockListedDate(stockInfo[0])) stockList.append(stockInfo) # del stockInfo # for i in range(0, len(stockList) ) : # print 'code : ', stockList[i][0], 'name : ', stockList[i][1], #, 'full : ', stockList[i][2] # print 'stocked date : ', stockList[i][3] print 'total num : ', len(stockList) print 'market start time : ', cpCodeMgr.GetMarketStartTime() print 'market end time : ', cpCodeMgr.GetMarketEndTime() print stockList[0][1], int(stockList[0][3]) # 19560303 stockChart.SetInputValue(0, stockList[0][0]) # stockChart.SetInputValue( 1, ord('1') ) # 기간 요청 stockChart.SetInputValue(1, ord('2')) # 개수 요청 stockChart.SetInputValue(4, 2800) # Week Max : 1027 # Day Max : 2800 # Month Max : 357 # minute Max : 265 # tick Max : 1000 # stockChart.SetInputValue( 2, 20120903 ) # stockChart.SetInputValue( 2, 20120831 ) # stockChart.SetInputValue( 2, 20120900 ) # stockChart.SetInputValue( 3, 19920900 ) # stockChart.SetInputValue( 3, 19560303 ) # stockChart.SetInputValue( 3, int(stockList[0][3]) ) # stockChart.SetInputValue( 3, 19560300 ) # stockChart.SetInputValue( 2, 20120901 ) # stockChart.SetInputValue( 3, 20120801 ) # stockChart.SetInputValue( 3, 20120820 ) # stockChart.SetInputValue( 4, 1000 ) fieldList = [0, 1, 2, 3, 4, 5, 8] # fieldList = [ 1, 1, 1, 0, 1, 0, 0 ] stockChart.SetInputValue(5, fieldList) stockChart.SetInputValue(10, ord('3')) # 거래 시간 외 정보 제외 chartType = 'D' #'D','W','M','m','T' # stockChart.SetInputValue( 6, ord('D') ) stockChart.SetInputValue(6, ord(chartType)) # 'D' : 일, 'W' : 주, 'M' : 월 'm' : 분 'T' : 틱 printAll = False fileName = stockList[0][0] + '_' + stockList[0][1] + '_' + chartType # hFile = open(fileName, 'w') hFile = codecs.open(fileName, 'w', 'utf-8') print hFile.name while stockChart.BlockRequest() == 0: beContinue = stockChart.Continue() print 'ChartType : ', chartType print 'DibStatus : ', stockChart.GetDibStatus() print 'DibMsg1 : ', stockChart.GetDibMsg1() print 'Continue : ', beContinue stockCode = stockChart.GetHeaderValue(0) fieldNum = stockChart.GetHeaderValue(1) rowNum = stockChart.GetHeaderValue(3) fieldNameList = stockChart.GetHeaderValue(2) writeLine = '' hFile.write( unicode(stockChart.GetDataValue(0, 0)) + unicode(stockChart.GetDataValue(1, 0)) + u'\n') hFile.write( unicode(stockChart.GetDataValue(0, (rowNum - 1))) + unicode(stockChart.GetDataValue(1, (rowNum - 1))) + u'\n') hFile.write(unicode(fieldNum) + u'\n') hFile.write(unicode(rowNum) + u'\n') for i in range(0, fieldNum): hFile.write(fieldNameList[i] + u'\t') #unicode hFile.write(u'\n') for i in range(0, rowNum): for j in range(0, fieldNum): writeLine += unicode(stockChart.GetDataValue(j, i)) + '\t' writeLine += '\n' writeLine += '\n' hFile.write(writeLine) if printAll == True: for i in range(0, fieldNum): print fieldNameList[i], '\t' print for i in range(0, rowNum): for j in range(0, fieldNum): print stockChart.GetDataValue(j, i), ' ', print print print 'code :', stockCode, print ' field : ', fieldNum, print ' rowNum : ', rowNum else: print 'code :', stockCode, print ' field : ', fieldNum, print ' rowNum : ', rowNum if rowNum == 0: break for i in range(0, fieldNum): print fieldNameList[i], '\t' print for j in range(0, fieldNum): print stockChart.GetDataValue(j, 0), ' ', print for j in range(0, fieldNum): print stockChart.GetDataValue(j, rowNum - 1), ' ', print if beContinue == 0: break hFile.close() print type(stockList[0][1]), stockList[0][1] """ readFile = open(fileName, 'r') for line in readFile.readlines() : print line, readFile.close() """ del cpStockCode del cpCodeMgr del stockChart
def genStockList() : stockList = [] cpStockCode = cxCpStockCode() cpCodeMgr = cxCpCodeMgr() stockChart = cxStockChart() stockListNum = cpStockCode.GetCount() for i in range(0, stockListNum ) : stockInfo = [] stockInfo.append(cpStockCode.GetData(0, i )) # stock code stockInfo.append(cpStockCode.GetData(1, i )) # stock name stockInfo.append(cpStockCode.GetData(2, i )) # full code name if cpCodeMgr.GetStockKospi200Kind( stockInfo[0] ) > 0 : # KOSPI 200 if cpCodeMgr.GetStockStatusKind( stockInfo[0] ) == 0 : # 주식 상태 : 정상 if cpCodeMgr.GetStockSupervisionKind( stockInfo[0] ) == 0 : # 관리 종목 여부 : 일반 종목 if cpCodeMgr.GetStockControlKind( stockInfo[0] ) == 0 : stockInfo.append(cpCodeMgr.GetStockListedDate( stockInfo[0] ) ) stockList.append(stockInfo) # del stockInfo # for i in range(0, len(stockList) ) : # print 'code : ', stockList[i][0], 'name : ', stockList[i][1], #, 'full : ', stockList[i][2] # print 'stocked date : ', stockList[i][3] print 'total num : ', len(stockList) print 'market start time : ', cpCodeMgr.GetMarketStartTime() print 'market end time : ', cpCodeMgr.GetMarketEndTime() print stockList[0][1], int(stockList[0][3]) # 19560303 stockChart.SetInputValue( 0, stockList[0][0] ) # stockChart.SetInputValue( 1, ord('1') ) # 기간 요청 stockChart.SetInputValue( 1, ord('2') ) # 개수 요청 stockChart.SetInputValue( 4, 2800 ) # Week Max : 1027 # Day Max : 2800 # Month Max : 357 # minute Max : 265 # tick Max : 1000 # stockChart.SetInputValue( 2, 20120903 ) # stockChart.SetInputValue( 2, 20120831 ) # stockChart.SetInputValue( 2, 20120900 ) # stockChart.SetInputValue( 3, 19920900 ) # stockChart.SetInputValue( 3, 19560303 ) # stockChart.SetInputValue( 3, int(stockList[0][3]) ) # stockChart.SetInputValue( 3, 19560300 ) # stockChart.SetInputValue( 2, 20120901 ) # stockChart.SetInputValue( 3, 20120801 ) # stockChart.SetInputValue( 3, 20120820 ) # stockChart.SetInputValue( 4, 1000 ) fieldList = [ 0, 1, 2, 3, 4, 5, 8 ] # fieldList = [ 1, 1, 1, 0, 1, 0, 0 ] stockChart.SetInputValue( 5, fieldList ) stockChart.SetInputValue(10, ord('3') ) # 거래 시간 외 정보 제외 chartType = 'D' #'D','W','M','m','T' # stockChart.SetInputValue( 6, ord('D') ) stockChart.SetInputValue( 6, ord(chartType) ) # 'D' : 일, 'W' : 주, 'M' : 월 'm' : 분 'T' : 틱 printAll = False fileName = stockList[0][0] + '_' + stockList[0][1] + '_' + chartType # hFile = open(fileName, 'w') hFile = codecs.open(fileName, 'w', 'utf-8') print hFile.name while stockChart.BlockRequest() == 0 : beContinue = stockChart.Continue() print 'ChartType : ', chartType print 'DibStatus : ', stockChart.GetDibStatus() print 'DibMsg1 : ', stockChart.GetDibMsg1() print 'Continue : ', beContinue stockCode = stockChart.GetHeaderValue( 0 ) fieldNum = stockChart.GetHeaderValue( 1 ) rowNum = stockChart.GetHeaderValue( 3 ) fieldNameList = stockChart.GetHeaderValue( 2 ) writeLine = '' hFile.write(unicode(stockChart.GetDataValue( 0, 0 )) + unicode(stockChart.GetDataValue( 1, 0 )) + u'\n') hFile.write(unicode(stockChart.GetDataValue( 0, (rowNum-1))) + unicode(stockChart.GetDataValue( 1, (rowNum-1))) + u'\n') hFile.write(unicode(fieldNum) + u'\n') hFile.write(unicode(rowNum) + u'\n') for i in range( 0, fieldNum ) : hFile.write(fieldNameList[i] + u'\t') #unicode hFile.write(u'\n') for i in range( 0, rowNum ) : for j in range( 0, fieldNum ) : writeLine += unicode(stockChart.GetDataValue( j, i )) + '\t' writeLine += '\n' writeLine += '\n' hFile.write(writeLine) if printAll == True : for i in range( 0, fieldNum ) : print fieldNameList[i],'\t' print for i in range( 0, rowNum ) : for j in range( 0, fieldNum ) : print stockChart.GetDataValue( j, i ),' ', print print print 'code :', stockCode, print ' field : ', fieldNum, print ' rowNum : ', rowNum else : print 'code :', stockCode, print ' field : ', fieldNum, print ' rowNum : ', rowNum if rowNum == 0 : break for i in range( 0, fieldNum ) : print fieldNameList[i],'\t' print for j in range( 0, fieldNum ) : print stockChart.GetDataValue( j, 0 ), ' ' , print for j in range( 0, fieldNum ) : print stockChart.GetDataValue( j, rowNum-1 ), ' ' , print if beContinue == 0 : break hFile.close() print type(stockList[0][1]), stockList[0][1] """ readFile = open(fileName, 'r') for line in readFile.readlines() : print line, readFile.close() """ del cpStockCode del cpCodeMgr del stockChart