返回介绍

第一部分 新手入门

第二部分 股票量化相关

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

第四部分 衍生品相关

Market Competitiveness

发布于 2022-02-20 22:26:18 字数 15743 浏览 827 评论 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
start = datetime(2012, 1, 1)
end = datetime(2014, 12, 1)
benchmark = 'HS300'
universe = ['000625.XSHE', # 长安汽车
            '002594.XSHE', # 比亚迪汽车
            '601633.XSHG'  # 长城汽车
            ]

capital_base = 1000000
refresh_rate = 5
window = 10

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

def handle_data(account):

    stk_0 = universe[0]
    stk_1 = universe[1]
    stk_2 = universe[2]

    prices_0 = account.hist[stk_0]['closePrice']
    prices_1 = account.hist[stk_1]['closePrice']
    prices_2 = account.hist[stk_2]['closePrice']

    mu_0 = prices_0.mean()
    mu_1 = prices_1.mean()
    mu_2 = prices_2.mean()

    # 两只下跌较大幅度,一只较稳定,买入较稳定这只股票
    if prices_0[-1] > mu_0 and prices_1[-1] < 0.975 * mu_1 and prices_2[-1] < 0.975 * mu_2:
        order(stk_0, account.amount)
    if prices_1[-1] > mu_1 and prices_2[-1] < 0.975 * mu_2 and prices_0[-1] < 0.975 * mu_0:
        order(stk_1, account.amount)
    if prices_2[-1] > mu_2 and prices_0[-1] < 0.975 * mu_0 and prices_1[-1] < 0.975 * mu_1:
        order(stk_2, account.amount)

    # 两只上涨较大幅度,一只较稳定,卖出较稳定这只股票
    if prices_0[-1] < mu_0 and prices_1[-1] > 1.025 * mu_1 and prices_2[-1] > 1.025 * mu_2:
        order_to(stk_0, 0)
    if prices_1[-1] < mu_1 and prices_0[-1] > 1.025 * mu_0 and prices_2[-1] > 1.025 * mu_2:
        order_to(stk_1, 0)
    if prices_2[-1] < mu_2 and prices_0[-1] > 1.025 * mu_0 and prices_1[-1] > 1.025 * mu_1:
        order_to(stk_2, 0)

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[]
694 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.448632577093
volatility: 0.397466535866
information: 0.825863671828
sharpe: 1.04326663926
max_drawdown: 0.518092986656
alpha: 0.392363999248
beta: 0.886220585368
perf['cumulative_return'].plot()
perf['benchmark_cumulative_return'].plot()
pylab.legend(['current_strategy','HS300'])

<matplotlib.legend.Legend at 0x4e27c50>

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

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

发布评论

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