市值最小300指数

来源:https://uqer.io/community/share/5604fbe6f9f06c597665ef37

刷爆沪深300

策略名称: 市值最小300指数

回测时间:2013-01-01 到 2015-09-24

调仓期 :20交易日

策略思想:找A股市场市值最小的300只股票,等权重构建最小300指数

注意 :

  • 内存不够请自行缩短回测时间或universe
  • 此贴有4个!!!
  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series, DataFrame
  4. start = '2013-01-01' # 回测起始时间
  5. end = '2015-09-24' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe0 = set_universe('A') # 证券池,支持股票和基金
  8. universe1 = set_universe('HS300')
  9. universe2 = set_universe('ZZ500')
  10. universe = list(set(universe0).difference(set(universe1+universe2))) #最小市值股一定不在中证500和沪深300 pass
  11. capital_base = 100000000 # 起始资金
  12. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  13. refresh_rate = 20 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  14. def initialize(account): # 初始化虚拟账户状态
  15. pass
  16. def handle_data(account): # 每个交易日的买入卖出指令
  17. total_money = account.referencePortfolioValue
  18. prices = account.referencePrice
  19. buylist = []
  20. marketValue = DataFrame()
  21. today = account.current_date.strftime('%Y%m%d')
  22. for s in range(len(account.universe)/40 + 1):
  23. if s == len(account.universe)/40:
  24. temp_list = account.universe[s*40:]
  25. else :
  26. temp_list = account.universe[s*40:(s+1)*40]
  27. #MktEqudGet接口一次最多选50个
  28. try: #排除最后一次temp_list为零的可能
  29. marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")
  30. except :
  31. pass
  32. marketValue = pd.concat([marketValue,marketValue_temp])
  33. marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')
  34. marketValue.set_index('secID',inplace=True)
  35. marketValue = marketValue.dropna()
  36. #排除新股发行日
  37. for s in list(marketValue.index) :
  38. if not (np.isnan(prices[s]) or prices[s] == 0) :
  39. buylist.append(s)
  40. if len(buylist) >= 300 :
  41. break
  42. sell_list = [x for x in account.valid_secpos if x not in buylist]
  43. for stk in sell_list:
  44. order_to(stk, 0)
  45. for stk in buylist:
  46. order_to(stk, int(total_money/300/prices[stk]/100)*100)

市值最小300指数 - 图1

更改权重比例 :加权流通市值倒数(越小越买) 买买买!

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series, DataFrame
  4. start = '2013-01-01' # 回测起始时间
  5. end = '2015-09-24' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe0 = set_universe('A') # 证券池,支持股票和基金
  8. universe1 = set_universe('HS300')
  9. universe2 = set_universe('ZZ500')
  10. universe = list(set(universe0).difference(set(universe1+universe2)))
  11. capital_base = 100000000 # 起始资金
  12. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  13. refresh_rate = 20 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  14. def initialize(account): # 初始化虚拟账户状态
  15. pass
  16. def handle_data(account): # 每个交易日的买入卖出指令
  17. total_money = account.referencePortfolioValue
  18. prices = account.referencePrice
  19. buylist = []
  20. marketValue = DataFrame()
  21. today = account.current_date.strftime('%Y%m%d')
  22. for s in range(len(account.universe)/40 + 1):
  23. if s == len(account.universe)/40:
  24. temp_list = account.universe[s*40:]
  25. else :
  26. temp_list = account.universe[s*40:(s+1)*40]
  27. #MktEqudGet接口一次最多选50个
  28. try: #排除最后一次temp_list为零的可能
  29. marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue,negMarketValue",pandas="1")
  30. except :
  31. pass
  32. marketValue = pd.concat([marketValue,marketValue_temp])
  33. marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')
  34. marketValue.set_index('secID',inplace=True)
  35. marketValue = marketValue.dropna()
  36. #排除新股发行日
  37. for s in list(marketValue.index) :
  38. if not (np.isnan(prices[s]) or prices[s] == 0) :
  39. buylist.append(s)
  40. if len(buylist) >= 300 :
  41. break
  42. sell_list = [x for x in account.valid_secpos if x not in buylist]
  43. for stk in sell_list:
  44. order_to(stk, 0)
  45. #加权流通市值倒数购买
  46. weight_list = []
  47. for stk in buylist:
  48. weight_list.append(1.0/marketValue['negMarketValue'][stk])
  49. temp_sum = 0
  50. for temp in weight_list:
  51. temp_sum += temp
  52. weight_list = [x/temp_sum for x in weight_list]
  53. i = 0
  54. for stk in buylist:
  55. order_to(stk, int(total_money*weight_list[i]/prices[stk]/100)*100)
  56. i += 1

市值最小300指数 - 图2

是不是在想賺钱分分钟了? 您有买300只个股的毛爷爷吗,啊! 〇_〇- ..

木有?我会告诉你买十只也很叼吗???

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series, DataFrame
  4. start = '2013-01-01' # 回测起始时间
  5. end = '2015-09-24' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe0 = set_universe('A') # 证券池,支持股票和基金
  8. universe1 = set_universe('HS300')
  9. universe2 = set_universe('ZZ500')
  10. universe = list(set(universe0).difference(set(universe1+universe2)))
  11. capital_base = 100000000 # 起始资金
  12. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  13. refresh_rate = 20 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  14. def initialize(account): # 初始化虚拟账户状态
  15. pass
  16. def handle_data(account): # 每个交易日的买入卖出指令
  17. total_money = account.referencePortfolioValue
  18. prices = account.referencePrice
  19. buylist = []
  20. marketValue = DataFrame()
  21. today = account.current_date.strftime('%Y%m%d')
  22. for s in range(len(account.universe)/40 + 1):
  23. if s == len(account.universe)/40:
  24. temp_list = account.universe[s*40:]
  25. else :
  26. temp_list = account.universe[s*40:(s+1)*40]
  27. #MktEqudGet接口一次最多选50个
  28. try: #排除最后一次temp_list为零的可能
  29. marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")
  30. except :
  31. pass
  32. marketValue = pd.concat([marketValue,marketValue_temp])
  33. marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')
  34. marketValue.set_index('secID',inplace=True)
  35. marketValue = marketValue.dropna()
  36. #排除新股发行日
  37. for s in list(marketValue.index) :
  38. if not (np.isnan(prices[s]) or prices[s] == 0) :
  39. buylist.append(s)
  40. if len(buylist) >= 10 :
  41. break
  42. sell_list = [x for x in account.valid_secpos if x not in buylist]
  43. for stk in sell_list:
  44. order_to(stk, 0)
  45. #只买最优十只
  46. for stk in buylist:
  47. order_to(stk, int(total_money/10/prices[stk]/100)*100)

市值最小300指数 - 图3

我会告诉你有庄家(机构持股)的股票更容易飞 ??? 小注 :此策略中DataAPI.JY.EquInstShJYGet(恒生聚源接口)暂不开放!!!活跃用户自行申请

  1. import pandas as pd
  2. import numpy as np
  3. from pandas import Series, DataFrame
  4. start = '2013-01-01' # 回测起始时间
  5. end = '2015-09-24' # 回测结束时间
  6. benchmark = 'HS300' # 策略参考标准
  7. universe0 = set_universe('A') # 证券池,支持股票和基金
  8. universe1 = set_universe('HS300')
  9. universe2 = set_universe('ZZ500')
  10. universe = list(set(universe0).difference(set(universe1+universe2)))
  11. capital_base = 100000000 # 起始资金
  12. freq = 'd' # 策略类型,'d'表示日间策略使用日线回测,'m'表示日内策略使用分钟线回测
  13. refresh_rate = 20 # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
  14. def initialize(account): # 初始化虚拟账户状态
  15. pass
  16. def handle_data(account): # 每个交易日的买入卖出指令
  17. total_money = account.referencePortfolioValue
  18. prices = account.referencePrice
  19. buylist = []
  20. marketValue = DataFrame()
  21. today = account.current_date.strftime('%Y%m%d')
  22. for s in range(len(account.universe)/40 + 1):
  23. if s == len(account.universe)/40:
  24. temp_list = account.universe[s*40:]
  25. else :
  26. temp_list = account.universe[s*40:(s+1)*40]
  27. #MktEqudGet接口一次最多选50个
  28. try: #排除最后一次temp_list为零的可能
  29. marketValue_temp = DataAPI.MktEqudGet(secID = temp_list,tradeDate= today, field=u"secID,marketValue",pandas="1")
  30. except :
  31. pass
  32. marketValue = pd.concat([marketValue,marketValue_temp])
  33. marketValue = marketValue.sort('marketValue',ascending=True).drop_duplicates('secID')
  34. marketValue.set_index('secID',inplace=True)
  35. marketValue = marketValue.dropna()
  36. # 机构持股 非第一天上市新股
  37. for s in list(marketValue.index) :
  38. try :
  39. # 处理巨源的数据接口没有此股
  40. temp = DataAPI.JY.EquInstShJYGet ( secID = s , field = u"instNrfaPct" , pandas = "1" )
  41. except :
  42. print account.current_date.strftime('%Y-%m-%d'),' ',s,' ','DataAPI.JY.EquInstShJYGet get wrong'
  43. continue
  44. #有机构持股 > 30%
  45. if temp['instNrfaPct'][0] > 30 and not (np.isnan(prices[s]) or prices[s] == 0) :
  46. buylist.append(s)
  47. if len(buylist) >= 10 :
  48. break
  49. sell_list = [x for x in account.valid_secpos if x not in buylist]
  50. for stk in sell_list:
  51. order_to(stk, 0)
  52. for stk in buylist:
  53. order_to(stk, int(total_money/10/prices[stk]/100)*100)

市值最小300指数 - 图4

呵呵,居然看完了,还不赶紧点赞克隆去賺钱!!!内存不够的还不赶紧签到 !!!