strategy7: recommendation based on theme

来源:https://uqer.io/community/share/54a3c0bff9f06c276f6519e7

  1. universe=set_universe('HS300')
  2. tickers=''
  3. for stk in universe:
  4. tickers += stk[0:6]+','
  5. tickers=tickers.strip(",")
  6. theme_infos=DataAPI.ThemeThemesGet(ticker=tickers,beginDate='20141201',endDate='20141230',field=['themeID','themeName'])
  7. #建立一个字典theme_id_name_dic,储存themeID与themeName对应关系
  8. #建立一个字典theme_stk_dic,储存主题与个股的对应关系
  9. theme_id_name_dic={}
  10. theme_stk_dic={}
  11. for i in range(len(theme_infos)):
  12. ticker = theme_infos.loc[i,'ticker']
  13. if ticker[0]=='6':
  14. ticker += '.XSHG'
  15. else:
  16. ticker += '.XSHE'
  17. themeID = theme_infos.loc[i,'themeID']
  18. themeName = theme_infos.loc[i,'themeName']
  19. #建立themeID与themeName的对应关系
  20. if themeID not in theme_id_name_dic:
  21. theme_id_name_dic[themeID] = themeName
  22. #建立主题与个股的对应关系,其中的stock均不带后缀
  23. if themeID not in theme_stk_dic:
  24. theme_stk_dic[themeID] = [ticker]
  25. else:
  26. theme_stk_dic[themeID].append(ticker)
  27. #过滤掉无效的主题
  28. filter_theme_id_name_dic={}
  29. filter_stk_theme_dic={}
  30. filter_theme_stk_dic={}
  31. #训练得到有效主题
  32. for (theme_id,stk_list) in theme_stk_dic.items():
  33. if len(stk_list)>5:
  34. filter_theme_id_name_dic[theme_id] = theme_id_name_dic[theme_id]
  35. filter_theme_stk_dic[theme_id] = stk_list
  36. for (theme_id,stk_list) in filter_theme_stk_dic.items():
  37. for stk in stk_list:
  38. if stk not in filter_stk_theme_dic:
  39. filter_stk_theme_dic[stk] = [theme_id]
  40. else:
  41. filter_stk_theme_dic[stk].append(theme_id)
  42. filter_universe = filter_stk_theme_dic.keys()
  1. start = datetime(2013, 6, 23) # 回测起始时间
  2. end = datetime(2014, 12, 23) # 回测结束时间
  3. benchmark = 'HS300' # 策略参考标准
  4. universe = filter_universe # 股票池
  5. capital_base = 100000 # 起始资金
  6. window=1
  7. hold_days=2 #股票持有时间
  8. sell_stk_list=[]
  9. for i in range(hold_days):
  10. sell_stk_list.append({})
  11. def initialize(account): # 初始化虚拟账户状态
  12. add_history('hist',window)
  13. def handle_data(account): # 每个交易日的买入卖出指令
  14. theme_increase_raw={}
  15. theme_increase = {}
  16. n_theme = 6 #挑选涨得最好的几个主题
  17. n_bigstk1 = 0 #根据过去涨幅挑选龙头股
  18. n_bigstk2 = 2 #根据过去成交量大小挑选龙头股
  19. for stk in account.universe:
  20. theme_list = filter_stk_theme_dic[stk]
  21. close_price = account.hist[stk]['closePrice'].iloc[-1]
  22. pre_close_price = account.hist[stk]['preClosePrice'].iloc[0]
  23. increase_rate = (close_price-pre_close_price)*1.0/pre_close_price
  24. sum_turnoverVol = sum(list(account.hist[stk]['turnoverVol']))
  25. num_increase = increase_rate * sum_turnoverVol
  26. #计算主题涨幅
  27. for theme_id in theme_list:
  28. if theme_id not in theme_increase:
  29. theme_increase_raw[theme_id] = [num_increase,sum_turnoverVol]
  30. else:
  31. theme_increase_raw[theme_id][0] += num_increase
  32. theme_increase_raw[theme_id][1] += sum_turnoverVol
  33. for (theme_id,theme_increase_list) in theme_increase_raw.items():
  34. theme_increase[theme_id] = theme_increase_raw[theme_id][0]*1.0/theme_increase_raw[theme_id][1]
  35. #将主题按涨幅排序,排名靠前的挑选出来。
  36. theme_list_sort = sorted(theme_increase.keys(), key = lambda x:theme_increase[x], reverse=True)
  37. good_theme = theme_list_sort[0:n_theme]
  38. buy_list = []
  39. #print 'theme_list_sort:',theme_list_sort
  40. #print 'good_theme',good_theme
  41. #挑选涨幅好的主题对应的龙头股,涨的多就是龙头股
  42. for theme in good_theme:
  43. #print account.current_date,theme_id_name_dic[theme],theme_increase[theme]
  44. stk_list = filter_theme_stk_dic[theme]
  45. stk_increase_dic = {}
  46. for stk in stk_list:
  47. if stk not in account.universe: continue
  48. close_price = account.hist[stk]['closePrice'].iloc[-1]
  49. pre_close_price = account.hist[stk]['preClosePrice'].iloc[0]
  50. increase_rate = (close_price-pre_close_price)*1.0/pre_close_price
  51. sum_turnoverVol = account.hist[stk]['turnoverVol'].iloc[window-1]
  52. stk_increase_dic[stk] = [increase_rate,sum_turnoverVol]
  53. stk_list_sort1 = sorted(stk_increase_dic.keys(), key =lambda x:stk_increase_dic[x][0], reverse = True)
  54. stk_list_sort2 = sorted(stk_increase_dic.keys(), key =lambda x:stk_increase_dic[x][1], reverse = True)
  55. #print stk_list_sort[0:n_bigstk]
  56. buy_list += stk_list_sort1[0:n_bigstk1]+stk_list_sort2[0:n_bigstk2]
  57. #买入股票,并将买入信息写入到卖出股票中
  58. per_money=account.cash/len(buy_list)
  59. sell_next_dic = {}
  60. for stk in buy_list:
  61. amount = int(per_money/account.hist[stk]['closePrice'].iloc[window-1])
  62. order(stk,amount)
  63. sell_next_dic[stk]=amount
  64. sell_stk_list.append(sell_next_dic)
  65. sell_today_dic=sell_stk_list.pop(0)
  66. if sell_today_dic!={}:
  67. for (stk,amt) in sell_today_dic.items():
  68. #如果股票今天不能交易,就下一天再卖
  69. if stk not in account.universe:
  70. sell_stk_list[0][stk]=amt
  71. else:
  72. order(stk,-amt)

strategy7: recommendation based on theme - 图1