核心内容摘要
总裁爹地,你真的太欠了:一场关于豪门、萌宝与极致拉扯的视听盛宴
前言信号强度是量化策略中的重要概念。
不同强度的信号应该采用不同的处理方式强信号可以加大仓位弱信号可以减小仓位或忽略。
本文将介绍如何评估信号强度并应用于策略。
本文将介绍信号强度的概念技术指标信号强度多因子信号强度信号强度量化方法基于强度的仓位调整
为什么选择天勤量化TqSdkTqSdk信号强度评估支持功能说明技术指标内置多种技术指标函数数据计算pandas/numpy支持强度计算实时数据支持实时信号强度评估历史回测支持信号强度回测验证安装方法pipinstalltqsdk pandas numpy scipy
信号强度基础
1 强度分类强度等级说明处理方式强信号多个指标一致信号明确加大仓位快速执行中等信号部分指标一致正常仓位弱信号指标不一致或信号模糊减小仓位或忽略无信号没有明确信号不交易
2 强度评估维度维度说明指标一致性多个指标是否一致信号幅度信号偏离程度信号持续性信号持续时间历史有效性历史表现如何
技术指标信号强度
1 单指标强度#!/usr/bin/env python# -*- coding: utf-8 -*- 功能单指标信号强度 说明本代码仅供学习参考 importpandasaspdimportnumpyasnpfromtqsdkimportTqApi,TqAuthfromtqsdk.tafuncimportma,macd,rsi apiTqApi(authTqAuth(快期账户,快期密码))SYMBOLSHFE.rb2510klinesapi.get_kline_serial(SYMBOL,3600,
api.wait_update()defcalculate_ma_signal_strength(df): 计算均线信号强度 参数: df: K线数据 ma5ma(df[close],
ma20ma(df[close],
ma60ma(df[close],
# 信号方向signal_directionpd.Series(0,indexdf.index)signal_direction[(ma5ma
(ma20ma
]1# 多头signal_direction[(ma5ma
(ma20ma
]-1# 空头# 信号强度基于均线距离strengthpd.Series(
0,indexdf.index)# 多头强度long_conditionsignal_direction1strength[long_condition]((ma5[long_condition]/ma20[long_condition]-
*10(ma20[long_condition]/ma60[long_condition]-
*
# 空头强度short_conditionsignal_direction-1strength[short_condition]-((ma20[short_condition]/ma5[short_condition]-
*10(ma60[short_condition]/ma20[short_condition]-
*
# 归一化到-1到1max_strengthstrength.abs().max()ifmax_strength0:strengthstrength/max_strengthreturnsignal_direction,strength# 计算信号强度signal_direction,strengthcalculate_ma_signal_strength(klines)print(均线信号强度统计:)print(f 多头信号:{(signal_direction
.sum()})print(f 空头信号:{(signal_direction-
.sum()})print(f 平均强度:{strength.abs().mean():.3f})print(f 最大强度:{strength.abs().max():.3f})api.close()
2 MACD信号强度#!/usr/bin/env python# -*- coding: utf-8 -*- 功能MACD信号强度 说明本代码仅供学习参考 importpandasaspdimportnumpyasnpfromtqsdkimportTqApi,TqAuthfromtqsdk.tafuncimportmacd apiTqApi(authTqAuth(快期账户,快期密码))SYMBOLSHFE.rb2510klinesapi.get_kline_serial(SYMBOL,3600,
api.wait_update()defcalculate_macd_signal_strength(df): 计算MACD信号强度 参数: df: K线数据 macd_datamacd(df[close],12,26,
macd_linemacd_data[macd]signal_linemacd_data[signal]histmacd_data[hist]# 信号方向signal_directionpd.Series(0,indexdf.index)signal_direction[macd_linesignal_line]1# 金叉signal_direction[macd_linesignal_line]-1# 死叉# 信号强度基于柱状图高度strengthhist/df[close]*100# 标准化# 归一化到-1到1max_strengthstrength.abs().max()ifmax_strength0:strengthstrength/max_strengthreturnsignal_direction,strength# 计算信号强度signal_direction,strengthcalculate_macd_signal_strength(klines)print(MACD信号强度统计:)print(f 金叉信号:{(signal_direction
.sum()})print(f 死叉信号:{(signal_direction-
.sum()})print(f 平均强度:{strength.abs().mean():.3f})api.close()
多因子信号强度
1 多指标综合强度#!/usr/bin/env python# -*- coding: utf-8 -*- 功能多指标综合信号强度 说明本代码仅供学习参考 importpandasaspdimportnumpyasnpfromtqsdkimportTqApi,TqAuthfromtqsdk.tafuncimportma,macd,rsi,boll apiTqApi(authTqAuth(快期账户,快期密码))SYMBOLSHFE.rb2510klinesapi.get_kline_serial(SYMBOL,3600,
api.wait_update()defcalculate_composite_signal_strength(df): 计算综合信号强度 参数: df: K线数据 signals{}strengths{}#
均线信号ma5ma(df[close],
ma20ma(df[close],
ma_signalpd.Series(0,indexdf.index)ma_signal[(ma5ma
]1ma_signal[(ma5ma
]-1ma_strengthabs(ma5/ma20-
*10ma_strengthma_strength/ma_strength.max()ifma_strength.max()0elsema_strength signals[ma]ma_signal strengths[ma]ma_strength#
MACD信号macd_datamacd(df[close],12,26,
macd_signalpd.Series(0,indexdf.index)macd_signal[macd_data[macd]macd_data[signal]]1macd_signal[macd_data[macd]macd_data[signal]]-1macd_strengthabs(macd_data[hist])/df[close]*100macd_strengthmacd_strength/macd_strength.max()ifmacd_strength.max()0elsemacd_strength signals[macd]macd_signal strengths[macd]macd_strength#
RSI信号rsi_valuersi(df[close],
rsi_signalpd.Series(0,indexdf.index)rsi_signal[rsi_value50]1rsi_signal[rsi_value50]-1rsi_strengthabs(rsi_value-
/50rsi_strengthrsi_strength/rsi_strength.max()ifrsi_strength.max()0elsersi_strength signals[rsi]rsi_signal strengths[rsi]rsi_strength#
布林带信号boll_databoll(df[close],20,
boll_signalpd.Series(0,indexdf.index)boll_signal[df[close]boll_data[upper]]-1# 超买boll_signal[df[close]boll_data[lower]]1# 超卖boll_position(df[close]-boll_data[lower])/(boll_data[upper]-boll_data[lower])boll_strengthabs(boll_position-
0.
*2boll_strengthboll_strength/boll_strength.max()ifboll_strength.max()0elseboll_strength signals[boll]boll_signal strengths[boll]boll_strength# 综合信号#
信号一致性多数指标方向一致signal_dfpd.DataFrame(signals)composite_signalsignal_df.sum(axis
composite_signal[composite_signal0]1composite_signal[composite_signal0]-1composite_signal[composite_signal0]0#
综合强度加权平均strength_dfpd.DataFrame(strengths)weights{ma:
3,macd:
3,rsi:
2,boll:
2}composite_strength(strength_df[ma]*weights[ma]strength_df[macd]*weights[macd]strength_df[rsi]*weights[rsi]strength_df[boll]*weights[boll])# 考虑信号一致性consistencysignal_df.apply(lambdax:len(x[xx.mode().iloc[0]])/len(x),axis
composite_strengthcomposite_strength*consistencyreturncomposite_signal,composite_strength,signals,strengths# 计算综合信号强度composite_signal,composite_strength,signals,strengthscalculate_composite_signal_strength(klines)print(综合信号强度统计:)print(f 买入信号:{(composite_signal
.sum()})print(f 卖出信号:{(composite_signal-
.sum()})print(f 平均强度:{composite_strength.mean():.3f})print(f 强信号 (
0.
:{(composite_strength
0.
.sum()})print(f 弱信号 (
0.
:{(composite_strength
0.
.sum()})api.close()
信号强度量化
1 强度分级#!/usr/bin/env python# -*- coding: utf-8 -*- 功能信号强度分级 说明本代码仅供学习参考 importpandasaspdimportnumpyasnpdefclassify_signal_strength(strength,thresholds[
3,
5,
7]): 信号强度分级 参数: strength: 强度序列 thresholds: 分级阈值 返回: levels: 强度等级 (0: 无, 1: 弱, 2: 中, 3: 强) levelspd.Series(0,indexstrength.index)levels[(strengththresholds[0])(strengththresholds[1])]1# 弱levels[(strengththresholds[1])(strengththresholds[2])]2# 中levels[strengththresholds[2]]3# 强returnlevels# 使用示例strengthpd.Series([
1,
4,
6,
8,
2,
9])levelsclassify_signal_strength(strength)print(信号强度分级:)level_names{0:无信号,1:弱信号,2:中等信号,3:强信号}forlevel,countinlevels.value_counts().items():print(f{level_names[level]}:{count})
基于强度的仓位调整
1 强度仓位映射#!/usr/bin/env python# -*- coding: utf-8 -*- 功能基于信号强度的仓位调整 说明本代码仅供学习参考 importpandasaspdimportnumpyasnpfromtqsdkimportTqApi,TqAuthclassStrengthBasedPosition:基于信号强度的仓位管理def__init__(self,base_position10,max_position
: 初始化 参数: base_position: 基础仓位 max_position: 最大仓位 self.base_positionbase_position self.max_positionmax_positiondefcalculate_position(self,signal,strength): 根据信号和强度计算仓位 参数: signal: 信号方向 (-1, 0,
strength: 信号强度 (0-
ifsignal0:return0# 根据强度调整仓位positionint(self.base_position(self.max_position-self.base_position)*strength)# 应用信号方向positionposition*signalreturnposition# 使用示例apiTqApi(authTqAuth(快期账户,快期密码))SYMBOLSHFE.rb2510klinesapi.get_kline_serial(SYMBOL,3600,
api.wait_update()fromsignal_strength_calculationimportcalculate_composite_signal_strength composite_signal,composite_strength,_,_calculate_composite_signal_strength(klines)position_managerStrengthBasedPosition(base_position10,max_position
positions[]foriinrange(len(klines)):signalcomposite_signal.iloc[i]strengthcomposite_strength.iloc[i]posposition_manager.calculate_position(signal,strength)positions.append(pos)positions_seriespd.Series(positions,indexklines.index)print(基于信号强度的仓位:)print(f 平均仓位:{positions_series.abs().mean():.1f})print(f 最大仓位:{positions_series.abs().max()})print(f 强信号平均仓位:{positions_series[composite_strength
7].abs().mean():.1f})print(f 弱信号平均仓位:{positions_series[composite_strength
3].abs().mean():.1f})api.close()
八、
常见问题Q1: 信号强度评估一定准确吗A: 不一定需要注意强度评估方法要合理历史有效性要验证市场环境会变化需要持续优化Q2: 如何选择强度评估方法A: 建议简单场景单指标强度复杂场景多指标综合需要精确机器学习方法实时应用轻量级方法Q3: 强度评估会增加延迟吗A: 可能但可以使用历史数据预计算优化计算逻辑使用缓存机制并行计算多个指标
九、
总结要点说明强度分类强、中、弱、无信号单指标强度基于指标偏离程度多指标综合考虑一致性和强度强度分级量化强度等级仓位调整根据强度调整仓位下一步学习建议学习机器学习信号强度预测研究动态强度调整探索强度与收益关系学习强度过滤优化免责声明本文仅供学习交流使用不构成任何投资建议。
期货交易有风险入市需谨慎。
更多资源天勤量化官网https://www.shinnytech.comGitHub开源地址https://github.com/shinnytech/tqsdk-python官方文档https://doc.shinnytech.com/tqsdk/latest