コード例 #1
0
def scan_stocks():
	pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME"))
	# 数据库获取沪、深、中小板
	zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')]
	hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')]
	ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')]
	candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks
	print("total stock size:", len(candidate_stocks))

	records = get_records(dt='2020-12-15', direction='limit', period='d1')
	print("涨停数量:", len(records))
	for index, row in tqdm(pd_names.iterrows()):
		code = row['code']
		name = row['code_name']
		if stock_utils.is_jiucaiban(code):
			continue
		if code[3:] not in candidate_stocks:
			# print(name, code)
			continue

		code_name = (code, name)
		df = stock_utils.read_data(code_name)
		df.reset_index(drop=True, inplace=True)
		if len(df) < 60:
			continue

		df = df.tail(n=5)
		df.reset_index(drop=True, inplace=True)
		# 涨停分析
		close = df.iloc[-1]['close']
		chg = df.iloc[-1]['pctChg']
		volume = df.iloc[-1]['volume']
		volume_rate = (volume - df.iloc[-2]['volume']) / df.iloc[-2]['volume']
		chgs = [c for c in df['pctChg']]  # 涨幅序列
		vols = [v / df.iloc[0]['volume'] for v in df['volume']]  # 量比序列

		feat = standardization(chgs[:-1]) + standardization(vols[:-1])
		feat = standardization(chgs[:-1])
		# print(feat)
		tmp_count = 0
		for record in records:
			tmp_chg = json.loads(record.extra)['chgs'][:-1]
			tmp_vol = json.loads(record.extra)['vols'][:-1]
			tmp_feat = standardization(tmp_chg) + standardization(tmp_vol)
			tmp_feat = standardization(tmp_chg)
			dist = cosine_dist(feat, tmp_feat)
			if dist > 0.9:
				# print(name, record.name, chg, "sim:", dist)
				tmp_count += 1
		if tmp_count > 5:
			print(code, name, chg)
コード例 #2
0
def gen_records(day_ago=0):
    records = list()

    pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME"))
    # 数据库获取沪、深、中小板
    zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')]
    hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')]
    ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')]
    candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks
    print("total stock size:", len(candidate_stocks))

    for index, row in tqdm(pd_names.iterrows()):
        code = row['code']
        name = row['code_name']
        if stock_utils.is_jiucaiban(code):
            continue
        if code[3:] not in candidate_stocks:
            continue

        code_name = (code, name)
        df = stock_utils.read_data(code_name)
        df = df.head(n=len(df) - day_ago)  # n天前 新高/低入口
        df.reset_index(drop=True, inplace=True)
        if len(df) < 60:
            continue

        for period in [500, 120, 60, 20, 10, 5]:
            df = df.tail(n=period)
            df.reset_index(drop=True, inplace=True)
            idxMax = df['close'].idxmax(axis=0)
            idxMin = df['close'].idxmin(axis=0)
            # print(idxMax, idxMin)
            dateMax = df.iloc[idxMax]['date']
            dateMin = df.iloc[idxMin]['date']
            date = df.iloc[-1]['date']
            volume = df.iloc[-1]['volume']
            record_date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
            d_period_inc = (df.iloc[-1]['close'] -
                            df.iloc[0]['close']) / df.iloc[0]['close']

            volume_avg = df['volume'].sum() / period
            amount_avg = df['amount'].sum() / period
            period_inc_avg = d_period_inc / period
            extraJson = {
                'volume_avg': volume_avg,
                'amount_avg': amount_avg,
                'inc_avg': period_inc_avg
            }

            flag, direction = False, None
            r = Record(name, code, record_date, df.iloc[-1]['close'],
                       "d{}".format(period), volume, d_period_inc)
            r.set_extra(json.dumps(extraJson))
            if df.iloc[-1]['date'] == dateMax:
                flag, direction = True, 'up'
            if df.iloc[-1]['date'] == dateMin and 'ST' not in name:
                flag, direction = True, 'down'

            if flag:
                r.set_direction(direction)
                records.append(r)

        # 涨停分析
        close = df.iloc[-1]['close']
        chg = df.iloc[-1]['pctChg']
        volume = df.iloc[-1]['volume']
        volume_rate = (volume - df.iloc[-2]['volume']) / df.iloc[-2]['volume']
        if float(chg) >= 9:
            chgs = [c for c in df['pctChg']]  # 涨幅序列
            vols = [v / df.iloc[0]['volume'] for v in df['volume']]  # 量比序列
            extraJson = {
                'volume_rate': volume_rate,
                'chgs': chgs,
                'vols': vols
            }
            r = Record(name, code, record_date, close, "d1", volume, chg)
            r.set_direction(direction='limit')
            r.set_extra(json.dumps(extraJson))
            records.append(r)
    return records
コード例 #3
0
def period_records():
    outResult = collections.defaultdict(dict)
    extraResult = collections.defaultdict(list)

    hsb = 1696
    ssb = 484
    zxb = 961

    HSB_NUM = 10
    SSB_NUM = 5
    ZXB_NUM = 5

    # 数据库获取沪、深、中小板
    zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')]
    hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')]
    ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')]
    candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks

    pool = {}
    bestStocks = []

    dt = get_recently_trade_date()
    print("recently date:", dt)
    # dt = '2020-12-22'
    new_highs = [
        r for r in get_records(dt=dt, direction='up', period='d500')
        if r.code[3:] in candidate_stocks
    ]
    new_lows = [
        r for r in get_records(dt=dt, direction='down', period='d500')
        if r.code[3:] in candidate_stocks
    ]

    pool['d60'] = get_records(dt=dt, direction='up', period='d60')
    pool['d20'] = get_records(dt=dt, direction='up', period='d20')
    pool['d10'] = get_records(dt=dt, direction='up', period='d10')
    pool['d5'] = get_records(dt=dt, direction='up', period='d5')
    print("d5:", len(pool['d5']), "d10:", len(pool['d10']), "d20:",
          len(pool['d20']), "d60:", len(pool['d60']))

    for k, records in pool.items():
        hsb = [r for r in records if r.code[3:] in hsb_stocks][:HSB_NUM]
        ssb = [r for r in records if r.code[3:] in ssb_stocks][:SSB_NUM]
        zxb = [r for r in records if r.code[3:] in zxb_stocks][:ZXB_NUM]

        for r in hsb + ssb + zxb:
            bestStocks.append((r.code, r.name))

        outResult[k]['hsb'] = hsb
        outResult[k]['ssb'] = ssb
        outResult[k]['zxb'] = zxb

    #  近一周的新高股票
    day_week_ago = datetime.datetime.now() + datetime.timedelta(days=-7)
    target_date = day_week_ago.strftime('%Y-%m-%d')
    records = get_period_records(start_date=target_date)
    week_bests = []
    for r in records:
        if r.code[3:] not in candidate_stocks:
            continue
        week_bests.append((r.code, r.name))

    # print(collections.Counter(bestStocks))
    # best = sorted(collections.Counter(bestStocks).items(), key=lambda x: x[1], reverse=True)
    # print(best)  # [(('sh.600121', '郑州煤电'), 4), (('sh.601015', '陕西黑猫'), 4), (('sh.600418', '江淮汽车'), 4),...
    extraResult['best'] = sorted(collections.Counter(bestStocks).items(),
                                 key=lambda x: x[1],
                                 reverse=True)
    extraResult['week_best'] = sorted(collections.Counter(week_bests).items(),
                                      key=lambda x: x[1],
                                      reverse=True)
    extraResult['high'] = new_highs
    extraResult['low'] = new_lows

    # # 月线趋势选股
    # # 结构 [(('sh.600196', '复星医药'), 44517688035.19, 2269476415.0), (('sh.600887', '伊利股份'), 29118278005.35, 1554822026.0), ..]
    # breakstocks, highstocks = get_m_candidates()
    # extraResult['m_break'] = breakstocks
    # extraResult['m_high'] = highstocks

    send_hot_share_mail(outResult, extraResult)
コード例 #4
0
def get_m_candidates():
    pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME"))
    # 数据库获取沪、深、中小板
    zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')]
    hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')]
    ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')]
    candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks
    print("total stock size:", len(candidate_stocks))

    t_date = datetime.date.today().strftime('%Y-%m')
    break_stocks = list()
    high_stocks = list()
    for index, row in tqdm(pd_names.iterrows()):
        code = row['code']
        name = row['code_name']
        if stock_utils.is_jiucaiban(code):
            continue
        if code[3:] not in candidate_stocks:
            continue

        code_name = (code, name)
        df = stock_utils.read_data(code_name,
                                   root=ConfigUtils.get_stock("DATA_M_DIR"))
        if df is None or len(df) < 2:
            continue
        df = df.tail(n=12)  # n天前 新高/低入口
        df.reset_index(drop=True, inplace=True)
        close_df = df.sort_values(by='close', ascending=False)
        volume_df = df.sort_values(by='volume', ascending=False)
        amount_df = df.sort_values(by='amount', ascending=False)
        pctChg_df = df.sort_values(by='pctChg', ascending=False)

        idxCloseMax0, idxCloseMax1 = close_df.index[0], close_df.index[1]
        idxVolMax0, idxVolMax1 = volume_df.index[0], volume_df.index[1]
        idxAmountMax0, idxAmountMax1 = amount_df.index[0], amount_df.index[1]
        idxChgMax0, idxChgMax1 = pctChg_df.index[0], pctChg_df.index[1]

        volMax0, volMax1 = df.iloc[idxVolMax0]['volume'], df.iloc[idxVolMax1][
            'volume']
        pctMax0, pctMax1 = df.iloc[idxChgMax0]['pctChg'], df.iloc[idxChgMax1][
            'pctChg']

        closeMax0, closeMax1 = df.iloc[idxCloseMax0]['close'], df.iloc[
            idxCloseMax1]['close']
        date0, date1 = df.iloc[idxCloseMax0]['date'], df.iloc[idxCloseMax1][
            'date']
        pch0, pch1 = df.iloc[idxCloseMax0]['pctChg'], df.iloc[idxCloseMax1][
            'pctChg']
        amount0, amount1 = df.iloc[idxCloseMax0]['amount'], df.iloc[
            idxCloseMax1]['amount']
        # 经过3个月洗盘, 最近股价将赶超新高
        if t_date == date1 and (idxCloseMax1 - idxCloseMax0) > 2:
            # print(code_name, volMax1, date0, date1, idxVolMax0, idxChgMax0, idxCloseMax0, closeMax0)
            r = Record(name, code, t_date, closeMax1, 'm', amount1, pch1)
            break_stocks.append(r)
        # 持续新高
        if t_date == date0:
            # print(code_name, dateMax0, dateMax1, idxVolMax0, idxChgMax0, idxCloseMax0, closeMax0)
            r = Record(name, code, t_date, closeMax0, 'm', amount0, pch0)
            high_stocks.append(r)

    break_stocks = sorted(break_stocks, key=lambda x: x.volume, reverse=True)
    high_stocks = sorted(high_stocks, key=lambda x: x.volume, reverse=True)

    # print(break_stocks[:30])
    # print(high_stocks[:30])

    break_stocks = [r for r in break_stocks if r.volume < 5e11]
    high_stocks = [r for r in high_stocks if r.volume < 90081936793]

    return break_stocks, high_stocks
コード例 #5
0
ファイル: monitor.py プロジェクト: learnerzhang/trade
# @Author  : zhangzhen12
# @Site    :
# @File    : monitor.py
# @Software: PyCharm
from data_modules.sina_reptile import get_real_price
from stock_analytic_modules.utils.helper import get_before_day
from common.sql_utils import get_records, get_mark_stocks
from common.stock_utils import get_recently_trade_date
import datetime
import threading
from time import ctime, sleep

dt = get_before_day(ago=1)
# print(dt)
# 数据库获取沪、深、中小板
zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')]
hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')]
ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')]
candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks


def monitor_low():
    new_lows = [
        r for r in get_records(dt=dt, direction='down', period='d500')
        if r.code[3:] in candidate_stocks
    ]
    print("low:", len(new_lows))
    while True:
        sleep(10)
        for r in new_lows:
            code = str(r.code).replace('.', '')