返回介绍

第一部分 新手入门

第二部分 股票量化相关

第三部分 基金、利率互换、固定收益类

第四部分 衍生品相关

Even More Conservative Bollinger Bands

发布于 2022-02-20 22:26:15 字数 9912 浏览 873 评论 0 收藏 0

import quartz
import quartz.backtest    as qb
import quartz.performance as qp
from   quartz.api         import *

import pandas as pd
import numpy  as np
from datetime   import datetime
from matplotlib import pylab

import talib
start = datetime(2011, 1, 1)
end = datetime(2014, 12, 1)
benchmark = 'HS300'
universe = ['601398.XSHG', '600028.XSHG', '601988.XSHG', '600036.XSHG', '600030.XSHG',
            '601318.XSHG', '600000.XSHG', '600019.XSHG', '600519.XSHG', '601166.XSHG']
capital_base = 1000000
refresh_rate = 5
window = 200

def initialize(account):
    account.amount = 10000
    account.universe = universe
    add_history('hist', window)

def handle_data(account, data):

    for stk in account.universe:
        prices = account.hist[stk]['closePrice']
        if prices is None:
            return

        mu = prices.mean()
        sd = prices.std()

        upper = mu + .5*sd 
        middle = mu
        lower = mu - .5*sd


        cur_pos = account.position.stkpos.get(stk, 0)
        cur_prc = prices[-1]
        if cur_prc > upper and cur_pos >= 0:
            order_to(stk, 0)
        if cur_prc < lower and cur_pos <= 0:
            order(stk, account.amount)

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[]
..................
948 rows × 6 columns
perf = qp.perf_parse(bt)
out_keys = ['annualized_return', 'volatility', 'information',
            'sharpe', 'max_drawdown', 'alpha', 'beta']

for k in out_keys:
    print '%s: %s' % (k, perf[k])

annualized_return: 0.118291633101
volatility: 0.134550735738
information: 0.776689524517
sharpe: 0.591647698281
max_drawdown: 0.135222029922
alpha: 0.109380091075
beta: 0.429849284472
perf['cumulative_return'].plot()
perf['benchmark_cumulative_return'].plot()
pylab.legend(['current_strategy','HS300'])

<matplotlib.legend.Legend at 0x49c0b10>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文