技术分析入门 【2】 —— 大家抢筹码(06年至12年版)— 更新版

来源:https://uqer.io/community/share/568e6f54228e5b18e5ba296e

从社区李大大以前的帖子,稍作修改,适合现在的uqer版本,感谢李大大的无私分享!

原帖地址:

https://uqer.io/community/share/5541d8a4f9f06c1c3d687fef

在本篇中,我们将使用流通股份的集中程度作为指标,为大家开发如何机智的抢筹码策略!

股市里面总是有这样的一种说法: 大股东总是会快小散一步,悄悄地进村,放枪的不要。大股东会在建仓期吸收世面上的廉价筹码,然后放出利好,逢高出货。所以大股东的建仓期,正是小散们入场分汤的好时机!

1. 数据准备

好了,说了这些原理,到底灵不灵呢?来,一试便知!这里我们首先要定义什么叫大股东呢?这里我们借助中诚信的数据,获取前十大流通股东的持股比例:

数据API:CCXE.EquMainshFCCXEGet 获取财报中十大流通股股东的持股比例(本API需要在数据商城购买)

下面的语句查询600000.XSHG浦发银行在2014年9月30日到2014年12月31日的十大流通股股东持股情况:

  1. import datetime as dt
  2. from CAL.PyCAL import *
  3. data = DataAPI.CCXE.EquMainshFCCXEGet('600000.XSHG', endDateStart='20140930', endDateEnd='20141231')
  4. data.head()
secIDtickerexchangeCDsecShortNamesecShortNameEnendDateshNumshRankshNameholdVolholdPctshareCharType
0600000.XSHG600000XSHG浦发银行NaN2014-12-31 00:00:0011上海国际集团有限公司315751391716.93101
1600000.XSHG600000XSHG浦发银行NaN2014-12-31 00:00:0022上海国际信托有限公司9759237945.23101
2600000.XSHG600000XSHG浦发银行NaN2014-12-31 00:00:0033上海国鑫投资发展有限公司3771019992.02101
3600000.XSHG600000XSHG浦发银行NaN2014-12-31 00:00:0044百联集团有限公司1900835171.02101
4600000.XSHG600000XSHG浦发银行NaN2014-12-31 00:00:0055雅戈尔集团股份有限公司1620000000.87101

我们按照报表日进行合并,并计算前十大流通股股东持股总比例:

  1. data.groupby('endDate').sum()

可以看到,2014年年报中流通股集中度是下降的,相对于上一个季报,持股总比例从29.76%降到了29.25%。看来他的大股东没啥动静,小散们先按兵不动!

2. 策略思路

有一句俗话:不要在一棵树上吊死!小散们可以“海选PK”,择优录取!我们以上证50成分股为例,挑选出满足以下条件的股票:

  • 2015年一季度季报中10大流通股股东持股比例相对于去年年报上升10% 这就是我们认定的大股东吸筹码的标志:
  1. from quartz.api import set_universe
  2. import datetime as dt
  3. universe = set_universe('SH50')
  4. for stock in universe:
  5. try:
  6. data = DataAPI.CCXE.EquMainshFCCXEGet(stock, endDateStart='20141231', endDateEnd='20150331')
  7. except:
  8. continue
  9. res = data.groupby('endDate').sum()[-2:]
  10. if len(res.index) == 2 and res.index[1] == '2015-03-31 00:00:00':
  11. chg = res['holdPct'].values[1] / res['holdPct'].values[0] - 1.0
  12. if chg > 0.1:
  13. print '%s: %.4f' % (stock, chg)

选出来有三只股票满足:601169.XSHG, 600887.XSHG, 600703.XSHG

下面的股价走势图来看,这样的股票总体还是上升的。但是按照这样投钱真的靠谱吗?

  1. import pandas as pd
  2. stock1 = DataAPI.MktEqudAdjGet(secID=['601169.XSHG'], beginDate='20150331', endDate='20150429', field = ['closePrice', 'tradeDate'])
  3. stock2 = DataAPI.MktEqudAdjGet(secID=['600887.XSHG'], beginDate='20150331', endDate='20150429', field = ['closePrice', 'tradeDate'])
  4. stock3 = DataAPI.MktEqudAdjGet(secID=['600703.XSHG'], beginDate='20150331', endDate='20150429', field = ['closePrice', 'tradeDate'])
  1. import seaborn as sns
  2. sns.set_style('white')
  3. total = pd.DataFrame({'601169.XSHG':stock1.closePrice.values, '600887.XSHG':stock2.closePrice.values, '600703.XSHG':stock3.closePrice.values})
  4. total.index = stock1.tradeDate.apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d'))
  5. total.plot(subplots=True, figsize=(12,8))
  6. array([<matplotlib.axes.AxesSubplot object at 0x5543d10>,
  7. <matplotlib.axes.AxesSubplot object at 0x5572850>,
  8. <matplotlib.axes.AxesSubplot object at 0x56a62d0>], dtype=object)

技术分析入门 【2】 —— 大家抢筹码(06年至12年版)— 更新版 - 图1

3. 完整策略

我们来吧上面的想法系统化,来看这个策略效率:

  • 投资域 :上证50成分股
  • 业绩基准 :上证50指数
  • 调仓频率 :3个月
  • 调仓日期 :每年的2月28日,5月31日,8月30日,11月30日,遇到节假日的话向后顺延
  • 开仓信号 :十大流通股股东持股比例集中度上升10%
  • 清仓信号 :每个调仓日前一个工作日,清空当前仓位
  • 买入方式 :等比例买入
  • 回测周期 :2006年1月1日至2015年4月28日 这里的调仓日期的设置,是满足每期报表结束日后的两个月,这样我们有比较大的把握,可以确实拿到当前的报表数据。
  1. import datetime as dt
  2. start = '2006-01-01' # 回测起始时间
  3. end = '2012-12-31' # 回测结束时间
  4. benchmark = 'SH50' # 策略参考标准
  5. universe = set_universe('SH50') # 证券池,支持股票和基金
  6. capital_base = 100000 # 起始资金
  7. longest_history = 1 # handle_data 函数中可以使用的历史数据最长窗口长度
  8. refresh_rate = 1 # 调仓频率,即每 refresh_rate 个交易日执行一次 handle_data() 函数
  9. def initialize(account): # 初始化虚拟账户状态
  10. account.reportingPair = [('0930', '1231'), ('1231', '0331'), ('0331', '0630'), ('0630', '0930')]
  11. def handle_data(account): # 每个交易日的买入卖出指令
  12. hist = account.get_history(longest_history)
  13. today = account.current_date
  14. year = today.year
  15. rebalance_dates = [dt.datetime(year, 2, 28), dt.datetime(year, 5,31), dt.datetime(year, 8, 30), dt.datetime(year, 11,30)]
  16. cal = Calendar('China.SSE')
  17. rebalance_dates = [cal.adjustDate(d, BizDayConvention.Following) for d in rebalance_dates]
  18. rebalanceFlag = False
  19. period = -1
  20. for i, d in enumerate(rebalance_dates):
  21. # 判断是否是调仓日
  22. if today == d.toDateTime():
  23. rebalanceFlag = True
  24. period = i
  25. break
  26. # 调仓日前一个交易日,清空所有的仓位
  27. elif today == cal.advanceDate(d, '-1B').toDateTime():
  28. for stock in account.valid_secpos:
  29. order_to(stock,0)
  30. if rebalanceFlag:
  31. if period == 0:
  32. year -= 1
  33. # 确定当前调仓日对应需要查询的报表日期
  34. if account.reportingPair[period][0] < account.reportingPair[period][1]:
  35. endDateStart = str(year) + account.reportingPair[period][0]
  36. else:
  37. endDateStart = str(year-1) + account.reportingPair[period][0]
  38. endDateEnd = str(year) + account.reportingPair[period][1]
  39. buyList = []
  40. # 确定哪些股票满足“筹码”集中要求
  41. for stock in account.universe:
  42. try:
  43. data = DataAPI.CCXE.EquMainshFCCXEGet(stock, endDateStart=endDateStart, endDateEnd=endDateEnd)
  44. except:
  45. continue
  46. res = data.groupby('endDate').sum()[-2:]
  47. tmp = account.reportingPair[period][1]
  48. if len(res.index) == 2 and res.index[1] == str(year) + '-' + tmp[:2] + '-' + tmp[2:]+ ' 00:00:00':
  49. chg = res['holdPct'].values[1] / res['holdPct'].values[0] - 1.0
  50. if chg > 0.1:
  51. buyList.append(stock)
  52. print u"%s 买入 : %s" % (today, buyList)
  53. # 等权重买入
  54. if len(buyList) != 0:
  55. singleCash = account.cash / len(buyList)
  56. for stock in buyList:
  57. approximationAmount = int(singleCash / hist[stock]['closePrice'][-1]/100.0) * 100
  58. order(stock, approximationAmount)

技术分析入门 【2】 —— 大家抢筹码(06年至12年版)— 更新版 - 图2

  1. 2006-02-28 00:00:00 买入 : ['600050.XSHG', '600893.XSHG', '600016.XSHG', '600104.XSHG', '600010.XSHG', '600518.XSHG', '600030.XSHG', '600150.XSHG']
  2. 2006-05-31 00:00:00 买入 : ['600036.XSHG', '600111.XSHG', '600104.XSHG', '600010.XSHG', '600030.XSHG']
  3. 2006-08-30 00:00:00 买入 : ['600050.XSHG', '600893.XSHG', '600000.XSHG', '600104.XSHG', '600637.XSHG', '600837.XSHG', '600150.XSHG']
  4. 2006-11-30 00:00:00 买入 : ['600050.XSHG', '600795.XSHG', '600036.XSHG', '600000.XSHG', '600111.XSHG', '600519.XSHG', '600016.XSHG', '600518.XSHG', '601988.XSHG', '600030.XSHG']
  5. 2007-02-28 00:00:00 买入 : ['600000.XSHG', '600111.XSHG', '601006.XSHG', '600048.XSHG', '600015.XSHG', '600518.XSHG', '600887.XSHG', '600150.XSHG']
  6. 2007-05-31 00:00:00 买入 : ['600795.XSHG', '600111.XSHG', '601166.XSHG', '600104.XSHG', '600015.XSHG', '600637.XSHG', '600837.XSHG']
  7. 2007-08-30 00:00:00 买入 : ['600000.XSHG', '600519.XSHG', '601166.XSHG', '600015.XSHG', '600109.XSHG', '600887.XSHG', '601318.XSHG']
  8. 2007-11-30 00:00:00 买入 : ['600050.XSHG', '600795.XSHG', '600111.XSHG', '601006.XSHG', '600048.XSHG', '600104.XSHG', '600015.XSHG', '600837.XSHG', '601988.XSHG', '600030.XSHG']
  9. 2008-02-28 00:00:00 买入 : ['601328.XSHG', '600050.XSHG', '600795.XSHG', '600000.XSHG', '600018.XSHG', '600016.XSHG', '601006.XSHG', '600104.XSHG', '600028.XSHG', '600518.XSHG', '600837.XSHG', '601169.XSHG', '601988.XSHG', '601398.XSHG']
  10. 2008-06-02 00:00:00 买入 : ['601006.XSHG', '601166.XSHG', '600010.XSHG', '600518.XSHG', '601318.XSHG']
  11. 2008-09-01 00:00:00 买入 : ['601328.XSHG', '600050.XSHG', '601601.XSHG', '600036.XSHG', '600000.XSHG', '600519.XSHG', '600016.XSHG', '601998.XSHG', '600015.XSHG', '600637.XSHG', '600150.XSHG']
  12. 2008-12-01 00:00:00 买入 : ['601601.XSHG', '600795.XSHG', '600104.XSHG', '600837.XSHG', '601169.XSHG', '600030.XSHG']
  13. 2009-03-02 00:00:00 买入 : ['601601.XSHG', '601390.XSHG', '600104.XSHG', '600028.XSHG', '600518.XSHG', '600887.XSHG', '600837.XSHG', '601988.XSHG']
  14. 2009-06-01 00:00:00 买入 : ['600893.XSHG', '600036.XSHG', '600111.XSHG', '600585.XSHG', '600048.XSHG', '600109.XSHG', '600887.XSHG', '601988.XSHG']
  15. 2009-08-31 00:00:00 买入 : ['600050.XSHG', '600893.XSHG', '600000.XSHG', '600111.XSHG', '600519.XSHG', '600015.XSHG', '600010.XSHG', '600887.XSHG', '601766.XSHG', '601398.XSHG', '600150.XSHG']
  16. 2009-11-30 00:00:00 买入 : ['600795.XSHG', '600893.XSHG', '600016.XSHG', '601006.XSHG', '600048.XSHG', '600887.XSHG', '601988.XSHG']
  17. 2010-03-01 00:00:00 买入 : ['601601.XSHG', '600893.XSHG', '600018.XSHG', '600016.XSHG', '601668.XSHG', '600585.XSHG', '601998.XSHG', '600104.XSHG', '600028.XSHG', '601398.XSHG']
  18. 2010-05-31 00:00:00 买入 : ['600111.XSHG', '600999.XSHG', '601628.XSHG', '601318.XSHG']
  19. 2010-08-30 00:00:00 买入 : ['601328.XSHG', '600893.XSHG', '600111.XSHG', '600585.XSHG', '601998.XSHG', '601688.XSHG', '600999.XSHG', '600109.XSHG', '601989.XSHG', '600837.XSHG']
  20. 2010-11-30 00:00:00 买入 : ['600010.XSHG', '601989.XSHG', '601169.XSHG', '600150.XSHG']
  21. 2011-02-28 00:00:00 买入 : ['601601.XSHG', '601857.XSHG', '601390.XSHG', '601288.XSHG', '601668.XSHG', '601088.XSHG', '600999.XSHG', '601989.XSHG', '600837.XSHG']
  22. 2011-05-31 00:00:00 买入 : ['600893.XSHG', '601668.XSHG', '601688.XSHG', '600010.XSHG', '600109.XSHG']
  23. 2011-08-30 00:00:00 买入 : ['600010.XSHG', '600887.XSHG']
  24. 2011-11-30 00:00:00 买入 : ['601288.XSHG', '601818.XSHG', '601766.XSHG']
  25. 2012-02-28 00:00:00 买入 : ['600893.XSHG', '600015.XSHG', '600030.XSHG', '601669.XSHG', '601901.XSHG']
  26. 2012-05-31 00:00:00 买入 : ['601336.XSHG', '601989.XSHG', '601669.XSHG']
  27. 2012-08-30 00:00:00 买入 : ['601336.XSHG', '600837.XSHG', '601901.XSHG']
  28. 2012-11-30 00:00:00 买入 : ['601668.XSHG', '601901.XSHG']