Skip to content

Instantly share code, notes, and snippets.

@mr-yoo
Last active November 30, 2021 15:24
Show Gist options
  • Save mr-yoo/6421caec2a4eb92ec0c4f544365e5b80 to your computer and use it in GitHub Desktop.
Save mr-yoo/6421caec2a4eb92ec0c4f544365e5b80 to your computer and use it in GitHub Desktop.
키움증권 코드 구조
import sys
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
class SingletonInstane:
__instance = None
@classmethod
def __getInstance(cls):
return cls.__instance
@classmethod
def instance(cls, *args, **kargs):
cls.__instance = cls(*args, **kargs)
cls.instance = cls.__getInstance
return cls.__instance
class KiwoomBase(SingletonInstane):
def __init__(self):
self.ocx = QAxWidget("KHOPENAPI.KHOpenAPICtrl.1")
self.ocx.OnEventConnect.connect(self._handler_login)
self.ocx.dynamicCall("CommConnect()")
self.loop = QEventLoop()
self.loop.exec()
def _handler_login(self, err):
self.loop.exit()
def CommRqData(self, rqname, trcode, next, screen):
self.ocx.dynamicCall("CommRqData(QString, QString, int, QString)", rqname, trcode, next, screen)
def SetInputValue(self, id, value):
self.ocx.dynamicCall("SetInputValue(QString, QString)", id, value)
def GetCommData(self, trcode, rqname, index, item):
data = self.ocx.dynamicCall("GetCommData(QString, QString, int, QString)", trcode, rqname, index, item)
return data.strip()
class Fundamental(KiwoomBase):
def __init__(self):
super().__init__()
self.ocx.OnReceiveTrData.connect(self._handler_tr)
def get(self, ticker):
data.SetInputValue("종목코드", ticker)
self.CommRqData("funadamental", "opt10001", 0, "0101")
self.loop = QEventLoop()
self.loop.exec()
return self.__tr_data
def _handler_tr(self, screen, rqname, trcode, record, next):
per = self.GetCommData(trcode, rqname, 0, "PER")
pbr = self.GetCommData(trcode, rqname, 0, "PBR")
self.__tr_data = {
'PER' : per,
'PBR' : pbr
}
self.loop.exit()
if __name__ == "__main__":
app = QApplication(sys.argv)
data = Fundamental()
print(data.get("005930"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment