浅谈python量化 双均线策略(金叉死叉)
作者:初心fly 时间:2022-05-28 02:21:58
#小策略,策略逻辑是在金叉时候买进,死叉时候卖出,所谓金叉死叉是两条均线的交叉,当短期均线上穿长期均线为金叉,反之为死叉
#下面是策略代码及结构
# 导入函数库
from jqdata import *
# 初始化函数
def initialize(context):
# 设定沪深300作为基准
set_benchmark('000300.XSHG')
# True为开启动态复权模式,使用真实价格交易
set_option('use_real_price', True)
# 股票类交易手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(open_tax=0, close_tax=0.001, \
open_commission=0.0003, close_commission=0.0003,\
close_today_commission=0, min_commission=5), type='stock')
#华谊股票
g.security='300027.XSHE'
#设置每天运行
run_daily(handle)
def handle(context):
security=g.security
n5=5
n20=20
# 获取股票的收盘价
close_data = attribute_history(security, n20, '1d',"close",df=False)
print(close_data)
# 取得过去 ma_n1 天的平均价格
ma_n5 = close_data['close'][-n5:].mean()
# 取得过去 ma_n2 天的平均价格
ma_n20 = close_data['close'][-n20:].mean()
print(ma_n5,ma_n20)
# 取得当前的现金
cash = context.portfolio.available_cash
# 如果当前有余额
if ma_n5 > ma_n20:
# 用所有 cash 买入股票,order_value是买卖价值
order_value(security, cash)
# 记录这次买入
log.info("Buying %s" % security)
# 如果n5日均线小于n20日均线,并且目前有头寸
elif ma_n5 < ma_n20 and context.portfolio.positions[security].closeable_amount > 0:
# 全部卖出,order_target是买卖数量
order_target(security, 0)
# 记录这次卖出
log.info("Selling %s" % (security))
# 绘制n5日均线价格
record(ma_n5=ma_n5)
# 绘制n20日均线价格
record(ma_n20=ma_n20)
#整体结果在12-16年回测测试结果效益不错,阿尔法贝塔最大回撤也还行,难点是在策略和框架的使用和调用,这就是这次的双均线策略记录
补充知识:(多头、空头、金叉、死叉、卖出信号、买出信号)的python处理
1.指标概念
均值性指标:以平均数据作为参考的指标
随机性指标:以最高价最低价等为参考的指标
2.多头、空头、金叉、死叉、卖出信号、买出信号
多头:短期均线在长期均线上方
空头:短期均线在长期均线下方
金叉:短期均线向上穿越长期均线
死叉:短期均线向下穿越长期均线
买出信号:金叉 + 一定的条件
卖出出信号:死叉 + 一定的条件
3.Python实现:以KDJ为例
4.测试:筛选沪深股票中卖出信号的股票
来源:https://blog.csdn.net/weixin_42357472/article/details/82952471
标签:python,量化,双均线策略,金叉死叉
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python正则表达式指南 推荐
2021-10-18 22:04:30
![](https://img.aspxhome.com/file/2023/1/125211_0s.png)
Python pyecharts模块安装与入门教程
2023-09-11 06:52:41
![](https://img.aspxhome.com/file/2023/3/118653_0s.png)
pyftplib中文乱码问题解决方案
2023-11-10 11:34:07
![](https://img.aspxhome.com/file/2023/9/90549_0s.png)
Android分包MultiDex策略详解
2021-11-14 13:28:42
Django项目中动态设置静态文件路径的全过程
2021-07-12 00:52:24
![](https://img.aspxhome.com/file/2023/9/126909_0s.jpg)
Python面向对象中类(class)的简单理解与用法分析
2021-01-05 00:07:37
pytorch如何利用ResNet18进行手写数字识别
2022-02-07 04:07:26
索引在Oracle中的应用深入分析
2023-07-04 19:50:49
GC与JS内存泄露
2010-09-25 19:01:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/01-21s.jpg)
python实现csv格式文件转为asc格式文件的方法
2021-10-12 19:10:37
![](https://img.aspxhome.com/file/2023/3/72823_0s.jpg)
Python利用Django如何写restful api接口详解
2022-11-16 13:26:32
![](https://img.aspxhome.com/file/2023/0/98830_0s.jpg)
asp模块化分页源码
2008-04-13 07:02:00
Pandas统计重复的列里面的值方法
2022-03-06 20:20:57
对python生成业务报表的实例详解
2021-09-07 03:47:30
![](https://img.aspxhome.com/file/2023/3/120453_0s.jpg)
python用线性回归预测股票价格的实现代码
2023-01-24 02:14:31
![](https://img.aspxhome.com/file/2023/9/94129_0s.png)
ext3下删除mysql数据库的数据恢复案例
2009-05-13 14:39:00
python提取页面内url列表的方法
2022-12-26 15:29:27
在Pandas DataFrame中插入一列的方法实例
2021-08-17 19:39:18
python用正则表达式提取/匹配中文汉字
2021-06-12 23:45:50
![](https://img.aspxhome.com/file/2023/3/99303_0s.jpg)
Python基于递归算法求最小公倍数和最大公约数示例
2021-07-20 09:36:23