Exemplo n.º 1
0
def async_weekly():
    main_session.query(models.WeeklyPro).delete()
    main_session.commit()
    for count, sbp in enumerate(
            main_session.query(models.StockBasicPro).all()):
        if_pass = False
        while not if_pass:
            try:
                weekly = pro.weekly(ts_code=sbp.ts_code,
                                    start_date=trade_dates[sampling_count],
                                    end_date=LAST_MARKET_DATE)
                if_pass = True
            except Exception as e:
                print('excetion in {}'.format(count))
                continue

        for i in range(len(weekly)):
            a_weekly = models.WeeklyPro(
                ts_code=weekly.loc[i, 'ts_code'],
                trade_date=weekly.loc[i, 'trade_date'],
                open=float(weekly.loc[i, 'open']),
                high=float(weekly.loc[i, 'high']),
                low=float(weekly.loc[i, 'low']),
                close=float(weekly.loc[i, 'close']),
                pre_close=float(weekly.loc[i, 'pre_close']),
                change=float(weekly.loc[i, 'change']),
                pct_chg=float(weekly.loc[i, 'pct_chg']),
                vol=float(weekly.loc[i, 'vol']),
                amount=float(weekly.loc[i, 'amount']))
            main_session.add(a_weekly)
        main_session.commit()
        print('##### async_weekly {i} #####'.format(i=count))
        time.sleep(0.2)
Exemplo n.º 2
0
def async_daily_basic():
    main_session.query(models.DailyBasicPro).delete()
    main_session.commit()
    for count, sbp in enumerate(
            main_session.query(models.StockBasicPro).all()):
        try:
            daily_basic = pro.daily_basic(ts_code=sbp.ts_code,
                                          trade_date=LAST_MARKET_DATE)
            a_daily_basic = models.DailyBasicPro(
                ts_code=daily_basic.loc[0, 'ts_code'],
                trade_date=daily_basic.loc[0, 'trade_date'],
                turnover_rate=float(daily_basic.loc[0, 'turnover_rate']),
                turnover_rate_f=float(daily_basic.loc[0, 'turnover_rate_f']),
                total_share=float(daily_basic.loc[0, 'total_share']),
                float_share=float(daily_basic.loc[0, 'float_share']),
                free_share=float(daily_basic.loc[0, 'free_share']),
                total_mv=float(daily_basic.loc[0, 'total_mv']),
                circ_mv=float(daily_basic.loc[0, 'circ_mv']))
        except Exception as e:
            print('excetion in {}'.format(count))
            continue

        main_session.add(a_daily_basic)
        main_session.commit()
        print('##### async_daily_basic {i} #####'.format(i=count))
        time.sleep(0.2)
Exemplo n.º 3
0
def async_float_holders():
    main_session.query(models.FloatHolderPro).delete()
    main_session.commit()

    key_words = ['基金', '资产管理', '中央', '信托']
    region_tree = build_actree(key_words)

    for count, sbp in enumerate(main_session.query(models.StockBasicPro).all()):
        if_pass = False
        while not if_pass:
            try:
                float_holders = pro.top10_floatholders(ts_code=sbp.ts_code, start_date=trade_dates[100], end_date=LAST_MARKET_DATE)
                if_pass = True
            except Exception as e:
                print('excetion in {}'.format(count))
                continue

        for i in range(len(float_holders)):
            matches = list(region_tree.iter(float_holders.loc[i, 'holder_name']))
            if matches:
                a_float_holder = models.FloatHolderPro(
                    ts_code=float_holders.loc[i, 'ts_code'],
                    ann_date=float_holders.loc[i, 'ann_date'],
                    holder_name=float_holders.loc[i, 'holder_name']
                )
                main_session.add(a_float_holder)
        main_session.commit()
        print('##### async_float_holders {i} #####'.format(i=count))
        time.sleep(0.2)
Exemplo n.º 4
0
def async_daily_basic_origin():
    main_session.query(models.DailyBasic).delete()
    main_session.commit()
    for count, sbp in enumerate(
            main_session.query(models.StockBasicPro).all()):
        market = sbp.ts_code.split('.')[1].lower()
        symbol = sbp.symbol
        code = market + symbol

        if_pass = False
        while not if_pass:
            try:
                res = requests.get('http://sqt.gtimg.cn/q={}'.format(code))
                if_pass = True
            except Exception as e:
                print('exception in {}'.format(count))
                continue

        try:
            res = res.text.split('~')
            a_daily_basic = models.DailyBasic(
                ts_code=sbp.ts_code,
                name=sbp.name,
                trade_date=LAST_MARKET_DATE,
                total_mv=float(res[45]) if res[45] else 0,
                circ_mv=float(res[44]) if res[44] else 0)
        except Exception as e:
            continue

        main_session.add(a_daily_basic)
        main_session.commit()
        print('##### async_daily_basic_origin {i} #####'.format(i=count))
Exemplo n.º 5
0
def async_stock_basic():
    main_session.query(models.StockBasicPro).delete()
    main_session.commit()
    stock_basic = pro.stock_basic(list_status='L', fields='ts_code,symbol,name,industry,fullname')
    for i in range(len(stock_basic)):
        a_stock_basic = models.StockBasicPro(ts_code=stock_basic.loc[i, 'ts_code'],
                                             symbol=stock_basic.loc[i, 'symbol'],
                                             name=stock_basic.loc[i, 'name'],
                                             industry=stock_basic.loc[i, 'industry']
                                             )
        main_session.add(a_stock_basic)

    main_session.commit()
    print('##### async stock basic finished #####')
Exemplo n.º 6
0
def main():
    pro = ts.pro_api()
    main_session.query(models.StockCompanyPro).delete()
    main_session.commit()
    for exchange in ['SZSE', 'SSE']:
        df = pro.stock_company(exchange=exchange,
                               fields='ts_code,main_business,business_scope')
        for i in range(len(df)):
            a_stock_company = models.StockCompanyPro(
                ts_code=df.loc[i, 'ts_code'],
                main_business=df.loc[i, 'main_business'],
                business_scope=df.loc[i, 'business_scope'])
            main_session.add(a_stock_company)
        main_session.commit()

    print('##### async stock company finished #####')
Exemplo n.º 7
0
def async_daily():
    main_session.query(models.DailyPro).delete()
    main_session.commit()
    for count, sbp in enumerate(
            main_session.query(models.StockBasicPro).all()):
        if_pass = False
        while not if_pass:
            try:
                daily = ts.pro_bar(ts_code=sbp.ts_code,
                                   adj='qfq',
                                   start_date=trade_dates[sampling_count],
                                   end_date=LAST_MARKET_DATE)
                if_pass = True
            except Exception as e:
                print('exception in {}'.format(count))
                continue

        if not isinstance(daily, DataFrame):
            continue

        check_for_nan = daily.isnull().values.any()
        if check_for_nan:
            continue

        for i in range(len(daily)):
            a_daily = models.DailyPro(ts_code=daily.loc[i, 'ts_code'],
                                      trade_date=daily.loc[i, 'trade_date'],
                                      open=float(daily.loc[i, 'open']),
                                      high=float(daily.loc[i, 'high']),
                                      low=float(daily.loc[i, 'low']),
                                      close=float(daily.loc[i, 'close']),
                                      pre_close=float(daily.loc[i,
                                                                'pre_close']),
                                      change=float(daily.loc[i, 'change']),
                                      pct_chg=float(daily.loc[i, 'pct_chg']),
                                      vol=float(daily.loc[i, 'vol']),
                                      amount=float(daily.loc[i, 'amount']))
            main_session.add(a_daily)
        main_session.commit()
        print('##### async_daily {i} #####'.format(i=count))
        time.sleep(0.2)
Exemplo n.º 8
0
def main():
    pro = ts.pro_api()

    main_session.query(models.ConceptPro).delete()
    main_session.query(models.ConceptDetailPro).delete()
    main_session.commit()
    concepts = pro.concept()
    for i in range(len(concepts)):
        a_concept = models.ConceptPro(code=concepts.loc[i, 'code'],
                                      name=concepts.loc[i, 'name'])
        main_session.add(a_concept)
    main_session.commit()

    print('##### async concept finished #####')

    for count, concept in enumerate(
            main_session.query(models.ConceptPro).all()):
        try:
            df = pro.concept_detail(id=concept.code, fields='ts_code,name')
        except Exception as e:
            print('excetion in {}'.format(count))
            continue

        for i in range(len(df)):
            a_detail = models.ConceptDetailPro(code=concept.code,
                                               ts_code=df.loc[i, 'ts_code'],
                                               name=df.loc[i, 'name'])
            main_session.add(a_detail)
        main_session.commit()
        print('##### {i} #####'.format(i=count))
        time.sleep(0.5)
Exemplo n.º 9
0
def main(offset=0):
    daily001 = main_session.query(models.DailyPro).filter(models.DailyPro.ts_code == '000001.SZ').order_by(models.DailyPro.trade_date.desc()).all()
    LAST_MARKET_DATE = daily001[offset].trade_date

    data_frame = DataFrame()
    for i, stock_basic in enumerate(main_session.query(models.StockBasicPro).all()):
        try:
            daily = main_session.query(models.DailyPro).filter(models.DailyPro.ts_code == stock_basic.ts_code,
                                                               models.DailyPro.trade_date <= LAST_MARKET_DATE).order_by(models.DailyPro.trade_date.desc()).limit(sampling_count).all()
            if len(daily) < 30:
                continue
            for key in models.StockBasicPro.keys:
                data_frame.loc[i, key] = getattr(stock_basic, key)
            data_frame.loc[i, COL_LASTPRICE] = daily[0].close
            data_frame.loc[i, COL_PCT_CHG] = daily[0].pct_chg

            highest_chg = (daily[0].high / daily[0].pre_close - 1) * 100
            data_frame.loc[i, COL_BLAST_BOARD] = highest_chg > 9.8 and daily[0].pct_chg < 9.8
            data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT] = api.daily_continuous_limit_count(daily)

        except Exception as e:
            print('wind chime exception in index:{index} {code} {name}'.format(index=i, code=stock_basic.ts_code, name=stock_basic.name))
            continue
        print('##### wind chime {i} #####'.format(i=i))

    data_frame = data_frame.reset_index(drop=True)

    up_count = 0
    down_count = 0
    up_limit_count = 0
    down_limit_count = 0
    blast_board_count = 0
    limit_count_1 = 0
    limit_count_2 = 0
    limit_count_3 = 0
    limit_count_over3 = 0
    max_limit_count = 0
    max_limit_stock = None

    for i in range(len(data_frame)):
        if data_frame.loc[i, COL_PCT_CHG] > 0:
            up_count += 1
        else:
            down_count += 1

        if data_frame.loc[i, COL_BLAST_BOARD]:
            blast_board_count += 1

        if data_frame.loc[i, COL_PCT_CHG] > 9.8:
            up_limit_count += 1

            if data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT] > 3:
                limit_count_over3 += 1
            elif data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT] > 2:
                limit_count_3 += 1
            elif data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT] > 1:
                limit_count_2 += 1
            else:
                limit_count_1 += 1

            if data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT] > max_limit_count:
                max_limit_count = int(data_frame.loc[i, COL_CONTINUOUS_LIMIT_COUNT])
                max_limit_stock = '{ts_code} {name}'.format(ts_code=data_frame.loc[i, 'ts_code'], name=data_frame.loc[i, 'name'])

        if data_frame.loc[i, COL_PCT_CHG] < -9.8:
            down_limit_count += 1

        print('wind chime analyse in index:{index}'.format(index=i))

    main_session.query(models.Analyst).filter(models.Analyst.trade_date == LAST_MARKET_DATE).delete()
    a_analyst = models.Analyst(
        trade_date=LAST_MARKET_DATE,
        up_count=up_count,
        down_count=down_count,
        up_limit_count=up_limit_count,
        down_limit_count=down_limit_count,
        blast_board_count=blast_board_count,
        limit_count_1=limit_count_1,
        limit_count_2=limit_count_2,
        limit_count_3=limit_count_3,
        limit_count_over3=limit_count_over3,
        max_limit_count=max_limit_count,
        max_limit_stock=max_limit_stock
    )

    main_session.add(a_analyst)
    main_session.commit()

    analysts = main_session.query(models.Analyst).order_by(models.Analyst.trade_date.desc()).all()
    analyst_df = DataFrame()
    for i, analyst in enumerate(analysts):
        for key in models.Analyst.keys:
            analyst_df.loc[i, key] = getattr(analyst, key)

    file_name = '{logs_path}/{date}@WindChime.csv'.format(date=LAST_MARKET_DATE, logs_path=env.logs_path)
    with open(file_name, 'w', encoding='utf8') as file:
        analyst_df.to_csv(file)
Exemplo n.º 10
0
def main():
    date = time.strftime("%Y-%m-%d", time.localtime())
    f = open('../../logs/{date}@liveshow.txt'.format(date=date),
             'a',
             encoding='utf8')
    # f_note = open('../../logs/{date}@liveshow_note.txt'.format(date=date), 'a', encoding='utf8')
    daily001 = main_session.query(models.DailyPro).filter(
        models.DailyPro.ts_code == '000001.SZ').order_by(
            models.DailyPro.trade_date.desc()).all()
    LAST_MARKET_DATE = daily001[0].trade_date

    while int(time.strftime("%H%M%S", time.localtime())) < 92500:
        print('sleep for 5 secs, {}'.format(
            time.strftime("%H:%M:%S", time.localtime())))
        time.sleep(5)

    while 1 or int(time.strftime("%H%M%S", time.localtime())) < 93000:
        for i, stock_basic in enumerate(
                main_session.query(models.StockBasicPro).all()):
            try:
                query_key = '{a}{b}'.format(
                    a=stock_basic.ts_code.split('.')[1].lower(),
                    b=stock_basic.symbol)
                response = requests.get(
                    'http://hq.sinajs.cn/list={}'.format(query_key))
                res = response.text.split(',')
                chg = round((float(res[3]) / float(res[2]) - 1) * 100, 2)

                # f.write(formatted)
                print(i)

                if chg > 4 and float(res[9]) > 50000000:
                    daily = main_session.query(models.DailyPro).filter(
                        models.DailyPro.ts_code == stock_basic.ts_code,
                        models.DailyPro.trade_date <=
                        LAST_MARKET_DATE).order_by(
                            models.DailyPro.trade_date.desc()).limit(
                                sampling_count).all()

                    continuous_limit = api.daily_continuous_limit_count(
                        daily=daily)

                    cons = main_session.query(models.ConceptPro).join(
                        models.ConceptDetailPro, models.ConceptPro.code ==
                        models.ConceptDetailPro.code).filter(
                            models.ConceptDetailPro.ts_code ==
                            stock_basic.ts_code).all()
                    concept_value = ''
                    for con in cons:
                        concept_value = concept_value + '{c}, '.format(
                            c=con.name)

                    formatted = '{index} {code} {name} 今开:{open} 昨收:{yesterday} 当前百分比:{chg} 当前:{current} 成交股数:{stocks}手 成交额:{amount}万 {date} ' \
                                '{timestamp} 连板:{conti_limit} 概念:{concept}\n'.format(index=i, code=stock_basic.ts_code, name=stock_basic.name, open=res[1],
                                                                                     yesterday=res[2], chg=chg, current=res[3], stocks=round(float(res[8]) / 100, 0),
                                                                                     amount=round(float(res[9]) / 10000, 2), date=res[30], timestamp=res[31], conti_limit=continuous_limit,
                                                                                     concept=concept_value)

                    f.write(formatted)
                    f.flush()
            except Exception as e:
                print('exception in index:{index} {code} {name}'.format(
                    index=i, code=stock_basic.ts_code, name=stock_basic.name))
                continue

        f.write(
            '\n############################################################################\n\n'
        )
        f.flush()

    while int(time.strftime("%H%M%S", time.localtime())) < 150000:
        for i, stock_basic in enumerate(
                main_session.query(models.StockBasicPro).all()):
            try:
                query_key = '{a}{b}'.format(
                    a=stock_basic.ts_code.split('.')[1].lower(),
                    b=stock_basic.symbol)
                response = requests.get(
                    'http://hq.sinajs.cn/list={}'.format(query_key))
                res = response.text.split(',')
                chg = round((float(res[3]) / float(res[2]) - 1) * 100, 2)
                print(i)

                stock_live = main_session.query(models.StockLive).filter(
                    models.StockLive.ts_code == stock_basic.ts_code).first()
                if stock_live:
                    if chg - stock_live.chg > 2:
                        daily = main_session.query(models.DailyPro).filter(
                            models.DailyPro.ts_code == stock_basic.ts_code,
                            models.DailyPro.trade_date <=
                            LAST_MARKET_DATE).order_by(
                                models.DailyPro.trade_date.desc()).limit(
                                    sampling_count).all()

                        continuous_limit = api.daily_continuous_limit_count(
                            daily=daily)

                        cons = main_session.query(models.ConceptPro).join(
                            models.ConceptDetailPro, models.ConceptPro.code ==
                            models.ConceptDetailPro.code).filter(
                                models.ConceptDetailPro.ts_code ==
                                stock_basic.ts_code).all()
                        concept_value = ''
                        for con in cons:
                            concept_value = concept_value + '{c}, '.format(
                                c=con.name)

                        formatted = '{index} {code} {name} 今开:{open} 昨收:{yesterday} 当前百分比:{chg} 当前:{current} {date} ' \
                                    '{timestamp} 连板:{conti_limit} 概念:{concept}\n'.format(index=i, code=stock_basic.ts_code, name=stock_basic.name, open=res[1],
                                                                                         yesterday=res[2], chg=chg, current=res[3], date=res[30], timestamp=res[31], conti_limit=continuous_limit,
                                                                                         concept=concept_value)
                        print(formatted)
                        f.write(formatted)
                        f.flush()
                    stock_live.chg = chg
                    stock_live.timestamp = datetime.datetime.now()
                else:
                    stock_live = models.StockLive(chg=chg,
                                                  ts_code=stock_basic.ts_code)
                    main_session.add(stock_live)
            except Exception as e:
                print('exception in index:{index} {code} {name}'.format(
                    index=i, code=stock_basic.ts_code, name=stock_basic.name))
                continue
        main_session.commit()
    f.close()