Even More Conservative Bollinger Bands

来源:https://uqer.io/community/share/54859edff9f06c8e77336729

  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
  9. import talib
  1. start = datetime(2011, 1, 1)
  2. end = datetime(2014, 12, 1)
  3. benchmark = 'HS300'
  4. universe = ['601398.XSHG', '600028.XSHG', '601988.XSHG', '600036.XSHG', '600030.XSHG',
  5. '601318.XSHG', '600000.XSHG', '600019.XSHG', '600519.XSHG', '601166.XSHG']
  6. capital_base = 1000000
  7. refresh_rate = 5
  8. window = 200
  9. def initialize(account):
  10. account.amount = 10000
  11. account.universe = universe
  12. add_history('hist', window)
  13. def handle_data(account, data):
  14. for stk in account.universe:
  15. prices = account.hist[stk]['closePrice']
  16. if prices is None:
  17. return
  18. mu = prices.mean()
  19. sd = prices.std()
  20. upper = mu + .5*sd
  21. middle = mu
  22. lower = mu - .5*sd
  23. cur_pos = account.position.stkpos.get(stk, 0)
  24. cur_prc = prices[-1]
  25. if cur_prc > upper and cur_pos >= 0:
  26. order_to(stk, 0)
  27. if cur_prc < lower and cur_pos <= 0:
  28. order(stk, account.amount)

Even More Conservative Bollinger Bands - 图1

  1. bt
tradeDatecashstock_positionportfolio_valuebenchmark_returnblotter
02011-01-041000000{}10000000.000000[]
12011-01-051000000{}1000000-0.004395[]
22011-01-061000000{}1000000-0.005044[]
32011-01-071000000{}10000000.002209[]
42011-01-101000000{}1000000-0.018454[]
52011-01-111000000{}10000000.005384[]
62011-01-121000000{}10000000.005573[]
72011-01-131000000{}1000000-0.000335[]
82011-01-141000000{}1000000-0.015733[]
92011-01-171000000{}1000000-0.038007[]
102011-01-181000000{}10000000.001109[]
112011-01-191000000{}10000000.022569[]
122011-01-201000000{}1000000-0.032888[]
132011-01-211000000{}10000000.013157[]
142011-01-241000000{}1000000-0.009795[]
152011-01-251000000{}1000000-0.005273[]
162011-01-261000000{}10000000.013536[]
172011-01-271000000{}10000000.016128[]
182011-01-281000000{}10000000.003393[]
192011-01-311000000{}10000000.013097[]
202011-02-011000000{}10000000.000252[]
212011-02-091000000{}1000000-0.011807[]
222011-02-101000000{}10000000.020788[]
232011-02-111000000{}10000000.005410[]
242011-02-141000000{}10000000.031461[]
252011-02-151000000{}1000000-0.000457[]
262011-02-161000000{}10000000.009590[]
272011-02-171000000{}1000000-0.000807[]
282011-02-181000000{}1000000-0.010484[]
292011-02-211000000{}10000000.014332[]
302011-02-221000000{}1000000-0.028954[]
312011-02-231000000{}10000000.003529[]
322011-02-241000000{}10000000.005101[]
332011-02-251000000{}10000000.002094[]
342011-02-281000000{}10000000.013117[]
352011-03-011000000{}10000000.004733[]
362011-03-021000000{}1000000-0.003562[]
372011-03-031000000{}1000000-0.006654[]
382011-03-041000000{}10000000.015193[]
392011-03-071000000{}10000000.019520[]
402011-03-081000000{}10000000.000884[]
412011-03-091000000{}10000000.000420[]
422011-03-101000000{}1000000-0.017551[]
432011-03-111000000{}1000000-0.010025[]
442011-03-141000000{}10000000.004787[]
452011-03-151000000{}1000000-0.018069[]
462011-03-161000000{}10000000.013806[]
472011-03-171000000{}1000000-0.015730[]
482011-03-181000000{}10000000.005813[]
492011-03-211000000{}1000000-0.002667[]
502011-03-221000000{}10000000.004942[]
512011-03-231000000{}10000000.013021[]
522011-03-241000000{}1000000-0.004155[]
532011-03-251000000{}10000000.013263[]
542011-03-281000000{}1000000-0.001188[]
552011-03-291000000{}1000000-0.009905[]
562011-03-301000000{}1000000-0.000583[]
572011-03-311000000{}1000000-0.010071[]
582011-04-011000000{}10000000.015339[]
592011-04-061000000{}10000000.011714[]
  1. 948 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.118291633101
  7. volatility: 0.134550735738
  8. information: 0.776689524517
  9. sharpe: 0.591647698281
  10. max_drawdown: 0.135222029922
  11. alpha: 0.109380091075
  12. beta: 0.429849284472
  1. perf['cumulative_return'].plot()
  2. perf['benchmark_cumulative_return'].plot()
  3. pylab.legend(['current_strategy','HS300'])
  4. <matplotlib.legend.Legend at 0x49c0b10>

Even More Conservative Bollinger Bands - 图2