【期货量化进阶】期货量化交易策略因子挖掘方法(Python量化)

📅 发布时间:2026/7/7 1:37:55 👁️ 浏览次数:
【期货量化进阶】期货量化交易策略因子挖掘方法(Python量化)
一、前言因子挖掘是量化交易的核心环节。好的因子能够带来稳定的超额收益。本文将介绍期货量化交易中的因子挖掘方法包括技术因子、基本面因子、统计因子的挖掘技巧。本文将介绍因子挖掘的基本流程技术指标因子挖掘价格形态因子挖掘统计因子挖掘因子有效性验证二、为什么选择天勤量化TqSdkTqSdk因子挖掘优势优势说明数据完整支持历史数据和实时数据计算高效pandas/numpy支持高效计算指标丰富内置多种技术指标灵活扩展支持自定义因子计算安装方法pipinstalltqsdk pandas numpy scipy三、因子挖掘基础3.1 因子定义因子是能够预测未来收益的变量通常分为因子类型说明示例技术因子基于价格和成交量均线、MACD基本面因子基于供需关系库存、仓单统计因子基于统计特征波动率、相关性价量因子基于价格和成交量关系量价背离3.2 因子挖掘流程数据准备 → 因子计算 → 因子测试 → 因子筛选 → 因子组合四、技术指标因子挖掘4.1 均线类因子#!/usr/bin/env python# -*- coding: utf-8 -*- 功能均线类因子挖掘 说明本代码仅供学习参考 fromtqsdkimportTqApi,TqAuthfromtqsdk.tafuncimportmaimportpandasaspdimportnumpyasnpdefcalculate_ma_factors(klines):计算均线类因子factors{}# 不同周期均线ma5ma(klines[close],5)ma10ma(klines[close],10)ma20ma(klines[close],20)ma60ma(klines[close],60)# 因子1价格相对均线位置factors[price_ma5_ratio]klines[close]/ma5-1factors[price_ma20_ratio]klines[close]/ma20-1# 因子2均线斜率factors[ma5_slope](ma5-ma5.shift(5))/ma5.shift(5)factors[ma20_slope](ma20-ma20.shift(5))/ma20.shift(5)# 因子3均线排列factors[ma_arrangement](ma5ma10).astype(int)(ma10ma20).astype(int)(ma20ma60).astype(int)returnpd.DataFrame(factors)# 使用示例apiTqApi(authTqAuth(快期账户,快期密码))klinesapi.get_kline_serial(SHFE.rb2510,3600,200)api.wait_update()factorscalculate_ma_factors(klines)print(factors.head())api.close()4.2 动量类因子defcalculate_momentum_factors(klines):计算动量类因子factors{}# 收益率因子factors[return_1d]klines[close].pct_change(1)factors[return_5d]klines[close].pct_change(5)factors[return_20d]klines[close].pct_change(20)# RSI因子fromtqsdk.tafuncimportrsi factors[rsi]rsi(klines[close],14)# MACD因子fromtqsdk.tafuncimportmacd macd_datamacd(klines[close],12,26,9)factors[macd]macd_data[macd]factors[macd_signal]macd_data[signal]factors[macd_hist]macd_data[hist]returnpd.DataFrame(factors)五、价格形态因子挖掘5.1 K线形态因子defcalculate_candle_pattern_factors(klines):计算K线形态因子factors{}# 实体大小bodyabs(klines[close]-klines[open])factors[body_ratio]body/(klines[high]-klines[low])# 上影线比例upper_shadowklines[high]-klines[[open,close]].max(axis1)factors[upper_shadow_ratio]upper_shadow/(klines[high]-klines[low])# 下影线比例lower_shadowklines[[open,close]].min(axis1)-klines[low]factors[lower_shadow_ratio]lower_shadow/(klines[high]-klines[low])# 涨跌判断factors[is_up](klines[close]klines[open]).astype(int)returnpd.DataFrame(factors)5.2 价格突破因子defcalculate_breakout_factors(klines,window20):计算突破因子factors{}# 突破高点high_maxklines[high].rolling(window).max()factors[break_high](klines[close]high_max.shift(1)).astype(int)# 突破低点low_minklines[low].rolling(window).min()factors[break_low](klines[close]low_min.shift(1)).astype(int)# 突破幅度factors[break_high_ratio](klines[close]-high_max.shift(1))/high_max.shift(1)factors[break_low_ratio](low_min.shift(1)-klines[close])/low_min.shift(1)returnpd.DataFrame(factors)六、统计因子挖掘6.1 波动率因子defcalculate_volatility_factors(klines):计算波动率因子factors{}returnsklines[close].pct_change()# 历史波动率factors[volatility_5d]returns.rolling(5).std()factors[volatility_20d]returns.rolling(20).std()# 波动率比率factors[volatility_ratio]factors[volatility_5d]/factors[volatility_20d]# ATR因子fromtqsdk.tafuncimportatr atr_valueatr(klines,14)factors[atr]atr_value factors[atr_ratio]atr_value/klines[close]returnpd.DataFrame(factors)6.2 相关性因子defcalculate_correlation_factors(klines,klines2):计算相关性因子factors{}returns1klines[close].pct_change()returns2klines2[close].pct_change()# 滚动相关性factors[correlation_20d]returns1.rolling(20).corr(returns2)returnpd.DataFrame(factors)七、价量因子挖掘7.1 成交量因子defcalculate_volume_factors(klines):计算成交量因子factors{}# 成交量变化factors[volume_change]klines[volume].pct_change()factors[volume_ma_ratio]klines[volume]/klines[volume].rolling(20).mean()# 价量关系price_changeklines[close].pct_change()factors[price_volume_corr]price_change.rolling(20).corr(klines[volume].pct_change())# OBV因子obv(np.sign(klines[close].diff())*klines[volume]).fillna(0).cumsum()factors[obv]obv factors[obv_change]obv.pct_change()returnpd.DataFrame(factors)八、因子有效性验证8.1 IC分析defcalculate_ic(factor,forward_return,methodpearson): 计算信息系数IC 参数: factor: 因子值 forward_return: 未来收益率 method: pearson 或 spearman ifmethodpearson:icfactor.corr(forward_return)else:fromscipy.statsimportspearmanr ic,_spearmanr(factor,forward_return)returnicdefanalyze_factor_ic(factors_df,returns,periods[1,5,20]):分析因子ICic_results{}forperiodinperiods:forward_returnsreturns.shift(-period)ic_values[]forcolinfactors_df.columns:iccalculate_ic(factors_df[col],forward_returns)ic_values.append(ic)ic_results[fIC_{period}d]ic_valuesreturnpd.DataFrame(ic_results,indexfactors_df.columns)8.2 因子分层回测deffactor_layering_backtest(factor,returns,n_layers5):因子分层回测# 将因子分成n层factor_quantilespd.qcut(factor,n_layers,labelsFalse,duplicatesdrop)layer_returns{}forlayerinrange(n_layers):layer_maskfactor_quantileslayer layer_returns[layer]returns[layer_mask].mean()returnpd.Series(layer_returns)九、因子组合9.1 因子合成defcombine_factors(factors_dict,weightsNone):合成多个因子ifweightsisNone:weights{k:1.0forkinfactors_dict.keys()}combinedNonetotal_weightsum(weights.values())forfactor_name,factor_datainfactors_dict.items():weightweights[factor_name]/total_weightifcombinedisNone:combinedfactor_data*weightelse:combinedfactor_data*weightreturncombined十、总结10.1 因子挖掘要点要点说明数据质量确保数据准确完整因子逻辑因子要有经济意义有效性验证通过IC和分层回测验证因子组合组合多个因子提高效果10.2 注意事项避免过拟合- 使用样本外数据验证因子稳定性- 选择稳定的因子计算效率- 优化因子计算速度持续挖掘- 市场变化需要新因子免责声明本文仅供学习交流使用不构成任何投资建议。期货交易有风险入市需谨慎。更多资源天勤量化官网https://www.shinnytech.comGitHub开源地址https://github.com/shinnytech/tqsdk-python官方文档https://doc.shinnytech.com/tqsdk/latest