Exemple #1
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        #인스턴스 생성 후 로그인
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        #1초타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        #10초 타이머
        self.timer2 = QTimer(self)
        self.timer2.start(10000)
        self.timer2.timeout.connect(self.timeout2)

        #계좌수 및 계좌번호
        accounts_num = int(self.kiwoom.get_login_info("ACCOUNT_CNT"))
        accounts = self.kiwoom.get_login_info("ACCNO")
        accounts_list = accounts.split(';')[0:accounts_num]

        #UI에 기능추가
        self.comboBox.addItems(accounts_list)
        self.lineEdit.textChanged.connect(self.code_changed)
        self.pushButton.clicked.connect(self.send_order)
        self.pushButton_2.clicked.connect(self.check_balance)
Exemple #2
0
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.ui = uic.loadUi("mainwindow.ui")
        self.time_start = 0
        self.time_end = 0
        self.upperLimit = 0
        self.lowerLimit = 0
        self.data = None  # 주식 table
        self.item = 0

        self.kiwoom = Kiwoom()

        # 함수 바인딩 부분
        self.ui.btnStart.clicked.connect(self.start)  # 인공지능 투자 실행
        self.ui.btnEnd.clicked.connect(self.end)  # 인공지능 투자 종료
        self.ui.btnAccountInfo.clicked.connect(
            self.check_balance)  # 계좌 정보 가져오기

        self.ui.btnSellPriceApply.clicked.connect(self.sellPriceApply)
        self.ui.btnSellPriceCancel.clicked.connect(self.sellPriceCancel)

        self.ui.actionLogIn.triggered.connect(self.login)
        self.ui.actionLogOut.triggered.connect(self.logout)
        self.ui.actionLogState.triggered.connect(self.logState)

        self.ui.show()
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        #self.show()

        #########################################################################
        # Load Class
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        #self.holicAgent = holic_agent.HolicAgent()
        self.mholicstock = modelholicstock.ModelHolicStock()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "==== PROD ===="
        else:
            self.serverGubun = "DEV"

        #########################################################################
        #메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # Timer auto 실행
        self.timer_auto = QTimer(self)
        self.timer_auto.start(1000 * 10)
        self.timer_auto.timeout.connect(self.timer_auto_timeout)

        accouns_num = int(self.kiwoom.getLoginInfo("ACCOUNT_CNT"))
        accounts = self.kiwoom.getLoginInfo("ACCNO")
        accounts_list = accounts.split(';')[0:accouns_num]
        self.accountComboBox.addItems(accounts_list)

        #########################################################################
        # Event Callback connect
        self.pushButton.clicked.connect(self.send_order)
        self.lineEdit.textChanged.connect(self.code_changed)
        self.pushButton_2.clicked.connect(self.check_balance)

        self.btn_autoorder.clicked.connect(self.start_auto_order)
        self.btn_stopautoorder.clicked.connect(self.stop_auto_order)

        self.btn_start_mon.clicked.connect(self.automated_stock_mon)

        self.btn_start_analysis.clicked.connect(self.analysis_condition)

        #########################################################################
        # 메인 변수
        self.df_condition = pd.DataFrame()
        self.df_anaysis = pd.DataFrame()
Exemple #4
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "실제운영"
        else:
            self.serverGubun = "모의투자"

        self.codeList = self.kiwoom.getCodeList("0")

        # 메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # 잔고 및 보유종목 조회 타이머
        self.inquiryTimer = QTimer(self)
        self.inquiryTimer.start(1000*10)
        self.inquiryTimer.timeout.connect(self.timeout)

        # 자동 매도 테스트 타이머
        self.testTimer = QTimer(self)
        self.testTimer.start(1000*60)
        self.testTimer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)
        self.inquiryBtn.clicked.connect(self.inquiryBalance)
        self.sellAllBtn.clicked.connect(self.sellAll)


        # 자동 주문
        # 자동 주문을 활성화 하려면 True로 설정
        self.isAutomaticOrder = False

        # 자동 선정 종목 리스트 테이블 설정
        self.setAutomatedStocks()

        # 한번 살 금액
        self.money = 100000
Exemple #5
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()
        self.codeList = self.kiwoom.getCodeList("0")

        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)
Exemple #6
0
def init():
    """ [init()함수] : 초기화 함수
        - QApplication 인스턴스를 전역 변수로 초기화
        - KIWOOM 인스턴스를 전역 변수로 초기화 및 로그인
        - Pymysql cursor 인스턴스를 전역 변수로 초기화
    """
    app = QApplication(sys.argv)  # QWidget 사용 전 QApplication 인스턴스 생성
    # Kiwoom 인스턴스 생성
    kiwoom = Kiwoom()
    # 2. 로그인
    kiwoom.login()

    # DB Connect
    conn = pymysql.connect(host='211.180.114.131',
                           port=3306,
                           user='******',
                           passwd='!J12341234',
                           db='financial_analysis',
                           charset='utf8')
    cur = conn.cursor()

    return app, kiwoom, cur
Exemple #7
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "실제운영"
        else:
            self.serverGubun = "모의투자"

        self.codeList = self.kiwoom.getCodeList("0")

        # 메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # 잔고 및 보유종목 조회 타이머
        self.inquiryTimer = QTimer(self)
        self.inquiryTimer.start(1000*10)
        self.inquiryTimer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)
        self.inquiryBtn.clicked.connect(self.inquiryBalance)

        # 자동 주문
        # 자동 주문을 활성화 하려면 True로 설정
        self.isAutomaticOrder = False

        # 자동 선정 종목 리스트 테이블 설정
        self.setAutomatedStocks()
Exemple #8
0
class Form(QtWidgets.QDialog):
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.ui = uic.loadUi("mainwindow.ui")
        self.time_start = 0
        self.time_end = 0
        self.upperLimit = 0
        self.lowerLimit = 0
        self.data = None  # 주식 table
        self.item = 0

        self.kiwoom = Kiwoom()

        # 함수 바인딩 부분
        self.ui.btnStart.clicked.connect(self.start)  # 인공지능 투자 실행
        self.ui.btnEnd.clicked.connect(self.end)  # 인공지능 투자 종료
        self.ui.btnAccountInfo.clicked.connect(
            self.check_balance)  # 계좌 정보 가져오기

        self.ui.btnSellPriceApply.clicked.connect(self.sellPriceApply)
        self.ui.btnSellPriceCancel.clicked.connect(self.sellPriceCancel)

        self.ui.actionLogIn.triggered.connect(self.login)
        self.ui.actionLogOut.triggered.connect(self.logout)
        self.ui.actionLogState.triggered.connect(self.logState)

        self.ui.show()

        # self.ui.previewSmall.setPixmap(QPixmap('cat04_256.png'))

    def check_balance(self):
        self.kiwoom.reset_opw00018_output()
        account_number = self.kiwoom.get_login_info('ACCNO')  # 계좌번호를 가져옴
        account_number = account_number.split(';')[0]

        #가져온 계좌번호를 통해 보유하고 있는 종목을 가져오
        self.kiwoom.set_input_value("계좌번호", account_number)
        self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 0, "2000")

        while self.kiwoom.remained_data:
            time.sleep(0.2)
            self.kiwoom.set_input_value("계좌번호", account_number)
            self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 2, "2000")

        #현재 내 계좌 현황
        self.item = self.kiwoom.opw00018_output['single']
        self.ui.lblTotalBuy.setText(self.item[0])
        self.ui.lblTotalEvaluation.setText(self.item[1])
        self.ui.lblEvalProfit.setText(self.item[2])
        self.ui.lblEvalProfitRatio.setText(self.item[3])
        self.ui.lblTotalAssets.setText(self.item[4])
        self.ui.lblRemainAssets.setText(str(self.item[5]))

        #보유 주식에 대한 정보 출력
        item_count = len(self.kiwoom.opw00018_output['multi'])
        self.ui.tblWgtTable.setRowCount(item_count)

        for j in range(item_count):
            row = self.kiwoom.opw00018_output['multi'][j]
            for i in range(len(row)):
                item = QTableWidgetItem(row[i])
                #item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.ui.tblWgtTable.setItem(j, i, item)

        self.ui.tblWgtTable.resizeRowsToContents()

    def start(self):
        now = datetime.datetime.now().strftime('%Y%m%d')
        self.data = self.getData("038160", now)  # 데이터 얻어오기
        print(self.data)
        print('Done')
        result = self.analysis()  # 분석(LSTM)
        self.trade_stocks()  # 매입 요청
        self.monitoring()  # TODO: 멀티쓰레딩으로 구현하기. 적정 주가 매도

    def end(self):
        # TODO: monitoring을 종료시키기
        self.ui.lblRunningTime.setText('0:00:00')

    #개인 계좌 정보 및 보유 주식목록 가져오기
    def getData(self, code, start):
        self.kiwoom.ohlcv = {
            'date': [],
            'open': [],
            'high': [],
            'low': [],
            'close': [],
            'volume': []
        }

        self.kiwoom.set_input_value("종목코드", code)  # 가져오고자 하는 종목에 대한 종목코드
        self.kiwoom.set_input_value("기준일자", start)  # 가자오고자 하는 종목의 일자
        self.kiwoom.set_input_value("수정주가부분", 1)
        self.kiwoom.comm_rq_data("opt10081_req", "opt10081", 0, "0101")
        time.sleep(0.2)  #tr요청시 0.2초 간격을 주기 위해 사용

        df = DataFrame(self.kiwoom.ohlcv,
                       columns=['open', 'high', 'low', 'close', 'volume'],
                       index=self.kiwoom.ohlcv['date'])

        df.to_csv(code + '.csv')

        return df

    def analysis(self):
        BUFSIZE = 2**30  # about 1 GB

        async def tcp_echo_client(message):
            reader, writer = await asyncio.open_connection('127.0.0.1', 13513)

            print(f'Send: {message!r}')
            # writer.write(message.encode())
            self.data.to_csv('./code_tests/data.csv')  # 파일을 먼저 저장해준 다음
            with open('./code_tests/data.csv', 'rb') as f:
                d = f.read()
            writer.write(d)  # 저장된 메세지만 날리기

            data = await reader.read(BUFSIZE)
            print(f'Received: {data.decode()!r}')

            print('Close the connection')
            writer.close()

        asyncio.run(tcp_echo_client('Hello World!'))

    def trade_stocks(self):
        hoga_lookup = {'지정가': "00", '시장가': "03"}

        #주식 거래 시 필요한 계좌번호 추출
        account = self.kiwoom.get_login_info('ACCNO')  # 계좌번호를 가져옴
        account = account.split(';')[0]

        self.check_balance()
        remain_deposit = self.item[5]

        f = open("buy_list.txt", "rt")
        total_list = f.readlines()
        f.close()

        row_count = len(total_list)

        for j in range(row_count):
            row_data = total_list[j]
            split_row_data = row_data.split(';')
            if float(split_row_data[2]) > 0:
                code = split_row_data[0]
                hoga = '시장가'
                self.kiwoom.set_input_value("종목코드", code)
                price = self.kiwoom._opt10001("opt_10001_req", "opt10001")
                num = int(int(remain_deposit) / int(price))
                self.kiwoom.send_order("send_order_req", "0101", account, 2,
                                       code, num, 0, hoga_lookup[hoga], "")

    def analysis(self):  #TODO: 기본적으로 LSTM
        pass  #TODO: 내일하기

    def buy(self):
        res = self.kiwoom.dynamicCall("GetLoginInfo(\"USER_NAME\")")
        print(res)
        # pass

    def monitoring(self):
        self.monitoringThread = MonitoringThread()  # 쓰레드 생성
        self.ui.btnEnd.clicked.connect(self.monitoringThread.terminate)
        self.monitoringThread.sigUpdate.connect(self.updated)
        self.monitoringThread.start()  # 쓰레드 시작

    def updated(self, duringTime):
        self.ui.lblRunningTime.setText("%s" %
                                       datetime.timedelta(seconds=duringTime))

    def sellPriceApply(self):
        self.upperLimit = self.ui.txtUpperLimit.text()
        self.lowerLimit = self.ui.txtLowerLimit.text()
        QMessageBox.about(self, "판매가 설정", "적용되었습니다.")

    def sellPriceCancel(self):
        self.ui.txtUpperLimit.setText('')
        self.ui.txtLowerLimit.setText('')

    def login(self):
        # self.kiwoom = QAxWidget("KHOPENAPI.KHOpenAPICtrl.1")
        self.kiwoom.comm_connect()

    def logout(self):
        pass

    def logState(self):
        ret = self.kiwoom.dynamicCall("GetConnectState()")
        _logState = "로그인 되어 있습니다." if ret else "로그인 되어 있지 않습니다."
        QMessageBox.about(self, "로그인 상태", _logState)
Exemple #9
0
class MyWindow(QMainWindow, ui):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "실제운영"
        else:
            self.serverGubun = "모의투자"

        self.codeList = self.kiwoom.getCodeList("0")

        # 메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # 잔고 및 보유종목 조회 타이머
        self.inquiryTimer = QTimer(self)
        self.inquiryTimer.start(1000 * 10)
        self.inquiryTimer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)
        self.inquiryBtn.clicked.connect(self.inquiryBalance)

        # 자동 주문
        # 자동 주문을 활성화 하려면 True로 설정
        self.isAutomaticOrder = False

        # 자동 선정 종목 리스트 테이블 설정
        self.setAutomatedStocks()

    def timeout(self):
        """ 타임아웃 이벤트가 발생하면 호출되는 메서드 """

        # 어떤 타이머에 의해서 호출되었는지 확인
        sender = self.sender()

        # 메인 타이머
        if id(sender) == id(self.timer):
            currentTime = QTime.currentTime().toString("hh:mm:ss")
            automaticOrderTime = QTime.currentTime().toString("hhmm")

            # 상태바 설정
            state = ""

            if self.kiwoom.getConnectState() == 1:

                state = self.serverGubun + " 서버 연결중"
            else:
                state = "서버 미연결"

            self.statusbar.showMessage("현재시간: " + currentTime + " | " + state)

            # 자동 주문 실행
            # 1100은 11시 00분을 의미합니다.
            if self.isAutomaticOrder and int(automaticOrderTime) >= 1100:
                self.isAutomaticOrder = False
                self.automaticOrder()
                self.setAutomatedStocks()

            # log
            if self.kiwoom.msg:
                self.logTextEdit.append(self.kiwoom.msg)
                self.kiwoom.msg = ""

        # 실시간 조회 타이머
        else:
            if self.realtimeCheckBox.isChecked():
                self.inquiryBalance()

    def setCodeName(self):
        """ 종목코드에 해당하는 한글명을 codeNameLineEdit에 설정한다. """

        code = self.codeLineEdit.text()

        if code in self.codeList:
            codeName = self.kiwoom.getMasterCodeName(code)
            self.codeNameLineEdit.setText(codeName)

    def setAccountComboBox(self):
        """ accountComboBox에 계좌번호를 설정한다. """

        try:
            cnt = int(self.kiwoom.getLoginInfo("ACCOUNT_CNT"))
            accountList = self.kiwoom.getLoginInfo("ACCNO").split(';')
            self.accountComboBox.addItems(accountList[0:cnt])
        except (KiwoomConnectError, ParameterTypeError,
                ParameterValueError) as e:
            self.showDialog('Critical', e)

    def sendOrder(self):
        """ 키움서버로 주문정보를 전송한다. """

        orderTypeTable = {'신규매수': 1, '신규매도': 2, '매수취소': 3, '매도취소': 4}
        hogaTypeTable = {'지정가': "00", '시장가': "03"}

        account = self.accountComboBox.currentText()
        orderType = orderTypeTable[self.orderTypeComboBox.currentText()]
        code = self.codeLineEdit.text()
        hogaType = hogaTypeTable[self.hogaTypeComboBox.currentText()]
        qty = self.qtySpinBox.value()
        price = self.priceSpinBox.value()

        try:
            self.kiwoom.sendOrder("수동주문", "0101", account, orderType, code,
                                  qty, price, hogaType, "")

        except (ParameterTypeError, KiwoomProcessingError) as e:
            self.showDialog('Critical', e)

    def inquiryBalance(self):
        """ 예수금상세현황과 계좌평가잔고내역을 요청후 테이블에 출력한다. """

        self.inquiryTimer.stop()

        try:
            # 예수금상세현황요청
            self.kiwoom.setInputValue("계좌번호",
                                      self.accountComboBox.currentText())
            self.kiwoom.setInputValue("비밀번호", "0000")
            self.kiwoom.commRqData("예수금상세현황요청", "opw00001", 0, "2000")

            # 계좌평가잔고내역요청 - opw00018 은 한번에 20개의 종목정보를 반환
            self.kiwoom.setInputValue("계좌번호",
                                      self.accountComboBox.currentText())
            self.kiwoom.setInputValue("비밀번호", "0000")
            self.kiwoom.commRqData("계좌평가잔고내역요청", "opw00018", 0, "2000")

            while self.kiwoom.inquiry == '2':
                time.sleep(0.2)

                self.kiwoom.setInputValue("계좌번호",
                                          self.accountComboBox.currentText())
                self.kiwoom.setInputValue("비밀번호", "0000")
                self.kiwoom.commRqData("계좌평가잔고내역요청", "opw00018", 2, "2")

        except (ParameterTypeError, ParameterValueError,
                KiwoomProcessingError) as e:
            self.showDialog('Critical', e)

        # accountEvaluationTable 테이블에 정보 출력
        item = QTableWidgetItem(self.kiwoom.opw00001Data)  # d+2추정예수금
        item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
        self.accountEvaluationTable.setItem(0, 0, item)

        for i in range(1, 6):
            item = QTableWidgetItem(
                self.kiwoom.opw00018Data['accountEvaluation'][i - 1])
            item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
            self.accountEvaluationTable.setItem(0, i, item)

        self.accountEvaluationTable.resizeRowsToContents()

        # stocksTable 테이블에 정보 출력
        cnt = len(self.kiwoom.opw00018Data['stocks'])
        self.stocksTable.setRowCount(cnt)

        for i in range(cnt):
            row = self.kiwoom.opw00018Data['stocks'][i]

            for j in range(len(row)):
                item = QTableWidgetItem(row[j])
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.stocksTable.setItem(i, j, item)

        self.stocksTable.resizeRowsToContents()

        # 데이터 초기화
        self.kiwoom.opwDataReset()

        # inquiryTimer 재시작
        self.inquiryTimer.start(1000 * 10)

    # 경고창
    def showDialog(self, grade, error):
        gradeTable = {
            'Information': 1,
            'Warning': 2,
            'Critical': 3,
            'Question': 4
        }

        dialog = QMessageBox()
        dialog.setIcon(gradeTable[grade])
        dialog.setText(error.msg)
        dialog.setWindowTitle(grade)
        dialog.setStandardButtons(QMessageBox.Ok)
        dialog.exec_()

    def setAutomatedStocks(self):
        fileList = ["buy_list.txt", "sell_list.txt"]
        automatedStocks = []

        try:
            for file in fileList:
                # utf-8로 작성된 파일을
                # cp949 환경에서 읽기위해서 encoding 지정
                with open(file, 'rt', encoding='utf-8') as f:
                    stocksList = f.readlines()
                    automatedStocks += stocksList
        except Exception as e:
            e.msg = "setAutomatedStocks() 에러"
            self.showDialog('Critical', e)
            return

        # 테이블 행수 설정
        cnt = len(automatedStocks)
        self.automatedStocksTable.setRowCount(cnt)

        # 테이블에 출력
        for i in range(cnt):
            stocks = automatedStocks[i].split(';')

            for j in range(len(stocks)):
                if j == 1:
                    name = self.kiwoom.getMasterCodeName(stocks[j].rstrip())
                    item = QTableWidgetItem(name)
                else:
                    item = QTableWidgetItem(stocks[j].rstrip())

                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.automatedStocksTable.setItem(i, j, item)

        self.automatedStocksTable.resizeRowsToContents()

    def automaticOrder(self):
        fileList = ["buy_list.txt", "sell_list.txt"]
        hogaTypeTable = {'지정가': "00", '시장가': "03"}
        account = self.accountComboBox.currentText()
        automatedStocks = []

        # 파일읽기
        try:
            for file in fileList:
                # utf-8로 작성된 파일을
                # cp949 환경에서 읽기위해서 encoding 지정
                with open(file, 'rt', encoding='utf-8') as f:
                    stocksList = f.readlines()
                    automatedStocks += stocksList
        except Exception as e:
            e.msg = "automaticOrder() 에러"
            self.showDialog('Critical', e)
            return

        cnt = len(automatedStocks)

        # 주문하기
        buyResult = []
        sellResult = []

        for i in range(cnt):
            stocks = automatedStocks[i].split(';')

            code = stocks[1]
            hoga = stocks[2]
            qty = stocks[3]
            price = stocks[4]

            try:
                if stocks[5].rstrip() == '매수전':
                    self.kiwoom.sendOrder("자동매수주문", "0101", account, 1, code,
                                          int(qty), int(price),
                                          hogaTypeTable[hoga], "")

                    # 주문 접수시
                    if self.kiwoom.orderNo:
                        buyResult += automatedStocks[i].replace(
                            "매수전", "매수주문완료")
                        self.kiwoom.orderNo = ""
                    # 주문 미접수시
                    else:
                        buyResult += automatedStocks[i]

                # 참고: 해당 종목을 현재도 보유하고 있다고 가정함.
                elif stocks[5].rstrip() == '매도전':
                    self.kiwoom.sendOrder("자동매도주문", "0101", account, 2, code,
                                          int(qty), int(price),
                                          hogaTypeTable[hoga], "")

                    # 주문 접수시
                    if self.kiwoom.orderNo:
                        sellResult += automatedStocks[i].replace(
                            "매도전", "매도주문완료")
                        self.kiwoom.orderNo = ""
                    # 주문 미접수시
                    else:
                        sellResult += automatedStocks[i]

            except (ParameterTypeError, KiwoomProcessingError) as e:
                self.showDialog('Critical', e)

        # 잔고및 보유종목 디스플레이 갱신
        self.inquiryBalance()

        # 결과저장하기
        for file, result in zip(fileList, [buyResult, sellResult]):
            with open(file, 'wt', encoding='utf-8') as f:
                for data in result:
                    f.write(data)
Exemple #10
0
class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        #self.show()

        #########################################################################
        # Load Class
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        #self.holicAgent = holic_agent.HolicAgent()
        self.mholicstock = modelholicstock.ModelHolicStock()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "==== PROD ===="
        else:
            self.serverGubun = "DEV"

        #########################################################################
        #메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # Timer auto 실행
        self.timer_auto = QTimer(self)
        self.timer_auto.start(1000 * 10)
        self.timer_auto.timeout.connect(self.timer_auto_timeout)

        accouns_num = int(self.kiwoom.getLoginInfo("ACCOUNT_CNT"))
        accounts = self.kiwoom.getLoginInfo("ACCNO")
        accounts_list = accounts.split(';')[0:accouns_num]
        self.accountComboBox.addItems(accounts_list)

        #########################################################################
        # Event Callback connect
        self.pushButton.clicked.connect(self.send_order)
        self.lineEdit.textChanged.connect(self.code_changed)
        self.pushButton_2.clicked.connect(self.check_balance)

        self.btn_autoorder.clicked.connect(self.start_auto_order)
        self.btn_stopautoorder.clicked.connect(self.stop_auto_order)

        self.btn_start_mon.clicked.connect(self.automated_stock_mon)

        self.btn_start_analysis.clicked.connect(self.analysis_condition)

        #########################################################################
        # 메인 변수
        self.df_condition = pd.DataFrame()
        self.df_anaysis = pd.DataFrame()

    ##################################################################################################### Analisys

    def automated_stock_mon(self):
        '''
        DB에 저장된 항목 / 조건검색 항목 DB 저장.

        :return:
        '''
        print("[automated_stock_mon]")

        #df_minute_data = self.kiwoom.get_min_chart("039490", "5")
        #print(df_minute_data)

        # CONDITIN 종목에 대한 정보 확보
        self.kiwoom.get_condition()

        for idx in range(len(self.kiwoom.condition_name_list)):
            #print(self.kiwoom.condition_name_list[idx])
            if self.kiwoom.condition_name_list[idx] != '':
                self.kiwoom.send_condition(idx)
        print("#### total condition list : " + self.kiwoom.conditionStockItem)
        # 학습 대상 선정
        ''' 매수가 완료된 종목에 대한 변동이 필요함.  '''
        # stock list 획득
        #self.mystock_list = self.mholicstock.read_my_stock('buy')
        #print("#### total mystock list : " + self.mystock_list)
        # [0. 조건검색 결과 + 저장된 리스트 ]
        #for j in range(len(self.mystock_list)):
        #    self.kiwoom.conditionStockItem += str(self.mystock_list[j][0]) + ';'

        #print(self.kiwoom.conditionStockItem)

        # [1. kiwoom get DATA ]
        self.kiwoom.comm_kw_rq_data(self.kiwoom.conditionStockItem)

        # [2. 조회된 데이타에 대한 DATA 분석  ret=df ]
        self.df_condition = pd.DataFrame.from_dict(
            self.kiwoom.automatedStockDict, orient='index')
        print(self.df_condition)
        #print(self.df_main_list.loc[:, ['code']].to_dict() )

        # [3. Display Qt MainTableWidget ]
        dict_count = len(self.kiwoom.automatedStockDict)
        header_list = [
            'signal', 'weight', 'name', 'cprice', 'updown', 'rate', 'volume',
            'code', 'date'
        ]
        column_idx_lookup = {
            header_list[i]: i
            for i in range(0, len(header_list))
        }
        #column_idx_lookup = {'date': 0, 'code': 1, 'name': 2, 'cprice': 3, 'updown': 4, 'rate': 5, 'volume': 6}
        #print(column_idx_lookup)

        self.tableAutomatedStock.setColumnCount(len(header_list))
        self.tableAutomatedStock.setRowCount(dict_count)
        self.tableAutomatedStock.setHorizontalHeaderLabels(header_list)

        if not isinstance(self.kiwoom.automatedStockDict,
                          collections.Iterable):
            raise ParameterTypeError()

        #print("loop start ")
        for row, stocK_value in enumerate(
                self.kiwoom.automatedStockDict.items()):
            #print(stocK_value[1])

            for key, value in stocK_value[1].items():
                #print("key : {}, value: {}".format(key, value))
                col = column_idx_lookup[key]

                item = QTableWidgetItem(value)

                if key == 'name':
                    item.setTextAlignment(Qt.AlignVCenter | Qt.AlignLeft)
                else:
                    item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.tableAutomatedStock.setItem(row, col, item)

        self.tableAutomatedStock.resizeRowsToContents()
        self.tableAutomatedStock.resizeColumnsToContents()

    def analysis_condition(self):
        # [3. 각종목에 대한 분봉 데이타 획득 1분 / 3분 / 5분 ]
        for index, row in self.df_condition.iterrows():
            print(row['name'], row['code'])
            self.df_anaysis = self.kiwoom.get_min_chart(row['code'], '5')
            self.df_anaysis['code'] = row['code']
            #print(self.df_anaysis)
            #sself.mholicstock.insert_stock_data(self.df_anaysis)

            prep_data = data_manager.preprocess(self.df_anaysis)

            print(prep_data)

    ###############################################################
    #   자동주문   및 수동 주문                                   #
    ###############################################################

    def init_auto_order(self):
        self.selectedCondition = ''  #선택된 조건식

        self.profit_Rate = ''  #이익률
        self.loss_Rate = ''  #손절률

        self.limit_Buying_Per_Stock = ''  #종목당 매수금액
        self.limit_Buying_Stock_Number = ''  #매입 제한 종목개수

        # 한번의 주문이 진행 되고 나면 다시 셋팅 하는 과정을 가져감.

        # 예수금상세현황요청을 하여야 하는지 종목 갯수에 대한 확인을 진행 해야 하는지

    def start_auto_order(self):
        # 자동 주문
        print("[ start_auto_order ]")
        print(self.df_machined_stock_item)

        # 시간에 따라 타임 아웃 나올때 마다 주문 프로세스를 호출함.
        self.isAutomaticOrder = True

    def stop_auto_order(self):
        print("[ stop_auto_order ] Finished Order... ")
        self.isAutomaticOrder = False

    def set_auto_order_stocks(self):
        fileList = ["buy_list.txt", "sell_list.txt"]
        automatedStocks = []

        try:
            for file in fileList:
                # utf-8로 작성된 파일을
                # cp949 환경에서 읽기위해서 encoding 지정
                with open(file, 'rt', encoding='utf-8') as f:
                    stocksList = f.readlines()
                    automatedStocks += stocksList
        except Exception as e:
            e.msg = "setAutomatedStocks() 에러"
            self.showDialog('Critical', e)
            return

        # 테이블 행수 설정
        cnt = len(automatedStocks)
        self.automatedStocksTable.setRowCount(cnt)

        # 테이블에 출력
        for i in range(cnt):
            stocks = automatedStocks[i].split(';')

            for j in range(len(stocks)):
                if j == 1:
                    name = self.kiwoom.getMasterCodeName(stocks[j].rstrip())
                    item = QTableWidgetItem(name)
                else:
                    item = QTableWidgetItem(stocks[j].rstrip())

                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.automatedStocksTable.setItem(i, j, item)

        self.automatedStocksTable.resizeRowsToContents()

    def send_order(self):
        order_type_lookup = {'신규매수': 1, '신규매도': 2, '매수취소': 3, '매도취소': 4}
        hoga_lookup = {'지정가': "00", '시장가': "03"}

        account = self.accountComboBox.currentText()
        order_type = self.comboBox_2.currentText()
        code = self.lineEdit.text()
        hoga = self.comboBox_3.currentText()
        num = self.spinBox.value()
        price = self.spinBox_2.value()

        self.kiwoom.send_order("send_order_req", "0101", account,
                               order_type_lookup[order_type], code, num, price,
                               hoga_lookup[hoga], "")

        # 성공 여부에 대한 확인도 필요
        self.reset_order()

    def reset_order(self):
        print("#### reset order ")
        self.accountComboBox.setCurrentIndex(0)
        self.comboBox_2.setCurrentIndex(0)
        self.lineEdit.setText('')
        self.comboBox_3.setCurrentIndex(0)
        self.spinBox.setValue(0)
        self.spinBox_2.setValue(0)

    ###############################################################
    #   예수금 및 보유 종목 정보
    ###############################################################
    def check_balance(self):
        print("#### cb_check_balance called.")

        self.kiwoom.reset_opw00018_output()
        account_number = self.kiwoom.getLoginInfo("ACCNO")
        account_number = account_number.split(';')[0]

        self.kiwoom.set_input_value("계좌번호", account_number)
        self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 0, "2000")

        while self.kiwoom.remained_data:
            time.sleep(0.2)
            self.kiwoom.set_input_value("계좌번호", account_number)
            self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 2, "2000")

        # opw00001
        self.kiwoom.set_input_value("계좌번호", account_number)
        self.kiwoom.comm_rq_data("opw00001_req", "opw00001", 0, "2000")

        # balance
        item = QTableWidgetItem(self.kiwoom.d2_deposit)
        item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
        self.tableWidget.setItem(0, 0, item)

        for i in range(1, 6):
            item = QTableWidgetItem(self.kiwoom.opw00018_output['single'][i -
                                                                          1])
            item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
            self.tableWidget.setItem(0, i, item)

        self.tableWidget.resizeRowsToContents()
        self.tableWidget.resizeColumnsToContents()

        # Item list
        item_count = len(self.kiwoom.opw00018_output['multi'])
        self.tableWidget_2.setRowCount(item_count)

        for j in range(item_count):
            row = self.kiwoom.opw00018_output['multi'][j]
            for i in range(len(row)):
                item = QTableWidgetItem(row[i])
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.tableWidget_2.setItem(j, i, item)

        self.tableWidget_2.resizeRowsToContents()
        self.tableWidget_2.resizeColumnsToContents()

    ###############################################################
    #   공통함수                                           #
    ###############################################################
    def code_changed(self):
        code = self.lineEdit.text()
        name = self.kiwoom.get_master_code_name(code)
        self.lineEdit_2.setText(name)

    def timeout(self):
        """ 타임아웃 이벤트가 발생하면 호출되는 메서드 """

        current_time = QTime.currentTime()
        text_time = current_time.toString("hh:mm:ss")
        time_msg = "현재시간: " + text_time

        state = self.kiwoom.GetConnectState()
        if state == 1:
            state_msg = "server connected"
        else:
            state_msg = "not connected"

        self.statusbar.showMessage(state_msg + " | " + time_msg)

    def timer_auto_timeout(self):
        print('time_auto ---- ')

        if self.checkBox.isChecked():
            self.check_balance()
Exemple #11
0
class MyWindow(QMainWindow, ui):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()
        self.codeList = self.kiwoom.getCodeList("0")

        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)

    def timeout(self):
        """ 타임아웃 이벤트가 발생하면 호출되는 메서드 """

        currentTime = QTime.currentTime().toString("hh:mm:ss")
        state = ""

        if self.kiwoom.getConnectState() == 1:
            state = "서버 연결중"
        else:
            state = "서버 미연결"

        self.statusbar.showMessage("현재시간: " + currentTime + " | " + state)

    def setCodeName(self):
        """ 종목코드에 해당하는 한글명을 codeNameLineEdit에 설정한다. """

        code = self.codeLineEdit.text()

        if code in self.codeList:
            codeName = self.kiwoom.getMasterCodeName(code)
            self.codeNameLineEdit.setText(codeName)

    def setAccountComboBox(self):
        """ accountComboBox에 계좌번호를 설정한다. """

        cnt = int(self.kiwoom.getLoginInfo("ACCOUNT_CNT"))
        accountList = self.kiwoom.getLoginInfo("ACCNO").split(';')
        self.accountComboBox.addItems(accountList[0:cnt])

    def sendOrder(self):
        """ 키움서버로 주문정보를 전송한다. """

        orderTypeTable = {'신규매수': 1, '신규매도': 2, '매수취소': 3, '매도취소': 4}
        hogaTypeTable = {'지정가': "00", '시장가': "03"}

        account = self.accountComboBox.currentText()
        orderType = orderTypeTable[self.orderTypeComboBox.currentText()]
        code = self.codeLineEdit.text()
        hogaType = hogaTypeTable[self.hogaTypeComboBox.currentText()]
        qty = self.qtySpinBox.value()
        price = self.priceSpinBox.value()

        try:
            returnCode = self.kiwoom.sendOrder("sendOrder_req", "0101",
                                               account, orderType, code, qty,
                                               price, hogaType, "")
            self.showDialog('Information',
                            "sendOrder() 결과: " + ReturnCode.CAUSE[returnCode])

        except (ParameterTypeError, KiwoomProcessingError) as e:
            self.showDialog('Critical', e)

    def showDialog(self, grade, text):
        gradeTable = {
            'Information': 1,
            'Warning': 2,
            'Critical': 3,
            'Question': 4
        }

        dialog = QMessageBox()
        dialog.setIcon(gradeTable[grade])
        dialog.setText(text)
        dialog.setWindowTitle(grade)
        dialog.setStandardButtons(QMessageBox.Ok)
        dialog.exec_()
Exemple #12
0
class MyWindow(QMainWindow, ui):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.kiwoom = Kiwoom()
        self.kiwoom.commConnect()

        self.server = self.kiwoom.getLoginInfo("GetServerGubun")

        if len(self.server) == 0 or self.server != "1":
            self.serverGubun = "실제운영"
        else:
            self.serverGubun = "모의투자"

        self.codeList = self.kiwoom.getCodeList("0")

        # 메인 타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        # 잔고 및 보유종목 조회 타이머
        self.inquiryTimer = QTimer(self)
        self.inquiryTimer.start(1000*10)
        self.inquiryTimer.timeout.connect(self.timeout)

        self.setAccountComboBox()
        self.codeLineEdit.textChanged.connect(self.setCodeName)
        self.orderBtn.clicked.connect(self.sendOrder)
        self.inquiryBtn.clicked.connect(self.inquiryBalance)

        # 자동 주문
        # 자동 주문을 활성화 하려면 True로 설정
        self.isAutomaticOrder = False

        # 자동 선정 종목 리스트 테이블 설정
        self.setAutomatedStocks()

    def timeout(self):
        """ 타임아웃 이벤트가 발생하면 호출되는 메서드 """

        # 어떤 타이머에 의해서 호출되었는지 확인
        sender = self.sender()

        # 메인 타이머
        if id(sender) == id(self.timer):
            currentTime = QTime.currentTime().toString("hh:mm:ss")
            automaticOrderTime = QTime.currentTime().toString("hhmm")

            # 상태바 설정
            state = ""

            if self.kiwoom.getConnectState() == 1:

                state = self.serverGubun + " 서버 연결중"
            else:
                state = "서버 미연결"

            self.statusbar.showMessage("현재시간: " + currentTime + " | " + state)

            # 자동 주문 실행
            # 1100은 11시 00분을 의미합니다.
            if self.isAutomaticOrder and int(automaticOrderTime) >= 1100:
                self.isAutomaticOrder = False
                self.automaticOrder()
                self.setAutomatedStocks()

            # log
            if self.kiwoom.msg:
                self.logTextEdit.append(self.kiwoom.msg)
                self.kiwoom.msg = ""

        # 실시간 조회 타이머
        else:
            if self.realtimeCheckBox.isChecked():
                self.inquiryBalance()

    def setCodeName(self):
        """ 종목코드에 해당하는 한글명을 codeNameLineEdit에 설정한다. """

        code = self.codeLineEdit.text()

        if code in self.codeList:
            codeName = self.kiwoom.getMasterCodeName(code)
            self.codeNameLineEdit.setText(codeName)

    def setAccountComboBox(self):
        """ accountComboBox에 계좌번호를 설정한다. """

        try:
            cnt = int(self.kiwoom.getLoginInfo("ACCOUNT_CNT"))
            accountList = self.kiwoom.getLoginInfo("ACCNO").split(';')
            self.accountComboBox.addItems(accountList[0:cnt])
        except (KiwoomConnectError, ParameterTypeError, ParameterValueError) as e:
            self.showDialog('Critical', e)

    def sendOrder(self):
        """ 키움서버로 주문정보를 전송한다. """

        orderTypeTable = {'신규매수': 1, '신규매도': 2, '매수취소': 3, '매도취소': 4}
        hogaTypeTable = {'지정가': "00", '시장가': "03"}

        account = self.accountComboBox.currentText()
        orderType = orderTypeTable[self.orderTypeComboBox.currentText()]
        code = self.codeLineEdit.text()
        hogaType = hogaTypeTable[self.hogaTypeComboBox.currentText()]
        qty = self.qtySpinBox.value()
        price = self.priceSpinBox.value()

        try:
            self.kiwoom.sendOrder("수동주문", "0101", account, orderType, code, qty, price, hogaType, "")

        except (ParameterTypeError, KiwoomProcessingError) as e:
            self.showDialog('Critical', e)

    def inquiryBalance(self):
        """ 예수금상세현황과 계좌평가잔고내역을 요청후 테이블에 출력한다. """

        self.inquiryTimer.stop()

        try:
            # 예수금상세현황요청
            self.kiwoom.setInputValue("계좌번호", self.accountComboBox.currentText())
            self.kiwoom.setInputValue("비밀번호", "0000")
            self.kiwoom.commRqData("예수금상세현황요청", "opw00001", 0, "2000")

            # 계좌평가잔고내역요청 - opw00018 은 한번에 20개의 종목정보를 반환
            self.kiwoom.setInputValue("계좌번호", self.accountComboBox.currentText())
            self.kiwoom.setInputValue("비밀번호", "0000")
            self.kiwoom.commRqData("계좌평가잔고내역요청", "opw00018", 0, "2000")

            while self.kiwoom.inquiry == '2':
                time.sleep(0.2)

                self.kiwoom.setInputValue("계좌번호", self.accountComboBox.currentText())
                self.kiwoom.setInputValue("비밀번호", "0000")
                self.kiwoom.commRqData("계좌평가잔고내역요청", "opw00018", 2, "2")

        except (ParameterTypeError, ParameterValueError, KiwoomProcessingError) as e:
            self.showDialog('Critical', e)

        # accountEvaluationTable 테이블에 정보 출력
        item = QTableWidgetItem(self.kiwoom.opw00001Data)   # d+2추정예수금
        item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
        self.accountEvaluationTable.setItem(0, 0, item)

        for i in range(1, 6):
            item = QTableWidgetItem(self.kiwoom.opw00018Data['accountEvaluation'][i-1])
            item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
            self.accountEvaluationTable.setItem(0, i, item)

        self.accountEvaluationTable.resizeRowsToContents()

        # stocksTable 테이블에 정보 출력
        cnt = len(self.kiwoom.opw00018Data['stocks'])
        self.stocksTable.setRowCount(cnt)

        for i in range(cnt):
            row = self.kiwoom.opw00018Data['stocks'][i]

            for j in range(len(row)):
                item = QTableWidgetItem(row[j])
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.stocksTable.setItem(i, j, item)

        self.stocksTable.resizeRowsToContents()

        # 데이터 초기화
        self.kiwoom.opwDataReset()

        # inquiryTimer 재시작
        self.inquiryTimer.start(1000*10)

    # 경고창
    def showDialog(self, grade, error):
        gradeTable = {'Information': 1, 'Warning': 2, 'Critical': 3, 'Question': 4}

        dialog = QMessageBox()
        dialog.setIcon(gradeTable[grade])
        dialog.setText(error.msg)
        dialog.setWindowTitle(grade)
        dialog.setStandardButtons(QMessageBox.Ok)
        dialog.exec_()

    def setAutomatedStocks(self):
        fileList = ["buy_list.txt", "sell_list.txt"]
        automatedStocks = []

        try:
            for file in fileList:
                # utf-8로 작성된 파일을
                # cp949 환경에서 읽기위해서 encoding 지정
                with open(file, 'rt', encoding='utf-8') as f:
                    stocksList = f.readlines()
                    automatedStocks += stocksList
        except Exception as e:
            e.msg = "setAutomatedStocks() 에러"
            self.showDialog('Critical', e)
            return

        # 테이블 행수 설정
        cnt = len(automatedStocks)
        self.automatedStocksTable.setRowCount(cnt)

        # 테이블에 출력
        for i in range(cnt):
            stocks = automatedStocks[i].split(';')

            for j in range(len(stocks)):
                if j == 1:
                    name = self.kiwoom.getMasterCodeName(stocks[j].rstrip())
                    item = QTableWidgetItem(name)
                else:
                    item = QTableWidgetItem(stocks[j].rstrip())

                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.automatedStocksTable.setItem(i, j, item)

        self.automatedStocksTable.resizeRowsToContents()

    def automaticOrder(self):
        fileList = ["buy_list.txt", "sell_list.txt"]
        hogaTypeTable = {'지정가': "00", '시장가': "03"}
        account = self.accountComboBox.currentText()
        automatedStocks = []

        # 파일읽기
        try:
            for file in fileList:
                # utf-8로 작성된 파일을
                # cp949 환경에서 읽기위해서 encoding 지정
                with open(file, 'rt', encoding='utf-8') as f:
                    stocksList = f.readlines()
                    automatedStocks += stocksList
        except Exception as e:
            e.msg = "automaticOrder() 에러"
            self.showDialog('Critical', e)
            return

        cnt = len(automatedStocks)

        # 주문하기
        buyResult = []
        sellResult = []

        for i in range(cnt):
            stocks = automatedStocks[i].split(';')

            code = stocks[1]
            hoga = stocks[2]
            qty = stocks[3]
            price = stocks[4]

            try:
                if stocks[5].rstrip() == '매수전':
                    self.kiwoom.sendOrder("자동매수주문", "0101", account, 1, code, int(qty), int(price), hogaTypeTable[hoga], "")

                    # 주문 접수시
                    if self.kiwoom.orderNo:
                        buyResult += automatedStocks[i].replace("매수전", "매수주문완료")
                        self.kiwoom.orderNo = ""
                    # 주문 미접수시
                    else:
                        buyResult += automatedStocks[i]

                # 참고: 해당 종목을 현재도 보유하고 있다고 가정함.
                elif stocks[5].rstrip() == '매도전':
                    self.kiwoom.sendOrder("자동매도주문", "0101", account, 2, code, int(qty), int(price), hogaTypeTable[hoga], "")

                    # 주문 접수시
                    if self.kiwoom.orderNo:
                        sellResult += automatedStocks[i].replace("매도전", "매도주문완료")
                        self.kiwoom.orderNo = ""
                    # 주문 미접수시
                    else:
                        sellResult += automatedStocks[i]

            except (ParameterTypeError, KiwoomProcessingError) as e:
                self.showDialog('Critical', e)

        # 잔고및 보유종목 디스플레이 갱신
        self.inquiryBalance()

        # 결과저장하기
        for file, result in zip(fileList, [buyResult, sellResult]):
            with open(file, 'wt', encoding='utf-8') as f:
                for data in result:
                    f.write(data)
Exemple #13
0
class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        #인스턴스 생성 후 로그인
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        #1초타이머
        self.timer = QTimer(self)
        self.timer.start(1000)
        self.timer.timeout.connect(self.timeout)

        #10초 타이머
        self.timer2 = QTimer(self)
        self.timer2.start(10000)
        self.timer2.timeout.connect(self.timeout2)

        #계좌수 및 계좌번호
        accounts_num = int(self.kiwoom.get_login_info("ACCOUNT_CNT"))
        accounts = self.kiwoom.get_login_info("ACCNO")
        accounts_list = accounts.split(';')[0:accounts_num]

        #UI에 기능추가
        self.comboBox.addItems(accounts_list)
        self.lineEdit.textChanged.connect(self.code_changed)
        self.pushButton.clicked.connect(self.send_order)
        self.pushButton_2.clicked.connect(self.check_balance)


    #라인에딧의 텍스트 받아와 라인에딧2에 종목명 출력
    def code_changed(self):
        code = self.lineEdit.text()
        name = self.kiwoom.get_master_code_name(code)
        self.lineEdit_2.setText(name)

    #주문요청
    def send_order(self):
        order_type_lookup = {'신규매수':1, '신규매도':2, '매수취소':3, '매도취소':4}
        hoga_lookup = {'지정가': "00", '시장가': "03"}

        account = self.comboBox.currentText()
        order_type = self.comboBox_2.currentText()
        code = self.lineEdit.text()
        hoga = self.comboBox_3.currentText()
        num = self.spinBox.Value()
        price = self.spinBox_2.value()

        self.kiwoom.send_order("send_order_req", "0101", account, order_type_lookup[order_type], code,
                               num, price, hoga_lookup[hoga], "")

    #현 계좌/투자정보 조회
    def check_balance(self):
        self.kiwoom.reset_opw00018_output()
        account_number = self.kiwoom.get_login_info("ACCNO")
        account_number = account_number.split(';')[0]

        self.kiwoom.set_input_value("계좌번호", account_number)
        self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 0, "2000")

        while self.kiwoom.remained_data:
            time.sleep(0.2)
            self.kiwoom.set_input_value("계좌번호", account_number)
            self.kiwoom.comm_rq_data("opw00018_req", "opw00018", 2, "2000")

        # opw00001
        self.kiwoom.set_input_value("계좌번호", account_number)
        self.kiwoom.comm_rq_data("opw00001_req", "opw00001", 0, "2000")

        # balance
        item = QTableWidgetItem(self.kiwoom.d2_deposit)
        item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
        self.tableWidget.setItem(0, 0, item)

        for i in range(1, 6):
            item = QTableWidgetItem(self.kiwoom.opw00018_output['single'][i - 1])
            item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
            self.tableWidget.setItem(0, i, item)

        self.tableWidget.resizeRowsToContents()

        # Item list
        item_count = len(self.kiwoom.opw00018_output['multi'])
        self.tableWidget_2.setRowCount(item_count)

        for j in range(item_count):
            row = self.kiwoom.opw00018_output['multi'][j]
            for i in range(len(row)):
                item = QTableWidgetItem(row[i])
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
                self.tableWidget_2.setItem(j, i, item)

        self.tableWidget_2.resizeRowsToContents()


    def timeout(self):
        current_time = QTime.currentTime()
        text_time = current_time.toString("hh:mm:ss")
        time_msg = "현재시간: " + text_time

        state = self.kiwoom.get_connect_state()
        if state == 1:
            state_msg = "서버 연결 중"
        else:
            state_msg = "서버 미 연결 중"

        self.statusbar.showMessage(state_msg + " | " + time_msg)

    def timeout2(self):
        if self.checkBox.isChecked():
            self.check_balance()
Exemple #14
0
 def __init__(self):
     super().__init__()
     self.kiwoom = Kiwoom()
     self.techIndi()
Exemple #15
0
class MyWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.kiwoom = Kiwoom()
        self.techIndi()

    def techIndi(self):
        try:
            daySqlOra = "SELECT NVL(MAX(SUBSTR(TRADE_TIME,1,8)),TO_CHAR(SYSDATE,'YYYYMMDD')) YMD FROM TRADE_TECH " \
                     "WHERE SUBSTR(TRADE_TIME,1,8) <> '%s' " \
                     % datetime.datetime.today().strftime("%Y%m%d")
            dateD = self.kiwoom.selectDBOra(daySqlOra)
            dateS = dateD[0][0]
        except Exception as e:
            e.msg = "techindi daySql 에러"
            print(e)

        try:
            sqlOra71 = "SELECT DISTINCT TRIM(A.STOCK_CODE) STOCK_CODE " \
                  "FROM TRADE_HOLD A " \
                  "WHERE A.YMD >= '20200401' " \
                  "  AND A.REQ_VOLUME <> 0" \
                  "UNION " \
                  "SELECT DISTINCT TRIM(B.STOCK_CODE) STOCK_CODE " \
                  "FROM TRADE_LIST B " \
                  "WHERE B.YMD >= '20200401' "

            summaryG = self.kiwoom.selectDBOra(sqlOra71)
        except Exception as e:
            e.msg = "techIndi Sql 에러"
            print(e)

        print("-----------------------------------------------------------------")
        print("["+ datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S") + "] " + "Process2 : 보조지표 만들기 시작!" )

        try:
            if len(summaryG) > 0:
                for row_data in summaryG:
                    try:
                        sql = "SELECT RN, STOCK_CODE, TRADE_TIME,  START_PRICE, HIGH_PRICE, LOW_PRICE, CURRENT_PRICE, VOLUME " \
                              "FROM ( " \
                              "       SELECT A.*, ROW_NUMBER() OVER(PARTITION BY STOCK_CODE ORDER BY TRADE_TIME DESC) RN" \
                              "                 , ROW_NUMBER() OVER(PARTITION BY STOCK_CODE ORDER BY TRADE_TIME ASC) RN2" \
                              "         FROM TRADE_CRR_BACK A" \
                              "        WHERE 1=1 " \
                              "          AND A.STOCK_CODE = '%s' " \
                              "          AND A.STOCK_CODE NOT IN ('155960','900090') " \
                              "      ) X " % row_data[0]

                        sql2 = "WHERE RN BETWEEN 1 AND 800 " \
                               "ORDER BY RN2"

                        summaryG = self.kiwoom.selectDBOra(sql + sql2)
                    except Exception as e:
                        print(e)

                    df = pd.DataFrame(summaryG)

                    try:
                        if len(df) > 0:
                            df.columns = ["rn", "stockCd", "tradeTime", "open", "high", "low", "close", "volume"]

                            # cloF = pd.Series(df.iloc[len(df) - 1]['open'], name='close')  # 3분봉 종가 만들어주는 로직
                            # clo = pd.Series(df.iloc[1:len(df)]['open'], name='close')
                            # clo = pd.DataFrame(clo.append(cloF))
                            # clo = clo.reset_index(drop=True)
                            #
                            # df = df.join(clo)

                            macd, macdsignal, macdhist = talib.MACD(df['close'], fastperiod=12, slowperiod=26,
                                                                    signalperiod=9)
                            slowk, slowd = talib.STOCH(df['high'], df['low'], df['close'], fastk_period=5, slowk_period=3,
                                                       slowk_matype=3,
                                                       slowd_period=3, slowd_matype=0)
                            fastk, fastd = talib.STOCHF(df['high'], df['low'], df['close'], fastk_period=20,
                                                        fastd_period=10)
                            sma5 = talib.SMA(df['close'], 5)
                            sma10 = talib.SMA(df['close'], 10)
                            sma20 = talib.SMA(df['close'], 20)
                            sma60 = talib.SMA(df['close'], 60)
                            sma120 = talib.SMA(df['close'], 120)
                            cci = talib.CCI(df['high'], df['low'], df['close'], 20)
                            rsi = talib.RSI(df['close'], 14)
                            roc1 = talib.ROC(df['close'], 1)
                            roc5 = talib.ROC(df['close'], 5)
                            rocr1 = talib.ROCR(df['close'], 1)
                            rocr5 = talib.ROCR(df['close'], 5)

                            df = df.join(pd.DataFrame(pd.Series(macd, name="MACD")))
                            df = df.join(pd.DataFrame(pd.Series(macdsignal, name="MACDSIG")))
                            df = df.join(pd.DataFrame(pd.Series(slowd, name="SLOWD")))
                            df = df.join(pd.DataFrame(pd.Series(fastd, name="FASTD")))
                            df = df.join(pd.DataFrame(pd.Series(sma5, name="SMA5")))
                            df = df.join(pd.DataFrame(pd.Series(sma10, name="SMA10")))
                            df = df.join(pd.DataFrame(pd.Series(sma20, name="SMA20")))
                            df = df.join(pd.DataFrame(pd.Series(sma60, name="SMA60")))
                            df = df.join(pd.DataFrame(pd.Series(sma120, name="SMA120")))
                            df = df.join(pd.DataFrame(pd.Series(cci, name="CCI")))
                            df = df.join(pd.DataFrame(pd.Series(rsi, name="RSI")))
                            df = df.join(pd.DataFrame(pd.Series(roc1, name="ROC1")))
                            df = df.join(pd.DataFrame(pd.Series(roc5, name="ROC5")))
                            df = df.join(pd.DataFrame(pd.Series(rocr1, name="ROCR1")))
                            df = df.join(pd.DataFrame(pd.Series(rocr5, name="ROCR5")))

                            df = df.fillna(0)

                            try:
                                for i in range(4, len(df)):
                                    if df.iloc[i]['SMA10'] == 0:
                                        dis10 = 0
                                    else:
                                        dis10 = float(df.iloc[i]['close']) / float(df.iloc[i]['SMA10']) * 100

                                    if df.iloc[i]['SMA20'] == 0:
                                        dis20 = 0
                                    else:
                                        dis20 = float(df.iloc[i]['close']) / float(df.iloc[i]['SMA20']) * 100

                                    file = open("C:/Users/jini/PycharmProjects/stock/sql/sqlOra72_1.sql", 'r') # TRADE_TECH에 데이터 넣기
                                    sqlOra72 = ''
                                    line = file.readline()
                                    while line:
                                        sqlOra72 += ' ' + line.strip('\n').strip('\t')
                                        line = file.readline()

                                    file.close()

                                    sqlOra72 = sqlOra72 % (
                                               df.iloc[i]['tradeTime'], df.iloc[i]['stockCd']
                                               , df.iloc[i - 3]['high'], df.iloc[i - 2]['high'], df.iloc[i - 1]['high']
                                               , df.iloc[i]['high'], df.iloc[i - 3]['low'], df.iloc[i - 2]['low'],
                                               df.iloc[i - 1]['low']
                                               , df.iloc[i]['low'], df.iloc[i - 3]['open'], df.iloc[i - 2]['open'],
                                               df.iloc[i - 1]['open']
                                               , df.iloc[i]['open'], df.iloc[i - 3]['close'], df.iloc[i - 2]['close']
                                               , df.iloc[i - 1]['close'], df.iloc[i]['close'], df.iloc[i - 3]['volume']
                                               , df.iloc[i - 2]['volume'], df.iloc[i - 1]['volume'],
                                               df.iloc[i]['volume']
                                               , (df.iloc[i - 2]['high'] - df.iloc[i - 3]['high']) / df.iloc[i - 3][
                                                   'high']
                                               , (df.iloc[i - 1]['high'] - df.iloc[i - 2]['high']) / df.iloc[i - 2][
                                                   'high']
                                               , (df.iloc[i]['high'] - df.iloc[i - 1]['high']) / df.iloc[i - 1]['high']
                                               , (df.iloc[i]['high'] - df.iloc[i - 3]['high']) / df.iloc[i - 3]['high']
                                               , (df.iloc[i]['high'] - df.iloc[i - 2]['high']) / df.iloc[i - 2]['high']
                                               , (df.iloc[i - 2]['low'] - df.iloc[i - 3]['low']) / df.iloc[i - 3]['low']
                                               , (df.iloc[i - 1]['low'] - df.iloc[i - 2]['low']) / df.iloc[i - 2]['low']
                                               , (df.iloc[i]['low'] - df.iloc[i - 1]['low']) / df.iloc[i - 1]['low']
                                               , (df.iloc[i]['low'] - df.iloc[i - 3]['low']) / df.iloc[i - 3]['low']
                                               , (df.iloc[i]['low'] - df.iloc[i - 2]['low']) / df.iloc[i - 2]['low']
                                               , df.iloc[i]['SMA5'], df.iloc[i]['SMA10'], df.iloc[i]['SMA20'], df.iloc[i]['SMA60']
                                               , df.iloc[i]['SMA120'], df.iloc[i]['RSI'], df.iloc[i]['CCI']
                                               , float(dis10)
                                               , float(dis20)
                                               , df.iloc[i]['MACD'], df.iloc[i]['MACDSIG'], df.iloc[i]['SLOWD'], df.iloc[i]['FASTD']
                                               , (max(df.iloc[i]['high'],df.iloc[i-1]['high'],df.iloc[i-2]['high']) - min(df.iloc[i]['low'],df.iloc[i-1]['low'],df.iloc[i-2]['low']))
                                               /  min(df.iloc[i]['low'],df.iloc[i-1]['low'],df.iloc[i-2]['low']) * 100
                                               , df.iloc[i]['MACD'] - df.iloc[i]['MACDSIG']
                                               , (df.iloc[i]['close'] - df.iloc[i]['high']) * 100 / df.iloc[i]['high']
                                               , (df.iloc[i]['close'] - max(df.iloc[i]['high'],df.iloc[i-1]['high'],df.iloc[i-2]['high'],df.iloc[i-3]['high'])) * 100 / max(df.iloc[i]['high'],df.iloc[i-1]['high'],df.iloc[i-2]['high'],df.iloc[i-3]['high'])
                                               , (df.iloc[i]['open'] - max(df.iloc[i]['high'],df.iloc[i-1]['high'],df.iloc[i-2]['high'],df.iloc[i-3]['high'])) * 100 / max(df.iloc[i]['high'],df.iloc[i-1]['high'],df.iloc[i-2]['high'],df.iloc[i-3]['high'])
                                               , (df.iloc[i]['close'] - min(df.iloc[i]['low'],df.iloc[i-1]['low'],df.iloc[i-2]['low'])) * 100 / min(df.iloc[i]['low'],df.iloc[i-1]['low'],df.iloc[i-2]['low'])
                                               , df.iloc[i]['tradeTime'])

                                    try:
                                        self.kiwoom.updateDBOra(sqlOra72)
                                    except Exception as e:
                                        print(e)
                            except Exception as e:
                                print(e)

                    except Exception as e:
                        print(e)

        except Exception as e:
            print(e)

        print("["+ datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S") + "] " + "Process2 : 보조지표 만들기 끝!" )