Contrarian strategy

来源:https://uqer.io/community/share/5545ff8df9f06c1c3d68802f

Contrarian strategy similar with Momentum strategy

  1. import pandas as pd
  2. start = '2010-01-01' # 回测起始时间
  3. end = '2015-01-01' # 回测结束时间
  4. benchmark = 'SH50' # 策略参考标准
  5. universe = set_universe('SH50')
  6. capital_base = 100000 # 起始资金
  7. longest_history = 40 # handle_data 函数中可以使用的历史数据最长窗口长度
  8. refresh_rate = 1 # 调仓频率,即每 refresh_rate 个交易日执行一次 handle_data() 函数
  9. def initialize(account): # 初始化虚拟账户状态
  10. pass
  11. def handle_data(account): # 每个交易日的买入卖出指令
  12. returndata = {'symbol':[], 'ret':[]}
  13. history_data = account.get_attribute_history('closePrice',40)
  14. for s in account.universe:
  15. returndata['symbol'].append(s)
  16. returndata['ret'].append(history_data[s][-1] / history_data[s][0])
  17. returndatanew = pd.DataFrame(returndata).sort(columns = 'ret').reset_index()
  18. returndatanew = returndatanew[0:len(returndatanew)/5]
  19. buylist = returndatanew['symbol'].tolist()
  20. for cur in account.valid_secpos:
  21. if cur not in buylist:
  22. order_to(cur,0)
  23. for sym in buylist:
  24. if sym not in account.valid_secpos:
  25. order_to(sym,300)

Contrarian strategy - 图1

worse than Momentum strategy