Esempio n. 1
0
def get_tick_data(stocks):

    for stock in stocks:
        stock_code = stock["stock_code"][:6]
        url = "http://push2ex.eastmoney.com/getStockFenShi?pagesize=14400&ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wzfscj&pageindex=0&id=3007122&sort=1&ft=1&code={}&market={}&_=1589466838125"

        if stock_code.startswith("6"):
            url = url.format(stock_code, 1)
        else:
            url = url.format(stock_code, 0)

        data = json.loads(requests.get(url).text)["data"]["data"]

        _count_price = 0
        _count_amount = 0
        for _data in data:
            price = int(_data["p"])
            amount = int(_data["v"])
            _count_price += price * amount
            _count_amount += amount

        _price = scale_util.round_up((_count_price / _count_amount) / 1000, 2)
        _close_price = int(data[-1]["p"]) / 1000
        _profit = scale_util.round_up((_close_price - _price) / _price, 2)
        print(stock["stock_name"], " 平均成交价格/收盘价/盈利率:", _price, _close_price,
              _profit)
def get_stock_data(time):
    # 查询最近2板股票
    sql = "SELECT stock_code,stock_name,create_date,current_price from stock_limit_up_analyse WHERE continuous_time = " + time + " AND stock_name not like '%ST%' and create_date < '" + limit_date + "' "
    pool = mysql_pool.sql_pool()
    data = pool.selectMany(sql)
    writebook = xlwt.Workbook()  # 打开excel
    test = writebook.add_sheet('two_time_stock')

    # 定义时间格式
    dateFormat = xlwt.XFStyle()
    dateFormat.num_format_str = 'yyyy-mm-dd'

    # 定义表头
    test.write(0, 0, "股票代码")
    test.write(0, 1, "股票名称")
    test.write(0, 2, "涨停时间")
    test.write(0, 3, "1进2竞价量")
    test.write(0, 4, "2进3竞价量")
    test.write(0, 5, "一字板(1是2否)")
    test.write(0, 6, "开盘幅度")
    test.write(0, 7, "最高幅度")
    test.write(0, 8, "最低幅度")
    test.write(0, 9, "收盘幅度")
    test.write(0, 10, "3板成功(1是2否)")

    index = 1
    for stock in data:
        try:
            test.write(index, 0, stock["stock_code"])
            test.write(index, 1, stock["stock_name"])
            test.write(index, 2, stock["create_date"], dateFormat)
            # 查询1进2 竞价量
            two_volume = Tushare.get_tick_data(
                stock["stock_code"][0:6], str(stock["create_date"])).iloc[0, 3]
            test.write(index, 3, int(two_volume))

            # 查询下个交易日
            sql = "select create_date from stock_limit_up_analyse where create_date > '" + str(
                stock["create_date"]
            ) + "' group by create_date order by create_date limit 1"
            next_date = str(pool.selectOne(sql)["create_date"])
            # 查询2进3 竞价信息
            three_data = Tushare.get_tick_data(stock["stock_code"][0:6],
                                               next_date)
            # 查询2进3 竞价量
            three_volume = three_data.iloc[0, 3]
            test.write(index, 4, int(three_volume))

            limit_up_price = scale_util.round_up(
                (float(stock["current_price"]) * 1.1), 2)
            # 获取2进3开盘价
            three_open_price = three_data.iloc[0, 1]

            if limit_up_price == three_open_price:
                test.write(index, 5, 1)
            else:
                test.write(index, 5, 2)

            # 获取2进3 价格信息
            day_data = ts.get_hist_data(stock["stock_code"][0:6],
                                        start=next_date,
                                        end=next_date)

            three_open_price = day_data.iloc[0, 0]
            three_max_price = day_data.iloc[0, 1]
            three_close_price = day_data.iloc[0, 2]
            three_min_price = day_data.iloc[0, 3]
            last_price = float(stock["current_price"])
            test.write(
                index, 6,
                scale_util.round_up(
                    ((three_open_price - last_price) / last_price), 2))
            test.write(
                index, 7,
                scale_util.round_up(
                    ((three_max_price - last_price) / last_price), 2))
            test.write(
                index, 8,
                scale_util.round_up(
                    ((three_min_price - last_price) / last_price), 2))
            test.write(
                index, 9,
                scale_util.round_up(
                    ((three_close_price - last_price) / last_price), 2))
            if limit_up_price == three_close_price:
                test.write(index, 10, 1)
            else:
                test.write(index, 10, 2)
        except Exception as e:
            print("异常信息---->", stock)
            traceback.print_exc()
        index += 1
    writebook.save('stock.xls')
Esempio n. 3
0
def get_diff_range_rate_photo():

    #按照开盘价分类  最高价卖出盈利分类
    sql = "SELECT ((two_open_price - one_close_price ) / one_close_price ) open_rate,((two_min_price - two_open_price) / two_open_price ) range_rate  from stock_limit_up_statistics  WHERE two_open_price != two_limit_up_price HAVING open_rate >= -0.1 AND open_rate <= 0.1 ORDER BY open_rate"
    datas = pool.selectMany(sql)

    open_rate_list = {}
    range_rate_list = {}
    for data in datas:
        open_rate = scale_util.round_up(float(data["open_rate"]), 2)
        range_rate = scale_util.round_up(float(data["range_rate"]), 2)

        open_rate_count = open_rate_list.get(open_rate)
        range_rate_sum = range_rate_list.get(open_rate)
        if (open_rate_count == None):
            open_rate_list[open_rate] = 1
            range_rate_list[open_rate] = range_rate
        else:
            open_rate_list[open_rate] = open_rate_count + 1
            range_rate_list[open_rate] = range_rate_sum + range_rate

    positions = np.arange(len(open_rate_list.keys()))
    # 区间
    section = list(open_rate_list.keys())
    # 个数
    count = list(open_rate_list.values())
    # 幅度
    profit = list(range_rate_list.values())

    temp = []
    for i in range(len(profit)):
        temp.append(scale_util.round_up(profit[i] / count[i], 4) * 100)

    profit = temp

    fig, ax1 = plt.subplots()

    # 成绩直方图
    ax1.bar(positions,
            count,
            width=0.5,
            align='center',
            color='r',
            label=u"count")
    ax1.set_xticks(positions)
    ax1.set_xticklabels(section, rotation=90)
    # ax1.set_xlim(-0.1,0.1)
    ax1.set_xlabel("section")
    ax1.set_ylabel("count")
    max_score = max(count)
    ax1.set_ylim(0, int(max_score * 1.2))
    # 成绩标签
    for x, y in zip(positions, count):
        ax1.text(x,
                 y + max_score * 0.02,
                 y,
                 ha='center',
                 va='center',
                 fontsize=13)

    # 变动折线图
    ax2 = ax1.twinx()
    ax2.plot(positions, profit, 'o-', label=u"profit")
    max_proges = max(profit)
    min_proges = min(profit)
    # 变化率标签
    for x, y in zip(positions, profit):
        ax2.text(x,
                 y + max_proges * 0.02, ('%.1f%%' % y),
                 ha='center',
                 va='bottom',
                 fontsize=13)
    # 设置纵轴格式
    ax2.set_ylim(int(min_proges * 1.2), int(max_proges * 1.2))
    ax2.set_ylabel(u"profit")

    # 图例
    handles1, labels1 = ax1.get_legend_handles_labels()
    handles2, labels2 = ax2.get_legend_handles_labels()
    plt.legend(handles1 + handles2, labels1 + labels2, loc='upper right')

    plt.show()