返回介绍

第一部分 新手入门

第二部分 股票量化相关

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

第四部分 衍生品相关

Simple Bollinger Bands

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

策略思路: 在一段时间内,股票价格可以认为是在一定水平上下波动。例如,可以简单认为股价在过去100天的平均值基础上,上下波动几个标准差。

策略实现: (1) 股价高于这个波动区间,说明股价虚高,故卖出 (2) 股价低于这个波动区间,说明股价虚低,故买入

PS:本策略适用于股市平稳波动时期,在大牛市或者大熊市不太适用

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, 10, 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):

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

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

        upper = mu + 2*sd
        middle = mu
        lower = mu - 2*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-11-011000000.00000{}1000000.000000.000000[]
12011-11-021000000.00000{}1000000.000000.016629[]
22011-11-031000000.00000{}1000000.000000.000697[]
32011-11-041000000.00000{}1000000.000000.007086[]
42011-11-071000000.00000{}1000000.00000-0.009950[]
52011-11-081000000.00000{}1000000.00000-0.003120[]
62011-11-091000000.00000{}1000000.000000.008778[]
72011-11-101000000.00000{}1000000.00000-0.018922[]
82011-11-111000000.00000{}1000000.00000-0.001699[]
92011-11-141000000.00000{}1000000.000000.020480[]
102011-11-151000000.00000{}1000000.00000-0.002005[]
112011-11-161000000.00000{}1000000.00000-0.027167[]
122011-11-171000000.00000{}1000000.00000-0.003033[]
132011-11-181000000.00000{}1000000.00000-0.020857[]
142011-11-211000000.00000{}1000000.000000.001225[]
152011-11-221000000.00000{}1000000.00000-0.000081[]
162011-11-231000000.00000{}1000000.00000-0.009758[]
172011-11-241000000.00000{}1000000.000000.001898[]
182011-11-251000000.00000{}1000000.00000-0.007317[]
192011-11-281000000.00000{}1000000.000000.001303[]
202011-11-291000000.00000{}1000000.000000.013697[]
212011-11-301000000.00000{}1000000.00000-0.033370[]
222011-12-011000000.00000{}1000000.000000.024625[]
232011-12-021000000.00000{}1000000.00000-0.010180[]
242011-12-051000000.00000{}1000000.00000-0.014048[]
252011-12-061000000.00000{}1000000.00000-0.002000[]
262011-12-071000000.00000{}1000000.000000.004724[]
272011-12-081000000.00000{}1000000.00000-0.001276[]
282011-12-091000000.00000{}1000000.00000-0.008531[]
292011-12-121000000.00000{}1000000.00000-0.010293[]
.....................
6792014-08-1996907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1149017.279890.000087[]
6802014-08-2096907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1168551.04989-0.003633[Order({'transact_price': 0.0, 'symbol': '6000...
6812014-08-2196907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1155719.88989-0.005028[]
6822014-08-2296907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1152026.339890.004723[]
6832014-08-2596907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1148250.45989-0.009513[]
6842014-08-2696907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1141866.53989-0.008012[]
6852014-08-2796907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1133729.609890.001507[]
6862014-08-2896907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1130309.43989-0.007010[]
6872014-08-2996907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1144849.979890.011686[]
6882014-09-0196907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1146127.249890.007283[]
6892014-09-0296907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1171994.819890.013222[]
6902014-09-0396907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1186363.069890.009377[Order({'transact_price': 0.0, 'symbol': '6000...
6912014-09-0496907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1188453.829890.007218[]
6922014-09-0596907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1203626.039890.009494[]
6932014-09-0996907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1182303.76989-0.001647[]
6942014-09-1096907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1180321.58989-0.005231[]
6952014-09-1196907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1168537.55989-0.003691[Order({'transact_price': 0.0, 'symbol': '6000...
6962014-09-1296907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1171787.869890.006150[]
6972014-09-1596907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1153269.96989-0.000479[]
6982014-09-1696907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1141194.90989-0.019869[]
6992014-09-1796907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1145778.589890.005258[]
7002014-09-1896907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1152235.309890.003056[]
7012014-09-1996907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1162000.359890.006870[]
7022014-09-2296907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1136441.48989-0.019087[]
7032014-09-2396907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1139988.079890.008635[]
7042014-09-2496907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1157613.469890.017671[]
7052014-09-2596907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1169367.30989-0.002006[]
7062014-09-2696907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1169791.299890.000094[]
7072014-09-2996907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1170503.499890.004349[]
7082014-09-3096907.47989{u'601166.XSHG': 3877.0, u'600036.XSHG': 0, u'...1168692.079890.001303[]
709 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.0594823977045
volatility: 0.214571802928
information: 0.407163653335
sharpe: 0.102806600884
max_drawdown: 0.36585509048
alpha: 0.052294592526
beta: 0.434097117249
perf['cumulative_return'].plot()
perf['benchmark_cumulative_return'].plot()
pylab.legend(['current_strategy','HS300'])

<matplotlib.legend.Legend at 0x568fe90>

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

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

发布评论

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