返回介绍

第一部分 新手入门

第二部分 股票量化相关

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

第四部分 衍生品相关

成交量因子

发布于 2022-02-20 22:26:13 字数 15194 浏览 869 评论 0 收藏 0

投资于沪深300成份股,每月调仓,每月调入成交量最低的成份股

start = '2015-1-1'                       # 回测起始时间
end = '2015-11-22'                         # 回测结束时间
benchmark = 'HS300'                        # 策略参考标准
universe = set_universe('HS300')  # 证券池,支持股票和基金
capital_base = 10000000                      # 起始资金
refresh_rate = 20                           # 调仓频率,表示执行handle_data的时间间隔,若freq = 'd'时间间隔的单位为交易日,若freq = 'm'时间间隔为分钟
stk_num = 60


def initialize(account):                   # 初始化虚拟账户状态
    pass

def handle_data(account):                    # 每个交易日的买入卖出指令
    #取出每日成交量 
    hist = account.get_attribute_history('turnoverValue',1)
    #print account.current_date
    #print hist

    cjl = {}
    # 排序
    for stock in account.universe:
        cjl[stock] = hist[stock][0]
    dd = sorted(cjl.iteritems(),key = lambda d:d[1],reverse = False)
    #print dd

    cash = account.cash   
    # 卖出持有的  
    for s,a in account.valid_secpos.items():
        order_to(s,0)
        cash += a*account.referencePrice[s]

    # 买入
    i = 0
    for s in dd:
        if i < stk_num :
            i = i + 1
            order(s[0],cash/60/account.referencePrice[s[0]])
            #print s[0]

bt
tradeDatecashsecurity_positionportfolio_valuebenchmark_returnblotter
02015-01-06980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1000650.1468-0.000132[Order(order_time: 2015-01-06 09:30, symbol: 0...
12015-01-07980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1000573.24680.000750[]
22015-01-08980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1000781.2468-0.023198[]
32015-01-09980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1000992.8468-0.003523[]
42015-01-12980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001146.3468-0.009344[]
52015-01-13980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001283.94680.000131[]
62015-01-14980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001027.0468-0.003307[]
72015-01-15980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001017.44680.029037[]
82015-01-16980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001257.64680.008607[]
92015-01-19980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1000788.4468-0.077023[]
102015-01-20980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1001703.54680.012241[]
112015-01-21980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002298.94680.044950[]
122015-01-22980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002567.54680.005278[]
132015-01-23980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002212.64680.001155[]
142015-01-26980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002646.44680.010149[]
152015-01-27980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1003116.3468-0.009160[]
162015-01-28980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002968.9468-0.013877[]
172015-01-29980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002741.4468-0.012348[]
182015-01-30980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002465.6468-0.013614[]
192015-02-02980827.6468{u'002252.XSHE': {u'amount': 100, u'cost': 22....1002310.2468-0.023419[]
202015-02-03979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002687.75050.024890[Order(order_time: 2015-02-03 09:30, symbol: 6...
212015-02-04979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002527.6505-0.010380[]
222015-02-05979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002320.0505-0.010236[]
232015-02-06979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1001810.1505-0.016193[]
242015-02-09979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002149.15050.010113[]
252015-02-10979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002526.55050.018237[]
262015-02-11979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1002846.55050.007978[]
272015-02-12979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1003098.65050.002548[]
282015-02-13979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1003569.55050.007828[]
292015-02-16979846.9505{u'601158.XSHG': {u'amount': 400, u'cost': 7.9...1004382.45050.008548[]
.....................
1842015-10-12979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1011749.08470.032208[]
1852015-10-13979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1011658.0847-0.000769[]
1862015-10-14979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1011279.0847-0.011301[]
1872015-10-15979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1011943.08470.023694[]
1882015-10-16979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012667.08470.013551[]
1892015-10-19979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012613.08470.000033[]
1902015-10-20979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012848.08470.012314[]
1912015-10-21979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1011284.0847-0.029194[]
1922015-10-22979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012610.08470.014764[]
1932015-10-23979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1013730.08470.013252[]
1942015-10-26979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1013604.08470.005046[]
1952015-10-27979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1013456.08470.001008[]
1962015-10-28979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012937.0847-0.018915[]
1972015-10-29979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1013093.08470.002379[]
1982015-10-30979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012739.08470.000218[]
1992015-11-02979810.0847{u'601158.XSHG': {u'amount': 400, u'cost': 8.2...1012311.0847-0.016445[]
2002015-11-03984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1012266.4817-0.003012[Order(order_time: 2015-11-03 09:30, symbol: 6...
2012015-11-04984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1013422.48170.047048[]
2022015-11-05984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014069.48170.021340[]
2032015-11-06984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014599.48170.023585[]
2042015-11-09984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1015194.48170.012385[]
2052015-11-10984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1015158.4817-0.001853[]
2062015-11-11984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1015091.48170.000106[]
2072015-11-12984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014932.4817-0.009996[]
2082015-11-13984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014318.4817-0.012932[]
2092015-11-16984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014373.48170.004774[]
2102015-11-17984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014520.4817-0.001525[]
2112015-11-18984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014182.4817-0.011390[]
2122015-11-19984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014563.48170.015984[]
2132015-11-20984251.4817{u'600011.XSHG': {u'amount': 400, u'cost': 9.0...1014545.4817-0.000155[]
214 rows × 6 columns

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

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

发布评论

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