繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

自定义因子分析模板

作者/jedfsjfjsdf 2019-07-25 20:00 0 来源: FX168财经网人物频道

自定义因子分析模板¶

from jqlib import alpha191
from jqlib.alpha191 import *
from jqfactor import get_factor_values
from jqfactor import Factor
from jqfactor import analyze_factor
warnings.filterwarnings('ignore') 

# 导入函数库
from jqfactor import Factor, calc_factors

# 定义因子,聚宽的因子定义标准形式
class ALPHA013(Factor):
    name = 'alpha013_name'
    max_window = 1
    dependencies = ['high','low','volume','money']
    def calc(self, data):
        high = data['high']
        low = data['low']
        vwap = data['money']/data['volume']
        return (np.power(high*low,0.5) - vwap).mean()

# 定义因子
class GROSSPROFITABILITY(Factor):
    name = 'gross_profitability'
    max_window = 1
    dependencies = ['total_operating_revenue','total_operating_cost','total_assets']
    def calc(self, data):
        total_operating_revenue = data['total_operating_revenue']
        total_operating_cost = data['total_operating_cost']
        total_assets = data['total_assets']
        gross_profitability = (total_operating_revenue - total_operating_cost)/total_assets
        return gross_profitability.mean()
# 定义股票池
securities = ['600000.XSHG','600016.XSHG']
# 计算因子值
factors = calc_factors(securities, [ALPHA013(),GROSSPROFITABILITY()], start_date='2017-01-01', end_date='2017-02-01',  use_real_price=False, skip_paused=False)

# 查看因子值
factors['gross_profitability'].head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
600000.XSHG 600016.XSHG
2017-01-03 0.003284 0.002626
2017-01-04 0.003284 0.002626
2017-01-05 0.003284 0.002626
2017-01-06 0.003284 0.002626
2017-01-09 0.003284 0.002626
#也可以不按聚宽的方式定义因子,直接用函数来自定义定义,只是接下来处理数据麻烦一些,
def factor_cal(pool,date):
    df = get_price(pool,end_date=date,count=21,fields=['close'])['close'] 
    far = df.iloc[-1,:]/df.iloc[0,:] - 1
    return far
#测试函数,看下面的数据结构,和上面的数据结构和上面的是有所不同的,需要用标准因子模板处理,因此自定义因子还是建议用标准模板,
#这样可以直接调用'analyze_factor'方法进行分析。
factor_cal(['000001.XSHE','600000.XSHG'],'2019-07-12')
000001.XSHE    0.142395
600000.XSHG   -0.022901
dtype: float64
far = analyze_factor(factor=ALPHA013, start_date='2017-01-01', end_date='2017-02-01',weight_method='mktcap',universe='000300.XSHG', industry='jq_l1', quantiles=8, periods=(1,5,22))
#分析结束后通过不同属性获取数据
far.ic_monthly #月度信息比率
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_5 period_22
2017-01 -0.040113 -0.043559 -0.07873
#分析结束后通过不同属性获取数据
far.mean_return_std_by_quantile #获取按分位数分组加权平均因子收益
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_5 period_22
factor_quantile
1 0.000558 0.000237 0.000102
2 0.000485 0.000231 0.000104
3 0.000469 0.000191 0.000082
4 0.000450 0.000195 0.000088
5 0.000448 0.000203 0.000089
6 0.000517 0.000232 0.000096
7 0.000515 0.000237 0.000106
8 0.000595 0.000238 0.000104
#
far.create_full_tear_sheet(demeaned=False, group_adjust=False, by_group=False, turnover_periods=None, avgretplot=(5, 15), std_bar=False)
分位数统计
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
min max mean std count count %
factor_quantile
1 -24.143409 -0.052357 -1.525214 2.195821 654 12.606014
2 -1.916535 0.000674 -0.346896 0.384357 648 12.490362
3 -1.094742 0.018931 -0.146860 0.204801 647 12.471087
4 -0.468798 0.060710 -0.054313 0.102077 648 12.490362
5 -0.215248 0.150670 -0.000489 0.059529 645 12.432537
6 -0.068598 0.353499 0.057233 0.085506 647 12.471087
7 -0.024016 0.822166 0.173772 0.171141 648 12.490362
8 0.004649 16.226558 1.002450 1.344224 651 12.548188
-------------------------

收益分析
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_5 period_22
Ann. alpha -0.243 -0.096 -0.021
beta -0.248 -0.272 -0.242
Mean Period Wise Return Top Quantile (bps) 0.331 -0.294 10.517
Mean Period Wise Return Bottom Quantile (bps) 7.351 5.634 15.974
Mean Period Wise Spread (bps) -7.020 -5.918 -5.480
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
-------------------------

IC 分析
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_5 period_22
IC Mean -0.040 -0.044 -0.079
IC Std. 0.142 0.129 0.094
IR -0.282 -0.337 -0.841
t-stat(IC) -1.198 -1.429 -3.566
p-value(IC) 0.247 0.171 0.002
IC Skew -0.052 -0.037 -0.396
IC Kurtosis -1.193 -1.138 0.707
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
-------------------------

换手率分析
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_22 period_5
Quantile 1 Mean Turnover 0.722 NaN 0.775
Quantile 2 Mean Turnover 0.843 NaN 0.857
Quantile 3 Mean Turnover 0.827 NaN 0.867
Quantile 4 Mean Turnover 0.845 NaN 0.868
Quantile 5 Mean Turnover 0.837 NaN 0.815
Quantile 6 Mean Turnover 0.831 NaN 0.880
Quantile 7 Mean Turnover 0.828 NaN 0.889
Quantile 8 Mean Turnover 0.724 NaN 0.757
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
period_1 period_5 period_22
Mean Factor Rank Autocorrelation 0.081 -0.036 NaN
<Figure size 432x288 with 0 Axes>
<Figure size 432x288 with 0 Axes>
-------------------------

<Figure size 432x288 with 0 Axes>
# 计算指定调仓周期的各分位数每日累积收益
df = far.calc_cumulative_return_by_quantile(period=5)
#进行数据展示
df.plot(figsize=(15,6))
<matplotlib.axes._subplots.AxesSubplot at 0x7f2c82e33940>
 
 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2184文章4145粉丝45

    暂无个人简介信息

  • 梁孟梵

    人气2176文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 指导老师

    人气1864文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2320文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 刘钥钥1

    人气2024文章3119粉丝34

    专业从事现货黄金、现货白银模似实盘操作分析指导

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2328文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4760文章8329粉丝119

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...

FX168财经

FX168财经学院

FX168财经

FX168北美