def main(): stockcode=[] correlationDict={} stockCodeDict = stockCode.stockCodeDict() stockCodeDictT1 = [k for k,v in stockCodeDict.items() if v[0] == "T1"] print "point1" for code in stockCodeDictT1: try: stock=stockPrice.stockPriceDict(code,20150803,20150803) if int(stock["volume"][0]) > 1000000: stockcode.append(code) except: print "error1" print code print traceback.format_exc() stockCodeCombination = itertools.combinations(stockcode,2) print "point2" for num,(code1,code2) in enumerate(stockCodeCombination): print num stockPriceDict1=stockPrice.stockPriceDict(code1,20150701,20150731) stockPriceDict2=stockPrice.stockPriceDict(code2,20150701,20150731) #両社の株価が取得出来た場合のみ処理を実施 if stockPriceDict1 != None and stockPriceDict2 != None: #初値と終値から1日あたりの変化率を取得 changeRate1=[(float(endPrice)-float(startPrice))/float(startPrice) * 100 for startPrice,endPrice in zip(stockPriceDict1.get("startPrice"),stockPriceDict1.get("endPrice"))] changeRate2=[(float(endPrice)-float(startPrice))/float(startPrice) * 100 for startPrice,endPrice in zip(stockPriceDict2.get("startPrice"),stockPriceDict2.get("endPrice"))] try: if len(changeRate1) == len(changeRate2): #相関係数を取得 correlation=numpy.corrcoef(changeRate1,changeRate2)[0,1] #相関係数ディクショナリ correlationDict.setdefault((code1,code2),correlation) except: print "error2" print traceback.format_exc() print len(changeRate1) print len(changeRate2) print changeRate1 print changeRate2 continue print "point3" with open(os.path.join(os.getcwd(),"correlation.txt"),"w") as fp: for num,(code,correlation) in enumerate(sorted(correlationDict.items(),key=lambda x:x[1])): print "%s,%s,%s" % (stockCodeDict.get(code[0])[1],stockCodeDict.get(code[1])[1],correlation) print >>fp,"%s,%s,%s" % (stockCodeDict.get(code[0])[1],stockCodeDict.get(code[1])[1],correlation)
def rangePrice(self,code,fromDay,toDay): return stockPrice.stockPriceDict(code,fromDay,toDay)
def currentPrice(self,code,day): stockPriceDict=stockPrice.stockPriceDict(code,day,day) if stockPriceDict is not None: return dict([(key,value[0]) for key,value in stockPriceDict.items()]) else: return {"startPrice":0,"endPrice":0}