Exemple #1
0
def realinfo(realtypes):
    from koapy.openapi.RealType import RealType

    def get_realtypes():
        if realtypes:
            if '-' in realtypes:
                with click.open_file('-', 'r') as f:
                    for realtype in f:
                        yield realtype.strip()
            else:
                for realtype in realtypes:
                    yield realtype
        else:
            while True:
                try:
                    realtype = click.prompt('realtype', prompt_suffix=' >>> ')
                    realtype = realtype.strip()
                    if realtype == 'exit':
                        break
                    if realtype:
                        yield realtype
                except EOFError:
                    break

    for realtype in get_realtypes():
        fids = RealType.get_fids_by_realtype(realtype)
        if fids:
            names = [RealType.Fid.get_name_by_fid(fid, str(fid)) for fid in fids]
            for fid, name in zip(fids, names):
                click.echo('  [%s] = %s' % (fid, name))
        else:
            click.echo('Given realtype is invalid')
 def register_code(self, code):
     request = KiwoomOpenApiService_pb2.BidirectionalRealRequest()
     code_list = [code]
     fid_list = RealType.get_fids_by_realtype('주식시세')
     request.register_request.code_list.extend(code_list)  # pylint: disable=no-member
     request.register_request.fid_list.extend(fid_list)  # pylint: disable=no-member
     self._request_observer.on_next(request)
     logging.debug('Registering code %s for real events', code)
 def GetRealDataForCodesAsStream(self,
                                 codes,
                                 fids=None,
                                 realtype=None,
                                 screen_no=None,
                                 infer_fids=False,
                                 readable_names=False,
                                 fast_parse=False):
     if isinstance(codes, str):
         codes = [codes]
     if fids is None:
         fids = RealType.get_fids_by_realtype('주식시세')
     if realtype is None:
         realtype = '0'
     responses = self.RealCall(screen_no, codes, fids, realtype, infer_fids,
                               readable_names, fast_parse)
     return responses
Exemple #4
0
def realdata(port, verbose):
    set_verbosity(verbose)
    from koapy import KiwoomOpenApiContext
    from koapy.openapi.RealType import RealType
    with KiwoomOpenApiContext(port=port, client_check_timeout=client_check_timeout) as context:
        RealType.dump_realtype_by_desc(context=context)
Exemple #5
0
def watch(codes, input, fids, realtype, output, format, port, verbose):
    if (codes, fids, realtype) == (tuple(), tuple(), None):
        fail_with_usage()

    set_verbosity(verbose)

    codes_len = len(codes)

    if codes_len == 0:
        if input is None:
            fail_with_usage('Either code or input should be given.')
        if not os.path.exists(input):
            fail_with_usage('Given input does not exist.')

        if os.path.isfile(input):
            if input.endswith('.xlsx'):
                import pandas as pd
                df = pd.read_excel(input, dtype=str)
                code_column = '종목코드'
                if code_column in df:
                    codes = df[code_column]
                else:
                    codes = df.iloc[0]
                codes_len = len(codes)
            elif input.endswith('.txt'):
                with open(input) as f:
                    codes = [line.strip() for line in f]
                codes_len = len(codes)
            else:
                fail_with_usage('Unrecognized input type.')
        else:
            fail_with_usage('Unrecognized input type.')

    if realtype is not None:
        from koapy.openapi.RealType import RealType
        fids_from_realtype = RealType.get_fids_by_realtype(realtype)
        fids = list(set(fids).union(set(fids_from_realtype)))

    if not codes:
        fail_with_usage('No codes to watch. Set --code or --input.')

    if not fids:
        fail_with_usage('Cannot infer fids to watch. Set either --fid or --realtype.')

    import datetime
    import pandas as pd

    from koapy import KiwoomOpenApiContext
    from koapy.openapi.RealType import RealType

    def parse_message(message):
        fids = event.single_data.names
        names = [RealType.Fid.get_name_by_fid(fid, str(fid)) for fid in fids]
        values = event.single_data.values
        dic = dict((name, value) for fid, name, value in zip(fids, names, values) if name != fid)
        series = pd.Series(dic)
        return series

    if format == 'json':
        def print_message(message):
            click.echo(parse_message(message).to_json(), file=output)
    else:
        def print_message(message):
            code = event.arguments[0].string_value
            name = event.arguments[1].string_value
            click.echo('[%s] [%s]' % (code, name), file=output)
            click.echo('[%s]' % datetime.datetime.now(), file=output)
            click.echo(parse_message(message).to_markdown(), file=output)

    with KiwoomOpenApiContext(port=port, client_check_timeout=client_check_timeout, verbosity=verbose) as context:
        context.EnsureConnected()

        for event in context.GetRealDataForCodesAsStream(codes, fids, infer_fids=True):
            print_message(event)
Exemple #6
0
def realdata(verbose):
    set_verbosity(verbose)
    from koapy.openapi.RealType import RealType
    RealType.dump_realtype_by_desc()