Market Competitiveness

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

来一个奇葩无厘头的市场竞争策略

策略思路

某一行业的几大龙头股票,在稳定时期此消彼长

策略实现

  • 股票池:选择一行业内的流动性比较好的龙头股票;例如三家自助品牌汽车,长安、比亚迪和长城,以下按照三只股票情况讨论

  • 观察某一天时,股票价格和该股票在过去几天内平均值的关系

  • 如果两只股票下跌,则预测另一只股票上涨;如果两只股票上涨,则预测另一只股票下跌

  • 如果某天三只股票中的两只较其平均值有较大幅度下跌,而另一只股票较其平均值比较稳定不变,则买入后面这只比较稳定的股票

  • 如果某天三只股票中的两只较其平均值有较大幅度上涨,而另一只股票较其平均值比较稳定不变,则卖出后面这只比较稳定的股票

  1. import quartz
  2. import quartz.backtest as qb
  3. import quartz.performance as qp
  4. from quartz.api import *
  5. import pandas as pd
  6. import numpy as np
  7. from datetime import datetime
  8. from matplotlib import pylab
  1. start = datetime(2012, 1, 1)
  2. end = datetime(2014, 12, 1)
  3. benchmark = 'HS300'
  4. universe = ['000625.XSHE', # 长安汽车
  5. '002594.XSHE', # 比亚迪汽车
  6. '601633.XSHG' # 长城汽车
  7. ]
  8. capital_base = 1000000
  9. refresh_rate = 5
  10. window = 10
  11. def initialize(account):
  12. account.amount = 100000
  13. account.universe = universe
  14. add_history('hist', window)
  15. def handle_data(account):
  16. stk_0 = universe[0]
  17. stk_1 = universe[1]
  18. stk_2 = universe[2]
  19. prices_0 = account.hist[stk_0]['closePrice']
  20. prices_1 = account.hist[stk_1]['closePrice']
  21. prices_2 = account.hist[stk_2]['closePrice']
  22. mu_0 = prices_0.mean()
  23. mu_1 = prices_1.mean()
  24. mu_2 = prices_2.mean()
  25. # 两只下跌较大幅度,一只较稳定,买入较稳定这只股票
  26. if prices_0[-1] > mu_0 and prices_1[-1] < 0.975 * mu_1 and prices_2[-1] < 0.975 * mu_2:
  27. order(stk_0, account.amount)
  28. if prices_1[-1] > mu_1 and prices_2[-1] < 0.975 * mu_2 and prices_0[-1] < 0.975 * mu_0:
  29. order(stk_1, account.amount)
  30. if prices_2[-1] > mu_2 and prices_0[-1] < 0.975 * mu_0 and prices_1[-1] < 0.975 * mu_1:
  31. order(stk_2, account.amount)
  32. # 两只上涨较大幅度,一只较稳定,卖出较稳定这只股票
  33. if prices_0[-1] < mu_0 and prices_1[-1] > 1.025 * mu_1 and prices_2[-1] > 1.025 * mu_2:
  34. order_to(stk_0, 0)
  35. if prices_1[-1] < mu_1 and prices_0[-1] > 1.025 * mu_0 and prices_2[-1] > 1.025 * mu_2:
  36. order_to(stk_1, 0)
  37. if prices_2[-1] < mu_2 and prices_0[-1] > 1.025 * mu_0 and prices_1[-1] > 1.025 * mu_1:
  38. order_to(stk_2, 0)

Market Competitiveness - 图1

  1. bt
tradeDatecashstock_positionportfolio_valuebenchmark_returnblotter
02012-01-181000000.00000{}1000000.000000.000000[]
12012-01-191000000.00000{}1000000.000000.019058[]
22012-01-201000000.00000{}1000000.000000.014478[]
32012-01-301000000.00000{}1000000.00000-0.017318[]
42012-01-311000000.00000{}1000000.000000.001439[]
52012-02-011000000.00000{}1000000.00000-0.014311[]
62012-02-021000000.00000{}1000000.000000.023567[]
72012-02-031000000.00000{}1000000.000000.007985[]
82012-02-061000000.00000{}1000000.00000-0.000705[]
92012-02-071000000.00000{}1000000.00000-0.018515[]
102012-02-081000000.00000{}1000000.000000.028594[]
112012-02-091000000.00000{}1000000.000000.000394[]
122012-02-101000000.00000{}1000000.000000.001737[]
132012-02-131000000.00000{}1000000.00000-0.000648[]
142012-02-141000000.00000{}1000000.00000-0.003900[]
152012-02-151000000.00000{}1000000.000000.010904[]
162012-02-161000000.00000{}1000000.00000-0.005308[]
172012-02-171000000.00000{}1000000.000000.000399[]
182012-02-201000000.00000{}1000000.000000.001427[]
192012-02-211000000.00000{}1000000.000000.008559[]
202012-02-221000000.00000{}1000000.000000.013668[]
212012-02-231000000.00000{}1000000.000000.003380[]
222012-02-241000000.00000{}1000000.000000.016023[]
232012-02-271000000.00000{}1000000.000000.003231[]
242012-02-281000000.00000{}1000000.000000.002217[]
252012-02-291000000.00000{}1000000.00000-0.010637[]
262012-03-011000000.00000{}1000000.00000-0.000303[]
272012-03-021000000.00000{}1000000.000000.017692[]
282012-03-051000000.00000{}1000000.00000-0.006432[]
292012-03-061000000.00000{}1000000.00000-0.015641[]
6642014-10-211.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1913031.23401-0.008685[]
6652014-10-221.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1933648.47401-0.006062[]
6662014-10-231.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1953640.67401-0.009385[]
6672014-10-241.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1823065.79401-0.002183[]
6682014-10-271.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1859302.04401-0.009152[]
6692014-10-281.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1863675.444010.020187[]
6702014-10-291.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1871797.244010.014371[]
6712014-10-301.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1883042.974010.007156[]
6722014-10-311.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1913656.094010.015958[]
6732014-11-031.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1902410.644010.001682[]
6742014-11-041.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1964886.424010.000247[]
6752014-11-051.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2049228.79401-0.003869[]
6762014-11-061.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2020489.704010.001047[]
6772014-11-071.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2027362.02401-0.001564[]
6782014-11-101.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2043606.064010.025410[]
6792014-11-111.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2022988.64401-0.002775[]
6802014-11-121.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2049228.914010.013957[]
6812014-11-131.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2054226.61401-0.005616[]
6822014-11-141.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1987377.184010.000519[]
6832014-11-171.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1988626.85401-0.005420[]
6842014-11-181.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2006744.97401-0.010004[]
6852014-11-191.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2014241.95401-0.001653[]
6862014-11-201.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1980504.81401-0.000047[]
6872014-11-211.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}1989251.554010.018273[]
6882014-11-241.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2085464.994010.025470[]
6892014-11-251.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2156687.784010.013702[]
6902014-11-261.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2142942.924010.013949[]
6912014-11-271.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2146691.264010.011557[]
6922014-11-281.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2276016.944010.019724[]
6932014-12-011.56401{u'000625.XSHE': 1.0, u'601633.XSHG': 62476.0}2245404.034010.003913[]
  1. 694 rows × 6 columns
  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.448632577093
  7. volatility: 0.397466535866
  8. information: 0.825863671828
  9. sharpe: 1.04326663926
  10. max_drawdown: 0.518092986656
  11. alpha: 0.392363999248
  12. beta: 0.886220585368
  1. perf['cumulative_return'].plot()
  2. perf['benchmark_cumulative_return'].plot()
  3. pylab.legend(['current_strategy','HS300'])
  4. <matplotlib.legend.Legend at 0x4e27c50>

Market Competitiveness - 图2