银行股轮动

来源:https://uqer.io/community/share/55d0314af9f06c915418c69a

策略说明:持续持有两支市净率最低银行股,每月换仓

  1. import numpy as np
  2. import pandas as pd
  3. from pandas import DataFrame
  4. import datetime
  5. start = '2011-01-01'
  6. end = '2015-08-14'
  7. benchmark = 'HS300'
  8. universe = ['000001.XSHE','002142.XSHE','600000.XSHG','600015.XSHG','600016.XSHG','600036.XSHG','601009.XSHG','601166.XSHG','601169.XSHG','601288.XSHG','601328.XSHG','601398.XSHG','601818.XSHG','601939.XSHG','601988.XSHG','601998.XSHG']
  9. capital_base = 10000000
  10. refresh_rate = 20
  11. def initialize(account):
  12. pass
  13. def handle_data(account):
  14. cal = Calendar('China.SSE')
  15. lastTDay = cal.advanceDate(account.current_date,'-1B',BizDayConvention.Preceding)
  16. today_str = lastTDay.strftime("%Y%m%d")
  17. tickers = []
  18. for stk in account.universe:
  19. if not np.isnan(account.referencePrice[stk]):
  20. tickers.append(stk[0:6])
  21. try:
  22. d=DataAPI.MktEqudGet(secID=u"",ticker=tickers,tradeDate=today_str,beginDate=u"",endDate=u"",field="secID,PB",pandas="1")
  23. d=d.sort(columns='PB',ascending=1)
  24. d=d.head(2)
  25. buylist = d['secID'].tolist()
  26. for stk in account.valid_secpos:
  27. if stk not in buylist:
  28. order_to(stk, 0)
  29. # 等权重买入所选股票
  30. portfolio_value = account.referencePortfolioValue
  31. for stk in buylist:
  32. if stk not in account.valid_secpos:
  33. order_to(stk, int(portfolio_value / account.referencePrice[stk] / 100.0 / len(buylist))*100)
  34. except:
  35. return

银行股轮动 - 图1