Beispiel #1
0
def algo(context):
    global candidate_stock, selected_stock
    date_now = context.now.strftime('%Y-%m-%d')
    date_previous = get_trading_date_from_now(date_now, -1,
                                              ql.Days)  # 前一个交易日,用于获取因子数据的日期
    print(date_now + '日回测程序执行中...')
    if date_now not in trading_date_list:  # 非调仓日
        pass  # 预留非调仓日的微调空间
    else:  # 调仓日执行算法
        # 根据指数获取股票候选池的代码
        code_list = SelectedStockPoolFromListV1(
            INCLUDED_INDEX, EXCLUDED_INDEX, date_previous).get_stock_pool()
        strategy = STRATEGY(code_list, date_previous)
        candidate_stock = strategy.select_code()  # 调仓日定期调节候选的股票池更新,非调仓日使用旧股票池
    sw1_industry = get_SW1_industry(date_now, candidate_stock)
    industry_wm_result = industry_wheel_movement[date_now]
    candidate_selected_stock = [
        stock for stock in candidate_stock if sw1_industry[stock] is not None
        and industry_wm_result[sw1_industry[stock]] == 1
    ]  # 忽略无行业信息的股票并根据行业择时信号选择候选股票

    if candidate_selected_stock == selected_stock:  # 候选股状态与之前一样,不用任何操作
        pass
    else:
        selected_stock = candidate_selected_stock  # 更新已持股池的信息
        if candidate_selected_stock == []:  # 空仓信号
            stock_dict[date_now] = {}
        else:
            candidate_selected_stock = list_wind2jq(candidate_selected_stock)
            stock_now = WEIGHTS(candidate_selected_stock,
                                date_previous).get_weights()
            stock_dict[date_now] = stock_now
def algo(context):
    global position_now, position_target
    date_now = context.now.strftime('%Y-%m-%d')
    date_previous = get_trading_date_from_now(date_now, -1,
                                              ql.Days)  # 前一个交易日,用于获取因子数据的日期
    select_time_value = select_time_model[date_now]  # 择时信号计算
    print(date_now + ('日回测程序执行中...,择时值:%.2f' % select_time_value))

    if date_now not in trading_date_list:  # 非调仓日
        pass
    else:  # 调仓日执行算法,更新position_target
        position_now = False  # 虚拟上,调仓日需要提前清仓
        stock_dict[date_now] = {}
        # 根据指数获取股票候选池的代码
        code_list = SelectedStockPoolFromListV1(
            INCLUDED_INDEX, EXCLUDED_INDEX, date_previous).get_stock_pool()
        strategy = STRATEGY(code_list, date_previous, 0.9)
        select_code_list = list_wind2jq(strategy.select_code())
        if len(select_code_list) > 0:  # 有可选股票时记录下可选股票
            stock_now = WEIGHTS(select_code_list, date_previous).get_weights()
            position_target = stock_now
        else:
            position_target = {}

    # LLT择时判定
    if select_time_value >= 0 and not position_now and position_target != {}:  # LLT择时信号为正,空仓且有目标持仓状态
        stock_dict[date_now] = position_target
        position_now = True
    elif select_time_value < 0 and position_now and position_target != {}:  # LLT择时信号为负且持仓状态:
        stock_dict[date_now] = {}
        position_now = False
Beispiel #3
0
def algo(context):
    date_now = context.now.strftime('%Y-%m-%d')
    date_previous = get_trading_date_from_now(date_now, -1,
                                              ql.Days)  # 前一个交易日,用于获取因子数据的日期
    if date_now not in trading_date_list:  # 非调仓日
        pass  # 预留非调仓日的微调空间
    else:  # 调仓日执行算法
        print(date_now + '日回测程序执行中...')
        try:
            code_list = list_gm2wind(
                list(
                    get_history_constituents(
                        INDEX,
                        start_date=date_previous,
                        end_date=date_previous)[0]['constituents'].keys()))
        except IndexError:
            code_list = w.wset("sectorconstituent", "date=" + date_previous +
                               ";windcode=" + INDEX).Data[1]
        strategy = STRATEGY(code_list, date_previous)
        select_code_list = list_wind2jq(strategy.select_code())
        if len(select_code_list) > 0:  # 有可选股票时选取合适的股票
            stock_now = {}
            for code in select_code_list:
                stock_now[code] = 1.0 / len(select_code_list)
            stock_dict[date_now] = stock_now
        else:
            stock_dict[date_now] = {}
Beispiel #4
0
def algo(context):
    date_now = context.now.strftime('%Y-%m-%d')
    date_previous = get_trading_date_from_now(date_now, -1, ql.Days)  # 前一个交易日,用于获取因子数据的日期
    if date_now not in trading_date_list:  # 非调仓日
        pass  # 预留非调仓日的微调空间
    else:  # 调仓日执行算法
        print(date_now+'日回测程序执行中...')
        # 根据指数获取股票候选池的代码
        code_list = SelectedStockPoolFromListV1(INCLUDED_INDEX, EXCLUDED_INDEX, date_previous).get_stock_pool()
        strategy = STRATEGY(code_list, date_previous, 0.9)
        select_code_list = list_wind2jq(strategy.select_code())
        if len(select_code_list) > 0:  # 有可选股票时选取合适的股票
            stock_now = WEIGHTS(select_code_list, date_previous).get_weights()
            stock_dict[date_now] = stock_now
        else:
            stock_dict[date_now] = {}
Beispiel #5
0
def algo(context):
    global position_now, position_target
    date_now = context.now.strftime('%Y-%m-%d')
    date_previous = get_trading_date_from_now(date_now, -1,
                                              ql.Days)  # 前一个交易日,用于获取因子数据的日期
    select_time_value = select_time_model[date_now]  # 择时信号计算

    if date_now not in trading_date_list:  # 非调仓日
        pass
    else:  # 调仓日执行算法,更新position_target
        position_now = False  # 虚拟上,调仓日需要提前清仓
        stock_dict[date_now] = {}
        # 根据指数获取股票候选池的代码
        all_code_set = set()
        for index in INDEX:
            code_set = set(
                w.wset("sectorconstituent",
                       "date=" + date_previous + ";windcode=" + index).Data[1])
            all_code_set = all_code_set | code_set
        code_list = list(all_code_set)  # 股票候选池
        strategy = STRATEGY(code_list, date_previous, 0.9)
        select_code_list = list_wind2jq(strategy.select_code())
        if len(select_code_list) > 0:  # 有可选股票时记录下可选股票
            stock_now = WEIGHTS(select_code_list, date_previous).get_weights()
            position_target = stock_now
        else:
            position_target = {}

    # 择时判定
    if select_time_value >= 0 and not position_now and position_target != {}:  # LLT择时信号为正,空仓且有目标持仓状态
        stock_dict[date_now] = position_target
        position_now = True
    elif select_time_value < 0 and position_now and position_target != {}:  # LLT择时信号为负且持仓状态:
        stock_dict[date_now] = {}
        position_now = False

    # 打印择时信号和仓位配置信息
    print(date_now + ('日回测程序执行中...,择时值:%.2f' % select_time_value))
    print(date_now + '日回测程序执行中...,选股:')
    print(position_target)