Пример #1
0
def get_stock_list(x_position):
    stock_list = select(StockIO.get_stock('sha'), x_position=x_position, kline_type=StockConfig.kline_type_day)
    stock_list += select(StockIO.get_stock('sza'), x_position=x_position, kline_type=StockConfig.kline_type_day)
    stock_list = sorted(stock_list, key=lambda stock: stock.vb, reverse=True)
    return stock_list[:40]
Пример #2
0
    # 该选股方法 每天要过滤的形态:
    for x in range(-1, 0):
        position = x
        date = position
        # 过滤重复的
        stock_code_list = []
        with open('{}/{}'.format(StockConfig.path_track, 'zixuan.txt'),
                  'r',
                  encoding='utf-8') as f:
            lines = f.readlines()
            for line in lines:
                if not line.startswith("#") and not '\n' == line:
                    data = line.strip('\n')
                    stock_code_list.append(data)

        new_list = select(StockIO.get_stock('week_tendency'),
                          x_position=position,
                          kline_type=StockConfig.kline_type_day)
        # 新结果
        result_list = [
            x.stock_code for x in new_list
            if x.stock_code not in stock_code_list
        ]
        with open('{}/{}.txt'.format(StockConfig.path_track, 'zixuan'),
                  mode='a',
                  encoding='utf-8') as f:
            f.write('#{}\n'.format(date))
            for stock_code in result_list:
                f.write("{}\n".format(stock_code))

        with open('{}/{}.txt'.format(StockConfig.path_track, 'today'),
Пример #3
0
def track():
    """
    :param targets: 需要追踪的目标集合
    @:param code:
    @:param price:
    :return:
    """
    # 读取本地数据
    track_data = get_track_data()
    kline_map = StockIO.get_kline_map(track_data[:, 0],
                                      StockConfig.kline_type_day)

    page = 0
    pageNum = 40
    result = []
    code_list = track_data[:, 0]
    while page * pageNum < len(code_list):
        from_position = page * pageNum
        to_position = min((page + 1) * pageNum, len(code_list))
        data = code_list[from_position:to_position]
        cur_track_data = track_data[from_position:to_position]
        page += 1
        # 请求网络数据
        codes = ''
        for code in data:
            if code.startswith('6'):
                code = '0' + code
            else:
                code = '1' + code
            codes = codes + code + ','

        session = requests.Session()
        session.trust_env = False
        url = 'http://api.money.126.net/data/feed/{}'.format(codes)[:-1]
        print(url)
        r = session.get(url)
        quote = json.loads(r.text[len('_ntes_quote_callback('):-2])

        # 分析数据
        for target in cur_track_data:
            target_code = target[0]
            target_sma_down = 0 if target[2].strip() == '' else int(target[2])
            target_sma_up = 0 if target[3].strip() == '' else int(target[3])
            target_price_down = 0 if target[4].strip() == '' else float(
                target[4])
            target_price_up = 0 if target[5].strip() == '' else float(
                target[5])
            target_chg_down = 0 if target[6].strip() == '' else float(
                target[6])
            target_chg_up = 0 if target[7].strip() == '' else float(target[7])
            if target_code.startswith('6'):
                target_code = '0' + target_code
            else:
                target_code = '1' + target_code
            new_date = datetime.datetime.strptime(
                quote[target_code]['time'],
                '%Y/%m/%d %H:%M:%S').strftime('%Y-%m-%d')
            kline = kline_map[target_code[1:]]
            old_kline_item = kline[-1]
            new_kline_item = np.array([
                new_date, quote[target_code]['open'], old_kline_item[-3],
                quote[target_code]['high'], quote[target_code]['low'],
                quote[target_code]['volume']
            ])
            if (new_kline_item[-1] == old_kline_item[-1]):
                kline[-1] = new_kline_item
            else:
                kline = np.row_stack((kline, new_kline_item))

            #print(kline[-1])

            # 跌破目标均线
            if target_sma_down != 0:
                message = ''
                sma, = StockIndicator.sma(kline, abs(target_sma_down))
                price = quote[target_code]['price']
                if (price < sma[-1]):
                    print(sma[-1], price)
                    print(r.text)
                    message = target_code[1:] + '跌破{}日线:'.format(
                        abs(target_sma_down))
                if message != '':
                    show_dialog("tips", message)

            # 突破目标均线
            if target_sma_up != 0:
                message = ''
                sma, = StockIndicator.sma(kline, abs(target_sma_up))
                price = quote[target_code]['low']
                if (price > sma[-1]):
                    message = target_code[1:] + '突破{}日线:'.format(
                        abs(target_sma_up))

                if message != '':
                    show_dialog("tips", message)

            # 跌到目标价位
            if target_price_down != 0:
                message = ''
                cur_price = float(quote[target_code]['price'])
                message = ''
                if cur_price <= abs(target_price_down):
                    message = target_code[1:] + '跌到目标价位:' + str(
                        abs(target_price_down))
                if message != '':
                    show_dialog("tips", message)

            # 涨到目标价位
            if target_price_up != 0:
                message = ''
                cur_price = float(quote[target_code]['price'])
                message = ''
                if cur_price >= abs(target_price_up):
                    message = target_code[1:] + '涨到目标价位:' + str(
                        abs(target_price_up))
                if message != '':
                    show_dialog("tips", message)

            # 每日目标跌幅
            if target_chg_down != 0:
                p_close = float(quote[target_code]['yestclose'])
                low = float(quote[target_code]['low'])
                chg = np.round(((low - p_close) / p_close * 100), decimals=2)
                if chg < target_chg_down:
                    message = target_code[1:] + '跌到目标跌幅:' + str(chg)
                    if message != '':
                        show_dialog("tips", message)

            if target_chg_up != 0:
                p_close = float(quote[target_code]['yestclose'])
                high = float(quote[target_code]['high'])
                chg = np.round(((high - p_close) / p_close * 100), decimals=2)
                if chg > target_chg_up:
                    message = target_code[1:] + '涨到目标涨幅:' + str(chg)
                    if message != '':
                        show_dialog("tips", message)
            # 每日目标涨幅

            # 默认会跟踪自动筛选出来的 sma变化
            # if target_sma_down == 0 and target_price_down == 0 and target_sma_up == 0 and target_price_up == 0:
            #     sma5, sma10, sma20, sma30 = StockIndicator.sma(kline, 5, 10, 20, 30)
            #     price = quote[target_code]['price']
            #     # # sma5 条件
            #     # if price < sma5[-1]:
            #     #    message = target_code[1:] + '跌破{}日线:'.format(5)
            #     # sma10 条件
            #     if price < sma20[-1]:
            #         message = target_code[1:] + '跌破{}日线:'.format(20)
            #     # sma20 条件
            #     # elif price < sma20[-1]:
            #     #     message = target_code[1:] + '跌破{}日线:'.format(20)
            #     else:
            #         message = ''
            #     if message != '':
            #         show_dialog("tips", message)
    return result
Пример #4
0
# -*-coding:utf-8 -*-
import StockIO
import StockConfig
import StockFilter
import StockIndicator
import numpy as np
from StockDownloader import save_stock

# 每日分析

# 1. 分析每日仍然保持了趋势的个股(两点: 一是新高底部, 二是最近新高靠近日线, 三是均线金叉)
stock_list = StockIO.get_stock('week_tendency')


def avg_up_trend(stock):
    kline = StockIO.get_kline(stock.stock_code, StockConfig.kline_type_day)
    sma5, sma10, sma20, sma60 = StockIndicator.sma(kline, 5, 10, 20, 60)
    if sma5[-1] > sma10[-1] > sma20[-1] and sma5[-1] > sma60[-1]:
        return True
    return False


StockIO.save_stock('today_trend.txt',
                   [stock for stock in stock_list if avg_up_trend(stock)])
Пример #5
0
    to_position = None if to_position == -1 else to_position
    close_b = kline[:, 2].astype(np.float)[from_position + 1:to_position]
    close_p = kline[:,
                    2].astype(np.float)[from_position:to_position -
                                        1 if to_position is not None else -1]
    return close_b - close_p, (close_b - close_p) / close_p * 100


def position(date, stock_code, kline_type=StockConfig.kline_type_day):
    kline = StockIO.get_kline(stock_code, kline_type)
    dates = kline[:, 0]
    length = dates.shape[0]
    if date in dates:
        index = np.argwhere(dates == date)[0][0] - length
        return index
    return None


if __name__ == '__main__':
    sma5, sma10 = sma(
        StockIO.get_kline('000001', kline_type=StockConfig.kline_type_day), 5,
        10)
    print(type(sma5))
    print(sma5)
    print(sma10)
    #
    # chg, chg_per = chg_per(get_kline('002040', kline_type_day), from_position=-4, to_position=-1)
    # print(chg)
    # print(chg_per)
    #
Пример #6
0
def track():
    """
    :param targets: 需要追踪的目标集合
    @:param code:
    @:param price:
    :return:
    """
    # 读取本地数据
    track_data = get_track_data('2_sma_track.txt')
    kline_map = StockIO.get_kline_map(track_data[:, 0],
                                      StockConfig.kline_type_day)

    page = 0
    pageNum = 40
    result = []
    code_list = track_data[:, 0]
    while page * pageNum < len(code_list):
        from_position = page * pageNum
        to_position = min((page + 1) * pageNum, len(code_list))
        data = code_list[from_position:to_position]
        cur_track_data = track_data[from_position:to_position]
        page += 1
        # 请求网络数据
        codes = ''
        for code in data:
            if code.startswith('6'):
                code = '0' + code
            else:
                code = '1' + code
            codes = codes + code + ','

        session = requests.Session()
        session.trust_env = False
        url = 'http://api.money.126.net/data/feed/{}'.format(codes)[:-1]
        print(url)
        r = session.get(url)
        quote = json.loads(r.text[len('_ntes_quote_callback('):-2])

        # 分析数据
        for target in cur_track_data:
            target_code = target[0]
            target_sma = 0 if target[2].strip() == '' else int(target[2])
            target_price = 0 if target[3].strip() == '' else float(target[3])
            if target_code.startswith('6'):
                target_code = '0' + target_code
            else:
                target_code = '1' + target_code
            new_date = datetime.datetime.strptime(
                quote[target_code]['time'],
                '%Y/%m/%d %H:%M:%S').strftime('%Y-%m-%d')
            kline = kline_map[target_code[1:]]
            old_kline_item = kline[-1]
            new_kline_item = np.array([
                new_date, quote[target_code]['open'], old_kline_item[-3],
                quote[target_code]['high'], quote[target_code]['low'],
                quote[target_code]['volume']
            ])
            if (new_kline_item[-1] == old_kline_item[-1]):
                kline[-1] = new_kline_item
            else:
                kline = np.row_stack((kline, new_kline_item))

            #print(kline[-1])

            # sma track
            if target_sma != 0:
                sma, = StockIndicator.sma(kline, target_sma)
                price = quote[target_code]['low']
                if (price < sma[-1]):
                    message = target_code[1:] + '跌破{}日线:'.format(target_sma)
                elif (price < sma[-1]):
                    message = target_code[1:] + '跌破{}日线:'.format(target_sma)
                else:
                    message = ''
                if message != '':
                    messagebox.showinfo("tips", message)

            # price track
            if target_price != 0:
                cur_price = float(quote[target_code]['price'])
                message = ''
                if target_price < 0 and cur_price <= abs(target_price):
                    message = target_code[1:] + '跌到目标价位:' + str(
                        abs(target_price))
                elif target_price > 0 and cur_price >= abs(target_price):
                    message = target_code[1:] + '涨到目标价位:' + str(
                        abs(target_price))

                if message != '':
                    messagebox.showinfo("tips", message)

            # 默认会跟踪自动筛选出来的 sma变化
            if target_price == 0 and target_sma == 0:
                sma5, sma10, sma20, sma30 = StockIndicator.sma(
                    kline, 5, 10, 20, 30)
                price = quote[target_code]['low']
                # sma5 条件
                # if (price < sma5[-1] and sma5[-1] > sma10[-1] > sma20[-1] > sma30[-1]):
                #     message = target_code[1:] + '跌破{}日线:'.format(sma5[-1])
                # sma10 条件
                if (price < sma10[-1] and sma10[-1] > sma20[-1] > sma30[-1]):
                    message = target_code[1:] + '跌破{}日线:'.format(10)
                # sma20 条件
                elif (price < sma20[-1] and sma5[-1] > sma20[-1]
                      and sma10[-1] > sma20[-1]) and sma20[-1] > sma30[-1]:
                    message = target_code[1:] + '跌破{}日线:'.format(20)
                else:
                    message = ''
                if message != '':
                    messagebox.showinfo("tips", message)

            # sma 自由追踪
            # else:
            #     sma5, sma10, sma20 = StockIndicator.sma(kline, 5, 10, 20)
            #     price = quote[target_code]['low']
            #     if ((price < sma10[-1] or price < sma10[-2]) and sma10[-1] > sma20[-1]):
            #         message = target_code[1:] + '跌破10日线:'
            #     elif ((price < sma20[-1] or price < sma20[-2]) and sma20[-1] > sma10[-1]):
            #         message = target_code[1:] + '跌破20日线:'
            #     else:
            #         message = ''
            #     if message != '':
            #         messagebox.showinfo("tips", message)

            # # analysis
            # if cur_price >= price_uts or cur_price <= price_dtb:
            #     result.append([target[0], cur_price, price_uts, price_dtb, 's' if cur_price >= price_uts else 'b', datetime.datetime.now().strftime('%H:%m')])
    return result
Пример #7
0
    stock_list_2 = StockIO.get_stock('sza')
    stock_list = stock_list_1 + stock_list_2
    for stock in stock_list:
        try:
            kline = StockIO.get_kline(stock.stock_code,
                                      kline_type=StockConfig.kline_type_week)
            close = kline[:, 2].astype(np.float)
            sma5, sma10, sma20, sma60 = StockIndicator.sma(
                kline, 5, 10, 20, 60)
        except Exception as e:
            print(e)
            continue
        if kline.shape[0] < 41:
            continue

        if sma5[-2] > sma10[-2] and sma5[-2] > sma20[-2]:
            stock_result.append(stock)
    return stock_result


if __name__ == '__main__':
    old_stock_list = StockIO.get_stock('week_tendency')
    stock_list = week_tendency2()

    # new_add_stock = [stock for stock in stock_list if stock not in old_stock_list]
    # new_reduce_stock = [stock for stock in old_stock_list if stock not in stock_list]
    # print('new_add_stock: ', new_add_stock)
    # print('new_reduce_stock: ', new_reduce_stock)
    #old_stock_list = StockIO.get_stock('')
    write_stock_pool('week_tendency', stock_list)
Пример #8
0
        if StockShape.is_lower_shadow(open_[x_position], close[x_position], high[x_position], low[x_position], min_vb=min_vb, ratio=ratio, red=False):
            if not StockAlgrithm.sumOfSubArray(chg[-15:])[0] > 20:

                print(stock)
                result.append(stock)
                # append to file
                path = '{root}/{name}'.format(root=StockConfig.path_track, name=append_file)
                with open(path, mode='a', encoding='utf-8') as f:
                    value = low[-1] + (high[-1] - low[-1]) * 0.3
                    f.write("{},{},{}\n".format(stock.stock_code, value, high[-1]))

    return result


if __name__ == '__main__':
    date = '2017-02-03'
    position = StockIndicator.position(date, '000001')
    result = {}
    for x in range(-4, 0):
        print('x = ', x)
        stock_list = select(StockIO.get_stock('level_1'), x_position=x, kline_type=StockConfig.kline_type_day, min_vb=6, ratio=0.3)

        print(stock_list)
        for stock in stock_list:
            result[stock] = result.get(stock, 0) + 1

    print(sorted(result.items(), key=lambda d: d[1], reverse=True))


    #print(down_to(StockIO.get_stock('sha'), duration=60))
Пример #9
0
    stock_list = StockIO.get_stock('sha')
    result = []
    for stock in stock_list:
        kline = StockIO.get_kline(stock.stock_code, StockConfig.kline_type_day)
        from_position = x_position - 4
        to_position = x_position + 1 if x_position != -1 else None
        open = kline[:, 1].astype(np.float)[from_position:to_position + 1]
        close = kline[:, 2].astype(np.float)[from_position:to_position]
        sma5 = StockIndicator.sma(kline, 5)[0][from_position:to_position]
        if kline.shape[0] > min_item:
            chg, chg_per = StockIndicator.chg_per(kline, from_position,
                                                  to_position)
            if chg[-4] > 0 and chg[-3] > 0 and chg[-2] < 0 and chg[-1] > 0:
                if close[-1] > close[-4] and chg_per[-1] < 3 and sma5[
                        -2] < close[-2] and open[-1] < close[-1]:
                    result.append(stock)
    return result


"""
todo: 观察不同position的后续变化, up down up  or up up
"""
from_date = '2016-11-03'
to_date = '2016-11-04'
stock_list = select_7(from_date)
StockIO.save_stock('s7_1_20170110',
                   stock_list=stock_list,
                   message='down to jx')
print(stock_list)
StockStatistics.stat_chg(stock_list, from_date=from_date, to_date=to_date)
Пример #10
0
                print(stock)

        # # 连续三天10日线飘红上升
        # if sma10_w[x_position] < open_week[x_position] < close_week[x_position]:
        #     if sma10_w[x_position - 1] < open_week[x_position] < close_week[x_position - 1]:
        #         if sma10_w[x_position - 2] < open_week[x_position - 2] < close_week[x_position - 2]:
        #             #if sma10_w[x_position - 2] > sma5_w[x_position - 2] and sma10_w[x_position] < sma5_w[x_position]:
        #                 result.append(stock)
        #                 print(stock)

    return result


def xia_ying_xian(kline_item):
    open = float(kline_item[1])
    close = float(kline_item[2])
    high = float(kline_item[3])
    low = float(kline_item[4])

    top = min(close, open)
    bottom = low
    if (top - bottom) / (top) * 100 > 6:
        return True


if __name__ == '__main__':
    # 均线处决胜负, 胜者向上,败者向下
    date = '2017-02-03'
    position = StockIndicator.position(date, '000001')
    print(trend_up_2(StockIO.get_stock('sha'), x_position=-5))
    #print(down_to(StockIO.get_stock('sha'), duration=60))
Пример #11
0
def trend_up_2(stock_list, x_position=-1, min_item=360):
    """
    当股价到达均线附近时, 要么调整, 要么突破
    :param stock_list:
    :param kline_type:
    :param avg:
    :return:
    """
    result = []
    for stock in stock_list:
        try:
            kline_day = StockIO.get_kline(
                stock.stock_code, kline_type=StockConfig.kline_type_day)
            kline_week = StockIO.get_kline(
                stock.stock_code, kline_type=StockConfig.kline_type_week)
        except:
            continue
        if kline_day.shape[0] < min_item:
            continue

        open_day = kline_day[:, 1].astype(np.float)
        open_week = kline_week[:, 1].astype(np.float)
        close_day = kline_day[:, 2].astype(np.float)
        close_week = kline_week[:, 2].astype(np.float)

        sma5_d, sma10_d, sma20_d = StockIndicator.sma(kline_day, 5, 10, 20)
        sma5_w, sma10_w, sma20_w = StockIndicator.sma(kline_week, 5, 10, 20)

        # if sma5_d[x_position] > sma5_d[x_position - 1]:
        #     if sma10_d[x_position] > sma10_d[x_position - 1]:
        #         if sma20_d[x_position] > sma20_d[x_position - 1]:
        #             if sma20_d[x_position] > sma5_d[x_position]:
        #                 if close_day[x_position] > sma5_d[x_position]:
        #                     result.append(stock)
        #                     print(stock)

        # if sma5_w[x_position] > sma10_w[x_position] > sma20_w[x_position]:
        #     if close_week[x_position] > sma5_w[x_position]:
        #         result.append(stock)
        #         print(stock)

        # if sma20_w[x_position - 1] > sma5_w[x_position - 1] and sma20_w[x_position] < sma5_w[x_position]:
        #     if sma5_w[x_position] > sma20_w[x_position] > sma10_w[x_position]:
        #         #if close_week[x_position] > sma10_w[x_position] > sma20_w[x_position]:
        #         result.append(stock)
        #         print(stock)

        # # 后置判断
        # if sma10_w[x_position - 1] > sma5_w[x_position - 1] and sma10_w[x_position] < sma5_w[x_position]:
        #     if sma20_w[x_position] > sma5_w[x_position] > sma10_w[x_position]:
        #         if sma10_w[x_position + 1] < open_week[x_position + 1] < close_week[x_position + 1]:
        #             result.append(stock)
        #             print(stock)

        # # 5日线穿过10日线
        if sma10_d[x_position - 1] > sma5_d[
                x_position - 1] and sma10_d[x_position] < sma5_d[x_position]:
            if sma5_d[x_position] > sma5_d[x_position - 1]:
                #if sma5_w[x_position - 2] < open_week[x_position - 2] < close_week[x_position - 2]:
                result.append(stock)
                print(stock)

        # # 连续三天10日线飘红上升
        # if sma10_w[x_position] < open_week[x_position] < close_week[x_position]:
        #     if sma10_w[x_position - 1] < open_week[x_position] < close_week[x_position - 1]:
        #         if sma10_w[x_position - 2] < open_week[x_position - 2] < close_week[x_position - 2]:
        #             #if sma10_w[x_position - 2] > sma5_w[x_position - 2] and sma10_w[x_position] < sma5_w[x_position]:
        #                 result.append(stock)
        #                 print(stock)

    return result
Пример #12
0
# for stock in stock_list:
#     kline = StockIO.get_kline(stock.stock_code, StockConfig.kline_type_week)
#     close = kline[:, 2].astype(np.float)
#     sma5 = StockIndicator.sma(kline, 5)
#     if close[-1] > sma5[0][-1]:
#         stock_result.append(stock)
#
# print(stock_result)
# StockReporter.query_report(stock_result)


# first step : filter stock and select target stock
@find_sma_up(StockConfig.kline_type_day, x_position=-1)
@find_kdj_jx(StockConfig.kline_type_week, x_position=-1, k_max=60)
def filter_stock(stock_list):
    """
    strategy:
    :param stock_list: buy in Tuesday, Monday for confirm up trend, Friday must quits if profited.
    :return:
    """
    return stock_list


stock_list = filter_stock(StockIO.get_stock('sha'))
print(stock_list)
StockReporter.query_report(stock_list)

# second step: track stock and select buy point

# third step: track stock and select sell point
Пример #13
0
            continue
        if kline.shape[0] < min_item:
            continue

        open = kline[:, 1].astype(np.float)
        close = kline[:, 2].astype(np.float)
        high = kline[:, 3].astype(np.float)
        low = kline[:, 4].astype(np.float)
        chg = StockIndicator.chg(kline)
        vb = StockIndicator.vibration(kline)
        entity = StockIndicator.entity(kline)
        sma5, sma10, sma20 = StockIndicator.sma(kline, 5, 10, 20)
        if entity[x_position] / abs(chg[x_position]) > 1.5 and entity[x_position] > 3:
                print(stock)
                result.append(stock)
    return result

if __name__ == '__main__':
    date = '2017-02-03'
    print(select(StockIO.get_stock('sha'), x_position=-4, kline_type=StockConfig.kline_type_day))

    # result = {}
    # for x in range(-5, 0):
    #     print('x = ', x)
    #     stock_list = select(StockIO.get_stock('sha'), x_position=x, kline_type=StockConfig.kline_type_day, min_vb=5, ratio=0.4)
    #     print(stock_list)
    #     for stock in stock_list:
    #         result[stock] = result.get(stock, 0) + 1
    #
    # print(sorted(result.items(), key=lambda d: d[1], reverse=True))
    #print(down_to(StockIO.get_stock('sha'), duration=60))
Пример #14
0
        sma5, sma10, sma20, sma30, sma60 = StockIndicator.sma(
            kline, 5, 10, 20, 30, 60)
        zf = StockIndicator.zf(kline)

        if close[x_position] > sma5[x_position] > sma10[x_position] > open[
                x_position] and sma10[x_position] > sma20[x_position]:
            if zf[x_position] > 5:
                print(stock)
                result.append(stock)
    return result


if __name__ == '__main__':
    position = -7
    date = '2018-03-20'
    stock_list = select(StockIO.get_stock('sha'), x_position=position, kline_type=StockConfig.kline_type_day) + \
                 select(StockIO.get_stock('sza'), x_position=position, kline_type=StockConfig.kline_type_day)
    print(stock_list)
    stock_code_list = []
    with open('data/track/2_sma_track.txt', mode='a', encoding='utf-8') as f:
        f.write('\n#{}\n'.format(date))
        for key in stock_list:
            if key.stock_code not in stock_code_list:
                f.write('{:>6},{: >5},{:>2},{:>2},{:>5},{:>5}, , ,\n'.format(
                    key.stock_code, key.stock_name, '', '', '00.00', '00.00'))

    with open('data/track/s3_{}.txt'.format(date), mode='w',
              encoding='utf-8') as f:
        for key in stock_list:
            if key.stock_code not in stock_code_list:
                f.write("{}\n".format(key.stock_code))
Пример #15
0
def select(stock_list,
           x_position=-1,
           w_x_position=-1,
           kline_type=StockConfig.kline_type_week,
           min_item=80):
    """
    均线选股法
    :param stock_list:
    :param kline_type:
    :param avg:
    :return:
    """
    result = []
    for stock in stock_list:
        try:
            kline = StockIO.get_kline(stock.stock_code, kline_type=kline_type)
        except Exception as e:
            print(e)
            continue
        if kline.shape[0] < min_item:
            continue

        open = kline[:, 1].astype(np.float)
        close = kline[:, 2].astype(np.float)
        low = kline[:, 4].astype(np.float)
        sma5, sma10, sma20, sma30, sma60 = StockIndicator.sma(
            kline, 5, 10, 20, 30, 60)
        cjl = StockIndicator.cjl(kline)
        add = False
        if close[x_position] > sma5[x_position] > sma10[x_position] and close[
                x_position] > sma20[x_position] and sma5[x_position] > sma20[
                    x_position]:

            add = False
            # 趋势
            count = 0
            while count < 5:
                if StockFilter2.is_jx(sma5, sma10, x_position - count):
                    jx_value = StockFilter2.jx_value(sma5, sma10,
                                                     x_position - count)
                    if jx_value > sma20[x_position - count]:
                        add = True
                    if close[x_position] > np.max(
                            close[x_position - 10:x_position]):
                        add = True
                    break
                count += 1

        if add:
            max_exceed = 5
            while close[x_position] > np.max(
                    close[x_position - max_exceed:x_position]):
                max_exceed += 1
                if max_exceed > 200:
                    break

            stock.max_exceed = max_exceed
        if add:
            print(stock)
            result.append(stock)

    return result
Пример #16
0
        low = kline[:, 4].astype(np.float)
        zf = abs((high - low) / low * 100)
        zf = np.delete(zf, [np.argmax(zf), np.argmin(zf)])  # 去掉最大值和最小值
        chg = (np.max(close) - np.min(close)) / np.min(close) * 100
        print(close)
        print(np.max(close), np.min(close))
        avg_zf = np.average(zf)
        #print(chg, avg_zf)
        if limit_chg[0] < chg < limit_chg[1] and limit_avg_zf[
                0] < avg_zf < limit_avg_zf[1]:
            last_close = close[-1]
            if (close[-1] - np.min(close)) / (
                    np.max(high) - np.min(close)) * 100 < limit_bottom:
                result.append([stock, chg, avg_zf, close[-1]])

    return result


if __name__ == '__main__':
    position = StockIndicator.position('2017-01-16',
                                       '601398',
                                       kline_type=StockConfig.kline_type_day)
    print(
        filter_by_zf(StockIO.get_stock('sha'),
                     x_position=-2,
                     kline_type=StockConfig.kline_type_week,
                     min_item=120))
    # for test single stock
    #stock_list = [StockConfig.Stock('600072', '')]
    #print(filter_by_zf(stock_list, x_position=position))
Пример #17
0
                print(stock)
                result.append(stock)
    return result


if __name__ == '__main__':
    date = '2017-02-03'
    # result = {}
    # for x in range(-5, 0):
    #     print('x = ', x)
    #     stock_list = select(StockIO.get_stock('level_1'), x_position=x, kline_type=StockConfig.kline_type_week, min_vb=14, ratio=0.5)
    #     print(stock_list)
    #     for stock in stock_list:
    #         result[stock] = result.get(stock, 0) + 1
    #
    # print(sorted(result.items(), key=lambda d: d[1], reverse=True))

    result = {}
    for x in range(-5, 0):
        print('x = ', x)
        stock_list = select(StockIO.get_stock('sha'),
                            x_position=x,
                            kline_type=StockConfig.kline_type_day,
                            min_vb=5,
                            ratio=0.4)
        print(stock_list)
        for stock in stock_list:
            result[stock] = result.get(stock, 0) + 1

    print(sorted(result.items(), key=lambda d: d[1], reverse=True))
    #print(down_to(StockIO.get_stock('sha'), duration=60))
Пример #18
0
            continue
        if kline.shape[0] < min_item:
            continue

        sma30 = StockIndicator.sma(kline, 30)[0]

        if close[x_position] > sma30[x_position]:
            #print(stock)
            result.append(stock)
    return result


if __name__ == '__main__':
    # date = '2017-02-03'
    # position = StockIndicator.position(date, '000001')
    #日线
    result = {}
    for x in range(-3, -1):
        print('x = ', x)
        stock_list = select(
            StockIO.get_stock('sza'),
            x_position=x,
            kline_type=StockConfig.kline_type_week,
        )
        print(stock_list)

        for stock in stock_list:
            result[stock] = result.get(stock, 0) + 1

    print(sorted(result.items(), key=lambda d: d[1], reverse=True))
Пример #19
0
def get_stock_list(x_position, zf=4, kline_type=StockConfig.kline_type_week):
    stock_list = select(StockIO.get_stock('gzw'), x_position=x_position, zf= zf, kline_type=kline_type)

    return stock_list
Пример #20
0
            continue

        chg = StockIndicator.chg(kline)
        vb = StockIndicator.vibration(kline)
        if max_chg > chg[x_position] > min_chg and max_vb > vb[
                x_position] > min_vb:
            print(stock)
            result.append(stock)
    return result


if __name__ == '__main__':
    for x in range(-6, 0):
        print('x = ', x)
        #     print(select(StockIO.get_stock('sza'), x_position=x))
        # 按照跌幅来选
        #print(select(StockIO.get_stock('sha'), '2017-07-03', x_position=-3, kline_type=StockConfig.kline_type_week, min_chg=-50, max_chg=0, min_vb=15, max_vb=100))
        # 按照涨幅来选
        print(
            select(StockIO.get_stock('level_2'),
                   x_position=-2,
                   kline_type=StockConfig.kline_type_week,
                   min_chg=-100,
                   max_chg=0,
                   min_vb=10,
                   max_vb=100))
# =======
#     for x in range(-3, 0):
#         print('x = ', x)
#         print(select(StockIO.get_stock('sha'), x_position=x, kline_type=StockConfig.kline_type_week))
Пример #21
0
                result.append(stock)
    return result


if __name__ == '__main__':
    date = '2017-02-03'
    position = StockIndicator.position(date, '000001')
    # result = {}
    # for x in range(-5, 0):
    #     print('x = ', x)
    #     stock_list = select(StockIO.get_stock('level_1'), x_position=x, kline_type=StockConfig.kline_type_week, min_vb=14, ratio=0.5)
    #     print(stock_list)
    #     for stock in stock_list:
    #         result[stock] = result.get(stock, 0) + 1
    #
    # print(sorted(result.items(), key=lambda d: d[1], reverse=True))

    result = {}
    for x in range(-5, 0):
        print('x = ', x)
        stock_list = select(StockIO.get_stock('level_1'),
                            x_position=x,
                            kline_type=StockConfig.kline_type_day,
                            min_vb=5,
                            ratio=0.01)
        print(stock_list)
        for stock in stock_list:
            result[stock] = result.get(stock, 0) + 1

    print(sorted(result.items(), key=lambda d: d[1], reverse=True))
    #print(down_to(StockIO.get_stock('sha'), duration=60))
Пример #22
0
        close = kline[:, 2].astype(np.float)
        high = kline[:, 3].astype(np.float)
        low = kline[:, 4].astype(np.float)
        vb = StockIndicator.vibration(kline)
        chg = StockIndicator.chg(kline)
        sma5, sma10, sma20 = StockIndicator.sma(kline, 5, 10, 20)

        if StockShape.is_lower_shadow(open[x_position], close[x_position], high[x_position], low[x_position], min_vb=min_vb, ratio=ratio, red=True):
            print(stock)
            result.append(stock)
                # print(stock)
                # result.append(stock)
                # append to file
                # path = '{root}/{name}'.format(root=StockConfig.path_track, name=append_file)
                # with open(path, mode='a', encoding='utf-8') as f:
                #     value = low[-1] + (high[-1] - low[-1]) * 0.3
                #     f.write("{},{},{}\n".format(stock.stock_code, value, high[-1]))

    return result


if __name__ == '__main__':
    stock_list = []
    for x in range(-1, 0):
        stock_list += select(StockIO.get_stock('sha'), x_position=x, kline_type=StockConfig.kline_type_day, min_vb=5, ratio=0.6)
        stock_list += select(StockIO.get_stock('sza'), x_position=x, kline_type=StockConfig.kline_type_day, min_vb=5,ratio=0.6)
    print(stock_list)

    with open('C:/Users/panha/Desktop/xgfx/1002.txt', mode='a', encoding='utf-8') as f:
        for stock in stock_list:
            f.write("{}\n".format(stock.stock_code))
Пример #23
0
def avg_up_trend(stock):
    kline = StockIO.get_kline(stock.stock_code, StockConfig.kline_type_day)
    sma5, sma10, sma20, sma60 = StockIndicator.sma(kline, 5, 10, 20, 60)
    if sma5[-1] > sma10[-1] > sma20[-1] and sma5[-1] > sma60[-1]:
        return True
    return False
Пример #24
0
            continue
        if kline.shape[0] < min_item:
            continue

        chg = StockIndicator.chg(kline)
        vb = StockIndicator.vibration(kline)
        if max_chg > chg[x_position] > min_chg and max_vb > vb[x_position] > min_vb:
            # 过滤掉单边上涨
            if not StockAlgrithm.sumOfSubArray(chg[-period:])[0] > 14:
                if np.max(vb[-period:]) > 14:
                    print(stock)
                    result.append(stock)
    return result

if __name__ == '__main__':
    date = '2017-02-03'
    position = StockIndicator.position(date, '000001')
    result = {}
    for x in range(-6, 0):
        print('x = ', x)
        stock_list = select(StockIO.get_stock('level_1'), x_position=x, kline_type=StockConfig.kline_type_week,
                 min_chg=-100, max_chg=100, min_vb=10, max_vb=100)
        print(stock_list)

        for stock in stock_list:
            result[stock.stock_code] = result.get(stock.stock_code, 0) + 1


    print(sorted(result.items(), key=lambda d: d[1], reverse=True))

Пример #25
0
                result.append(stock)
                # print(stock)
                # result.append(stock)
                # append to file
                # path = '{root}/{name}'.format(root=StockConfig.path_track, name=append_file)
                # with open(path, mode='a', encoding='utf-8') as f:
                #     value = low[-1] + (high[-1] - low[-1]) * 0.3
                #     f.write("{},{},{}\n".format(stock.stock_code, value, high[-1]))

    return result


if __name__ == '__main__':
    stock_list = []
    for x in range(-10, 0):
        stock_list += select(StockIO.get_stock('sha'),
                             x_position=x,
                             kline_type=StockConfig.kline_type_day,
                             min_vb=5,
                             ratio=0.4)
        stock_list += select(StockIO.get_stock('sza'),
                             x_position=x,
                             kline_type=StockConfig.kline_type_day,
                             min_vb=5,
                             ratio=0.4)
    print(stock_list)

    with open('C:/Users/panha/Desktop/xgfx/1002.txt',
              mode='a',
              encoding='utf-8') as f:
        for stock in stock_list:
Пример #26
0
        vbs.append(
            (stock, float('%.2f' % vb_factor), float('%.2f' % buy_facotr)))

    vbs.sort(key=lambda x: x[1])
    return vbs


def stat_low(stock_list, peoriod=120, x_position=-2, min_item=360):
    vbs = []
    for stock in stock_list:
        origin_kline = StockIO.get_kline(stock.stock_code,
                                         kline_type=StockConfig.kline_type_day)
        if origin_kline.shape[0] < min_item:
            continue
        from_position = x_position - peoriod + 1
        to_position = None if x_position == -1 else x_position
        kline = origin_kline[from_position:to_position]
        open = kline[:, 1].astype(np.float)
        close = kline[:, 2].astype(np.float)
        high = kline[:, 3].astype(np.float)
        low = kline[:, 4].astype(np.float)
        if low[-1] <= np.min(low) and low[-1] != 0:
            vbs.append(stock)
    return vbs


if __name__ == '__main__':
    stock_list = [StockConfig.Stock('601766', 'ZGZC')]
    vbs = stat_low(StockIO.get_stock('sha'))
    print(vbs)
Пример #27
0
# -*-coding:utf-8 -*-
"""
根据分红派息情况选股
以2016年的分红派息情况作为参考,根据当前的股票价格,算出预期分红点数,并做排名
"""
import StockIO
import StockConfig
import requests
import json

result = []  #[stock, price, profit, per, date]
stock_list = StockIO.get_stock('sha')
for stock in stock_list:
    kline = StockIO.get_kline(stock.stock_code, StockConfig.kline_type_day)
    result.append([stock, float(kline[:, 2][-1]), 0, 0, ''])
print(result)
for item in result:
    stock_code = item[0].stock_code
    stock_code = 'sh' + stock_code if stock_code.startswith(
        '6') else 'sz' + stock_code
    url = 'http://183.57.48.75/ifzqgtimg/stock/corp/cwbb/search?symbol={}&type=sum&jianjie=1'.format(
        stock_code)
    session = requests.Session()
    session.trust_env = False
    r = session.get(url)
    json_obj = json.loads(r.text)
    if json_obj['code'] == 0:
        gegu = json_obj['data']['gegu']
        if 'fenhong' in gegu:
            fenhong = gegu['fenhong']
            if len(fenhong) > 1:
Пример #28
0
        for vb_value in vb[x_position - period:x_position]:
            if StockFilter.between(vb_value, min_vb, max_vb):
                count += 1
        if count > count_limit:
            print(stock)
            result.append(stock)
    return result


if __name__ == '__main__':
    date = '2017-02-03'
    position = StockIndicator.position(date, '000001')
    for x in range(-20, 0):
        print('x = ', x)
        # print(select(StockIO.get_stock('sza'), x_position=x))
        # 按照跌幅来选
        #print(select(StockIO.get_stock('sha'), '2017-07-03', x_position=-3, kline_type=StockConfig.kline_type_week, min_chg=-50, max_chg=0, min_vb=15, max_vb=100))
        # 按照涨幅来选
        print(
            select(StockIO.get_stock('sza'),
                   x_position=-1,
                   kline_type=StockConfig.kline_type_week,
                   period=5,
                   count_limit=3,
                   min_vb=10,
                   max_vb=100))
# =======
#     for x in range(-3, 0):
#         print('x = ', x)
#         print(select(StockIO.get_stock('sha'), x_position=x, kline_type=StockConfig.kline_type_week))
Пример #29
0
def limit(stock_list, output_file='', high_limit_point=5, low_limit_point=4):
    result = []
    for stock in stock_list:
        try:
            kline = StockIO.get_kline(stock.stock_code,
                                      kline_type=StockConfig.kline_type_day)
        except Exception as e:
            print(e)
            continue

        close = kline[:, 2].astype(np.float)
        cur = close[-2]
        # 获取最低价和最高价
        up = cur * (1 + high_limit_point / 100)
        down = cur * (1 - low_limit_point / 100)
        result.append((stock.stock_code, down, up, stock.stock_name))

    path = '{root}/{name}'.format(root=StockConfig.path_track,
                                  name=output_file)
    with open(path, mode='w', encoding='utf-8') as f:
        for item in result:
            print(item[0], item[1], item[2], item[3])
            f.write("{}, {}, {}, {}, {}\n".format(item[0], '%05.2f' % item[1],
                                                  '%05.2f' % item[2], item[3],
                                                  '(* N)'))


if __name__ == '__main__':
    limit(StockIO.get_stock('level_1'), 'level_1_track.txt')
Пример #30
0

if __name__ == '__main__':
    result={}
    # for x in range(-10, 0):
    #     print('x = ', x)
    #     stock_list = select(StockIO.get_stock('sha'), x_position=x, kline_type=StockConfig.kline_type_week)
    #     print(stock_list)
    #
    #     for stock in stock_list:
    #         result[stock] = result.get(stock, 0) + 1
    #
    # with open('{}/{}'.format(StockConfig.path_stock, 'wsma10'), 'w', encoding='utf-8') as f:
    #     for key in result:
    #         if result[key] >= 7:
    #             f.write("{},{}\n".format(key.stock_code, key.stock_name))
    #
    # print(sorted(result.items(), key=lambda d: d[1], reverse=True))
    date = '2017-10-09'
    position = StockIndicator.position(date, '000001')
    stock_list = select(StockIO.get_stock('sza'), x_position=-1)
    stock_list2 = select(StockIO.get_stock('sha'), x_position=-1)
    stock_list = stock_list + stock_list2
    print(len(stock_list))
    with open('C:/Users/panha/Desktop/xgfx/1002.txt', mode='w', encoding='utf-8') as f:
        for key in stock_list:
            f.write("{}\n".format(key.stock_code))