def plotdivratio(code): sz50code, namedf, price, volume = getdata.GetPriceVolume(code) div = getdata.GetDiv('divi').ix[code] reportdate = getdata.GetDiv('report_date').ix[code] print div, reportdate ##do backfill divvalue = Series() for idx in range(0, len(reportdate.index)): if idx != len(reportdate.index) - 1: startdate = reportdate[reportdate.index[idx]] enddate = reportdate[reportdate.index[idx + 1]] else: startdate = reportdate[reportdate.index[idx]] enddate = '2017-04-28' print startdate, enddate dr = pd.date_range(startdate, end=enddate, freq='B') ctr = dr.to_pydatetime() for date in ctr: divvalue[date] = div[div.index[idx]] daterange = pd.date_range('01/01/2012', end='04/28/2017', freq='B') convertrange = daterange.to_pydatetime() ratio = Series() for date in convertrange: if date in price.index and date in divvalue.index: ratio[date] = divvalue[date] / price[code][date] #print ratio fig, ax = plt.subplots() ratio.plot(ax=ax) plt.show()
def plotpe(ax): global profit, hs300 daterange = pd.date_range('01/01/2012', end='03/03/2017', freq='B') #daterange = pd.date_range('03/01/2014', end = '05/20/2014', freq='B') convertrange = daterange.to_pydatetime() sz50code, namedf, price, volume = getdata.GetPriceVolume() epsall = getdata.GetProfit('eps') peseries = Series() validpe = Series() for date in convertrange: if date not in price.index: continue combinepe = 0 for code in hs300.index: weight = hs300['weight'][code] eps = epsall.ix[code] quarter = date.month / 4 + 1 if date.year == 2012: earning = eps[20124] elif date.year > 2016: earning = eps[20163] + eps[20154] - eps[20153] else: if quarter == 1: earning = eps[(date.year - 1) * 10 + 4] else: earning = eps[date.year * 10 + quarter - 1] + eps[ (date.year - 1) * 10 + 4] - eps[(date.year - 1) * 10 + quarter - 1] temppe = float(price[code][date]) / earning if not isnan(temppe): validpe[code] = temppe if isnan(temppe) and code in validpe.index: temppe = validpe[code] if (not isnan(temppe)) and temppe > 0: combinepe = combinepe + weight * temppe #print combinepe #if date.year == 2014 and date.month==4 and date.day == 3 and temppe < 0: # print date, code, temppe, earning peseries[date] = combinepe print date, combinepe #normalizedpe = (peseries - peseries.min()) / (peseries.max() - peseries.min()) ax.set_title("hs300PE") peseries.plot(ax=ax)
def plotcorrelation(): code1 = '601668' sz50code1, namedf1, price1, volume1 = getdata.GetPriceVolume() price1 = price1['601668'].pct_change()[1:] #price1 = price1.apply(lambda x: (x - np.mean(x)) / (np.max(x) - np.min(x))) volume1 = volume1.apply(lambda x: (x - np.mean(x)) / (np.max(x) - np.min(x))) volume_ma = pd.rolling_mean(volume1, window=30) #price_ma = pd.rolling_mean(price1, window = 30) #ss = getdata.GetWindowCorr(price_ma[code1], volume_ma[code1], 90) #price_ma[code1].plot(color='r') price1.plot(label="PE") #volume_ma[code1].plot(color='y') #ss.plot(color='b') plt.show()
def GetHighestDiv(): global divi, hs300 sz50code, namedf, price, volume = getdata.GetPriceVolume() dt = datetime.datetime(2017, 04, 28) df = DataFrame(columns=[2013, 2014, 2015, 'avg', 'price', 'ratio', 'name'], index=hs300.index) for ii in df.index: for year in [2013, 2014, 2015]: df[year][ii] = divi[year][ii] #print ii, df[2013][ii] , df[2014][ii] , df[2015][ii] df['avg'][ii] = float(df[2013][ii] + df[2014][ii] + df[2015][ii]) / 3 df['price'][ii] = price[ii][dt] df['ratio'][ii] = df['avg'][ii] / price[ii][dt] df['name'][ii] = hs300['name'][ii] #print df.ix[ii] topcode = df.sort(columns='ratio', ascending=False)[0:50] for code in topcode.index: if not np.isnan(divi[2016][code]): print code, divi[2016][code] / price[code][dt], df['name'][code]
currentquarter = int((month - 1) / 3) + 1 pair = [(year - 1, 3), (year - 1, 4), (year, 1), (year, 2), (year, 3)] return pair[currentquarter] def getsecondlastquarter(date): year = date.year month = date.month currentquarter = int((month - 1) / 3) + 1 pair = [(year - 1, 3), (year - 1, 4), (year, 1), (year, 2), (year, 3)] return pair[currentquarter - 1] sz50code, namedf, price, volume = getdata.GetPriceVolume() daterange = pd.date_range(startdate, end=genddate, freq='B') convertrange = daterange.to_pydatetime() price = price.ix[convertrange[0]:convertrange[-1]] def getvalue(tsseries, date): quarter = int((date.month - 1) / 3) + 1 lastq = getlastquarter(date) if date.year == 2011: earning = tsseries[20114] ## data for most recent days, first check wheter value of last quarter is valid, if yes, use last quarter; ## if not, use second last quarter elif date > convertrange[-180]:
ret[d] = firstunzero return ret date = pd.date_range('01/01/2012', end='06/30/2017') #sz50 = ts.get_sz50s() sz50 = getdata.GetHs300() #sz50 = getdata.GetHs300() #sz50 = sz50.append({'code':'sh', 'name':'index','date': '2016-08-01','weight':0}, ignore_index=True) sz50code = sz50.index df = DataFrame(columns=sz50code) rawdict = {} namedf = {} sz50code, nameseries, price, volume = getdata.GetPriceVolume() corrdf = price.corr() vvdf = volume.corr() #print corrdf['000625']['sh'] #print corrdf['000423']['sh'] #print corrdf['000963']['sh'] #codelist = ['000625', '000423', '000963', '600887', '600048', '600066'] codelist = download.LoadCodelist() for code in codelist: for c2 in codelist: if code != c2: print code, c2 print "%s: %s : %f, %f" % (sz50['name'][code].decode('UTF-8'), sz50['name'][c2].decode('UTF-8'), corrdf[code][c2], vvdf[code][c2])
def plotpb(code, ax, name): global profit, hs300, basic, roe, retpbarray sz50code, namedf, price, volume = getdata.GetPriceVolume(code) print "PB: %s: %s" % (code, name) daterange = pd.date_range('01/01/2012', end=genddate, freq='B') convertrange = daterange.to_pydatetime() netprofit = profit.ix[code] / basic[code] / 100 roeix = roe.ix[code] #print netprofit #print roeix #price = price.ix[convertrange[-1]:convertrange[0]] pbseries = Series() earningseries = Series() for date in convertrange: if date in price.index: if date.month % 3 == 0: quarter = date.month / 3 else: quarter = date.month / 3 + 1 if date.year == 2011: earning = netprofit[20114] temproe = roeix[20114] elif (date.year == 2017 and quarter == 4) or date.year > 2017: earning = netprofit[20173] temproe = roeix[20173] # temproe = roeix[20163] + roeix[20172] - roeix[20162] # if np.isnan(earning): # earning = netprofit[20163] + netprofit[20154] - netprofit[20153] # temproe = roeix[20164] # if np.isnan(temproe): # temproe = roeix[20163] + roeix[20154] - roeix[20153] else: if quarter == 1: earning = netprofit[(date.year - 1) * 10 + 4] temproe = roeix[(date.year - 1) * 10 + 4] else: earning = netprofit[date.year * 10 + quarter - 1] temproe = roeix[date.year * 10 + quarter - 1] book = earning * 100 / temproe #if code == '600340' and date.year == 2017 and quarter > 2 : # print date, earning, temproe, earning * 100 / temproe, price[code][date], price[code][date] / book temp = price[code][date] / book #print code, date, price[code][date] pbseries[date] = temp #print date, earning, temproe, book, price[code][date], temp earningseries[date] = earning normalizedpe = pbseries print code, normalizedpe.ix[-1], normalizedpe.mean(), normalizedpe.ix[ -1] / normalizedpe.mean(), normalizedpe.std() / normalizedpe.mean() sortedpe = normalizedpe.sort_values() print code, sortedpe.index[0], sortedpe[ sortedpe.index[0]], sortedpe.index[-1], sortedpe[sortedpe.index[-1]] retpbarray[code] = normalizedpe.ix[-1] / sortedpe[sortedpe.index[0]] ax.set_title(code + ' PB') normalizedpe.plot(ax=ax, label="PB") #normalizedpe = (peseries - peseries.min()) / (peseries.max() - peseries.min()) #normalizedearning = (earningseries - earningseries.min()) / (earningseries.max() - earningseries.min()) #ax.set_yticks(np.arange(0.,1.3,0.1)) #normalizedprice.plot(ax = ax, label="Price") print '------------------------------------------'
def plotpe(code, ax, name): global profit, hs300, basic, retpearray sz50code, namedf, price, volume = getdata.GetPriceVolume(code) print "PE: %s: %s" % (code, name) daterange = pd.date_range('01/01/2012', end=genddate, freq='B') convertrange = daterange.to_pydatetime() netprofit = profit.ix[code] / basic[code] / 100 #print netprofit #price = price.ix[convertrange[-1]:convertrange[0]] #print price peseries = Series() earningseries = Series() for date in convertrange: if date in price.index: if date.month % 3 == 0: quarter = date.month / 3 else: quarter = date.month / 3 + 1 if date.year == 2011: earning = netprofit[20114] elif (date.year == 2017 and quarter == 4) or date.year > 2017: earning = netprofit[20173] + netprofit[20164] - netprofit[20163] # earning = netprofit[20164] # if np.isnan(earning): # earning = netprofit[20163] + netprofit[20154] - netprofit[20153] else: if quarter == 1: earning = netprofit[(date.year - 1) * 10 + 4] else: earning = netprofit[ date.year * 10 + quarter - 1] + netprofit[(date.year - 1) * 10 + 4] - netprofit[ (date.year - 1) * 10 + quarter - 1] temp = price[code][date] / earning #print date, price[code][date], earning peseries[date] = temp earningseries[date] = earning #print peseries.min(), peseries.max(), peseries.mean(), peseries.median() #normalizedpe = (peseries - peseries.min()) / (peseries.max() - peseries.min()) normalizedpe = peseries #print normalizedpe print code, normalizedpe.ix[-1], normalizedpe.mean(), normalizedpe.ix[ -1] / normalizedpe.mean(), normalizedpe.std() / normalizedpe.mean() sortedpe = normalizedpe.sort_values() print code, sortedpe.index[0], sortedpe[ sortedpe.index[0]], sortedpe.index[-1], sortedpe[sortedpe.index[-1]] retpearray[code] = normalizedpe.ix[-1] / sortedpe[sortedpe.index[0]] ax.set_title(code + ' PE') normalizedpe.plot(ax=ax, label="PE") print '------------------------------------------'