金融量化 - 技术分析策略和交易系统_SMA+CCI交易系统

2023-11-22 08:10

本文主要是介绍金融量化 - 技术分析策略和交易系统_SMA+CCI交易系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

双技术指标:SMA+CCI交易系统

以SMA作为开平仓信号,同时增加CCI作为过滤器;
当股价上穿SMA,同时CCI要小于-100,说明是在超卖的情况下,上穿SMA,做多;交易信号更可信;
当股价下穿SMA,同时CCI要大于+100,说明是在超买的情况下,下穿SMA,做空;交易信号更可信;

import numpy as np
import pandas as pd
import talib as ta
import tushare as ts
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
# 确保‘-’号显示正常
mpl.rcParams['axes.unicode_minus']=False
# 确保中文显示正常
mpl.rcParams['font.sans-serif'] = ['SimHei']  

1. 数据准备

# 获取数据
stock_index = ts.get_k_data('hs300', '2016-01-01', '2017-06-30')
stock_index.set_index('date', inplace=True)
stock_index.sort_index(inplace = True)
stock_index.head()
openclosehighlowvolumecode
date
2016-01-043725.863470.413726.253469.01115370674.0hs300
2016-01-053382.183478.783518.223377.28162116984.0hs300
2016-01-063482.413539.813543.743468.47145966144.0hs300
2016-01-073481.153294.383481.153284.7444102641.0hs300
2016-01-083371.873361.563418.853237.93185959451.0hs300
# 计算指标sma,cci
stock_index['sma'] = ta.SMA(np.asarray(stock_index['close']), 5)
stock_index['cci'] = ta.CCI(np.asarray(stock_index['high']), np.asarray(stock_index['low']), np.asarray(stock_index['close']), timeperiod=20)
plt.subplot(2,1,1)
plt.title('沪深300 sma cci指标图')
plt.gca().axes.get_xaxis().set_visible(False)
stock_index['close'].plot(figsize = (10,8))
stock_index['sma'].plot(figsize=(10,8))
plt.legend()
plt.subplot(2,1,2)
stock_index['cci'].plot(figsize = (10,8))
plt.legend()
plt.show()

在这里插入图片描述

2. 交易信号、持仓信号和策略逻辑

2.1 交易信号
# 产生开仓信号时应使用昨日及前日数据,以避免未来数据
stock_index['yes_close'] = stock_index['close'].shift(1)
stock_index['yes_sma'] = stock_index['sma'].shift(1)
stock_index['yes_cci'] = stock_index['cci'].shift(1)   #CCI是作为策略的一个过滤器;
stock_index['daybeforeyes_close'] = stock_index['close'].shift(2)
stock_index['daybeforeyes_sma'] = stock_index['sma'].shift(2)
stock_index.head()
# 产生交易信号
# sma开多信号:昨天股价上穿SMA;
stock_index['sma_signal'] = np.where(np.logical_and(stock_index['daybeforeyes_close']<stock_index['daybeforeyes_sma'],stock_index['yes_close']>stock_index['yes_sma']), 1, 0)
# sma开空信号:昨天股价下穿SMA
stock_index['sma_signal'] = np.where(np.logical_and(stock_index['daybeforeyes_close']>stock_index['daybeforeyes_sma'],stock_index['yes_close']<stock_index['yes_sma']),-1,stock_index['sma_signal'])
# 产生cci做多过滤信号
stock_index['cci_filter'] = np.where(stock_index['yes_cci'] < -100, 1, 0)
# 产生cci做空过滤信号
stock_index['cci_filter']  = np.where(stock_index['yes_cci'] > 100,-1, stock_index['cci_filter'])
# 过滤后的开多信号
stock_index['filtered_signal'] = np.where(stock_index['sma_signal']+stock_index['cci_filter']==2, 1, 0)
# 过滤后的开空信号
stock_index['filtered_signal'] = np.where(stock_index['sma_signal']+stock_index['cci_filter']==-2, -1,stock_index['filtered_signal'])
# 生成交易信号
stock_index.tail()
openclosehighlowvolumecodesmacciyes_closeyes_smayes_ccidaybeforeyes_closedaybeforeyes_smasma_signalcci_filterfiltered_signal
date
2017-06-263627.023668.093671.943627.02134637995.0hs3003603.152190.6622443622.883580.268127.7669723590.343559.4440-10
2017-06-273665.583674.723676.533648.7697558702.0hs3003628.798180.0563393668.093603.152190.6622443622.883580.2680-10
2017-06-283664.163646.173672.193644.0397920858.0hs3003640.440138.0384753674.723628.798180.0563393668.093603.1520-10
2017-06-293649.253668.833669.133644.7385589498.0hs3003656.138130.6850003646.173640.440138.0384753674.723628.7980-10
2017-06-303654.733666.803669.763646.2381510028.0hs3003664.922118.3146723668.833656.138130.6850003646.173640.4400-10
# 绘制cci,sma指标图
plt.subplot(3,1,1)
plt.title('沪深300 CCI SMA指标图')
plt.gca().axes.get_xaxis().set_visible(False)
stock_index['close'].plot(figsize=(12,8))
stock_index['sma'].plot()
plt.legend(loc='upper left')
plt.subplot(3,1,2)
stock_index['cci'].plot(figsize=(12, 8))
plt.legend(loc='upper left')
plt.subplot(3,1,3)
stock_index['filtered_signal'].plot(figsize=(12, 8), marker='o', linestyle='')
plt.legend(loc='upper left')
plt.show()

在这里插入图片描述

2.2 持仓信号
# 记录持仓情况,默认为0
position = 0
# 对每一交易日进行循环
for i, item in stock_index.iterrows():# 判断交易信号if item['filtered_signal'] == 1:# 交易信号为1,则记录仓位为1position = 1elif item['filtered_signal'] == -1:# 交易信号为-1, 则记录仓位为-1position = -1else:pass# 记录每日持仓情况stock_index.loc[i, 'position'] = position
stock_index.head()
plt.subplot(3, 1, 1)
plt.title('600030 CCI持仓图')
plt.gca().axes.get_xaxis().set_visible(False)
stock_index['close'].plot(figsize = (12,12))
plt.legend()
plt.subplot(3, 1, 2)
stock_index['cci'].plot(figsize = (12,12))
plt.legend()
plt.gca().axes.get_xaxis().set_visible(False)
plt.subplot(3, 1, 3)
stock_index['position'].plot(marker='o', figsize=(12,12),linestyle='')
plt.legend()
plt.show()

在这里插入图片描述

3.策略收益和数据可视化

# 计算策略收益
# 计算股票每日收益率
stock_index['pct_change'] = stock_index['close'].pct_change()
# 计算策略每日收益率
stock_index['strategy_return'] = stock_index['pct_change'] * stock_index['position']
# 计算股票累积收益率
stock_index['return'] = (stock_index['pct_change']+1).cumprod()
# 计算策略累积收益率
stock_index['strategy_cum_return'] = (1 + stock_index['strategy_return']).cumprod()
stock_index.head()
openclosehighlowvolumecodesmacciyes_closeyes_sma...daybeforeyes_closedaybeforeyes_smasma_signalcci_filterfiltered_signalpositionpct_changestrategy_returnreturnstrategy_cum_return
date
2016-01-043725.863470.413726.253469.01115370674.0hs300NaNNaNNaNNaN...NaNNaN0000.0NaNNaNNaNNaN
2016-01-053382.183478.783518.223377.28162116984.0hs300NaNNaN3470.41NaN...NaNNaN0000.00.0024120.01.0024121.0
2016-01-063482.413539.813543.743468.47145966144.0hs300NaNNaN3478.78NaN...3470.41NaN0000.00.0175440.01.0199981.0
2016-01-073481.153294.383481.153284.7444102641.0hs300NaNNaN3539.81NaN...3478.78NaN0000.0-0.069334-0.00.9492771.0
2016-01-083371.873361.563418.853237.93185959451.0hs3003428.988NaN3294.38NaN...3539.81NaN0000.00.0203920.00.9686351.0

5 rows × 21 columns

# 将股票累积收益率和策略累积收益率绘图
stock_index[['return', 'strategy_cum_return']].plot(figsize = (12,6))plt.title('600030 CCI收益图')
plt.legend()
plt.show()

在这里插入图片描述

这篇关于金融量化 - 技术分析策略和交易系统_SMA+CCI交易系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/408728

相关文章

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

SpringBoot中4种数据水平分片策略

《SpringBoot中4种数据水平分片策略》数据水平分片作为一种水平扩展策略,通过将数据分散到多个物理节点上,有效解决了存储容量和性能瓶颈问题,下面小编就来和大家分享4种数据分片策略吧... 目录一、前言二、哈希分片2.1 原理2.2 SpringBoot实现2.3 优缺点分析2.4 适用场景三、范围分片

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三