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

相关文章

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Java集成Onlyoffice的示例代码及场景分析

《Java集成Onlyoffice的示例代码及场景分析》:本文主要介绍Java集成Onlyoffice的示例代码及场景分析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 需求场景:实现文档的在线编辑,团队协作总结:两个接口 + 前端页面 + 配置项接口1:一个接口,将o

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

IDEA下"File is read-only"可能原因分析及"找不到或无法加载主类"的问题

《IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题》:本文主要介绍IDEA下Fileisread-only可能原因分析及找不到或无法加载主类的问题,具有很好的参... 目录1.File is read-only”可能原因2.“找不到或无法加载主类”问题的解决总结1.File

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

Dubbo之SPI机制的实现原理和优势分析

《Dubbo之SPI机制的实现原理和优势分析》:本文主要介绍Dubbo之SPI机制的实现原理和优势,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Dubbo中SPI机制的实现原理和优势JDK 中的 SPI 机制解析Dubbo 中的 SPI 机制解析总结Dubbo中

C#继承之里氏替换原则分析

《C#继承之里氏替换原则分析》:本文主要介绍C#继承之里氏替换原则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#里氏替换原则一.概念二.语法表现三.类型检查与转换总结C#里氏替换原则一.概念里氏替换原则是面向对象设计的基本原则之一:核心思想:所有引py

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细