Esempio n. 1
0
def generate_signal(context, code_list, stocks_info, lock, round_):
    """
    生成策略信号
    :param round_:
    :param lock:
    :param context:
    :param code_list:
    :param stocks_info:
    :return:
    """

    short_code_list = code_list.apply(
        dataUtil.get_short_code)  # 把 sh.600000 替换成  sh600000
    # 获取当前价格
    current_data_df = dataUtil.get_current_data(short_code_list, lock)
    # 遍历每个小组内的code
    for code in zip(code_list):
        code = code[0]
        # 获取历史价格
        history_data = baoStockUtil.attribute_history(context, code, 90)
        try:
            # 填充基本信息
            current_data = dataUtil.update_base_info(stocks_info,
                                                     current_data_df, code)
        except KeyError:
            logger.error("未查询到" + code + "今日的实时价格")
            continue

        # 执行策略
        w_shape.strategy_w_shape(code, current_data, history_data, round_)
Esempio n. 2
0
def generate_signal(context, all_code_list, stocks_info):
    """
    生成每只股票的策略信号
    """
    # 将全部code按每组chunk_len个进行分组
    chunk_len = 50
    for code_list in np.array_split(all_code_list,
                                    len(all_code_list) / chunk_len + 1):
        short_code_list = code_list.apply(
            baoStockUtil.get_short_code)  # 把 sh.600000 替换成  sh600000
        # 获取当前价格
        current_data_df = baoStockUtil.get_current_data(short_code_list)
        # 遍历每个小组内的code
        for code in zip(code_list):
            code = code[0]
            # 获取历史价格
            history_data = baoStockUtil.attribute_history(context, code, 30)
            try:
                # 更新基本信息
                current_data = baoStockUtil.update_base_info(
                    stocks_info, current_data_df, code)
            except KeyError:
                logger.error("未查询到" + code + "今日的实时价格")
                continue

            # 执行策略
            doubleLine.strategy_ma30(code, stocks_info, current_data,
                                     history_data)
            doubleLine.strategy_ma8(code, stocks_info, current_data,
                                    history_data)
Esempio n. 3
0
def run():
    plt_df = pd.DataFrame(index=pd.to_datetime(
        context.date_range).strftime('%Y-%m-%d'),
                          columns=['mean'])

    # 均线时间(天)
    g.date = 20
    # 布林带宽度
    g.k = 1.5

    for cursor_date in context.date_range:
        # 计算游标日期
        context.cursor_date = dateutil.parser.parse(cursor_date)

        sr = baoStockUtil.attribute_history(context, g.security,
                                            g.date)['close']
        # 均线
        ma = sr.mean()
        # 上线
        up = ma + g.k * sr.std()
        # 下线
        down = ma - g.k * sr.std()
        # 当前价格
        price = baoStockUtil.get_today_data(context, g.security)['open']

        plt_df.loc[cursor_date, 'mean'] = ma
        plt_df.loc[cursor_date, 'price'] = price
        plt_df.loc[cursor_date, 'up'] = up
        plt_df.loc[cursor_date, 'down'] = down

    # 画图
    plt_df[['price', 'up', 'down']].plot()
    plt.show()
Esempio n. 4
0
def handle_data(context):
    # 双均线策略

    hist = baoStockUtil.attribute_history(context, g.security, g.p2)
    ma5 = hist['close'][-g.p1:].mean()
    ma60 = hist['close'].mean()

    if ma5 > ma60 and g.security not in context.positions:
        orderUtil.order_value(context, g.security, context.cash)
    elif ma5 < ma60 and g.security in context.positions:
        orderUtil.order_target(context, g.security, 0)
Esempio n. 5
0
def handle_data(context, data=None):
    sr = baoStockUtil.attribute_history(context, g.security, g.M)['close']
    # 均线
    ma = sr.mean()
    # 上线
    up = ma + g.k * sr.std()
    # 下线
    down = ma - g.k * sr.std()
    # 当前价格
    p = baoStockUtil.get_today_data(context, g.security)['open']
    # 可用金额
    cash = context.cash
    cursor_date = context.cursor_date.strftime("%Y-%m-%d")
    if p < down and g.security not in context.positions:
        print("%s价格%s低于支撑线,进行买入" % (cursor_date, p))
        orderUtil.order_value(context, g.security, cash)
    elif p > up and g.security in context.positions:
        print("%s价格%s高于压力线,进行卖出" % (cursor_date, p))
        orderUtil.order_target(context, g.security, 0)
Esempio n. 6
0
def run():
    min_float_per = 20  # 触底条件,大于最小值的百分之多少
    rise_per_condition = 8  # 涨幅条件

    # sample_stocks = historyUtil.get_sample_stocks('all')['code']
    sample_stocks = baoStockUtil.get_sample_stocks('sz50')['code']
    # sample_stocks = historyUtil.get_sample_stocks('zz500')['code']

    for value in zip(sample_stocks):
        security = value[0]
        # print("\n开始解析"+security)
        plt_df = baoStockUtil.attribute_history(context, security, 180)

        min_price = plt_df['close'].min()
        mean_price = plt_df['close'].mean()
        max_price = plt_df['close'].max()
        yesterday_open = plt_df['open'][-1]
        yesterday_close = plt_df['close'][-1]

        rise_flag = False
        min_flag = False
        #涨跌幅
        pctChg = plt_df['pctChg'][-1]
        if pctChg >= rise_per_condition:
            rise_flag = True

        min_per = (yesterday_open - min_price) / min_price * 100  # 昨日价格与最低价的比值

        if min_per < min_float_per:
            min_flag = True

        if rise_flag & min_flag:
            print('%s符合条件' % security)
            print("从%s开始,最小值:%f,最大值:%f,平均值:%f" % (START_DATE,min_price, max_price, mean_price))
            print("昨日开盘:%f,昨日收盘:%f,涨幅:%s" % (yesterday_open, yesterday_close, mainUtil.parse_percent(pctChg)))

    print("分析完成")