Competitive Securities

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

策略实现:

  • 计算三只同一行业股票过去4天内前3天的平均成交价(VWAP),这里选用的是中国平安 (601318.XSHG)、中国太保 (601601.XSHG)和中国人寿 (601628.XSHG)

  • 当某两只股票的价格低于0.995 * VWAP,同时另一只股票价格高于VWAP时,买入后者

  • 当某两只股票的价格高于1.025 * VWAP,同时另一只股票价格低于VWAP时,清空后者

  1. import pandas as pd
  2. import numpy as np
  3. from datetime import datetime
  4. from matplotlib import pylab
  5. import quartz
  6. import quartz.backtest as qb
  7. import quartz.performance as qp
  8. from quartz.api import *
  1. "Competitive Securities"
  2. start = pd.datetime(2012, 1, 1)
  3. end = pd.datetime(2014, 12, 1)
  4. bm = 'HS300'
  5. universe = ['601601.XSHG', '601318.XSHG', '601628.XSHG']
  6. csvs = []
  7. capital_base = 5000
  8. window = 4
  9. threshold_dn = 0.995
  10. threshold_up = 1.025
  11. refresh_rate = 4
  12. def initialize(account):
  13. account.amount = 1000
  14. account.universe = universe
  15. add_history('hist', window)
  16. def handle_data(account):
  17. vwap3, price = {}, {}
  18. for stk in account.universe:
  19. if stk not in account.hist:
  20. continue
  21. vwap3[stk] = sum(account.hist[stk]['turnoverValue'][:3])/sum(account.hist[stk]['turnoverVol'][:3])
  22. price[stk] = account.hist[stk].iloc[window-1,:]['closePrice']
  23. if len(vwap3)!=3:
  24. return
  25. stk_0 = account.universe[0]
  26. stk_1 = account.universe[1]
  27. stk_2 = account.universe[2]
  28. if price[stk_1] <= threshold_dn * vwap3[stk_1] and price[stk_2] <= threshold_dn * vwap3[stk_2] and price[stk_0] > vwap3[stk_0]:
  29. order(stk_0, account.amount)
  30. if price[stk_2] <= threshold_dn * vwap3[stk_2] and price[stk_0] <= threshold_dn * vwap3[stk_0] and price[stk_1] > vwap3[stk_1]:
  31. order(stk_1, account.amount)
  32. if price[stk_0] <= threshold_dn * vwap3[stk_0] and price[stk_1] <= threshold_dn * vwap3[stk_1] and price[stk_2] > vwap3[stk_2]:
  33. order(stk_2, account.amount)
  34. if price[stk_1] >= threshold_up * vwap3[stk_1] and price[stk_2] >= threshold_up * vwap3[stk_2] and price[stk_0] < vwap3[stk_0]:
  35. order_to(stk_0, 0)
  36. if price[stk_2] >= threshold_up * vwap3[stk_2] and price[stk_0] >= threshold_up * vwap3[stk_0] and price[stk_1] < vwap3[stk_1]:
  37. order_to(stk_1, 0)
  38. if price[stk_0] >= threshold_up * vwap3[stk_0] and price[stk_1] >= threshold_up * vwap3[stk_1] and price[stk_2] < vwap3[stk_2]:
  39. order_to(stk_2, 0)

Competitive Securities - 图1

  1. perf = qp.perf_parse(bt)
  2. out_keys = ['annualized_return', 'volatility', 'information',
  3. 'sharpe', 'max_drawdown', 'alpha', 'beta']
  4. for k in out_keys:
  5. print '%s: %s' % (k, perf[k])
  6. annualized_return: 0.14708285
  7. volatility: 0.285959506628
  8. information: 0.525131029268
  9. sharpe: 0.395275720443
  10. max_drawdown: 0.391931712536
  11. alpha: 0.089663482291
  12. beta: 1.15117691695
  1. perf['cumulative_return'].plot()
  2. perf['benchmark_cumulative_return'].plot()
  3. pylab.legend(['current_strategy','HS300'])
  4. <matplotlib.legend.Legend at 0x55bf290>

Competitive Securities - 图2