本文主要是对于【新手入门教程】多股票追涨策略的修订,
主要是发现策略思想和策略代码中有较多的出入,错误太多,有很多代码根本就没有执行,因此对原策略进行了修订,
这个策略并没有什么亮点,也很垃圾,
我写她的主要目的也是便于新人学习,
学习内容:
?学会多股票追涨策略
?动态设置股票池,并去除ST、停牌
?通过attribute_history读取历史数据
?通过逻辑语言设置多重买入卖出条件并生成list股票列表
?list切片方法初体验以及最值、均值
?止损设置
1 确定策略内容
在之前的多股票策略中,我们学习了利用计算机强大的数据处理能力,同时监视市场上多只股票,如果满足条件就进行相应交易。简言之,对多个股票分别实行原本的单股票策略。但当时策略中我们使用的股票池手动添加的。
def initialize(context):
g.security = ['002043.XSHE','002582.XSHE'] # 将兔宝宝、好想你 两只股票代码存入list中
这种方法存在的一个明显缺陷是,操作对象始终是这两只股票,难以将范围扩大到全部股票市场中,同时也缺乏必要的止损设置。为了达到动态调整股票池的效果,这就要用到我们今天涉及的多股票的追涨策略。
基本思路:
?筛选出符合:
0<市盈率(pe_ratio)<30, 市净率(pb_ratio)<3, 净利润环比增长率(inc_net_profit_annual)>0.3的股票,按照销售毛利率进行降序排列,选取前150只
?买入:股票成交量突破20日内最高并且股票价格向上突破10日均线;
?卖出:成交量创10日内新低或者股价向下突破5日新低
?止损:当个股价格比买入价下跌超过10%时卖出止损
理清楚了基本思路,下一步就是将这个思路翻译成计算机能懂的语言:
1.运用get_fundalmentals函数查询相关财务数据并筛选出符合条件的股票
2.运用attribute_history查询单只股票确定时段内成交量和收盘价信息
3.利用if条件语句以及and、or逻辑语句进行买入卖出条件设置
4.设置个股止损条件
感谢您使用JoinQuant的研究功能~~
JoinQuant 研究功能基于IPython Notebook,支持灵活的图表处理、数据处理,后续我们还会增加财务数据。
研究功能支持导入第三方库,您可以根据需要引入。
https://www.joinquant.com/api#third_lib
研究功能提供分钟级数据,采用Docker技术隔离,资源独立、安全性更高、性能更好,同步支持Python2、Python3。
1 使用帮助(只读)
新手指引,常用API示例。
2 新手指引
研究功能简介,操作说明,常用API示例。
3 常用操作
New:
Text File:新建txt文件;
Folder:新建文件夹;
Python 2:新建基于Python 2的notebook;
Python 3:新建基于Python 3的notebook。
Upload:
上传文件,文件上传后可以在研究和回测中使用。
#使用“?”获取帮助示例,使用“Shift+Enter”运行;
get_price?
import pandas as pd
pd.DataFrame?
#使用Tab键补全函数名
get_
研究新增如下API
详见 https://www.joinquant.com/api#res_api
#导入需要的程序包
import pandas as pd
import seaborn as sns
#获取股票510300.XSHG2015年1月的日级交易数据
df = get_price('510300.XSHG', start_date='2014-01-01', end_date='2015-01-31', frequency='daily', fields=['open','close'])
df
open | close | |
---|---|---|
2014-01-02 | 2.37 | 2.37 |
2014-01-03 | 2.36 | 2.34 |
2014-01-06 | 2.34 | 2.29 |
2014-01-07 | 2.27 | 2.29 |
2014-01-08 | 2.29 | 2.28 |
2014-01-09 | 2.28 | 2.27 |
2014-01-10 | 2.27 | 2.26 |
2014-01-13 | 2.26 | 2.24 |
2014-01-14 | 2.24 | 2.25 |
2014-01-15 | 2.25 | 2.25 |
2014-01-16 | 2.25 | 2.26 |
2014-01-17 | 2.25 | 2.22 |
2014-01-20 | 2.22 | 2.21 |
2014-01-21 | 2.17 | 2.18 |
2014-01-22 | 2.18 | 2.24 |
2014-01-23 | 2.24 | 2.23 |
2014-01-24 | 2.22 | 2.24 |
2014-01-27 | 2.23 | 2.21 |
2014-01-28 | 2.22 | 2.22 |
2014-01-29 | 2.22 | 2.22 |
2014-01-30 | 2.22 | 2.19 |
2014-02-07 | 2.17 | 2.20 |
2014-02-10 | 2.22 | 2.27 |
2014-02-11 | 2.26 | 2.28 |
2014-02-12 | 2.28 | 2.28 |
2014-02-13 | 2.28 | 2.28 |
2014-02-14 | 2.28 | 2.29 |
2014-02-17 | 2.31 | 2.30 |
2014-02-18 | 2.30 | 2.28 |
2014-02-19 | 2.27 | 2.30 |
... | ... | ... |
2014-12-18 | 3.39 | 3.38 |
2014-12-19 | 3.39 | 3.43 |
2014-12-22 | 3.43 | 3.41 |
2014-12-23 | 3.38 | 3.34 |
2014-12-24 | 3.34 | 3.25 |
2014-12-25 | 3.27 | 3.37 |
2014-12-26 | 3.37 | 3.50 |
2014-12-29 | 3.57 | 3.48 |
2014-12-30 | 3.48 | 3.48 |
2014-12-31 | 3.49 | 3.57 |
2015-01-05 | 3.60 | 3.69 |
2015-01-06 | 3.66 | 3.66 |
2015-01-07 | 3.64 | 3.66 |
2015-01-08 | 3.67 | 3.58 |
2015-01-09 | 3.57 | 3.56 |
2015-01-12 | 3.56 | 3.54 |
2015-01-13 | 3.54 | 3.54 |
2015-01-14 | 3.54 | 3.53 |
2015-01-15 | 3.53 | 3.63 |
2015-01-16 | 3.64 | 3.66 |
2015-01-19 | 3.47 | 3.30 |
2015-01-20 | 3.31 | 3.38 |
2015-01-21 | 3.39 | 3.54 |
2015-01-22 | 3.54 | 3.54 |
2015-01-23 | 3.57 | 3.56 |
2015-01-26 | 3.58 | 3.59 |
2015-01-27 | 3.60 | 3.56 |
2015-01-28 | 3.53 | 3.50 |
2015-01-29 | 3.46 | 3.46 |
2015-01-30 | 3.48 | 3.42 |
265 rows × 2 columns
#获取平台支持的所有股票, ETF基金信息
get_all_securities()
display_name | name | start_date | end_date | |
---|---|---|---|---|
000001.XSHE | 平安银行 | PAYH | 1991-04-03 | 9999-01-01 |
000002.XSHE | 万 科A | WKA | 1991-01-29 | 9999-01-01 |
000004.XSHE | 国农科技 | GNKJ | 1990-12-01 | 9999-01-01 |
000005.XSHE | 世纪星源 | SJXY | 1990-12-10 | 9999-01-01 |
000006.XSHE | 深振业A | SZYA | 1992-04-27 | 9999-01-01 |
000007.XSHE | 零七股份 | LQGF | 1992-04-13 | 9999-01-01 |
000008.XSHE | 神州高铁 | SZGT | 1992-05-07 | 9999-01-01 |
000009.XSHE | 中国宝安 | ZGBA | 1991-06-25 | 9999-01-01 |
000010.XSHE | 深华新 | SHX | 1995-10-27 | 9999-01-01 |
000011.XSHE | 深物业A | SWYA | 1992-03-30 | 9999-01-01 |
000012.XSHE | 南 玻A | NBA | 1992-02-28 | 9999-01-01 |
000014.XSHE | 沙河股份 | SHGF | 1992-06-02 | 9999-01-01 |
000016.XSHE | 深康佳A | SKJA | 1992-03-27 | 9999-01-01 |
000017.XSHE | 深中华A | SZHA | 1992-03-31 | 9999-01-01 |
000018.XSHE | 中冠A | ZGA | 1992-06-16 | 9999-01-01 |
000019.XSHE | 深深宝A | SSBA | 1992-10-12 | 9999-01-01 |
000020.XSHE | 深华发A | SHFA | 1992-04-28 | 9999-01-01 |
000021.XSHE | 深科技 | SKJ | 1994-02-02 | 9999-01-01 |
000022.XSHE | 深赤湾A | SCWA | 1993-05-05 | 9999-01-01 |
000023.XSHE | 深天地A | STDA | 1993-04-29 | 9999-01-01 |
000024.XSHE | 招商地产 | ZSDC | 1993-06-07 | 9999-01-01 |
000025.XSHE | 特 力A | TLA | 1993-06-21 | 9999-01-01 |
000026.XSHE | 飞亚达A | FYDA | 1993-06-03 | 9999-01-01 |
000027.XSHE | 深圳能源 | SZNY | 1993-09-03 | 9999-01-01 |
000028.XSHE | 国药一致 | GYYZ | 1993-08-09 | 9999-01-01 |
000029.XSHE | 深深房A | SSFA | 1993-09-15 | 9999-01-01 |
000030.XSHE | 富奥股份 | FAGF | 1993-09-29 | 9999-01-01 |
000031.XSHE | 中粮地产 | ZLDC | 1993-10-08 | 9999-01-01 |
000032.XSHE | 深桑达A | SSDA | 1993-10-28 | 9999-01-01 |
000033.XSHE | *ST新都 | STXD | 1994-01-03 | 9999-01-01 |
... | ... | ... | ... | ... |
IF1407 | 期货 | None | 2014-05-19 | 2014-07-18 |
IF1408 | 期货 | None | 2014-06-23 | 2014-08-15 |
IF1409 | 期货 | None | 2014-01-20 | 2014-09-19 |
IF1410 | 期货 | None | 2014-08-18 | 2014-10-17 |
IF1411 | 期货 | None | 2014-09-22 | 2014-11-21 |
IF1412 | 期货 | None | 2014-04-21 | 2014-12-19 |
IF1501 | 期货 | None | 2014-11-24 | 2015-01-16 |
IF1502 | 期货 | None | 2014-12-22 | 2015-02-25 |
IF1503 | 期货 | None | 2014-07-21 | 2015-03-20 |
IF1504 | 期货 | None | 2015-02-26 | 2015-04-17 |
IF1505 | 期货 | None | 2015-03-23 | 2015-05-15 |
IF1506 | 期货 | None | 2014-10-20 | 2015-06-19 |
IF1507 | 期货 | None | 2015-05-18 | 2015-07-17 |
IF1508 | 期货 | None | 2015-06-23 | 2015-08-21 |
IF1509 | 期货 | None | 2015-01-19 | 2015-09-18 |
IF1510 | 期货 | None | 2015-08-24 | 2015-10-16 |
IF1511 | 期货 | None | 2015-09-21 | 2015-11-20 |
IF1512 | 期货 | None | 2015-04-20 | 2015-12-18 |
IF1603 | 期货 | None | 2015-07-20 | 2016-03-18 |
IF1606 | 期货 | None | 2015-10-19 | 2016-06-17 |
IH1505 | 期货 | None | 2015-04-16 | 2015-05-15 |
IH1506 | 期货 | None | 2015-04-16 | 2015-06-19 |
IH1507 | 期货 | None | 2015-05-18 | 2015-07-17 |
IH1508 | 期货 | None | 2015-06-23 | 2015-08-21 |
IH1509 | 期货 | None | 2015-04-16 | 2015-09-18 |
IH1510 | 期货 | None | 2015-08-24 | 2015-10-16 |
IH1511 | 期货 | None | 2015-09-21 | 2015-11-20 |
IH1512 | 期货 | None | 2015-04-16 | 2015-12-18 |
IH1603 | 期货 | None | 2015-07-20 | 2016-03-18 |
IH1606 | 期货 | None | 2015-10-19 | 2016-06-17 |
2937 rows × 4 columns
#获取沪深300指数的所有股票
get_index_stocks('000300.XSHG')
[u'000630.XSHE', u'000750.XSHE', u'002344.XSHE', u'600546.XSHG', u'601216.XSHG', u'600648.XSHG', u'601857.XSHG', u'601238.XSHG', u'000061.XSHE', u'601299.XSHG', u'002400.XSHE', u'600111.XSHG', u'601118.XSHG', u'600340.XSHG', u'600741.XSHG', u'600100.XSHG', u'000876.XSHE', u'000729.XSHE', u'002353.XSHE', u'000024.XSHE', u'600252.XSHG', u'600718.XSHG', u'002450.XSHE', u'002065.XSHE', u'002500.XSHE', u'600011.XSHG', u'601866.XSHG', u'000783.XSHE', u'601666.XSHG', u'002202.XSHE', u'600066.XSHG', u'601088.XSHG', u'601991.XSHG', u'600900.XSHG', u'002008.XSHE', u'600498.XSHG', u'000826.XSHE', u'002146.XSHE', u'000425.XSHE', u'600316.XSHG', u'000069.XSHE', u'600489.XSHG', u'600332.XSHG', u'600597.XSHG', u'601166.XSHG', u'601699.XSHG', u'600048.XSHG', u'601992.XSHG', u'600516.XSHG', u'000027.XSHE', u'600406.XSHG', u'601555.XSHG', u'603000.XSHG', u'002269.XSHE', u'600277.XSHG', u'601179.XSHG', u'600660.XSHG', u'002001.XSHE', u'600010.XSHG', u'600637.XSHG', u'600395.XSHG', u'000338.XSHE', u'601018.XSHG', u'600123.XSHG', u'000100.XSHE', u'002422.XSHE', u'600518.XSHG', u'600166.XSHG', u'002673.XSHE', u'600259.XSHG', u'000963.XSHE', u'000960.XSHE', u'600157.XSHG', u'601111.XSHG', u'600690.XSHG', u'601009.XSHG', u'002081.XSHE', u'600079.XSHG', u'000878.XSHE', u'601225.XSHG', u'600535.XSHG', u'600019.XSHG', u'600062.XSHG', u'600809.XSHG', u'600256.XSHG', u'600372.XSHG', u'600267.XSHG', u'000718.XSHE', u'000012.XSHE', u'002475.XSHE', u'600893.XSHG', u'002416.XSHE', u'600315.XSHG', u'002007.XSHE', u'600663.XSHG', u'600519.XSHG', u'000413.XSHE', u'000002.XSHE', u'600880.XSHG', u'000869.XSHE', u'601288.XSHG', u'600887.XSHG', u'601818.XSHG', u'601006.XSHG', u'000402.XSHE', u'002385.XSHE', u'000686.XSHE', u'600018.XSHG', u'000538.XSHE', u'600089.XSHG', u'002470.XSHE', u'000800.XSHE', u'600118.XSHG', u'601929.XSHG', u'000157.XSHE', u'002038.XSHE', u'002310.XSHE', u'000917.XSHE', u'601258.XSHG', u'600104.XSHG', u'000598.XSHE', u'601098.XSHG', u'000001.XSHE', u'600352.XSHG', u'600376.XSHG', u'002653.XSHE', u'600705.XSHG', u'000656.XSHE', u'601336.XSHG', u'000651.XSHE', u'601600.XSHG', u'000709.XSHE', u'600999.XSHG', u'600497.XSHG', u'600547.XSHG', u'600549.XSHG', u'600783.XSHG', u'600588.XSHG', u'601117.XSHG', u'600873.XSHG', u'600837.XSHG', u'002294.XSHE', u'002415.XSHE', u'601988.XSHG', u'601633.XSHG', u'601231.XSHG', u'600096.XSHG', u'601398.XSHG', u'601998.XSHG', u'600150.XSHG', u'601333.XSHG', u'600998.XSHG', u'600309.XSHG', u'002399.XSHE', u'601607.XSHG', u'600177.XSHG', u'600827.XSHG', u'600036.XSHG', u'000401.XSHE', u'600583.XSHG', u'601888.XSHG', u'000581.XSHE', u'000400.XSHE', u'000568.XSHE', u'600000.XSHG', u'601899.XSHG', u'000839.XSHE', u'000961.XSHE', u'002456.XSHE', u'600895.XSHG', u'000895.XSHE', u'002129.XSHE', u'002230.XSHE', u'601390.XSHG', u'600276.XSHG', u'000629.XSHE', u'000983.XSHE', u'601668.XSHG', u'600863.XSHG', u'000776.XSHE', u'000793.XSHE', u'600403.XSHG', u'601158.XSHG', u'600188.XSHG', u'600023.XSHG', u'600600.XSHG', u'601618.XSHG', u'002051.XSHE', u'601168.XSHG', u'002292.XSHE', u'000536.XSHE', u'601139.XSHG', u'000423.XSHE', u'000623.XSHE', u'000937.XSHE', u'000858.XSHE', u'600642.XSHG', u'601688.XSHG', u'600436.XSHG', u'002252.XSHE', u'600008.XSHG', u'600867.XSHG', u'600028.XSHG', u'601377.XSHG', u'600170.XSHG', u'600664.XSHG', u'000039.XSHE', u'600362.XSHG', u'600108.XSHG', u'601628.XSHG', u'600633.XSHG', u'603699.XSHG', u'000999.XSHE', u'601928.XSHG', u'600058.XSHG', u'600196.XSHG', u'601169.XSHG', u'600348.XSHG', u'000562.XSHE', u'600208.XSHG', u'600804.XSHG', u'000625.XSHE', u'600886.XSHG', u'600221.XSHG', u'601318.XSHG', u'601898.XSHG', u'601918.XSHG', u'000758.XSHE', u'601901.XSHG', u'601328.XSHG', u'600050.XSHG', u'600031.XSHG', u'600029.XSHG', u'601601.XSHG', u'600027.XSHG', u'000831.XSHE', u'601808.XSHG', u'600109.XSHG', u'600009.XSHG', u'600655.XSHG', u'600085.XSHG', u'000768.XSHE', u'002375.XSHE', u'000728.XSHE', u'603993.XSHG', u'600016.XSHG', u'600703.XSHG', u'002429.XSHE', u'600271.XSHG', u'600674.XSHG', u'600068.XSHG', u'600369.XSHG', u'000060.XSHE', u'600115.XSHG', u'600585.XSHG', u'601939.XSHG', u'002142.XSHE', u'600839.XSHG', u'600143.XSHG', u'601800.XSHG', u'002594.XSHE', u'002410.XSHE', u'600649.XSHG', u'000725.XSHE', u'600153.XSHG', u'600415.XSHG', u'002570.XSHE', u'002304.XSHE', u'000503.XSHE', u'000883.XSHE', u'600216.XSHG', u'002024.XSHE', u'600015.XSHG', u'002236.XSHE', u'600219.XSHG', u'000333.XSHE', u'002603.XSHE', u'600875.XSHG', u'600688.XSHG', u'600060.XSHG', u'000063.XSHE', u'002465.XSHE', u'000156.XSHE', u'601933.XSHG', u'601989.XSHG', u'600383.XSHG', u'000009.XSHE', u'000970.XSHE', u'601958.XSHG', u'601766.XSHG', u'600795.XSHG', u'002241.XSHE', u'000778.XSHE', u'002106.XSHE', u'600030.XSHG', u'601669.XSHG', u'600832.XSHG', u'601186.XSHG', u'000792.XSHE', u'600739.XSHG']
#获取互联网和相关行业的所有股票
get_industry_stocks('I64')
[u'000503.XSHE', u'002095.XSHE', u'002174.XSHE', u'002315.XSHE', u'002354.XSHE', u'002439.XSHE', u'002467.XSHE', u'300059.XSHE', u'300104.XSHE', u'300113.XSHE', u'300431.XSHE', u'300467.XSHE', u'300226.XSHE', u'300295.XSHE', u'300383.XSHE', u'300392.XSHE', u'300418.XSHE', u'600804.XSHG', u'603000.XSHG']
为了增加研究功能的体验,我们新增了如下两个彩蛋
为了打通回测和研究功能,我们新增了两个回测API,回测过程中需要保存的文件,您可以保存到研究模块,也可以直接调用研究模块的文件。
详见https://www.joinquant.com/api#write_file
为了便于交流,研究模块支持分享,您可以把notebook分享到社区,与大家交流。
|
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程