recommendation based on subject

来源:https://uqer.io/community/share/549d0203f9f06c4bb8863242

策略思路:

  • step1:计算昨日所有主题的涨跌幅,根据涨跌幅排名,挑出涨幅最高的前n_sub个主题
  • step2:根据昨日成交量挑选出每个主题的龙头股n_bigstk
  • 买入策略:昨日涨幅最高的前n_sub个主题,每个主题龙头股n_bigstk只,当日一共买入m*n只个股
  • 卖出策略:持有固定天数hold_days,卖出。 此实验中,n_sub=5, n_bigstk=5, hold_days=10

文件sub_stk_info.txt是根据dataapi获得的文件,里面储存了多个主题及对应的股票列表,点击这里下载

  1. a2=read('sub_stk_info.txt')
  2. b2=a2.split('\r\n')
  3. b2=b2[:-1]
  4. sub_stk_dic={}
  5. universe1=set([])
  6. IDmap=lambda x:x +'.XSHG' if x[0]=='6' else x+'.XSHE'
  7. for i2 in b2:
  8. i2=i2.split(':')
  9. i3=i2[1].split(',')
  10. sub_stk_dic[i2[0]]=map(IDmap,i3)
  11. universe1 |= set(i3)
  12. start = datetime(2013, 6, 23) # 回测起始时间
  13. end = datetime(2014, 12, 23) # 回测结束时间
  14. benchmark = 'HS300' # 使用沪深 300 作为参考标准
  15. universe = map(IDmap, list(universe1))
  16. capital_base = 100000 # 起始资金
  17. #print len(universe)
  18. hold_days=10
  19. sell_stk_list=[]
  20. for i in range(hold_days):
  21. sell_stk_list.append({})
  22. j=hold_days
  23. def initialize(account): # 初始化虚拟账户状态
  24. add_history('hist',1)
  25. def handle_data(account): # 每个交易日的买入卖出指令
  26. global sell_stk_list
  27. global j
  28. #计算昨日主题涨跌幅
  29. sub_increase_rate_dic={}
  30. sub_bigstk_dic={}
  31. #print 'today:',account.current_date
  32. for (subid,stkid_list) in sub_stk_dic.items():
  33. increase_rate_list=[]
  34. turnvol_list=[]
  35. #记录每只股票的成交量
  36. stk_turnvol_dic={}
  37. for stk in stkid_list:
  38. #停盘的情况
  39. if (stk not in account.universe) :
  40. continue
  41. close_price=account.hist[stk].iloc[0,3]
  42. pre_close_price=account.hist[stk].iloc[0,4]
  43. turnoverVol=account.hist[stk].iloc[0,5]
  44. stk_turnvol_dic[stk]=turnoverVol
  45. increase_rate_yes=(close_price-pre_close_price)*turnoverVol/pre_close_price
  46. increase_rate_list.append(increase_rate_yes)
  47. turnvol_list.append(turnoverVol)
  48. big_stk_list=sorted(stk_turnvol_dic.keys(),key=lambda x:stk_turnvol_dic[x], reverse=True)
  49. #买龙头股,每个主题买n只龙头股
  50. n_bigstk=5
  51. big_stk_list=big_stk_list[0:n_bigstk]
  52. sub_bigstk_dic[subid]=big_stk_list
  53. increase_rate_w=sum(increase_rate_list)/sum(turnvol_list)
  54. sub_increase_rate_dic[subid]=increase_rate_w
  55. sub_increase_rate_dic_sorted=sorted(sub_increase_rate_dic.keys(), key = lambda x:sub_increase_rate_dic[x], reverse = True)
  56. n_sub=5
  57. buy_subject_list=sub_increase_rate_dic_sorted[0:n_sub]
  58. buy_stk_list=[]
  59. for sub_id in buy_subject_list:
  60. buy_stk_list +=sub_bigstk_dic[sub_id]
  61. sell_next_dic={}
  62. for stk in buy_stk_list:
  63. if j>0:
  64. amount=int(account.position.cash/hold_days/len(buy_stk_list)/account.hist[stk].iloc[0,3])
  65. j -=1
  66. else:
  67. amount=int(account.position.cash/len(buy_stk_list)/account.hist[stk].iloc[0,3])
  68. order(stk,amount)
  69. sell_next_dic[stk]=amount
  70. sell_stk_list.insert(0,sell_next_dic)
  71. #print 'sell_stk_list:',sell_stk_list
  72. sell_today_dic=sell_stk_list.pop()
  73. #print 'sell_today_dic',sell_today_dic
  74. if sell_today_dic!={}:
  75. for (stk,amt) in sell_today_dic.items():
  76. #如果股票今天不能交易,就过hold_days再卖
  77. if stk not in account.universe:
  78. sell_stk_list[0][stk]=amt
  79. else:
  80. order(stk,-amt)

recommendation based on subject - 图1