Пример #1
0
def get_past_datas(code, today, t, code_dict):
    yesterday = holidays.get_yesterday(today)
    data = morning_client.get_past_day_data(code, yesterday, yesterday)
    if len(data) != 1:
        print('Cannot get yesterday data', code, yesterday)
        return
    code_dict[code]['yesterday_data'] = data[0]
    min_data = morning_client.get_minute_data(code, today, today)
    hour = int(t / 10000)
    minute = int((t % 10000) / 100)
    vi_datetime = datetime.combine(today, time(hour, minute))
    before_vi_datetime = vi_datetime - timedelta(seconds=60)
    after_vi_datetime = vi_datetime + timedelta(seconds=120)

    before_datas = []
    after_datas = []
    for mdata in min_data:
        if mdata[
                'time'] <= before_vi_datetime.hour * 100 + before_vi_datetime.minute:
            before_datas.append(mdata)
        elif mdata[
                'time'] >= after_vi_datetime.hour * 100 + after_vi_datetime.minute:
            after_datas.append(mdata)

    code_dict[code]['today_min_data'] = before_datas
    code_dict[code]['today_min_after_vi'] = after_datas[:-1]
    for mdata in code_dict[code]['today_min_data']:
        if mdata['highest_price'] > code_dict[code]['vi_highest']:
            code_dict[code]['vi_highest'] = mdata['highest_price']
    """
Пример #2
0
 def GetDayData(self, request, context):
     _LOGGER.info('GetDayData %s, %s, %s', request.code,
                  request.from_datetime.ToDatetime() + timedelta(hours=9),
                  request.until_datetime.ToDatetime() + timedelta(hours=9))
     day_datas = morning_client.get_past_day_data(
         request.code,
         request.from_datetime.ToDatetime() + timedelta(hours=9),
         request.until_datetime.ToDatetime() + timedelta(hours=9))
     protoc_converted = []
     for d in day_datas:
         protoc_converted.append(
             stock_provider_pb2.CybosDayData(
                 date=d['0'],
                 time=d['time'],
                 start_price=int(d['start_price']),
                 highest_price=int(d['highest_price']),
                 lowest_price=int(d['lowest_price']),
                 close_price=int(d['close_price']),
                 volume=d['volume'],
                 amount=d['amount'],
                 cum_sell_volume=d['cum_sell_volume'],
                 cum_buy_volume=d['cum_buy_volume'],
                 foreigner_hold_volume=d['foreigner_hold_volume'],
                 foreigner_hold_rate=d['foreigner_hold_rate'],
                 institution_buy_volume=d['institution_buy_volume'],
                 institution_cum_buy_volume=d['institution_cum_buy_volume'])
         )
     return stock_provider_pb2.CybosDayDatas(day_data=protoc_converted)
Пример #3
0
def today_bull_record():
    while True:
        now = datetime.now()
        start_time = now.replace(hour=18, minute=5)
        if now > start_time:
            result = morning_client.get_past_day_data(
                'A005930', date(now.year, now.month, now.day),
                date(now.year, now.month, now.day))
            print('today result len', len(result))
            if len(result) == 1:
                break

        gevent.sleep(60)

    slack.send_slack_message('VI FOLLOWER COLLECT TODAY BULL START')
    now = datetime.now()
    now_date = now.year * 10000 + now.month * 100 + now.day
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm
    tdata = list(db_collection['yamount'].find({'date': now_date}))
    if len(tdata) == 0:
        market_code = morning_client.get_all_market_code()
        today_list = get_day_data(now, market_code)
        today_list = sorted(today_list,
                            key=lambda x: x['amount'],
                            reverse=True)
        today_list = today_list[:1000]
        codes = [c['code'] for c in today_list]
        db_collection['yamount'].insert_one({'date': now_date, 'codes': codes})

    record_uni_data()
    slack.send_slack_message('VI FOLLOWER COLLECT TODAY BULL DONE')
Пример #4
0
def get_day_data(query_date, market_code):
    for progress, code in enumerate(market_code):
        print('collect data', f'{progress+1}/{len(market_code)}', end='\r')
        data = morning_client.get_past_day_data(code, query_date, query_date)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            if data['amount'] >= 1000000000:
                yesterday_data[code] = data
    print('')
Пример #5
0
def get_yesterday_day_data(query_date, market_code):
    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect data', f'{progress+1}/{len(market_code)}', end='\r')
        data = morning_client.get_past_day_data(code, query_date, query_date)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_list.append(data)
    print('')
    return yesterday_list
Пример #6
0
def is_price_in_range(query_date, code):
    data = morning_client.get_past_day_data(code,
                                            query_date - timedelta(days=365),
                                            query_date)
    if len(data) > 2:
        highest_price = max([d['highest_price'] for d in data])
        highest_in_year[code] = highest_price
        current_price = data[-1]['close_price']
        profit_from_highest = (current_price -
                               highest_price) / highest_price * 100.0
        if -20.0 <= profit_from_highest:
            return True
    return False
Пример #7
0
def get_past_datas(code, today, yesterday, t):
    data = morning_client.get_past_day_data(code, yesterday, yesterday)
    if len(data) != 1:
        print('Cannot get yesterday data', code, yesterday)
        return
    code_dict[code]['yesterday_data'] = data[0]
    # get minute data time < (t / 100)
    min_data = morning_client.get_minute_data(code, today, today, int(t / 100))
    code_dict[code]['today_min_data'] = min_data

    code_dict[code]['today_tick_data'] = get_tick_data(code, today, t)

    for mdata in code_dict[code]['today_min_data']:
        if mdata['highest_price'] > code_dict[code]['vi_highest']:
            code_dict[code]['vi_highest'] = mdata['highest_price']
Пример #8
0
def _get_yesterday_day_data(query_date, market_code):
    global _yesterday_day_data
    fail_count = 0
    _LOGGER.info('START collect yesterday day data')
    for progress, code in enumerate(market_code):
        if _request_stop:
            break
        #print('collect yesterday day data', f'{progress+1}/{len(market_code)}/{fail_count}', end='\r')
        data = morning_client.get_past_day_data(code, query_date, query_date)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            _yesterday_day_data[code] = data
        else:
            fail_count += 1
    _LOGGER.info('DONE collect yesterday day data, fail cnt %d', fail_count)
Пример #9
0
def get_yesterday_amount_rank(today):
    market_code = morning_client.get_market_code()
    yesterday = holidays.get_yesterday(today)
    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    # For testing
    yesterday_list = yesterday_list[:100]
    return yesterday_list
Пример #10
0
def tick_data():
    code = 'A290720'
    from_datetime = datetime(2020, 2, 27, 8, 0, 0)
    until_datetime = datetime(2020, 2, 27, 10, 0, 0)
    trade_start_time = datetime(2020, 2, 27, 9, 0, 3)
    yesterday = holidays.get_yesterday(from_datetime.date())
    yesterday_data = morning_client.get_past_day_data(code, yesterday,
                                                      yesterday)[0]
    yesterday_data['code'] = code

    print('call fixture')
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm
    all_tick = []
    tick = get_tick_data(code, from_datetime, until_datetime, db_collection,
                         False)
    all_tick.extend(tick)
    ba_tick = get_tick_data(code + '_BA', trade_start_time, until_datetime,
                            db_collection, True)
    all_tick.extend(ba_tick)
    all_tick = sorted(all_tick, key=lambda x: x['date'])
    return code, all_tick, yesterday_data
Пример #11
0
def get_yesterday_data(today, market_code):
    yesterday = holidays.get_yesterday(today)
    one_week_before = holidays.get_date_by_previous_working_day_count(today, 5)
    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            min_data = morning_client.get_minute_data(code, one_week_before,
                                                      yesterday)
            if len(min_data) > 0:
                data['minute_max_volume'] = max(
                    [d['volume'] for d in min_data])
            else:
                data['minute_max_volume'] = 0

            yesterday_list.append(data)
    print('')
    return yesterday_list
Пример #12
0
def start_vi_follower():
    global db_collection

    market_code = morning_client.get_market_code()
    today = datetime.now().date()
    #if holidays.is_holidays(today):
    #    print('today is holiday')
    #    sys.exit(1)

    yesterday = holidays.get_yesterday(today)
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm

    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data', f'{progress+1}/{len(market_code)}', end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_data[code] = data
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list, key=lambda x: x['amount'], reverse=True)

    yesterday_list = yesterday_list[:100]
    for ydata in yesterday_list:
        code = ydata['code']
        sf = stock_follower.StockFollower(morning_client.get_reader(), db_collection, code, yesterday_data[code])
        sf.subscribe_at_startup()
        subscribe_code[code] = sf

    print('Start Listening...')

    stock_api.subscribe_alarm(morning_client.get_reader(), vi_handler)
    watch_thread = gevent.spawn(start_watch)
    gevent.joinall([watch_thread])
Пример #13
0
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm
    tick_data = get_tick_data(target_code, search_time.date(), db_collection)

    if len(tick_data) == 0:
        print('No Tick Data for', target_code, search_time.date())
        sys.exit(1)

    print('Tick Data len', len(tick_data), tick_data[0])
    market_code = morning_client.get_market_code()
    yesterday = holidays.get_yesterday(search_time.date())
    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    yesterday_list = yesterday_list[:300]

    if len(tick_data) > 0:
        start_search(target_code, search_time, yesterday_list, price, qty,
                     db_collection)
    else:
        print('Cannot find tick data')
Пример #14
0
def start_validation(codes=[]):
    global db_collection

    if len(codes) > 0:
        market_code = codes
    else:
        market_code = morning_client.get_market_code()

    today = datetime.now().date()
    yesterday = holidays.get_yesterday(today)
    db_collection = MongoClient(db.HOME_MONGO_ADDRESS).trade_alarm

    yesterday_list = []
    for progress, code in enumerate(market_code):
        print('collect yesterday data',
              f'{progress+1}/{len(market_code)}',
              end='\r')
        data = morning_client.get_past_day_data(code, yesterday, yesterday)
        if len(data) == 1:
            data = data[0]
            data['code'] = code
            #yesterday_data[code] = data
            yesterday_list.append(data)
    print('')
    yesterday_list = sorted(yesterday_list,
                            key=lambda x: x['amount'],
                            reverse=True)
    yesterday_list = yesterday_list[:1000]
    failed_tick_codes = []
    failed_ba_tick_codes = []
    # 1. validate yesterday_list :100's today tick data
    for ydata in yesterday_list:
        if not validate_tick_data(ydata['code'], today):
            failed_tick_codes.append(ydata['code'])
        if not validate_ba_tick_data(ydata['code'], today):
            failed_ba_tick_codes.append(ydata['code'])

    if len(failed_tick_codes) > 0:
        print('FAILED TICK', failed_tick_codes)
    else:
        print('TICK ALL SUCCESS')

    if len(failed_ba_tick_codes) > 0:
        print('FAILED BA TICK', failed_ba_tick_codes)
    else:
        print('TICK BA ALL SUCCESS')

    # 2. validate today alarm tick data
    alarm_list = get_alarm_list(today)
    alarm_failed_codes = []
    if len(alarm_list) > 0:
        for ac in alarm_list:
            if len(codes) > 0 and ac['3'] not in codes:
                continue

            if not validate_alarm_data(ac['3'], today, ac['date']):
                alarm_failed_codes.append(ac['3'])

        if len(alarm_failed_codes) > 0:
            print('FAILED ALARM CODES', alarm_failed_codes,
                  len(alarm_failed_codes), '/', len(alarm_list))
        else:
            print('ALARM ALL SUCCESS', len(alarm_list))
    else:
        print('NO ALARM TODAY')