羊驼策略

策略实现

羊驼做为上古十大神兽之一, 选股祥瑞, 名号响亮, 本策略由一个羊驼类负责每周生成买入卖出信号, 验证羊驼是否名实相符.

  • 投资域 :沪深300成分股
  • 业绩基准 :沪深300指数
  • 调仓频率 :5个交易日
  • 买入卖出信号 :初始时任意买10只羊驼,每次调仓时,剔除收益最差的一只羊驼,再任意买一只羊驼.
  • 回测周期 :2014年1月1日至2015年5月5日 羊驼策略 - 图1
  1. import numpy as np
  2. import operator
  3. from datetime import datetime
  4. start = datetime(2010, 1, 1)
  5. end = datetime(2015, 5, 5)
  6. benchmark = 'HS300'
  7. universe = set_universe('HS300')
  8. capital_base = 100000
  9. longest_history = 10
  10. refresh_rate = 5
  11. def initialize(account):
  12. account.stocks_num = 10
  13. def handle_data(account):
  14. hist_prices = account.get_attribute_history('closePrice', 5)
  15. yangtuos = list(YangTuo(set(account.universe)-set(account.valid_secpos.keys()), account.stocks_num))
  16. cash = account.cash
  17. if account.stocks_num == 1:
  18. hist_returns = {}
  19. for stock in account.valid_secpos:
  20. hist_returns[stock] = hist_prices[stock][-1]/hist_prices[stock][0]
  21. sorted_returns = sorted(hist_returns.items(), key=operator.itemgetter(1))
  22. sell_stock = sorted_returns[0][0]
  23. cash = account.cash + hist_prices[sell_stock][-1]*account.valid_secpos.get(sell_stock)
  24. order_to(sell_stock, 0)
  25. else:
  26. account.stocks_num = 1
  27. for stock in yangtuos:
  28. order(stock, cash/len(yangtuos)/hist_prices[stock][-1])
  29. class YangTuo:
  30. def __init__(self, caoyuan=[], count=10):
  31. self.count = count
  32. self.i = 0
  33. self.caoyuan = list(caoyuan)
  34. def __iter__(self):
  35. return self
  36. def next(self):
  37. if self.i < self.count:
  38. self.i += 1
  39. return self.caoyuan.pop(np.random.randint(len(self.caoyuan)))
  40. else:
  41. raise StopIteration()

羊驼策略 - 图2

也许你会说,这只是运气好,并不能说明羊驼的厉害啊!好,接下来我们运行100次,看看羊驼的威力.

  1. start = datetime(2010, 1, 1)
  2. end = datetime(2015, 5, 5)
  3. benchmark = 'HS300'
  4. universe = set_universe('HS300')
  5. capital_base = 100000
  6. sim_params = quartz.sim_condition.env.SimulationParameters(start, end, benchmark, universe, capital_base)
  7. idxmap_all, data_all = quartz.sim_condition.data_generator.get_daily_data(sim_params)
  1. import numpy as np
  2. import operator
  3. longest_history = 10
  4. refresh_rate = 5
  5. def initialize(account):
  6. account.stocks_num = 10
  7. def handle_data(account):
  8. hist_prices = account.get_attribute_history('closePrice', 5)
  9. yangtuos = list(YangTuo(set(account.universe)-set(account.valid_secpos.keys()), account.stocks_num))
  10. cash = account.cash
  11. if account.stocks_num == 1:
  12. hist_returns = {}
  13. for stock in account.valid_secpos:
  14. hist_returns[stock] = hist_prices[stock][-1]/hist_prices[stock][0]
  15. sorted_returns = sorted(hist_returns.items(), key=operator.itemgetter(1))
  16. sell_stock = sorted_returns[0][0]
  17. cash = account.cash + hist_prices[sell_stock][-1]*account.valid_secpos.get(sell_stock)
  18. order_to(sell_stock, 0)
  19. else:
  20. account.stocks_num = 1
  21. for stock in yangtuos:
  22. order(stock, cash/len(yangtuos)/hist_prices[stock][-1])
  23. class YangTuo:
  24. def __init__(self, caoyuan=[], count=10):
  25. self.count = count
  26. self.i = 0
  27. self.caoyuan = list(caoyuan)
  28. def __iter__(self):
  29. return self
  30. def next(self):
  31. if self.i < self.count:
  32. self.i += 1
  33. return self.caoyuan.pop(np.random.randint(len(self.caoyuan)))
  34. else:
  35. raise StopIteration()
  36. strategy = quartz.sim_condition.strategy.TradingStrategy(initialize, handle_data)
  37. perfs = []
  38. for i in xrange(100):
  39. bt, acct = quartz.quick_backtest(sim_params, strategy, idxmap_all, data_all, refresh_rate = refresh_rate, longest_history=longest_history)
  40. perf = quartz.perf_parse(bt, acct)
  41. perfs.append(perf)
  1. from matplotlib import pylab
  2. import seaborn
  3. x = sorted([p['annualized_return']-p['benchmark_annualized_return'] for p in perfs])
  4. pylab.plot(x)
  5. pylab.plot([0]*len(x))
  6. [<matplotlib.lines.Line2D at 0x7702a10>]

羊驼策略 - 图3

100%的胜率! 大家闭着眼睛,跟着羊驼买就行了!

接下来的工作:

由于指数并没有分红等概念, 直接拿HS300指数做benchmark, 对HS300并不公平. 所以接下来考虑把benchmark换成某只指数基金, 再做对比.

羊驼策略 - 图4