Created
April 7, 2018 16:42
-
-
Save jeffo201/cc341e5dd80fba294416749872309490 to your computer and use it in GitHub Desktop.
动量因子、行业涨跌比率和预测收益率的数据可视化
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
import pandas as pd | |
import time, datetime | |
from sklearn import preprocessing | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from pyecharts import Pie | |
def callist(list): | |
copylist = list[:] | |
copylist.sort(reverse=True) | |
dic = {v: i + 1 for i, v in enumerate(copylist)} | |
result = [{v: dic[v]} for i, v in enumerate(list)] | |
return [float(i.values()[0]) for i in result] | |
wp = pd.read_table(r'J:\fi-data\analyse\whole_pic\whole_pic.txt', delimiter='\t') ########创建wp | |
t1 = wp.groupby(['industry']).count().iloc[:, 1] ########全部股票行业的频数统计 | |
print wp['return_r'].mean(), '全部股票收益率' | |
wp = wp[wp.h_tr < 100] | |
plt.scatter(wp.h_tr, wp.return_r) | |
wp2 = wp[wp.h_rr > 0] | |
print wp['return_r'].mean(), '历史收益率大于0股票收益率' | |
plt.show() | |
model_earn = wp['return_r'][wp.h_rr + wp.h_tr > 0.1].mean() ##########动量大于0.1的因子 | |
print model_earn, '动量因子大于0.1股票收益率' | |
t2 = wp2.groupby(['industry']).count().iloc[:, 1] ########筛选动量因子的股票行业的频数统计 | |
t = t2 / t1 | |
industry_factor = t.sort_values(ascending=False) ########行业所占个股涨跌比重 | |
wp['industry_factor'] = [industry_factor[i] for i in wp.industry.values.tolist()] #####转化为行业因子 | |
wp['industry_num'] = [t1[i] for i in wp.industry.values.tolist()] #####转化为行业因子 | |
wp['momentum'] = wp.h_tr + wp.h_rr | |
wp = wp[['date', 'code', 'return_r', 'h_rr', 'h_tr', 'momentum', 'industry_factor', 'industry', 'industry_num']] | |
plt.scatter(wp[wp['return_r'] > 0].industry_factor, wp[wp['return_r'] > 0].momentum, c='r', | |
s=abs(wp.return_r[wp['return_r'] > 0] - wp.return_r[wp['return_r'] > 0].mean()) / wp[ | |
wp['return_r'] > 0].return_r.std() + 10, marker='>') | |
plt.scatter(wp[wp['return_r'] < 0].industry_factor, wp[wp['return_r'] < 0].momentum, c='g', | |
s=abs(wp[wp['return_r'] < 0].return_r - wp[wp['return_r'] < 0].return_r.mean()) / wp[ | |
wp['return_r'] < 0].return_r.std() + 10, marker='<') | |
#plt.savefig(r'C:\\Users\\Administrator\\Desktop\\stat_anlys\\past-pattern\\' + str( | |
#time.strftime(r'%Y-%m-%d', time.localtime(time.time())) + r'.png')) | |
plt.show() | |
print wp | |
wp = wp[wp.industry_factor < 0.75][wp.industry_factor > 0.3] | |
wp = wp.sort_values(by='momentum', ascending=False) | |
print wp['return_r'][0:10].mean(), '优势板块+动量因子的前十股票收益率' | |
direct = r'C:\Users\Administrator\Desktop\stat_anlys\past-pattern\last2day.txt' | |
wp.to_csv(path_or_buf=direct, sep='\t') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment