joinquant 策略
代码
# 克隆自聚宽文章:https://www.joinquant.com/post/20590
# 标题:ETF单均线跟踪轮动
# 作者:那時花開海布裡
'''
=================================================
总体回测前设置参数和回测
=================================================
'''
def initialize(context):
set_params() #1设置策参数
set_variables() #2设置中间变量
set_backtest() #3设置回测条件
#1 设置参数
def set_params():
# 设置基准收益
set_benchmark('000300.XSHG')
g.lag = 13
g.hour = 14
g.minute = 25
g.hs = '000300.XSHG' #300指数
g.zz = '399006.XSHE'#创业板指数
g.sz = '000016.XSHG' #50指数
g.ETF300 = '000300.XSHG'#'300指数
g.ETF500 = '399006.XSHE'#'300指数
g.ETF50 = '000016.XSHG' #50指数
g.ETFrili = '511880.XSHG' #银华日利
#2 设置中间变量
def set_variables():
return
#3 设置回测条件
def set_backtest():
set_option('use_real_price', True) #用真实价格交易
log.set_level('order', 'error')
'''
=================================================
每天开盘前
=================================================
'''
#每天开盘前要做的事情
def before_trading_start(context):
set_slip_fee(context)
#4
# 根据不同的时间段设置滑点与手续费
def set_slip_fee(context):
# 将滑点设置为0
set_slippage(FixedSlippage(0))
# 根据不同的时间段设置手续费
dt=context.current_dt
if dt>datetime.datetime(2013,1, 1):
set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
elif dt>datetime.datetime(2011,1, 1):
set_commission(PerTrade(buy_cost=0.001, sell_cost=0.002, min_cost=5))
elif dt>datetime.datetime(2009,1, 1):
set_commission(PerTrade(buy_cost=0.002, sell_cost=0.003, min_cost=5))
else:
set_commission(PerTrade(buy_cost=0.003, sell_cost=0.004, min_cost=5))
'''
=================================================
每日交易时
=================================================
'''
def handle_data(context, data):
# 获得当前时间
hour = context.current_dt.hour
minute = context.current_dt.minute
# 每天收盘时调整仓位
if hour == g.hour and minute == g.minute:
signal = get_signal(context)
if signal == 'sell_the_stocks':
sell_the_stocks(context)
print(context.portfolio.positions[g.ETFrili].total_amount)
elif signal == 'ETF300' or signal == 'ETF500':
buy_the_stocks(context,signal)
elif signal == 's500b300':
sell_the_stocks(context)
buy_the_stocks(context,'ETF300')
elif signal == 'sell_the_stocks':
sell_the_stocks(context)
buy_the_stocks(context,'ETF500')
elif signal == 's500b50':
sell_the_stocks(context)
buy_the_stocks(context,'ETF50')
elif signal == 's300b50':
sell_the_stocks(context)
buy_the_stocks(context,'ETF50')
elif signal == 's50b300':
sell_the_stocks(context)
buy_the_stocks(context,'ETF300')
elif signal == 's50b500':
sell_the_stocks(context)
buy_the_stocks(context,'ETF500')
#5
#获取信号
def get_signal(context):
#沪深300与创业板和上证50的当前股价
hs300,cp300,av300 = getStockPrice(g.hs, g.lag)
zz500,cp500,av500 = getStockPrice(g.zz, g.lag)
sz50,cp50,av50 = getStockPrice(g.sz, g.lag)
#计算前13日变动
hs300increase = (cp300 - hs300) / hs300
zz500increase = (cp500 - zz500) / zz500
sz50increase = (cp50 - sz50) / sz50
hold300 = context.portfolio.positions[g.ETF300].total_amount
hold500 = context.portfolio.positions[g.ETF500].total_amount
hold50 = context.portfolio.positions[g.ETF50].total_amount
if (hold300>0 and cp300<av300 and cp500<av500 and cp50<av50) or (hold500>0 and cp300<av300 and cp500<av500 and cp50<av50 ) or (hold50>0and cp300<av300 and cp500<av500 and cp50<av50):
return 'sell_the_stocks'
elif hs300increase>zz500increase and hs300increase>sz50increase and (hold300==0 and hold500==0 and hold50==0) and cp300>av300:
return 'ETF300'
elif zz500increase>hs300increase and zz500increase>sz50increase and (hold50==0 and hold300==0 and hold500==0) and cp500>av500:
return 'ETF500'
elif sz50increase>hs300increase and sz50increase>zz500increase and (hold50==0 and hold300==0 and hold500==0) and cp500>av500:
return 'ETF50'
elif hold500>0 and zz500increase<hs300increase and hs300increase>sz50increase and cp300>av300:
return 's500b300'
elif hold500>0 and zz500increase<sz50increase and hs300increase<sz50increase and cp50>av50:
return 's500b50'
elif hold300>0 and zz500increase>hs300increase and zz500increase>sz50increase and cp500>av500:
return 's300b500'
elif hold300>0 and sz50increase>hs300increase and sz50increase>zz500increase and cp50>av50:
return 's300b50'
elif hold50>0 and hs300increase>sz50increase and hs300increase>zz500increase and cp300>av300:
return's50b300'
elif hold50>0 and zz500increase>sz50increase and zz500increase>hs300increase and cp500>av500:
return's50b500'
#6
#取得股票某个区间内的所有收盘价(用于取前13日和当前价格)
def getStockPrice(stock, interval):
h = attribute_history(stock, interval*240, unit='1m', fields=('close'), skip_paused=True)
return (h['close'].values[0],h['close'].values[-1],h['close'].mean())
def getCurrentPrice(stock):
h= attribute_history(stock, 1, unit='1m', fields=('close'), skip_paused=True)
return (h['close'].values[-1])
#7
#卖出股票
def sell_the_stocks(context):
for stock in context.portfolio.positions.keys():
return (log.info("Selling %s" % stock), order_target_value(stock, 0),order_value('511880.XSHG', context.portfolio.cash))
#8
#买入股票
def buy_the_stocks(context,signal):
holdrili = context.portfolio.positions[g.ETFrili].total_amount
prili = getCurrentPrice(g.ETFrili)
if holdrili ==0 :
return (log.info("Buying %s"% signal ),order_value(eval('g.%s'% signal), context.portfolio.cash))
elif holdrili !=0:
return (log.info("Buying %s"% signal ),order_target_value(g.ETFrili, 0),order_value(eval('g.%s'% signal), holdrili*prili))
'''
=================================================
每日收盘后(本策略中不需要)
=================================================
'''
def after_trading_end(context):
return
代码, 清晰版
# 克隆自聚宽文章:https://www.joinquant.com/post/20590
# 标题:ETF单均线跟踪轮动
# 作者:那時花開海布裡
'''
=================================================
总体回测前设置参数和回测
=================================================
'''
def initialize(context):
set_params() #1设置策参数
set_variables() #2设置中间变量
set_backtest() #3设置回测条件
#1 设置参数
def set_params():
# 设置基准收益
set_benchmark('000300.XSHG')
g.lag = 13
g.hour = 14
g.minute = 25
g.hs = '000300.XSHG' #300指数
g.zz = '399006.XSHE'#创业板指数
g.sz = '000016.XSHG' #50指数
g.ETF300 = '000300.XSHG'#'300指数
g.ETF500 = '399006.XSHE'#'300指数
g.ETF50 = '000016.XSHG' #50指数
g.ETFrili = '511880.XSHG' #银华日利
#2 设置中间变量
def set_variables():
return
#3 设置回测条件
def set_backtest():
set_option('use_real_price', True) #用真实价格交易
log.set_level('order', 'error')
'''
=================================================
每天开盘前
=================================================
'''
#每天开盘前要做的事情
def before_trading_start(context):
set_slip_fee(context)
#4
# 根据不同的时间段设置滑点与手续费
def set_slip_fee(context):
# 将滑点设置为0
set_slippage(FixedSlippage(0))
# 根据不同的时间段设置手续费
dt=context.current_dt
if dt>datetime.datetime(2013,1, 1):
set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
elif dt>datetime.datetime(2011,1, 1):
set_commission(PerTrade(buy_cost=0.001, sell_cost=0.002, min_cost=5))
elif dt>datetime.datetime(2009,1, 1):
set_commission(PerTrade(buy_cost=0.002, sell_cost=0.003, min_cost=5))
else:
set_commission(PerTrade(buy_cost=0.003, sell_cost=0.004, min_cost=5))
'''
=================================================
每日交易时
=================================================
'''
def handle_data(context, data):
# 获得当前时间
hour = context.current_dt.hour
minute = context.current_dt.minute
# 每天收盘时调整仓位
if hour == g.hour and minute == g.minute:
signal = get_signal(context)
if signal == 'sell_the_stocks':
sell_the_stocks(context)
print(context.portfolio.positions[g.ETFrili].total_amount)
elif signal == 'ETF300' or signal == 'ETF500':
buy_the_stocks(context,signal)
elif signal == 's500b300':
sell_the_stocks(context)
buy_the_stocks(context,'ETF300')
elif signal == 'sell_the_stocks':
sell_the_stocks(context)
buy_the_stocks(context,'ETF500')
elif signal == 's500b50':
sell_the_stocks(context)
buy_the_stocks(context,'ETF50')
elif signal == 's300b50':
sell_the_stocks(context)
buy_the_stocks(context,'ETF50')
elif signal == 's50b300':
sell_the_stocks(context)
buy_the_stocks(context,'ETF300')
elif signal == 's50b500':
sell_the_stocks(context)
buy_the_stocks(context,'ETF500')
#5
#获取信号
def get_signal(context):
#沪深300与创业板和上证50的当前股价
hs300,cp300,av300 = getStockPrice(g.hs, g.lag)
zz500,cp500,av500 = getStockPrice(g.zz, g.lag)
sz50,cp50,av50 = getStockPrice(g.sz, g.lag)
#计算前13日变动
hs300increase = (cp300 - hs300) / hs300
zz500increase = (cp500 - zz500) / zz500
sz50increase = (cp50 - sz50) / sz50
hold300 = context.portfolio.positions[g.ETF300].total_amount
hold500 = context.portfolio.positions[g.ETF500].total_amount
hold50 = context.portfolio.positions[g.ETF50].total_amount
dpj = zz500increase<sz50increase and hs300increase<sz50increase and cp50>av50
zpj = zz500increase<hs300increase and hs300increase>sz50increase and cp300>av300
xpj = zz500increase>hs300increase and zz500increase>sz50increase and cp500>av500
pos_isEmpty = (hold300==0 and hold500==0 and hold50==0)
all_isDown = (cp300<av300 and cp500<av500 and cp50<av50)
#if (hold300>0 and all_isDown) or \
# (hold500>0 and all_isDown) or \
# (hold50>0 and all_isDown):
if (not pos_isEmpty) and all_isDown:
return 'sell_the_stocks'
elif pos_isEmpty:
if zpj:
return 'ETF300'
elif xpj:
return 'ETF500'
elif dpj:
return 'ETF50'
elif hold500>0 and zpj:
return 's500b300'
elif hold500>0 and dpj:
return 's500b50'
elif hold300>0 and xpj:
return 's300b500'
elif hold300>0 and dpj:
return 's300b50'
elif hold50>0 and zpj:
return's50b300'
elif hold50>0 and xpj:
return's50b500'
#6
#取得股票某个区间内的所有收盘价(用于取前13日和当前价格)
def getStockPrice(stock, interval):
h = attribute_history(stock, interval*240, unit='1m', fields=('close'), skip_paused=True)
return (h['close'].values[0],h['close'].values[-1],h['close'].mean())
def getCurrentPrice(stock):
h= attribute_history(stock, 1, unit='1m', fields=('close'), skip_paused=True)
return (h['close'].values[-1])
#7
#卖出股票
def sell_the_stocks(context):
for stock in context.portfolio.positions.keys():
return (log.info("Selling %s" % stock), order_target_value(stock, 0),order_value('511880.XSHG', context.portfolio.cash))
#8
#买入股票
def buy_the_stocks(context,signal):
holdrili = context.portfolio.positions[g.ETFrili].total_amount
prili = getCurrentPrice(g.ETFrili)
if holdrili ==0 :
return (log.info("Buying %s"% signal ),order_value(eval('g.%s'% signal), context.portfolio.cash))
elif holdrili !=0:
return (log.info("Buying %s"% signal ),order_target_value(g.ETFrili, 0),order_value(eval('g.%s'% signal), holdrili*prili))
'''
=================================================
每日收盘后(本策略中不需要)
=================================================
'''
def after_trading_end(context):
return
joinquant 策略的更多相关文章
- JoinQuant策略代码示例
总体回测前 ''' ================================================================================ 总体回测前 === ...
- 金融量化分析【day112】:量化交易策略基本框架
摘要 策略编写的基本框架及其实现 回测的含义及其实现 初步学习解决代码错误 周期循环的开始时间 自测与自学 通过前文对量化交易有了一个基本认识之后,我们开始学习做量化交易.毕竟就像学游泳,有些东西讲是 ...
- Python之关于量化投资实现代码--根据策略提出的代码--还未完善
# 根据缺口的模式选股买股票 ''' -------------------------------------------- 1.总体回测前要做的事情 initialize(context) 1.1 ...
- 高性能Javascript--脚本的无阻塞加载策略
Javascript在浏览器中的性能,可以说是前端开发者所要面对的最重要的可用性问题. 在Yahoo的Yslow23条规则当中,其中一条是将JS放在底部 .原因是,事实上,大多数浏览器使用单进程处理U ...
- javascript设计模式:策略模式
前言 策略模式有效利用组合.委托.多态等技术和思想,可以有效避免多重条件选择语句. 策略模式对开放-封闭原则提供了很好的支持,将算法封装在strategy中,使得他们易于切换.理解.扩展. 策略模式中 ...
- Unity3D 5.3 新版AssetBundle使用方案及策略
1.概览 Unity3D 5.0版本之后的AssetBundle机制和之前的4.x版本已经发生了很大的变化,一些曾经常用的流程已经不再使用,甚至一些老的API已经被新的API所取代. 因此,本文的主要 ...
- StrategyPattern (策略模式)
/** * 策略模式 * @author TMAC-J * 根据环境的不同选择不同的策略,把策略用接口抽象出来 */ public class StrategyPattern { interface ...
- 直播推流端弱网优化策略 | 直播 SDK 性能优化实践
弱网优化的场景 网络直播行业经过一年多的快速发展,衍生出了各种各样的玩法.最早的网络直播是主播坐在 PC 前,安装好专业的直播设备(如摄像头和麦克风),然后才能开始直播.后来随着手机性能的提升和直播技 ...
- JAVA 设计模式之策略模式
定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 类型:行为类模式 策略模式是对算法的封装,把一系列的算法分别封装到对应的类中,并且这些类实现相同的接口,相互之间可以替换.在前面说过 ...
随机推荐
- qq邮箱问卷,测试不支持form表单
想做个类似苹果调查问卷的: 找到qq邮箱的代码编辑器: 写好我们的网页(h5) <!DOCTYPE html> <html lang="en"> <h ...
- LA2965 Jurassic Remains
Jurassic Remains https://vjudge.net/problem/UVALive-2965 Paleontologists in Siberia have recently fo ...
- 使用requireJs进行模块化开发
requireJs使用 requireJs 使用require.js的第一步,是先去官方网站下载最新版本. 下载后,假定把它放在js子目录下面,就可以加载了. <script src=" ...
- Java review-basic6
1. Weak references: In computer programming, a weak reference is a reference that does not protect t ...
- 模板方法(Template Method)(父类声明算法骨架,子类具体不同实现)
在阎宏博士的<JAVA与模式>一书中开头是这样描述模板方法(Template Method)模式的: 模板方法模式是类的行为模式.准备一个抽象类,将部分逻辑以具体方法以及具体构造函数的形式 ...
- Angular js 具体应用(一)
1,首先引用Angular 百度静态资源库搜索Angular 复制链接,在HTML中嵌入script 最好写在正文下面 <script type="text/javascript& ...
- iTerm2+oh-my-zsh配色
效果图,很帅气有木有 一.首先安装iTem2 安装好后的截图如下: 安装好后的截图 二.安装oh-my-zsh. sh -c "$(curl -fsSL https://raw.gi ...
- Python实例 包机制
每一个.py文件称为一个module,module之间可以互相导入.请参看以下例子: # a.py def add_func(a,b): return a+b # b.py from a im ...
- R语言的可视化
1. 完整的数据分析流程 定义研究问题 定义理想数据集 确定能够获取什么数据 清理数据 2. 变量的类型: 数值变量(可进行加减乘除运算):连续(可在给定区间取任意数值).离散(给定集合内不连续取值) ...
- 避免闲置云资源浪费 | 阿里云轻量级分布式应用服务 SAE 邀您公测
您是否遇到过: 资源利用率低,多数服务器CPU平均利用率在10%以下,用户需为大量闲置资源买单. 感知 IaaS 购买和集群运维,人员技能要求高,运维效率低. 想拥抱 Kubernetes.微服务架构 ...