最经典的Momentum和Contrarian在中国市场的测试-yanheven改进

来源:https://uqer.io/community/share/5663f369f9f06c6c8a91b3af

Momentum

策略思路

  • Momentum:业绩好的股票会继续保持其上涨的势头,业绩差的股票会保持其下跌的势头 策略实现

  • Momentum:每次调仓将股票按照前一段时间的累计收益率排序并分组,买入历史累计收益 最高 的那一组

  1. start = datetime(2011, 1, 1) # 回测起始时间
  2. end = datetime(2015, 12, 5) # 回测结束时间
  3. benchmark = 'HS300' # 使用沪深 300 作为参考标准
  4. universe = set_universe('HS300') # 股票池,沪深 300
  5. capital_base = 100000 # 起始资金
  6. refresh_rate = 10
  7. def initialize(account): # 初始化虚拟账户状态
  8. pass
  9. def handle_data(account): # 每个交易日的买入卖出指令
  10. history = account.get_attribute_history('closePrice', 20)
  11. momentum = []
  12. holding = account.valid_secpos
  13. for stk in history:
  14. if stk in account.universe:
  15. his = history[stk]
  16. change = his[-1] / his[0]
  17. momentum.append((stk, change))
  18. momentum = sorted(momentum, key=lambda x: x[1])
  19. momentum = momentum[:10]
  20. momentum_list = [i[0] for i in momentum]
  21. buy_list = set(momentum_list) - set(holding)
  22. sell_list = set(holding) - set(momentum_list)
  23. for i in buy_list:
  24. try:
  25. order_pct_to(i, 0.1)
  26. except Exception as e:
  27. log.warn(i + str(e))
  28. for i in sell_list:
  29. order_to(i, 0)

最经典的Momentum和Contrarian在中国市场的测试-yanheven改进 - 图1

Contrarian

策略思路

  • Contrarian:股票在经过一段时间的上涨之后会出现回落,一段时间的下跌之后会出现反弹 策略实现

  • Contrarian:每次调仓将股票按照前一段时间的累计收益率排序并分组,买入历史累计收益 最低 的那一组

  1. start = datetime(2011, 1, 1) # 回测起始时间
  2. end = datetime(2015, 12, 5) # 回测结束时间
  3. benchmark = 'HS300' # 使用沪深 300 作为参考标准
  4. universe = set_universe('HS300') # 股票池,沪深 300
  5. capital_base = 100000 # 起始资金
  6. refresh_rate = 10
  7. def initialize(account): # 初始化虚拟账户状态
  8. pass
  9. def handle_data(account): # 每个交易日的买入卖出指令
  10. history = account.get_attribute_history('closePrice', 20)
  11. momentum = []
  12. holding = account.valid_secpos
  13. for stk in history:
  14. if stk in account.universe:
  15. his = history[stk]
  16. change = his[-1] / his[0]
  17. momentum.append((stk, change))
  18. momentum = sorted(momentum, key=lambda x: x[1], reverse=True)
  19. # if momentum[-1][1] < 1:
  20. # log.info(holding)
  21. # for i in holding:
  22. # order_to(i, 0)
  23. # # return
  24. momentum = momentum[:10]
  25. momentum_list = [i[0] for i in momentum]
  26. buy_list = set(momentum_list) - set(holding)
  27. sell_list = set(holding) - set(momentum_list)
  28. for i in buy_list:
  29. try:
  30. order_pct_to(i, 0.1)
  31. except Exception as e:
  32. log.warn(i + str(e))
  33. for i in sell_list:
  34. order_to(i, 0)

最经典的Momentum和Contrarian在中国市场的测试-yanheven改进 - 图2