ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [CREON PLUS API 활용] 차트 지표 계산 하기
    개발/파이썬 2018.06.06 11:09

    CREON PLUS 에서 제공하는 차트 지표 계산 라이브러리를 이용해서 지표계산하는 간단한 코드입니다 

    ■ 지표 리스트 - CREON PLUS 에서 제공하는 모든 계산 가능한 지표 나열

    ■ 계산할 지표를 선택 - 예제에서는 이동평균, Stochastic Slow, MACD, RSI, Binary Wave MACD, TSF 지표 계산 코드가 들어 있습니다. 

    ■ 지표 조건 변경 - 지표 조건을 변경 하는 예제 코드입니다. 


    지표 계산 로직은 공통으로 만들어서, 다른 지표 계산하는 데도 별 어려움없이 적용 가능합니다.

    코드를 쉽게 하기 위해, 하이닉스(000660) 종목의 100일치 일간 차트를 먼저 가져온 후 해당 데이터를 이용해 지표를 계산합니다.





    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    import sys
    from PyQt5.QtWidgets import *
    import win32com.client
    import ctypes
     
    ################################################
    # PLUS 공통 OBJECT
    g_objCodeMgr = win32com.client.Dispatch('CpUtil.CpCodeMgr')
    g_objCpStatus = win32com.client.Dispatch('CpUtil.CpCybos')
    g_objCpTrade = win32com.client.Dispatch('CpTrade.CpTdUtil')
     
     
    ################################################
    # PLUS 실행 기본 체크 함수
    def InitPlusCheck():
        # 프로세스가 관리자 권한으로 실행 여부
        if ctypes.windll.shell32.IsUserAnAdmin():
            print('정상: 관리자권한으로 실행된 프로세스입니다.')
        else:
            print('오류: 일반권한으로 실행됨. 관리자 권한으로 실행해 주세요')
            return False
     
        # 연결 여부 체크
        if (g_objCpStatus.IsConnect == 0):
            print("PLUS가 정상적으로 연결되지 않음. ")
            return False
     
        # # 주문 관련 초기화 - 계좌 관련 코드가 있을 때만 사용
        # if (g_objCpTrade.TradeInit(0) != 0):
        #     print("주문 초기화 실패")
        #     return False
     
        return True
     
     
    # 차트 기본 데이터 통신
    class CpStockChart:
        def __init__(self):
            self.objCpCybos = win32com.client.Dispatch("CpUtil.CpCybos")
            self.objStockChart = win32com.client.Dispatch("CpSysDib.StockChart")
     
        def Request(self, code, objSeries):
            #######################################################
            # 1. 일간 차트 데이터 요청
            self.objStockChart.SetInputValue(0, code)  # 종목 코드 -
            self.objStockChart.SetInputValue(1, ord('2'))  # 개수로 조회
            self.objStockChart.SetInputValue(4100)  # 최근 100일치
            self.objStockChart.SetInputValue(5, [023458])  # 날짜,시가,고가,저가,종가,거래량
            self.objStockChart.SetInputValue(6, ord('D'))  # '차트 주기 - 일간 차트 요청
            self.objStockChart.SetInputValue(9, ord('1'))  # 수정주가 사용
            self.objStockChart.BlockRequest()
     
            rqStatus = self.objStockChart.GetDibStatus()
            rqRet = self.objStockChart.GetDibMsg1()
            print("통신상태", rqStatus, rqRet)
            if rqStatus != 0:
                exit()
     
            #######################################################
            # 2. 일간 차트 데이터 --> CpIndexes.CpSeries 로 변환
            len = self.objStockChart.GetHeaderValue(3)
     
            print("날짜""시가""고가""저가""종가""거래량")
            print("==============================================-")
            for i in range(len):
                day = self.objStockChart.GetDataValue(0len - i - 1)
                open = self.objStockChart.GetDataValue(1len - i - 1)
                high = self.objStockChart.GetDataValue(2len - i - 1)
                low = self.objStockChart.GetDataValue(3len - i - 1)
                close = self.objStockChart.GetDataValue(4len - i - 1)
                vol = self.objStockChart.GetDataValue(5len - i - 1)
                print(day, open, high, low, close, vol)
                # objSeries.Add 종가, 시가, 고가, 저가, 거래량, 코멘트
                objSeries.Add(closeopen, high, low, vol)
            print("==============================================-")
     
            return
     
     
    # 지표 계산 관리 클래스
    class CpIndexTest:
        def __init__(self):
            # 1. 차트 데이터 통신 요청
            self.objChart = CpStockChart()
            # CpIndexes.CpSeries : 차트 기본 데이터 관리 PLUS 객체
            self.objSeries = win32com.client.Dispatch("CpIndexes.CpSeries")
            # CpIndexes.CpIndex : 지표 계산을 담당하는 PLUS 객체
            self.objIndex = win32com.client.Dispatch("CpIndexes.CpIndex")
     
            # 테스트를 위해 하이닉스 종목으로 미리 차트 데이터 구성
            self.objChart.Request('A000660', self.objSeries)
     
        # 플러스에서 제공하는 모든 지표와 지표의 기본 조건 나열
        def listAllIndex(self):
            allIndexList = []
            for i in range(06):
                indexlist = self.objIndex.GetChartIndexCodeListByIndex(i)
                # print(indexlist)
                allIndexList += indexlist
     
            for index in allIndexList:
                self.objIndex.put_IndexKind(index)
                self.objIndex.put_IndexDefault(index)
     
                print('%s 변수1 %d 변수2 %d 변수3 %d 변수4 %d Signal %d'
                      % (index, self.objIndex.get_Term1(), self.objIndex.get_Term2(), self.objIndex.get_Term3(),
                         self.objIndex.get_Term4(), self.objIndex.get_Signal()))
     
        # 주어진 지표의 이름(indexName) 으로 지표 계산 및 데이터 리턴
        def makeIndex(self, indexName, lineName, chartvalue):
            self.objIndex.series = self.objSeries
            self.objIndex.put_IndexKind(indexName)  # 계산할 지표:
            self.objIndex.put_IndexDefault(indexName)  # 지표 기본 변수 자동 세팅
     
            condList = {}
            condList['조건1'= self.objIndex.get_Term1()
            condList['조건2'= self.objIndex.get_Term2()
            condList['조건3'= self.objIndex.get_Term3()
            condList['조건4'= self.objIndex.get_Term4()
            condList['Signal'= self.objIndex.get_Signal()
     
            condNameList = ['조건1''조건2''조건3''조건4''Signal']
     
            indexcond = indexName
            for conName in condNameList:
                if (condList[conName] != 0):
                    indexcond += ' ' + conName + ' ' + str(condList[conName])
     
            print(indexcond)
     
            # 지표 데이터 계산 하기
            self.objIndex.Calculate()
     
            cntofIndex = self.objIndex.ItemCount
            print("지표 개수:  ", cntofIndex)
            # 지표의 각 라인 이름은 HTS 차트의 각 지표 조건 참고
            for index in range(cntofIndex):
                name = lineName[index]
                chartvalue[name] = []
                cnt = self.objIndex.GetCount(index)
                for j in range(cnt):
                    value = self.objIndex.GetResult(index, j)
                    chartvalue[name].append(value)
     
        # 예제를 위해 StochasticSlow 지표의 조건 값을 12,5,5 로 변경
        def makeStochasticSlow(self, chart_value):
            self.objIndex.series = self.objSeries
            self.objIndex.put_IndexKind("Stochastic Slow")  # 계산할 지표: Stochastic Slow
            self.objIndex.put_IndexDefault("Stochastic Slow")  # Stochastic Slow 지표 기본 변수 자동 세팅
     
            print("Stochastic Slow 변수", self.objIndex.get_Term1(), self.objIndex.get_Term2(), self.objIndex.get_Signal())
     
            print('지표 조건값 변경')
            self.objIndex.Term1 = 12
            self.objIndex.Term2 = 5
            self.objIndex.Signal = 5
            print("Stochastic Slow 변수", self.objIndex.get_Term1(), self.objIndex.get_Term2(), self.objIndex.get_Signal())
            # 지표 데이터 계산 하기
            self.objIndex.Calculate()
     
            cntofIndex = self.objIndex.ItemCount
            print("지표 개수:  ", cntofIndex)
            # 지표의 각 라인 이름은 HTS 차트의 각 지표 조건 참고
            indexName = ["SLOW K""SLOW D"]
            for index in range(cntofIndex):
                name = indexName[index]
                chart_value[name] = []
                cnt = self.objIndex.GetCount(index)
                for j in range(cnt):
                    value = self.objIndex.GetResult(index, j)
                    chart_value[name].append(value)
     
     
    ################################################
    # 테스트를 위한 메인 화면
    class MyWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.objCpIndex = CpIndexTest()
     
            # plus 상태 체크
            if InitPlusCheck() == False:
                exit()
     
            # 지표의 기본 정보 세팅
            self.initIndexInfo()
     
            self.setWindowTitle("지표테스트")
            self.setGeometry(300300300180)
     
            nH = 20
            btnPrint = QPushButton('지표리스트', self)
            btnPrint.move(20, nH)
            btnPrint.resize(20030)
            btnPrint.clicked.connect(self.btnPrint_clicked)
            nH += 50
     
            labelDesc1 = QLabel('계산할 지표를 선택', self)
            labelDesc1.move(20, nH)
            labelDesc1.resize(20030)
            nH += 30
     
            self.comboStg = QComboBox(self)
            # self.comboStg.move(20, nH)
            for ikey, ivalue in self.indexList.items():
                self.comboStg.addItem(ikey)
     
            self.comboStg.currentIndexChanged.connect(self.OnComboChanged)
            self.comboStg.move(20, nH)
            self.comboStg.resize(20030)
            nH += 50
     
            labelDesc2 = QLabel('지표 조건 변경 방법 5,3,3 --> 12,5,5', self)
            labelDesc2.move(20, nH)
            labelDesc2.resize(20030)
            nH += 30
     
            btnStochSlow = QPushButton('STOCHASTIC SLOW 조건변경', self)
            btnStochSlow.move(20, nH)
            btnStochSlow.resize(20030)
            btnStochSlow.clicked.connect(self.btnStochSlow_clicked)
            nH += 50
     
            btnExit = QPushButton('종료', self)
            btnExit.move(20, nH)
            btnExit.clicked.connect(self.btnExit_clicked)
            nH += 50
     
            self.setGeometry(300300300, nH)
     
        # 지표의 기본 정보 세팅
        def initIndexInfo(self):
            # 지표 기본 정보, 지표 이름 = 지표 라인 리스트
            self.indexList = {}
            self.indexList['지표선택 없음'= ['없음']
            self.indexList['이동평균(라인1개)'= ['이동평균']
            self.indexList['Stochastic Slow'= ['SLOW K''SLOW D']
            self.indexList['MACD'= ['MACD''SIGNAL''OSCILLATOR']
            self.indexList['RSI'= ['RSI''SIGNAL']
            self.indexList['Binary Wave MACD'= ['BWMACD''SIGNAL''OSCILLATOR']
            self.indexList['TSF'= ['TSF''SIGNAL']
     
        def OnComboChanged(self):
            indexname = self.comboStg.currentText()
            if (indexname == '지표선택 없음'):
                return
            print(indexname)
     
            chartData = {}
            lineName = self.indexList[indexname]
     
            self.objCpIndex.makeIndex(indexname, lineName, chartData)
     
            for key, values in chartData.items():
                print(key)
                print(values)
     
        def btnPrint_clicked(self):
            self.objCpIndex.listAllIndex()
     
        # 지표 변수 값을 조정함
        def btnStochSlow_clicked(self):
            chartData = {}
            self.objCpIndex.makeStochasticSlow(chartData)
     
            for key, values in chartData.items():
                print(key)
                print(values)
     
        def btnExit_clicked(self):
            exit()
            return
     
     
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        myWindow = MyWindow()
        myWindow.show()
        app.exec_()
    cs


    댓글 0

Designed by black7375.