事件驱动策略示例——盈利预增

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

策略思路

  • 从DataAPI中获取沪深300成分股的盈利预增事件数据
  • 每个交易日,将昨天发布盈利预增事件公司加入买入列表
  • 根据调仓限制和买入列表进行调仓
  • 调仓限制
    • (1) 股票持有不超过50只
    • (2) 一旦买入,持有40个交易日
    • (3) 仅当持有数量低于50只时才买入股票,补满50只
  1. import pandas as pd
  2. from datetime import datetime
  3. from functools import partial
  4. fields_ef = ['secID', 'publishDate']
  5. get_data = partial(DataAPI.FdmtEfGet, forecastType = 22, field = fields_ef) # forecastType 22: 盈利预增
  6. data_ef = []
  7. for stock in set_universe('HS300'):
  8. try:
  9. if len(data_ef):
  10. data_ef = data_ef.append(get_data(secID = stock))
  11. else:
  12. data_ef = get_data(secID = stock)
  13. except:
  14. pass
  15. data_ef['publishDate'] = pd.to_datetime(data_ef['publishDate'])
  16. data_ef = data_ef.sort(columns = 'publishDate')
  17. data_ef = data_ef[data_ef.publishDate >= datetime(2010, 1, 1)]
  1. start = '2010-01-01'
  2. end = '2015-04-01'
  3. benchmark = 'HS300'
  4. universe = set_universe('HS300')
  5. capital_base = 1000000
  6. longest_history = 1
  7. max_t = 40 # 持仓时间
  8. max_n = 50 # 持仓数量
  9. def initialize(account):
  10. account.hold_period = {}
  11. def handle_data(account):
  12. yesterday = account.get_symbol_history('tradeDate', 1)[0]
  13. data_sub = data_ef[data_ef.publishDate == yesterday]
  14. if len(data_sub):
  15. buylist = [s for s in data_sub['secID'].tolist() if s in account.universe]
  16. rebalance(account, buylist)
  17. def rebalance(account, buylist):
  18. n = 0
  19. for stock, t in account.hold_period.items():
  20. if t == max_t:
  21. order_to(stock, 0)
  22. del account.hold_period[stock]
  23. else:
  24. account.hold_period[stock] += 1
  25. n += 1
  26. if n == max_n or buylist == []:
  27. return
  28. b = max_n - n
  29. buylist = [s for s in buylist if s not in account.hold_period]
  30. for stock in buylist[:b]:
  31. order(stock, account.referencePortfolioValue / b / account.referencePrice[stock])
  32. account.hold_period[stock] = 0

事件驱动策略示例——盈利预增 - 图1