设为首页收藏本站

 找回密码
 注册
查看: 2803|回复: 2
打印 上一主题 下一主题

请问这个策略有偷价或者未来函数行为吗?回测效果很好 [复制链接]

Rank: 1

精华
0
UID
186298
积分
10
帖子
7
主题
3
阅读权限
10
注册时间
2014-5-7
最后登录
2016-2-22
跳转到指定楼层
1#
发表于 2016-2-22 14:37:19 |只看该作者 |倒序浏览
import pandas as pd
import numpy as np

def init(context):
    context.s1 = '000001.XSHG'
    context.max_num_stocks = 40
    context.days = 0
    context.period_days = 1
    context.relative_strength_6m = {}

def period_passed(context):
    return context.days % context.period_days == 0
   
def before_trading(context):
    context.days += 1
    if not period_passed(context):
        return
   
    dofilter(context)
    update_universe(context.fundamental_df.columns.values)

def dofilter(context):
   
    fundamental_df = get_fundamentals(
        query(fundamentals.eod_derivative_indicator.market_cap)
        .order_by(fundamentals.eod_derivative_indicator.market_cap.asc())
        .limit(context.max_num_stocks)
    )
   
    #Update context
    context.stocks = [stock for stock in fundamental_df]
    context.fundamental_df = fundamental_df
   
def rebalance(context, bar_dict):
   
    for stock in context.portfolio.positions:
        if stock not in context.fundamental_df:
            order_target_percent(stock, 0)
            
    context.stocks = [stock for stock in context.stocks
                      if stock in bar_dict and bar_dict[stock].is_trading and context.relative_strength_6m[stock] <-0.5]
   
    if len(context.stocks) == 0:
        return
   
    weight = 1.0/len(context.stocks)
   
    for stock in context.stocks:
        order_target_percent(stock, weight)
   
def handle_bar(context, bar_dict):
   
    his = history(10, '1d', 'close')['000001.XSHG']
   
    if period_passed(context):
        if his[9]/his[8]< 0.97:
            if len(context.portfolio.positions)>0:
                for stock in context.portfolio.positions.keys():
                    order_target_percent(stock, 0)
            return
   
    if not period_passed(context):
        return
   
    compute_relative_strength(context,bar_dict)
    rebalance(context, bar_dict)
   
def compute_relative_strength(context,bar_dict):
   
    prices = history (150, '1d', 'close')

    #过去六个月的价格变化率
    pct_change = (prices.ix[149] - prices.ix[19]) / prices.ix[19]
    #print(prices.ix[19])
    #print(pct_change)
    priceofbase = history (150, '1d', 'close')[context.s1]
    pct_changeforbase = (priceofbase.ix[149] - priceofbase.ix[19]) / priceofbase.ix[19]
    pct_change = pct_change - pct_changeforbase
    print(pct_change.index)
    print(bar_dict)
    if pct_changeforbase != 0:
        pct_change = pct_change / abs(pct_changeforbase)
    context.relative_strength_6m = pct_change

Rank: 2

精华
0
UID
173393
积分
107
帖子
89
主题
10
阅读权限
30
注册时间
2013-10-9
最后登录
2023-4-4
2#
发表于 2016-2-22 15:31:29 |只看该作者
你这个代码都不是TB的,怎么回测的?

使用道具 举报

Rank: 4

精华
0
UID
88963
积分
269
帖子
208
主题
16
阅读权限
50
注册时间
2011-12-25
最后登录
2022-12-9
3#
发表于 2016-2-26 08:37:26 |只看该作者
本帖最后由 bahuang 于 2016-2-26 08:40 编辑

这里也有人用python

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

bottom

静态版|手机版|联系我们|交易开拓者 ( 粤ICP备07044698   

GMT+8, 2024-5-10 06:26

Powered by Discuz! X2 LicensedChrome插件扩展

© 2011-2012 交易开拓者 Inc.

回顶部