Momentum策略

来源:https://uqer.io/community/share/5526191ef9f06c8f33904571

  1. import pandas as pd
  2. from pandas import Series, DataFrame
  3. start = datetime(2011, 1, 1)
  4. end = datetime(2014, 8, 1)
  5. benchmark = 'SH50'
  6. universe = set_universe('SH50')
  7. capital_base = 100000
  8. refresh_rate = 10
  9. window = 20
  10. def initialize(account):
  11. account.amount = 300
  12. add_history('hist', window)
  13. def handle_data(account):
  14. momentum = {'symbol':[], 'c_ret':[]}
  15. for stk in account.universe:
  16. momentum = pd.DataFrame(momentum)
  17. momentum = momentum.append([{'symbol':stk,'c_ret':account.hist[stk]['closePrice'].iloc[-1]/account.hist[stk]['closePrice'].iloc[0]}])
  18. momentum = momentum.sort(columns='c_ret').reset_index(drop=True)
  19. momentum = momentum[len(momentum)*4/5:len(momentum)]
  20. buylist = momentum['symbol'].tolist()
  21. for stk in account.stkpos:
  22. if (stk not in buylist) and (account.stkpos[stk]>0):
  23. order_to(stk, 0)
  24. for stk in buylist:
  25. if account.stkpos.get(stk,0) == 0:
  26. order_to(stk, account.amount)

Momentum策略 - 图1