示例#1
0
文件: creon.py 项目: rosejam/TTT
    def get_chart(self,
                  code,
                  target='A',
                  unit='D',
                  n=None,
                  date_from=None,
                  date_to=None):
        """
        https://money2.creontrade.com/e5/mboard/ptype_basic/HTS_Plus_Helper/DW_Basic_Read_Page.aspx?boardseq=284&seq=102&page=1&searchString=StockChart&p=8841&v=8643&m=9505
        "전일대비"는 제공하지 않으므로 직접 계산해야 함
        target: 'A', 'U' == 종목, 업종
        unit: 'D', 'W', 'M', 'm', 'T' == day, week, month, min, tick
        return <dict>dict_chart
        """
        _fields = []
        _keys = []
        if unit == 'm':
            _fields = [0, 1, 2, 3, 4, 5, 6, 8, 9, 37]
            _keys = [
                'date', 'time', 'open', 'high', 'low', 'close', 'diff',
                'volume', 'price', 'diffsign'
            ]
        else:
            _fields = [0, 2, 3, 4, 5, 6, 8, 9]  # , 37]
            _keys = [
                'date', 'open', 'high', 'low', 'close', 'diff', 'volume',
                'price'
            ]  # , 'diffsign']

        if date_to is None:
            date_to = util.get_str_today()

        self.obj_CpSysDib_StockChart.SetInputValue(
            0, code)  # ! 동주가 앞에 target직접 붙이는 거로 바꿈 # 주식코드: A, 업종코드: U
        if n is not None:
            self.obj_CpSysDib_StockChart.SetInputValue(
                1, ord('2'))  # 0: ?, 1: 기간, 2: 개수
            self.obj_CpSysDib_StockChart.SetInputValue(4, n)  # 요청 개수
        if date_from is not None or date_to is not None:
            if date_from is not None and date_to is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(
                    1, ord('1'))  # 0: ?, 1: 기간, 2: 개수
            if date_from is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(3, date_from)  # 시작일
            if date_to is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(2, date_to)  # 종료일
        self.obj_CpSysDib_StockChart.SetInputValue(5, _fields)  # 필드
        self.obj_CpSysDib_StockChart.SetInputValue(6, ord(unit))
        self.obj_CpSysDib_StockChart.SetInputValue(7, 1)  # 분틱차트 주기
        self.obj_CpSysDib_StockChart.SetInputValue(
            9, ord('1'))  # 0: 무수정주가, 1: 수정주가

        def req(prev_result):
            self.obj_CpSysDib_StockChart.BlockRequest()

            status = self.obj_CpSysDib_StockChart.GetDibStatus()
            msg = self.obj_CpSysDib_StockChart.GetDibMsg1()
            if status != 0:
                return None

            cnt = self.obj_CpSysDib_StockChart.GetHeaderValue(3)
            list_item = []
            for i in range(cnt):
                dict_item = {
                    k:
                    self.obj_CpSysDib_StockChart.GetDataValue(j, cnt - 1 - i)
                    for j, k in enumerate(_keys)
                }
                # type conversion
                # dict_item['diffsign'] = chr(dict_item['diffsign'])
                for k in ['open', 'high', 'low', 'close', 'diff']:
                    dict_item[k] = float(dict_item[k])

                # additional fields
                if not (dict_item['close'] == dict_item['diff']):
                    dict_item['diffratio'] = (
                        dict_item['diff'] /
                        (dict_item['close'] - dict_item['diff'])) * 100
                else:
                    dict_item['diffratio'] = 0
                if not (dict_item['volume'] == 0):
                    dict_item[
                        'average'] = dict_item['price'] / dict_item['volume']
                else:
                    dict_item['average'] = dict_item['open']

                list_item.append(dict_item)
            return list_item

        # 연속조회 처리
        result = req([])
        while self.obj_CpSysDib_StockChart.Continue:
            self.wait()
            _list_item = req(result)
            if len(_list_item) > 0:
                result = _list_item + result
                if n is not None and n <= len(result):
                    break
            else:
                break
        return result
示例#2
0
    def get_chart(self,
                  code,
                  target='A',
                  unit='D',
                  n=None,
                  date_from=None,
                  date_to=None):
        _fields = []
        _keys = []
        if unit == 'm':
            _fields = [0, 1, 2, 3, 4, 5, 6, 8, 9, 37]
            _keys = [
                'date', 'time', 'open', 'high', 'low', 'close', 'diff',
                'volume', 'price', 'diffsign'
            ]
        else:
            _fields = [0, 2, 3, 4, 5, 6, 8, 9, 37]
            _keys = [
                'date', 'open', 'high', 'low', 'close', 'diff', 'volume',
                'price', 'diffsign'
            ]

        if date_to is None:
            date_to = util.get_str_today()

        self.obj_CpSysDib_StockChart.SetInputValue(0, target +
                                                   code)  # 주식코드: A, 업종코드: U
        if n is not None:
            self.obj_CpSysDib_StockChart.SetInputValue(
                1, ord('2'))  # 0: ?, 1: 기간, 2: 개수
            self.obj_CpSysDib_StockChart.SetInputValue(4, n)  # 요청 개수
        if date_from is not None or date_to is not None:
            if date_from is not None and date_to is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(
                    1, ord('1'))  # 0: ?, 1: 기간, 2: 개수
            if date_from is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(3, date_from)  # 시작일
            if date_to is not None:
                self.obj_CpSysDib_StockChart.SetInputValue(2, date_to)  # 종료일
        self.obj_CpSysDib_StockChart.SetInputValue(5, _fields)  # 필드
        self.obj_CpSysDib_StockChart.SetInputValue(6, ord(unit))
        self.obj_CpSysDib_StockChart.SetInputValue(
            9, ord('1'))  # 0: 무수정주가, 1: 수정주가

        def req(prev_result):
            self.obj_CpSysDib_StockChart.BlockRequest()

            status = self.obj_CpSysDib_StockChart.GetDibStatus()
            msg = self.obj_CpSysDib_StockChart.GetDibMsg1()
            if status != 0:
                return None

            cnt = self.obj_CpSysDib_StockChart.GetHeaderValue(3)
            list_item = []
            for i in range(cnt):
                dict_item = {
                    k:
                    self.obj_CpSysDib_StockChart.GetDataValue(j, cnt - 1 - i)
                    for j, k in enumerate(_keys)
                }

                # type conversion
                dict_item['diffsign'] = chr(dict_item['diffsign'])
                for k in ['open', 'high', 'low', 'close', 'diff']:
                    dict_item[k] = float(dict_item[k])
                for k in ['volume', 'price']:
                    dict_item[k] = int(dict_item[k])

                # additional fields
                dict_item['diffratio'] = (
                    dict_item['diff'] /
                    (dict_item['close'] - dict_item['diff'])) * 100
                list_item.append(dict_item)
            return list_item

        # 연속조회 처리
        result = req([])
        while self.obj_CpSysDib_StockChart.Continue:
            self.avoid_reqlimitwarning()
            _list_item = req(result)
            if len(_list_item) > 0:
                result = _list_item + result
                if n is not None and n <= len(result):
                    break
            else:
                break
        return result