金融量化 - 技术分析策略和交易系统_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中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

Deepseek使用指南与提问优化策略方式

《Deepseek使用指南与提问优化策略方式》本文介绍了DeepSeek语义搜索引擎的核心功能、集成方法及优化提问策略,通过自然语言处理和机器学习提供精准搜索结果,适用于智能客服、知识库检索等领域... 目录序言1. DeepSeek 概述2. DeepSeek 的集成与使用2.1 DeepSeek API

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

SpringBoot中的404错误:原因、影响及解决策略

《SpringBoot中的404错误:原因、影响及解决策略》本文详细介绍了SpringBoot中404错误的出现原因、影响以及处理策略,404错误常见于URL路径错误、控制器配置问题、静态资源配置错误... 目录Spring Boot中的404错误:原因、影响及处理策略404错误的出现原因1. URL路径错

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Redis多种内存淘汰策略及配置技巧分享

《Redis多种内存淘汰策略及配置技巧分享》本文介绍了Redis内存满时的淘汰机制,包括内存淘汰机制的概念,Redis提供的8种淘汰策略(如noeviction、volatile-lru等)及其适用场... 目录前言一、什么是 Redis 的内存淘汰机制?二、Redis 内存淘汰策略1. pythonnoe

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ