策略运行之后啥也没发生,希望能看到回测数据

策略分享
用户成长系列
新手专区
标签: #<Tag:0x00007fb11bc42830> #<Tag:0x00007fb11bc426f0> #<Tag:0x00007fb11bc42588>

(cflk) #1
克隆策略
In [10]:
def initialize(context):
    set_commission(PerTrade(buy_cost=0.0003, sell_cost=0.0013, min_cost=5))
    set_option('use_real_price', True)
    g.buy_stock_count = 3
    
def handle_data(context, data):
    hour = context.current_dt.hour
    minute = context.current_dt.minute
    if hour == 14 and minute == 40:
        buy_stocks = select_stocks(context,data)
        adjust_position(context,buy_stocks)
        adjust_position2(context,buy_stocks)
        
def filter_paused_and_st_stock(stock_list):
    current_data = get_current_data()
    return [stock for stock in stock_list if not current_data[stock].paused 
    and not current_data[stock].is_st and '*ST' not in current_data[stock].
    name and '*' not in current_data[stock].name and '退' not in current_data[stock].name]

'''
def filter_limit_stock(context, stock_list):
    tmpList = []
    last_prices = history(1, '1m', 'close', security_list=stock_list)
    curr_data = get_current_data()
    for stock in stock_list:
        # 未涨停,也未跌停
        if curr_data[stock].low_limit < last_prices[stock][-1] < curr_data[stock].high_limit:
            tmpList.append(stock)
    return tmpList
'''
def filter_limit_stock(context, data, stock_list):
    tmpList = []
    curr_data = get_current_data()
    for stock in stock_list:
        # 未涨停,也未跌停
        if curr_data[stock].low_limit < data[stock].close < curr_data[stock].high_limit:
            tmpList.append(stock)
    return tmpList

def select_stocks(context,data):
    # 选取流通市值小于100亿的100只股票
    q = query(valuation.code, valuation.circulating_market_cap).order_by(
            valuation.circulating_market_cap.asc()).filter(
            valuation.circulating_market_cap <= 100).limit(100)
    df = get_fundamentals(q)
    stock_list = list(df['code'])
    
    # 过滤掉停牌的和ST的
    stock_list = filter_paused_and_st_stock(stock_list)
    # 过滤掉现在涨停或者跌停的
    # stock_list = filter_limit_stock(context, stock_list)
    stock_list = filter_limit_stock(context, data, stock_list)
    # 选取前N只股票放入“目标池”
    stock_list = stock_list[:g.buy_stock_count]  
    return stock_list
 
def adjust_position(context,buy_stocks):
    # 现持仓的股票,如果不在“目标池”中,且未涨停,就卖出
    if len(context.portfolio.positions)>0:
        last_prices = history(1, '1m', 'close', security_list=list(context.portfolio.positions.keys()))
        for stock in list(context.portfolio.positions.keys()):
            if stock not in buy_stocks:
                curr_data = get_current_data()
                if last_prices[stock][-1] < curr_data[stock].high_limit:
                    order_target_value(stock, 0)
                    
def adjust_position2(context,buy_stocks):
    # 依次买入“目标池”中的股票            
    for stock in buy_stocks:
        position_count = len(context.portfolio.positions)
        if g.buy_stock_count > position_count:
            value = context.portfolio.cash / (g.buy_stock_count - position_count)
            if context.portfolio.positions[stock].total_amount == 0:
                order_target_value(stock, value)

这个代码写完之后执行了一下,什么也没发生,需要怎么改才能看到回测?

(iQuant) #2

您好,可以参考一下样例模板策略,新建代码策略就会生成,数据等接口可以参考文档板块。