请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3364477 新帖:3

自定义因子分析模板

交易资深人士发表于:7 月 25 日 20:00回复(1)

自定义因子分析模板¶

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>
 
 

全部回复

0/140

达人推荐

量化课程

    移动端课程