代码

# 克隆自聚宽文章: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 策略的更多相关文章

  1. JoinQuant策略代码示例

    总体回测前 ''' ================================================================================ 总体回测前 === ...

  2. 金融量化分析【day112】:量化交易策略基本框架

    摘要 策略编写的基本框架及其实现 回测的含义及其实现 初步学习解决代码错误 周期循环的开始时间 自测与自学 通过前文对量化交易有了一个基本认识之后,我们开始学习做量化交易.毕竟就像学游泳,有些东西讲是 ...

  3. Python之关于量化投资实现代码--根据策略提出的代码--还未完善

    # 根据缺口的模式选股买股票 ''' -------------------------------------------- 1.总体回测前要做的事情 initialize(context) 1.1 ...

  4. 高性能Javascript--脚本的无阻塞加载策略

    Javascript在浏览器中的性能,可以说是前端开发者所要面对的最重要的可用性问题. 在Yahoo的Yslow23条规则当中,其中一条是将JS放在底部 .原因是,事实上,大多数浏览器使用单进程处理U ...

  5. javascript设计模式:策略模式

    前言 策略模式有效利用组合.委托.多态等技术和思想,可以有效避免多重条件选择语句. 策略模式对开放-封闭原则提供了很好的支持,将算法封装在strategy中,使得他们易于切换.理解.扩展. 策略模式中 ...

  6. Unity3D 5.3 新版AssetBundle使用方案及策略

    1.概览 Unity3D 5.0版本之后的AssetBundle机制和之前的4.x版本已经发生了很大的变化,一些曾经常用的流程已经不再使用,甚至一些老的API已经被新的API所取代. 因此,本文的主要 ...

  7. StrategyPattern (策略模式)

    /** * 策略模式 * @author TMAC-J * 根据环境的不同选择不同的策略,把策略用接口抽象出来 */ public class StrategyPattern { interface ...

  8. 直播推流端弱网优化策略 | 直播 SDK 性能优化实践

    弱网优化的场景 网络直播行业经过一年多的快速发展,衍生出了各种各样的玩法.最早的网络直播是主播坐在 PC 前,安装好专业的直播设备(如摄像头和麦克风),然后才能开始直播.后来随着手机性能的提升和直播技 ...

  9. JAVA 设计模式之策略模式

    定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 类型:行为类模式 策略模式是对算法的封装,把一系列的算法分别封装到对应的类中,并且这些类实现相同的接口,相互之间可以替换.在前面说过 ...

随机推荐

  1. 2019-5-25-如何在-CMD-启动的软件传入带空格的路径

    title author date CreateTime categories 如何在 CMD 启动的软件传入带空格的路径 lindexi 2019-05-25 09:31:46 +0800 2019 ...

  2. 跟我一起做一个vue的小项目(六)

    接下来我们编写周末游组件 <template> <div> <div class="recommend-title">周末去哪儿</div ...

  3. 阿里云应用高可用服务 AHAS 流控降级实现 SQL 自动防护功能

    在影响系统稳定性的各种因素中,慢 SQL 是相对比较致命的,可能会导致 CPU.LOAD 异常.系统资源耗尽.线上生产环境出现慢 SQL 往往有很多原因: 硬件问题.如网络速度慢,内存不足,I/O 吞 ...

  4. Winform 分页

    1.图列展示 2.分页控件代码 Paging.Designer.cs partial class Paging { /// <summary> /// 必需的设计器变量. /// < ...

  5. @Service ,@Controller,@Component注解

    首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...

  6. hdoj 1325 Is It A Tree? 【并查集】

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shengweisong/article/details/34099151 做了一上午,最终ac了 w ...

  7. NKOJ3485 【2015多校联训4】数据

    问题描述 Mr_H 出了一道信息学竞赛题,就是给 n 个数排序.输入格式是这样的:试题有若干组数据.每组数据的第一个是一个整数 n,表示总共有 n 个数待排序:接下来 n 个整数,分别表示这n 个待排 ...

  8. webpack学习之—— Manifest

    Runtime runtime,以及伴随的 manifest 数据,主要是指:在浏览器运行时,webpack 用来连接模块化的应用程序的所有代码.runtime 包含:在模块交互时,连接模块所需的加载 ...

  9. dns-prefetch对网站速度能提升有多少?详解dns-prefetch。

    DNS解析场景 有很多大型的网站,都会用N 个CDN 域名来做图片.静态文件等资源访问.比如新浪,我们经常会看到有下列域. img1.sina.com.cn . img2.sina.com.cn .i ...

  10. perfcurve.m

    function [X,Y,T,auc,optrocpt,subY,subYnames] = ... perfcurve(labels,scores,posClass,varargin) %PERFC ...