金融量化 - 技术分析策略和交易系统_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

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3

找不到Anaconda prompt终端的原因分析及解决方案

《找不到Anacondaprompt终端的原因分析及解决方案》因为anaconda还没有初始化,在安装anaconda的过程中,有一行是否要添加anaconda到菜单目录中,由于没有勾选,导致没有菜... 目录问题原因问http://www.chinasem.cn题解决安装了 Anaconda 却找不到 An

Spring定时任务只执行一次的原因分析与解决方案

《Spring定时任务只执行一次的原因分析与解决方案》在使用Spring的@Scheduled定时任务时,你是否遇到过任务只执行一次,后续不再触发的情况?这种情况可能由多种原因导致,如未启用调度、线程... 目录1. 问题背景2. Spring定时任务的基本用法3. 为什么定时任务只执行一次?3.1 未启用

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

Spring、Spring Boot、Spring Cloud 的区别与联系分析

《Spring、SpringBoot、SpringCloud的区别与联系分析》Spring、SpringBoot和SpringCloud是Java开发中常用的框架,分别针对企业级应用开发、快速开... 目录1. Spring 框架2. Spring Boot3. Spring Cloud总结1. Sprin