Exemple #1
0
    def __init__(self, log, key):
        self.broker = Kiwoom(self, log, key)
        # self.broker = Bankis(self, log, key)
        self.algorithm = FMAlgorithm2(self, log)
        self.general_account_index = 1
        self.futures_account_index = 0
        super().__init__(log)

        # Initial work
        self.connect_broker()
        self.get_account_list()
        # self.broker.request_deposit_info()
        # self.broker.request_portfolio_info()
        # self.broker.request_order_history()

        # Initial Values
        self.sb_capital.setValue(200000000)
        self.sb_interval.setValue(0.3)
        self.sb_loss_cut.setValue(0.15)
        self.sb_fee.setValue(0.003)
        self.sb_min_transaction.setValue(10)
        self.sb_amount.setValue(1)

        # Init Fields
        self.interval = self.sb_interval.value()
        self.loss_cut = self.sb_loss_cut.value()

        # Test setup
        self.cbb_item_name.setCurrentIndex(3)
        # self.broker.request_futures_deposit_info()
        # self.broker.request_futures_portfolio_info()

        self.t = 0
Exemple #2
0
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.initUI()

        self.kiwoom = Kiwoom(self.view)
        # self.kiwoom.login()

    def initUI(self):
        self.view = uic.loadUi('StockWindow.ui', self)

    # @pyqtSlot()
    # def login_clicked(self):
    #     self.kiwoom.login()

    @pyqtSlot()
    def get_info(self):
        self.kiwoom.get_login_info()

    @pyqtSlot()
    def get_stock_list(self):
        stock_list = self.kiwoom.get_jongmok_code()
        for stock in stock_list:
            self.view.listStock.addItem(stock['code'])

    @pyqtSlot()
    def jongmok_selected(self):
        self.view.tableView = QTableWidget
        self.view.tableView
        pass
Exemple #3
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()  # 키움 로그인

        self.trade_stocks_done = False  # 자동 주문

        self.timer = QTimer(self)
        self.timer.start(1000)  # 1초에 한 번씩 주기적으로 timeout 시그널 발생
        self.timer.timeout.connect(
            self.timeout)  # timeout 시그널이 발생할 때 이를 처리할 슬롯으로 self.timeout을 설정
        # StatusBar 위젯에 서버 연결 상태 및 현재 시간을 출력

        self.lineEdit.textChanged.connect(
            self.code_changed)  # lineEdit 객체가 변경될 때 호출되는 슬롯을 지정
        self.pushButton_2.clicked.connect(self.send_order)  # 현금주문

        accouns_num = int(self.kiwoom.get_login_info(
            "ACCOUNT_CNT"))  # 계좌 정보를 QComboBox 위젯에 출력하는 코드
        accounts = self.kiwoom.get_login_info("ACCNO")  # 로그인 정보 가져오기
        accounts_list = accounts.split(';')[
            0:accouns_num]  # 계좌가 여러 개인 경우 각 계좌는';'를 통해 구분

        self.comboBox.addItems(accounts_list)
        self.pushButton.clicked.connect(self.check_balance)  # 시그널과 슬롯을 연결

        self.load_buy_sell_list()  # 선정 종목 리스트 출력
Exemple #4
0
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.trade_stocks_done = False

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

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

        self.timer2 = QTimer(self)
        self.timer2.start(1000 * 10)
        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]
        self.comboBox.addItems(accounts_list)
        self.comboBox.setCurrentIndex(2)  # 기본값 설정(실서버)
        # self.comboBox.setCurrentIndex(0) # 기본값 설정(모의서버)
        self.lineEdit.textChanged.connect(self.code_changed)
        self.pushButton.clicked.connect(self.send_order)
        self.pushButton_2.clicked.connect(self.check_balance)

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

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        self.trade_stocks_done = False

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

        # Timer2 : 실시간 조회 체크박스 체크시 10초에 한 번씩 데이터 자동 갱신
        self.timer2 = QTimer(self)
        self.timer2.start(1000 * 10)
        #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]
        self.comboBox_2.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)


        # load buy_list, sell_list
        self.load_buy_sell_list()
Exemple #6
0
    def set_connect_hook(self, event, param):
        """
        Set parameter defined in event as a hook to find the right slot when event is called.

        When an event needs multiple slots to connect, depending on specific tasks, set
        a hook(key) to select which slot to map. The hook must be one of the parameters
        in the definition of the event method. Parameters can be found by help built-in
        function or Kiwoom.api_arg_spec(event). This raises a KeyError if given param is
        not defined in event method.

        If hook is set to the given parameter, argument passed into the parameter when
        the event is called, is going to be a key to connect event, signal and slot.

        Convention is that the name of signal and slot that deal with the related task
        is recommended to be the same, so that 'key' is set to be the method name of
        signal and slot by default. See examples on the tutorials link below.
        https://github.com/breadum/kiwoom/blob/main/tutorials/5.%20TR%20Data.py

        :param event: str
            One of the pre-defined event names in string. See kiwoom.config.EVENTS.
        :param param: str
            Parameter name defined in given event. To see all parameters to event,
            use Kiwoom.api_arg_spec(event) method or help(...) built-in function.
        """
        if not valid_event(event):
            return

        # To check given arg is valid
        from kiwoom import Kiwoom  # lazy import
        args = Kiwoom.api_arg_spec(event)
        if param not in args:
            raise KeyError(f"{param} is not valid.\nSelect one of {args}.")

        # To set connect hook and its index in args
        self._hooks[event] = param
        self._indices[event] = list(
            args.keys()).index(param) - 1  # except 'self'

        # Initialize structure to get signal/slot method by dic[event][key]
        self._signals[event] = dict()
        self._slots[event] = dict()
Exemple #7
0
class MyWindow(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

        self.trade_stocks_done = False

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

        # Timer2 : 실시간 조회 체크박스 체크시 10초에 한 번씩 데이터 자동 갱신
        self.timer2 = QTimer(self)
        self.timer2.start(1000 * 10)
        #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]
        self.comboBox_2.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)


        # load buy_list, sell_list
        self.load_buy_sell_list()

    def timeout(self):

        market_start_time = QTime(9, 0, 0)

        current_time = QTime.currentTime()

        if current_time > market_start_time and self.trade_stocks_done is False:
            self.trade_stocks()
            self.trade_stocks_done = True

        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 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_2.currentText()
        order_type = self.comboBox.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.data_remained:
            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(QTableWidget객체에 넣음)
        # rowcount값을 반드시 지정해야 한다.
        item = QTableWidgetItem(self.kiwoom.d2_deposit)
        item.setTextAlignment(Qt.AlignVCenter | Qt.AlignRight)
        self.tableWidget.setItem(0, 0, item)


        # setItem 메서드로 QTableWidget에 추가
        for i in range(1, 6):
            item = QTableWidgetItem(self.kiwoom.opw00018_output['single'][i-1])
            print('gg', 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 timeout2(self):
        if self.chechBox.isChecked():
            self.check_balance()

    def load_buy_sell_list(self):
        f = open("C:/dev_python/PyTrader/buy_list.txt", 'rt', encoding='utf-8')
        buy_list = f.readlines()
        f.close()

        f = open("C:/dev_python/PyTrader/sell_list.txt", 'rt', encoding='utf-8')
        sell_list = f.readlines()
        f.close()

        row_count = len(buy_list) + len(sell_list)
        self.tableWidget_3.setRowCount(row_count)

        #buy list
        for j in range(len(buy_list)):
            row_data = buy_list[j]
            split_row_data = row_data.split(';')
            split_row_data[1] = self.kiwoom.get_master_code_name(split_row_data[1].rsplit())

            for i in range(len(split_row_data)):
                item = QTableWidgetItem(split_row_data[i].rstrip())
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.tableWidget_3.setItem(j, i, item)

        #sell list
        for j in range(len(sell_list)):
            row_data = sell_list[j]
            split_row_data = row_data.split(';')
            split_row_data[1] = self.kiwoom.get_master_code_name(split_row_data[1].rstrip())

            for i in range(len(split_row_data)):
                item = QTableWidgetItem(split_row_data[i].rstrip())
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.tableWidget_3.setItem(len(buy_list) + j, i, item)

        self.tableWidget_3.resizeRowsToContents()

    # 자동 주문 구현
    def trade_stocks(self):
        hoga_lookup = {'지정가': "00", '시장가': "03"}

        f = open("C:/dev_python/PyTrader/buy_list.txt", 'rt', encoding='utf-8')
        buy_list = f.readlines()
        f.close()

        f = open("C:/dev_python/PyTrader/sell_list.txt", 'rt', encoding='utf-8')
        sell_list = f.readlines()
        f.close()

        account = self.comboBox_2.currentText()

        # buy_list
        for row_data in buy_list:
            split_row_data = row_data.split(';')
            hoga = split_row_data[2]
            code = split_row_data[1]
            num = split_row_data[3]
            price = split_row_data[4]

            if split_row_data[-1].rstrip() == '매수전':
                self.kiwoom.send_order("send_order_req", "0101", account, 1, code, num, price, hoga_lookup[hoga], "")

        # sell_list
        for row_data in sell_list:
            split_row_data = row_data.split(';')
            hoga = split_row_data[2]
            code = split_row_data[1]
            num = split_row_data[3]
            price = split_row_data[4]

            if split_row_data[-1].rstrip() == '매도전':
                self.kiwoom.send_order("send_order_req", "0101", account, 2, code, num, price, hoga_lookup[hoga], "")

        # 데이터 변경
        for i, row_data in enumerate(buy_list):
            buy_list[i] = buy_list[i].replace("매수전", "매수완료")

        # file update
        f = open("C:/dev_python/PyTrader/buy_list.txt", 'wt', encoding='utf-8')
        for row_data in buy_list:
            f.write(row_data)
        f.close()

        for i, row_data in enumerate(sell_list):
            sell_list[i] = sell_list[i].replace("매도전", "매도완료")

        f = open("C:/dev_python/PyTrader/sell_list.txt", 'wt', encoding='utf-8')
        for row_data in sell_list:
            f.write(row_data)
        f.close()
Exemple #8
0
 def __init__(self):
     super().__init__()  # initialize ProcessTracker
     self.starting()
     self.app = QApplication(["kiwoom.py"])
     self.kiwoom = Kiwoom()
     self.kiwoom.comm_connect()
Exemple #9
0
class UpdateGobble(ProcessTracker):
    @timeit
    def __init__(self):
        super().__init__()  # initialize ProcessTracker
        self.starting()
        self.app = QApplication(["kiwoom.py"])
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

    @timeit
    def step_one_kiwoom(self):
        self.step_one()
        etf = pd.read_csv('./etf_list.csv', header=None)
        etf = etf[0].tolist()
        etf = [str(e).zfill(6) for e in etf]
        for market_type in ["0", "10"]:
            pickle_name = "kospi-dict.pickle" if market_type == "0" else "kosdaq-dict.pickle"
            if market_type == "0":
                code_list = self.kiwoom.get_code_list_by_market(market_type)
                kospi_code_list = [
                    code for code in code_list if code not in etf
                ]
                etf_code_list = [code for code in code_list if code in etf]
                kospi_name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in kospi_code_list
                ]
                etf_name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in etf_code_list
                ]
                kospi_market_dict = dict(zip(kospi_code_list, kospi_name_list))
                pickle_out = open("./update_data/" + pickle_name, "wb")
                pickle.dump(kospi_market_dict, pickle_out)
                etf_market_dict = dict(zip(etf_code_list, etf_name_list))
                etf_pickle_out = open("./update_data/etf-dict.pickle", "wb")
                pickle.dump(etf_market_dict, etf_pickle_out)
                pickle_out.close()
            else:
                code_list = self.kiwoom.get_code_list_by_market(market_type)
                name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in code_list
                ]
                market_dict = dict(zip(code_list, name_list))
                pickle_out = open("./update_data/" + pickle_name, "wb")
                pickle.dump(market_dict, pickle_out)
                pickle_out.close()
        self.step_one_finish()

    def set_tasks(self, market_type=None):
        if market_type == None:
            kospi_in = open("./update_data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            kosdaq_in = open("./update_data/kosdaq-dict.pickle", "rb")
            self.kosdaq_task = pickle.load(kosdaq_in)
            etf_in = open("./update_data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)
        elif market_type == 'kospi':
            kospi_in = open("./update_data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            self.kosdaq_task = {}
            self.etf_task = {}
        elif market_type == 'kosdaq':
            self.kospi_task = {}
            kosdaq_in = open("./update_data/kosdaq-dict.pickle", "rb")
            self.kosdaq_task = pickle.load(kosdaq_in)
            self.etf_task = {}
        elif market_type == 'etf':
            self.kospi_task = {}
            self.kosdaq_task = {}
            etf_in = open("./update_data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)
        elif market_type == 'kospi-etf':
            kospi_in = open("./update_data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            self.kosdaq_task = {}
            etf_in = open("./update_data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)

    def _get_total_stock_num(self):
        kospi_len = len(list(self.kospi_task.keys()))
        kosdaq_len = len(list(self.kosdaq_task.keys()))
        etf_len = len(list(self.etf_task.keys()))
        return kospi_len + kosdaq_len + etf_len

    def make_folder(self):
        global TODAY
        for market in ['kospi', 'kosdaq', 'etf']:
            os.mkdir('./update_data/stock-buy/{}-buy/{}'.format(market, TODAY))
            print('{} {}-buy success'.format(TODAY, market))
            os.mkdir('./update_data/stock-sell/{}-sell/{}'.format(
                market, TODAY))
            print('{} {}-sell success'.format(TODAY, market))
            os.mkdir('./update_data/stock-net/{}-net/{}'.format(market, TODAY))
            print('{} {}-net success'.format(TODAY, market))
            os.mkdir('./update_data/stock-short/{}-short/{}'.format(
                market, TODAY))
            print('{} {}-short success'.format(TODAY, market))
        print('mkdir process complete')

    def check_start_log(self, datatype):
        if datatype == 'buysell':
            f = open('./daily-log/buysell_daycheck.txt', 'r')
            date = f.read()
            date_list = date.split('\n')
            f.close()
        elif datatype == 'short':
            f = open('./daily-log/short_daycheck.txt', 'r')
            date = f.read()
            date_list = date.split('\n')
            f.close()
        else:
            f = open('./daily-log/ohlcv_daycheck.txt', 'r')
            date = f.read()
            date_list = date.split('\n')
            f.close()
        week = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
        self.last_date = datetime.strptime(date_list[-1], '%Y%m%d').date()
        w = self.last_date.weekday()
        if week[w] == 'fri':
            self.start_date = self.last_date + timedelta(days=3)
            self.start_date = self.start_date.strftime('%Y%m%d')
        elif week[w] == 'sat':
            self.start_date = self.last_date + timedelta(days=2)
            self.start_date = self.start_date.strftime('%Y%m%d')
        else:
            self.start_date = self.last_date + timedelta(days=1)
            self.start_date = self.start_date.strftime('%Y%m%d')
        return self.start_date

    def check_start_day_file(self, datatype):
        os.chdir('./data/stock-{}/kospi-{}'.format(datatype, datatype))
        try:
            df = pd.read_csv('./005930.csv', sep=',')
        except UnicodeError:
            df = pd.read_csv('./005930.csv', sep=',', encoding='CP949')
        os.chdir('../../..')
        week = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
        if datatype == 'short':
            last_date = datetime.strptime(str(df['date'][-1:].values[0]),
                                          '%Y%m%d').date()
        else:
            last_date = datetime.strptime(str(df['date'][0]), '%Y%m%d').date()
        w = last_date.weekday()
        if week[w] == 'fri':
            self.start_date = last_date + timedelta(days=3)
            self.start_date = self.start_date.strftime('%Y%m%d')
        elif week[w] == 'sat':
            self.start_date = last_date + timedelta(days=2)
            self.start_date = self.start_date.strftime('%Y%m%d')
        else:
            self.start_date = last_date + timedelta(days=1)
            self.start_date = self.start_date.strftime('%Y%m%d')
        return self.start_date

    def add_data_log(self, datatype):
        if datatype == 'buysell':
            f = open('./daily-log/buysell_daycheck.txt', 'a')
            today = datetime.today().strftime('%Y%m%d')
            f.write('\n' + today)
            f.close()
        elif datatype == 'short':
            f = open('./daily-log/short_daycheck.txt', 'a')
            today = datetime.today().strftime('%Y%m%d')
            f.write('\n' + today)
            f.close()
        else:
            f = open('./daily-log/ohlcv_daycheck.txt', 'a')
            today = datetime.today().strftime('%Y%m%d')
            f.write('\n' + today)
            f.close()

    def _ohlcv_skip_codes(self):
        os.chdir("./update_data/stock-ohlcv/kospi-ohlcv")
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./update_data/stock-ohlcv/kosdaq-ohlcv")
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./update_data/stock-ohlcv/etf-ohlcv")
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        return kospi_list + kosdaq_list + etf_list

    def _buysell_skip_codes(self):
        os.chdir("./update_data/stock-net/kospi-net/{}".format(TODAY))
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        os.chdir("./update_data/stock-net/kosdaq-net/{}".format(TODAY))
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        os.chdir("./update_data/stock-net/etf-net/{}".format(TODAY))
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        return kospi_list + kosdaq_list + etf_list

    def _short_skip_codes(self):
        os.chdir("./update_data/stock-short/kospi-short/{}".format(TODAY))
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        os.chdir("./update_data/stock-short/kosdaq-short/{}".format(TODAY))
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        os.chdir("./update_data/stock-short/etf-short/{}".format(TODAY))
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../../")
        return kospi_list + kosdaq_list + etf_list

    def req_ohlcv(self):
        done_list = self._ohlcv_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        # start = self.check_start_day_file('ohlcv')
        start = self.check_start_log()
        code_looped = 0
        total_time = 0

        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_ohlcv_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " ohlcv save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")
        self.add_data_log()

    def _initialize_ohlcv_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " ohlcv data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        # opt10059 TR 요청
        kiwoom.set_input_value("기준일자", time.strftime('%Y%m%d'))
        kiwoom.set_input_value("종목코드", code)
        kiwoom.set_input_value("수정주가구분 ", 1)
        kiwoom.comm_rq_data("opt10081_req", "opt10081", 0, "0101")
        time.sleep(TR_REQ_TIME_INTERVAL)
        print("first request sent in successfully")

        while kiwoom.remained_data == True:
            kiwoom.set_input_value("기준일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("수정주가구분 ", 1)
            kiwoom.comm_rq_data("opt10081_req", "opt10081", 2, "0101")
            print("requesting...")
            current_date = kiwoom.get_date()
            print("date loop is on: ", str(current_date))
            if current_date <= int(start):
                print("loop breaking b/c " + str(current_date) +
                      " lower than " + str(start))
                break
            time.sleep(TR_REQ_TIME_INTERVAL)
        print("OHLCV data saved, ready for DB")
        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "open_price", "high_price", "low_price",
            "close_price", "volume"
        ]
        kiwoom.data = kiwoom.data[cols]
        path = ".\\update_data\\stock-ohlcv\\" + market + "-ohlcv\\"
        file_name = code + ".csv"
        Update_ohlcv = kiwoom.data[kiwoom.data['date'] >= int(start)]
        Update_ohlcv.to_csv(os.path.join(path, file_name), index=False)
        print(code + ": " + name + " ohlcv data successfully saved")

    def req_buysell(self):
        done_list = self._buysell_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        # start = self.check_start_day_file('buy')
        start = self.check_start_log('buysell')
        code_looped = 0
        total_time = 0

        # get code list (0: KOSPI, 10: KOSDAQ)
        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_buysell_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " buysell save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")
        self.add_data_log('buysell')

    def _initialize_buysell_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL
        global TODAY

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " buysell data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        for buysell in [1, 2]:
            if buysell == 1:
                kiwoom.set_buysell_state("buy")
            elif buysell == 2:
                kiwoom.set_buysell_state("sell")

            # opt10059 TR 요청
            kiwoom.set_input_value("일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("금액수량구분", 2)
            kiwoom.set_input_value("매매구분", buysell)
            kiwoom.set_input_value("단위구분", 1)
            kiwoom.comm_rq_data("opt10059_req", "opt10059", 0, "0101")
            time.sleep(TR_REQ_TIME_INTERVAL)
            print("first request sent in successfully")

            while kiwoom.remained_data == True:
                kiwoom.set_input_value("일자", time.strftime('%Y%m%d'))
                kiwoom.set_input_value("종목코드", code)
                kiwoom.set_input_value("금액수량구분", 2)
                kiwoom.set_input_value("매매구분", buysell)
                kiwoom.set_input_value("단위구분", 1)
                kiwoom.comm_rq_data("opt10059_req", "opt10059", 2, "0101")
                print("requesting...")
                current_date = kiwoom.get_date()
                print("date loop is on: ", str(current_date))
                if current_date <= int(start):
                    print("loop breaking b/c " + str(current_date) +
                          " lower than " + str(start))
                    break
                time.sleep(TR_REQ_TIME_INTERVAL)
            if buysell == 1:
                print("BUY data saved, ready for DB")
            elif buysell == 2:
                print("SELL data saved, ready for DB")

        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "close_price", "individual",
            "foreign_retail", "institution", "financial", "insurance", "trust",
            "etc_finance", "bank", "pension", "private", "nation",
            "etc_corporate", "foreign", "buysell"
        ]
        kiwoom.data = kiwoom.data[cols]
        path_buy = ".\\update_data\\stock-buy\\{}-buy\\{}\\".format(
            market, TODAY)
        path_sell = ".\\update_data\\stock-sell\\{}-sell\\{}\\".format(
            market, TODAY)
        path_net = ".\\update_data\\stock-net\\{}-net\\{}".format(
            market, TODAY)
        file_name = code + ".csv"
        tmp_buy = kiwoom.data[kiwoom.data['buysell'] == 'buy']
        tmp_sell = kiwoom.data[kiwoom.data['buysell'] == 'sell']
        tmp_buy = tmp_buy[tmp_buy['date'] >= int(start)]
        tmp_sell = tmp_sell[tmp_sell['date'] >= int(start)]
        del tmp_buy['buysell']
        del tmp_sell['buysell']
        cols2 = [
            "date", "code", "name", "close_price", "individual",
            "foreign_retail", "institution", "financial", "insurance", "trust",
            "etc_finance", "bank", "pension", "private", "nation",
            "etc_corporate", "foreign"
        ]
        add_col = [
            "date", "individual", "foreign_retail", "institution", "financial",
            "insurance", "trust", "etc_finance", "bank", "pension", "private",
            "nation", "etc_corporate", "foreign"
        ]
        add_buy = tmp_buy[add_col].set_index('date')
        add_sell = tmp_sell[add_col].set_index('date')
        tmp_net = add_buy.add(add_sell, fill_value=0)
        tmp_net = tmp_net.reset_index()
        tmp_net['name'] = tmp_buy['name']
        tmp_net['code'] = tmp_buy['code']
        tmp_net['close_price'] = tmp_buy['close_price']
        tmp_net = tmp_net[cols2]
        tmp_buy.to_csv(os.path.join(path_buy, file_name), index=False, sep=',')
        tmp_sell.to_csv(os.path.join(path_sell, file_name),
                        index=False,
                        sep=',')
        tmp_net.to_csv(os.path.join(path_net, file_name), index=False, sep=',')
        print(code + ": " + name + " buysell data successfully saved")

    def req_short(self):
        done_list = self._short_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        # start = self.check_start_day_file('short')
        start = self.check_start_log('short')
        code_looped = 0
        total_time = 0

        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_short_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " short save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")
        self.add_data_log('short')

    def _initialize_short_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL
        global TODAY

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " short data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        # opt10059 TR 요청
        kiwoom.set_input_value("종목코드", code)
        kiwoom.set_input_value("시간구분 ", 0)
        kiwoom.set_input_value("시작일자", time.strftime('%Y%m%d'))
        kiwoom.set_input_value("종료일자", time.strftime('%Y%m%d'))
        kiwoom.comm_rq_data("opt10014_req", "opt10014", 0, "0101")
        time.sleep(TR_REQ_TIME_INTERVAL)
        print("first request sent in successfully")

        while kiwoom.remained_data == True:
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("시간구분 ", 0)
            kiwoom.set_input_value("시작일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종료일자", time.strftime('%Y%m%d'))
            kiwoom.comm_rq_data("opt10014_req", "opt10014", 2, "0101")
            print("requesting...")
            current_date = kiwoom.get_date()
            print("date loop is on: ", str(current_date))
            if current_date <= int(start):
                print("loop breaking b/c " + str(current_date) +
                      " lower than " + str(start))
                break
            time.sleep(TR_REQ_TIME_INTERVAL)
        print("short data saved, ready for DB")
        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "short", "short_proportion",
            "short_total_price", "short_average_price"
        ]
        kiwoom.data = kiwoom.data[cols]
        update_short = kiwoom.data[kiwoom.data['date'] >= int(start)]
        path = ".\\update_data\\stock-short\\{}-short\\{}".format(
            market, TODAY)
        file_name = code + ".csv"
        update_short.to_csv(os.path.join(path, file_name), index=False)
        print(code + ": " + name + " short data successfully saved")
Exemple #10
0
        ("/balance", BalanceHandler),
    ]
    # Autoreload seems troublesome.
    return Application(urls, debug=True, autoreload=False)


def shutdown():
    # It seems there's no logout so... nothing here.
    pass


#
# Shared variables
#
app = QApplication(sys.argv)
hts = Kiwoom()

if __name__ == "__main__":
    # login
    if hts.kiwoom_GetConnectState() != 0:
        logger.debug('Connection failed')
        sys.exit()

    logger.debug('로그인 시도')
    res = hts.kiwoom_CommConnect()
    if res.get('result') != 0:
        logger.debug('Login failed')
        sys.exit()

    # To see list of your accounts...
    if True:
Exemple #11
0
class Gobble(ProcessTracker):
    @timeit
    def __init__(self):
        super().__init__()  # initialize ProcessTracker
        self.starting()
        self.app = QApplication(["kiwoom.py"])
        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()

    @timeit
    def step_one_kiwoom(self):
        self.step_one()
        etf = pd.read_csv('./etf_list.csv', header=None)
        etf = etf[0].tolist()
        etf = [str(e).zfill(6) for e in etf]
        for market_type in ["0", "10"]:
            pickle_name = "kospi-dict.pickle" if market_type == "0" else "kosdaq-dict.pickle"
            if market_type == "0":
                code_list = self.kiwoom.get_code_list_by_market(market_type)
                kospi_code_list = [
                    code for code in code_list if code not in etf
                ]
                etf_code_list = [code for code in code_list if code in etf]
                kospi_name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in kospi_code_list
                ]
                etf_name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in etf_code_list
                ]
                kospi_market_dict = dict(zip(kospi_code_list, kospi_name_list))
                pickle_out = open("./data/" + pickle_name, "wb")
                pickle.dump(kospi_market_dict, pickle_out)
                etf_market_dict = dict(zip(etf_code_list, etf_name_list))
                etf_pickle_out = open("./data/etf-dict.pickle", "wb")
                pickle.dump(etf_market_dict, etf_pickle_out)
                pickle_out.close()
            else:
                code_list = self.kiwoom.get_code_list_by_market(market_type)
                name_list = [
                    self.kiwoom.get_master_code_name(code)
                    for code in code_list
                ]
                market_dict = dict(zip(code_list, name_list))
                pickle_out = open("./data/" + pickle_name, "wb")
                pickle.dump(market_dict, pickle_out)
                pickle_out.close()
        self.step_one_finish()

    def set_tasks(self, market_type=None):
        if market_type == None:
            kospi_in = open("./data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            kosdaq_in = open("./data/kosdaq-dict.pickle", "rb")
            self.kosdaq_task = pickle.load(kosdaq_in)
            etf_in = open("./data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)
        elif market_type == 'kospi':
            kospi_in = open("./data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            self.kosdaq_task = {}
            self.etf_task = {}
        elif market_type == 'kosdaq':
            self.kospi_task = {}
            kosdaq_in = open("./data/kosdaq-dict.pickle", "rb")
            self.kosdaq_task = pickle.load(kosdaq_in)
            self.etf_task = {}
        elif market_type == 'etf':
            self.kospi_task = {}
            self.kosdaq_task = {}
            etf_in = open("./data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)
        elif market_type == 'kospi-etf':
            kospi_in = open("./data/kospi-dict.pickle", "rb")
            self.kospi_task = pickle.load(kospi_in)
            self.kosdaq_task = {}
            etf_in = open("./data/etf-dict.pickle", "rb")
            self.etf_task = pickle.load(etf_in)

    def _get_total_stock_num(self):
        kospi_len = len(list(self.kospi_task.keys()))
        kosdaq_len = len(list(self.kosdaq_task.keys()))
        etf_len = len(list(self.etf_task.keys()))
        return kospi_len + kosdaq_len + etf_len

    def _ohlcv_skip_codes(self):
        os.chdir("./data/stock-ohlcv/kospi-ohlcv")
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-ohlcv/kosdaq-ohlcv")
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-ohlcv/etf-ohlcv")
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        return kospi_list + kosdaq_list + etf_list

    def _buysell_skip_codes(self):
        os.chdir("./data/stock-buy/kospi-buy")
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-buy/kosdaq-buy")
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-buy/etf-buy")
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        return kospi_list + kosdaq_list + etf_list

    def _short_skip_codes(self):
        os.chdir("./data/stock-short/kospi-short")
        kospi_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-short/kosdaq-short")
        kosdaq_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        os.chdir("./data/stock-short/etf-short")
        etf_list = [json.split(".")[0] for json in os.listdir()]
        os.chdir("../../../")
        return kospi_list + kosdaq_list + etf_list

    def req_ohlcv(self, start):
        done_list = self._ohlcv_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        code_looped = 0
        total_time = 0

        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_ohlcv_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " ohlcv save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")

    def _initialize_ohlcv_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " ohlcv data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        # opt10059 TR 요청
        kiwoom.set_input_value("기준일자", time.strftime('%Y%m%d'))
        kiwoom.set_input_value("종목코드", code)
        kiwoom.set_input_value("수정주가구분 ", 1)
        kiwoom.comm_rq_data("opt10081_req", "opt10081", 0, "0101")
        time.sleep(TR_REQ_TIME_INTERVAL)
        print("first request sent in successfully")

        while kiwoom.remained_data == True:
            kiwoom.set_input_value("기준일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("수정주가구분 ", 1)
            kiwoom.comm_rq_data("opt10081_req", "opt10081", 2, "0101")
            print("requesting...")
            current_date = kiwoom.get_date()
            print("date loop is on: ", str(current_date))
            if current_date <= int(start):
                print("loop breaking b/c " + str(current_date) +
                      " lower than " + str(start))
                break
            time.sleep(TR_REQ_TIME_INTERVAL)
        print("OHLCV data saved, ready for DB")
        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "open_price", "high_price", "low_price",
            "close_price", "volume"
        ]
        kiwoom.data = kiwoom.data[cols]
        path = ".\\data\\stock\\" + market + "-ohlcv\\"
        file_name = code + ".csv"
        kiwoom.data.to_csv(os.path.join(path, file_name), index=False)
        print(code + ": " + name + " ohlcv data successfully saved")

    def req_buysell(self, start):
        done_list = self._buysell_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        code_looped = 0
        total_time = 0

        # get code list (0: KOSPI, 10: KOSDAQ)
        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_buysell_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " buysell save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")

    def _initialize_buysell_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " buysell data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        for buysell in [1, 2]:
            if buysell == 1:
                kiwoom.set_buysell_state("buy")
            elif buysell == 2:
                kiwoom.set_buysell_state("sell")

            # opt10059 TR 요청
            kiwoom.set_input_value("일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("금액수량구분", 2)
            kiwoom.set_input_value("매매구분", buysell)
            kiwoom.set_input_value("단위구분", 1)
            kiwoom.comm_rq_data("opt10059_req", "opt10059", 0, "0101")
            time.sleep(TR_REQ_TIME_INTERVAL)
            print("first request sent in successfully")

            while kiwoom.remained_data == True:
                kiwoom.set_input_value("일자", time.strftime('%Y%m%d'))
                kiwoom.set_input_value("종목코드", code)
                kiwoom.set_input_value("금액수량구분", 2)
                kiwoom.set_input_value("매매구분", buysell)
                kiwoom.set_input_value("단위구분", 1)
                kiwoom.comm_rq_data("opt10059_req", "opt10059", 2, "0101")
                print("requesting...")
                current_date = kiwoom.get_date()
                print("date loop is on: ", str(current_date))
                if current_date <= int(start):
                    print("loop breaking b/c " + str(current_date) +
                          " lower than " + str(start))
                    break
                time.sleep(TR_REQ_TIME_INTERVAL)
            if buysell == 1:
                print("BUY data saved, ready for DB")
            elif buysell == 2:
                print("SELL data saved, ready for DB")

        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "close_price", "individual",
            "foreign_retail", "institution", "financial", "insurance", "trust",
            "etc_finance", "bank", "pension", "private", "nation",
            "etc_corporate", "foreign", "buysell"
        ]
        kiwoom.data = kiwoom.data[cols]
        for bs in ['buy', 'sell']:
            path = ".\\data\\stock\\{}-{}\\".format(market, bs)
            file_name = code + ".csv"
            tmp = kiwoom.data[kiwoom.data['buysell'] == bs]
            del tmp['buysell']
            tmp.to_csv(os.path.join(path, file_name),
                       index=False,
                       sep=',',
                       encoding='utf-8')
        print(code + ": " + name + " buysell data successfully saved")

    def req_short(self, start):
        done_list = self._short_skip_codes()
        total_stock_num = self._get_total_stock_num() - len(done_list)
        code_looped = 0
        total_time = 0

        for market_type in [0, 10, 2]:
            if market_type == 0:
                market = "kospi"
                task = self.kospi_task
            elif market_type == 10:
                market = "kosdaq"
                task = self.kosdaq_task
            else:
                market = "etf"
                task = self.etf_task

            for code, name in task.items():
                if code in done_list:
                    continue
                ts = time.time()
                try:
                    self._initialize_short_data(code, market, start)
                except:
                    print(code + ", " + name +
                          " short save skipped due to error")
                te = time.time()
                time_took = te - ts
                total_time += time_took
                code_looped += 1
                avg_time_took = total_time / code_looped
                stocks_left = total_stock_num - code_looped
                time_left = avg_time_took * stocks_left
                print(str(stocks_left) + " stocks left to save")
                print(str(time_left) + " seconds left to finish whole request")
                print("---------------------------------------------------")

    def _initialize_short_data(self, code, market, start):
        global TR_REQ_TIME_INTERVAL

        kiwoom = self.kiwoom
        name = kiwoom.get_master_code_name(code)
        time.sleep(TR_REQ_TIME_INTERVAL)
        print(code + ": " + name + " short data initializing")
        kiwoom.prepare_data()
        print("update data dict created")

        # opt10059 TR 요청
        kiwoom.set_input_value("종목코드", code)
        kiwoom.set_input_value("시간구분 ", 0)
        kiwoom.set_input_value("시작일자", time.strftime('%Y%m%d'))
        kiwoom.set_input_value("종료일자", time.strftime('%Y%m%d'))
        kiwoom.comm_rq_data("opt10014_req", "opt10014", 0, "0101")
        time.sleep(TR_REQ_TIME_INTERVAL)
        print("first request sent in successfully")

        while kiwoom.remained_data == True:
            kiwoom.set_input_value("종목코드", code)
            kiwoom.set_input_value("시간구분 ", 0)
            kiwoom.set_input_value("시작일자", time.strftime('%Y%m%d'))
            kiwoom.set_input_value("종료일자", time.strftime('%Y%m%d'))
            kiwoom.comm_rq_data("opt10014_req", "opt10014", 2, "0101")
            print("requesting...")
            current_date = kiwoom.get_date()
            print("date loop is on: ", str(current_date))
            if current_date <= int(start):
                print("loop breaking b/c " + str(current_date) +
                      " lower than " + str(start))
                break
            time.sleep(TR_REQ_TIME_INTERVAL)
        print("short data saved, ready for DB")
        length = kiwoom.data.shape[0]
        code_list = [code] * length
        name_list = [name] * length
        kiwoom.data['code'] = code_list
        kiwoom.data['name'] = name_list
        cols = [
            "date", "code", "name", "short", "short_proportion",
            "short_total_price", "short_average_price"
        ]
        kiwoom.data = kiwoom.data[cols]
        path = ".\\data\\stock\\" + market + "-short\\"
        file_name = code + ".csv"
        kiwoom.data.to_csv(os.path.join(path, file_name), index=False)
        print(code + ": " + name + " short data successfully saved")
Exemple #12
0
    def __init__(self):
        super().__init__()

        self.initUI()

        self.kiwoom = Kiwoom(self.view)
Exemple #13
0
    def __init__(self):
        print("Main() Start")

        self.app = QApplication(sys.argv)
        self.kiwoom = Kiwoom()
        self.app.exec_()
Exemple #14
0
    def __init__(self):
        print("Main() start")

        self.app = QApplication(sys.argv)  # PyQt5로 실행할 파일명을 자동 설정
        self.kiwoom = Kiwoom()  # 키움 클래스 실행
        self.app.exec_()  # 이벤트 루프 실행
Exemple #15
0
class Trader(TraderBase):
    def __init__(self, log, key):
        self.broker = Kiwoom(self, log, key)
        # self.broker = Bankis(self, log, key)
        self.algorithm = FMAlgorithm2(self, log)
        self.general_account_index = 1
        self.futures_account_index = 0
        super().__init__(log)

        # Initial work
        self.connect_broker()
        self.get_account_list()
        # self.broker.request_deposit_info()
        # self.broker.request_portfolio_info()
        # self.broker.request_order_history()

        # Initial Values
        self.sb_capital.setValue(200000000)
        self.sb_interval.setValue(0.3)
        self.sb_loss_cut.setValue(0.15)
        self.sb_fee.setValue(0.003)
        self.sb_min_transaction.setValue(10)
        self.sb_amount.setValue(1)

        # Init Fields
        self.interval = self.sb_interval.value()
        self.loss_cut = self.sb_loss_cut.value()

        # Test setup
        self.cbb_item_name.setCurrentIndex(3)
        # self.broker.request_futures_deposit_info()
        # self.broker.request_futures_portfolio_info()

        self.t = 0

    def test1(self):
        self.debug('test1 button clicked')

        # start = int(self.le_test.text())
        # self.t = start
        # self.algorithm.turnaround(start)

        # self.algorithm.episode_count += 1
        # self.algorithm.episode_in_progress = True
        # self.algorithm.trade_position = LONG_POSITION
        # self.set_reference(current_price)
        # print('Episode in progress')

        chart = self.algorithm.futures.chart
        y = self.algorithm.get_linear_regression(chart, chart.MA5)
        print(y)

    def test2(self):
        self.debug('test2 button clicked')

        # self.t += 1
        # self.algorithm.turnaround(self.t)

        self.algorithm.episode_in_progress = False
        print('Episode NOT in progress')

    def connect_broker(self):
        # if self.cb_auto_login.isChecked():
        #     self.broker.auto_login()
        # else:
        #     self.broker.login()
        #     self.broker.set_account_password()

        self.broker.auto_login()

    def get_account_list(self):
        account_list = self.broker.get_account_list()
        if account_list is not None:
            self.cbb_account.addItems(self.broker.account_list)

    def get_deposit(self):
        if self.running_futures_account():
            self.broker.request_futures_deposit_info()
        else:
            self.broker.request_deposit_info()

    def get_portfolio(self):
        if self.running_futures_account():
            self.broker.request_futures_portfolio_info()
        else:
            self.broker.request_portfolio_info()

    def get_order_history(self):
        self.broker.request_order_history()

    def go_chart(self):
        if self.algorithm.is_running:
            return

        self.chart_item_code = self.cbb_item_code.currentText()
        item_name = self.cbb_item_name.currentText()
        self.broker.chart_prices.clear()
        self.chart = self.chart[0:0]
        self.chart_locator = list()
        self.chart_formatter = list()
        self.interval_prices = list()
        self.loss_cut_prices = list()
        self.top_price = 0
        self.bottom_price = 0

        self.broker.chart_prices.clear()
        if self.chart_item_code[:3] == FUTURES_CODE:
            self.broker.request_futures_stock_price_min(self.chart_item_code)
        else:
            self.broker.request_stock_price_min(self.chart_item_code)

        self.broker.is_running_chart = True
        self.timer.start()
        self.on_add_item()
        self.info('Go Charting', item_name)

    def stop_chart(self):
        self.broker.is_running_chart = False
        self.timer.stop()
        self.info('Stop Charting')

    def send_order(self):
        item_code = self.cbb_item_code.currentText()
        order_position = self.cbb_order_position.currentText()
        price = self.sb_price.value()
        amount = self.sb_amount.value()
        order_type = self.cbb_order_type.currentText()
        order_number = self.le_order_number.text()

        self.broker.order(item_code, price, amount, order_position, order_type,
                          order_number)

    def update_deposit_info(self):
        if not self.broker.deposit or not self.broker.orderable_money:
            self.debug('No deposit information')
            return

        deposit = '\\' + self.formalize(self.broker.deposit)
        orderable_money = '\\' + self.formalize(self.broker.orderable_money)
        self.lb_deposit.setText(deposit)
        self.lb_orderable.setText(orderable_money)

    def update_order_variables(self):
        if not self.broker.account_number:
            return

        item = Item()
        item_code = self.cbb_item_code.currentText()
        if item_code in self.broker.portfolio:
            item = self.broker.portfolio[item_code]

        buyable_amount = 'no info'
        if item.current_price != 0 and type(
                self.broker.orderable_money) != str:
            buyable_amount = self.formalize(self.broker.orderable_money //
                                            item.current_price)
        self.lb_buyable.setText(buyable_amount)
        self.lb_sellable.setText(self.formalize(item.holding_amount))
        self.sb_price.setValue(item.current_price)

    def go(self):
        if self.algorithm.is_running:
            self.debug('Algorithm is already running')
            return

        capital = self.sb_capital.value()
        interval = self.sb_interval.value()
        loss_cut = self.sb_loss_cut.value()
        fee = self.sb_fee.value()
        minimum_transaction_amount = self.sb_min_transaction.value()
        self.algorithm.start(self.broker, capital, interval, loss_cut, fee,
                             minimum_transaction_amount)
        self.log_info('Algorithm started')

    def stop(self):
        self.algorithm.stop()
        self.log_info('Algorithm stopped')

    def display_portfolio(self):
        self.clear_table(self.table_portfolio)
        for item in self.broker.portfolio.values():
            self.table_portfolio.insertRow(0)
            self.table_portfolio.setRowHeight(0, 8)
            self.table_portfolio.setItem(0, 0, self.to_item(item.item_name))
            self.table_portfolio.setItem(
                0, 1, self.to_item_float2(item.current_price))
            self.table_portfolio.setItem(
                0, 2, self.to_item_float3(item.purchase_price))
            self.table_portfolio.setItem(
                0, 3, self.to_item_sign(item.holding_amount))
            self.table_portfolio.setItem(0, 4, self.to_item(item.purchase_sum))
            self.table_portfolio.setItem(0, 5,
                                         self.to_item(item.evaluation_sum))
            self.table_portfolio.setItem(0, 6, self.to_item(item.total_fee))
            self.table_portfolio.setItem(0, 7, self.to_item(item.tax))
            self.table_portfolio.setItem(0, 8, self.to_item_sign(item.profit))
            self.table_portfolio.setItem(0, 9,
                                         self.to_item_sign(item.profit_rate))
            if isinstance(item, FuturesItem):
                self.display_futures_portfolio(item)
        self.table_portfolio.sortItems(0, Qt.AscendingOrder)

    def display_futures_portfolio(self, futures_item):
        for index, contract in enumerate(futures_item.contracts):
            self.table_portfolio.insertRow(0)
            self.table_portfolio.setRowHeight(0, 8)
            self.table_portfolio.setItem(
                0, 0,
                self.to_item_gray(contract.item_name + ' ({})'.format(index)))
            self.table_portfolio.setItem(
                0, 1, self.to_item_float2(contract.current_price))
            self.table_portfolio.setItem(
                0, 2, self.to_item_float2(contract.purchase_price))
            self.table_portfolio.setItem(
                0, 3, self.to_item_sign(contract.holding_amount))
            self.table_portfolio.setItem(0, 4,
                                         self.to_item(contract.purchase_sum))
            self.table_portfolio.setItem(0, 5,
                                         self.to_item(contract.evaluation_sum))
            self.table_portfolio.setItem(0, 6,
                                         self.to_item(contract.total_fee))
            self.table_portfolio.setItem(0, 7, self.to_item(contract.tax))
            self.table_portfolio.setItem(0, 8,
                                         self.to_item_sign(contract.profit))
            self.table_portfolio.setItem(
                0, 9, self.to_item_sign(contract.profit_rate))

    def display_monitoring_items(self):
        self.clear_table(self.table_monitoring_items)
        for item in self.broker.monitoring_items.values():
            self.table_monitoring_items.insertRow(0)
            self.table_monitoring_items.setRowHeight(0, 8)
            self.table_monitoring_items.setItem(0, 0,
                                                self.to_item(item.item_name))
            self.table_monitoring_items.setItem(
                0, 1, self.to_item_time(item.transaction_time))
            self.table_monitoring_items.setItem(
                0, 2, self.to_item_float2(item.current_price))
            self.table_monitoring_items.setItem(
                0, 3, self.to_item_float2(item.ask_price))
            self.table_monitoring_items.setItem(
                0, 4, self.to_item_float2(item.bid_price))
            self.table_monitoring_items.setItem(0, 5,
                                                self.to_item(item.volume))
            self.table_monitoring_items.setItem(
                0, 6, self.to_item(item.accumulated_volume))
            self.table_monitoring_items.setItem(
                0, 7, self.to_item_float2(item.high_price))
            self.table_monitoring_items.setItem(
                0, 8, self.to_item_float2(item.low_price))
            self.table_monitoring_items.setItem(
                0, 9, self.to_item_float2(item.open_price))
        self.table_monitoring_items.sortItems(0, Qt.DescendingOrder)

    def display_balance(self):
        self.clear_table(self.table_balance)
        for item in self.broker.balance.values():
            self.table_balance.insertRow(0)
            self.table_balance.setRowHeight(0, 8)
            self.table_balance.setItem(0, 0, self.to_item(item.item_name))
            self.table_balance.setItem(0, 1,
                                       self.to_item_float2(item.current_price))
            self.table_balance.setItem(
                0, 2, self.to_item_float2(item.reference_price))
            self.table_balance.setItem(0, 3,
                                       self.to_item(item.purchase_price_avg))
            self.table_balance.setItem(0, 4, self.to_item(item.holding_amount))
            self.table_balance.setItem(0, 5, self.to_item(item.purchase_sum))
            self.table_balance.setItem(
                0, 6, self.to_item_sign(item.purchase_amount_net_today))
            self.table_balance.setItem(
                0, 7, self.to_item_sign(item.balance_profit_net_today))
            self.table_balance.setItem(
                0, 8, self.to_item_sign(item.balance_profit_rate))
            self.table_balance.setItem(
                0, 9, self.to_item_sign(item.balance_profit_realization))
        self.table_balance.sortItems(0, Qt.DescendingOrder)

    def display_open_orders(self):
        self.clear_table(self.table_open_orders)
        for order in self.broker.open_orders.values():
            self.table_open_orders.insertRow(0)
            self.table_open_orders.setRowHeight(0, 8)
            self.table_open_orders.setItem(0, 0, self.to_item(order.item_name))
            self.table_open_orders.setItem(
                0, 1, self.to_item_time(order.executed_time))
            self.table_open_orders.setItem(0, 2,
                                           self.to_item(order.order_amount))
            self.table_open_orders.setItem(
                0, 3, self.to_item(order.executed_amount_sum))
            self.table_open_orders.setItem(0, 4,
                                           self.to_item(order.open_amount))
            self.table_open_orders.setItem(
                0, 5, self.to_item_plain(order.order_number))
            self.table_open_orders.setItem(
                0, 6, self.to_item_plain(order.original_order_number))
            self.table_open_orders.setItem(
                0, 7, self.to_item_float2(order.order_price))
            self.table_open_orders.setItem(
                0, 8, self.to_item(order.executed_price_avg))
            self.table_open_orders.setItem(
                0, 9, self.to_item_center(order.order_position))
            self.table_open_orders.setItem(
                0, 10, self.to_item_center(order.order_state))
        self.table_open_orders.sortItems(1, Qt.DescendingOrder)

    def display_order_history(self):
        self.clear_table(self.table_order_history)
        for order in self.broker.order_history.values():
            self.table_order_history.insertRow(0)
            self.table_order_history.setRowHeight(0, 8)
            self.table_order_history.setItem(0, 0,
                                             self.to_item(order.item_name))
            self.table_order_history.setItem(
                0, 1, self.to_item_time(order.executed_time))
            self.table_order_history.setItem(0, 2,
                                             self.to_item(order.order_amount))
            self.table_order_history.setItem(
                0, 3, self.to_item(order.executed_amount_sum))
            self.table_order_history.setItem(0, 4,
                                             self.to_item(order.open_amount))
            self.table_order_history.setItem(
                0, 5, self.to_item_plain(order.order_number))
            self.table_order_history.setItem(
                0, 6, self.to_item_plain(order.original_order_number))
            self.table_order_history.setItem(
                0, 7, self.to_item_float2(order.order_price))
            self.table_order_history.setItem(
                0, 8, self.to_item_float2(order.executed_price_avg))
            self.table_order_history.setItem(
                0, 9, self.to_item_center(order.order_position))
            self.table_order_history.setItem(
                0, 10, self.to_item_center(order.order_state))
        self.table_order_history.sortItems(1, Qt.DescendingOrder)

    def display_algorithm_trading(self):
        self.clear_table(self.table_algorithm_trading)
        for algorithm_number, order in self.algorithm.orders.items():
            self.table_algorithm_trading.insertRow(0)
            self.table_algorithm_trading.setRowHeight(0, 8)
            self.table_algorithm_trading.setItem(0, 0,
                                                 self.to_item(order.item_name))
            self.table_algorithm_trading.setItem(
                0, 1, self.to_item_time(order.executed_time))
            self.table_algorithm_trading.setItem(
                0, 2, self.to_item_plain(order.episode_number))
            self.table_algorithm_trading.setItem(
                0, 3, self.to_item_plain(order.order_number))
            self.table_algorithm_trading.setItem(
                0, 4, self.to_item_center(order.order_position))
            self.table_algorithm_trading.setItem(
                0, 5, self.to_item_center(order.order_state))
            self.table_algorithm_trading.setItem(
                0, 6, self.to_item_float2(order.order_price))
            self.table_algorithm_trading.setItem(
                0, 7, self.to_item_float2(order.executed_price_avg))
            self.table_algorithm_trading.setItem(
                0, 8, self.to_item(order.order_amount))
            self.table_algorithm_trading.setItem(
                0, 9, self.to_item(order.executed_amount_sum))
            self.table_algorithm_trading.setItem(
                0, 10, self.to_item_sign(order.open_amount))
            self.table_algorithm_trading.setItem(
                0, 11, self.to_item_sign(order.net_profit))
        self.table_algorithm_trading.sortItems(2, Qt.DescendingOrder)

    def clear_table(self, table):
        for row in range(table.rowCount()):
            table.removeRow(0)

    def display_algorithm_results(self):
        total_profit = self.algorithm.total_profit
        formalized_profit = self.formalize(total_profit)
        profit_rate = round(total_profit / self.sb_capital.value() * 100, 2)
        profit_display = '{} ({}%)'.format(formalized_profit, profit_rate)
        self.lb_total_profit.setText(profit_display)

        total_fee = self.algorithm.total_fee
        formalized_fee = self.formalize(total_fee)
        self.lb_total_fee.setText(formalized_fee)

        net_profit = self.algorithm.net_profit
        formalized_net_profit = self.formalize(net_profit)
        net_profit_rate = round(net_profit / self.sb_capital.value() * 100, 2)
        net_profit_display = '{} ({}%)'.format(formalized_net_profit,
                                               net_profit_rate)
        self.lb_net_profit.setText(net_profit_display)

    def process_past_chart_prices(self, item_code, chart_prices):
        if self.algorithm.is_running:
            self.algorithm.process_past_chart_prices(item_code, chart_prices)
            return
        elif item_code != self.chart_item_code:
            return

        columns = ['Time', 'Open', 'High', 'Low', 'Close', 'Volume']
        past_chart = pandas.DataFrame(chart_prices, columns=columns)
        past_chart.Time = pandas.to_datetime(past_chart.Time)
        past_chart.set_index('Time', inplace=True)
        # self.chart = past_chart.append(self.chart)
        self.chart = past_chart

        self.draw_chart.start()

    def update_chart_prices(self, price, volume, item_code):
        if item_code != self.chart_item_code:
            return

        current_time = datetime.now().replace(second=0, microsecond=0)
        if not len(self.chart):
            price_data = [price, price, price, price, volume]
            self.chart.loc[current_time] = price_data
        elif current_time > self.chart.index[-1]:
            price_data = [price, price, price, price, volume]
            self.chart.loc[current_time] = price_data
        else:
            if price > self.chart.High[-1]:
                self.chart.loc[current_time, 'High'] = price
            elif price < self.chart.Low[-1]:
                self.chart.loc[current_time, 'Low'] = price
            last_price = self.chart.Close[-1]
            self.chart.loc[current_time, 'Close'] = price
            self.chart.loc[current_time, 'Volume'] += volume
            if last_price == price:
                return
        self.draw_chart.start()

    def on_every_minute(self):
        current_time = datetime.now().replace(second=0, microsecond=0)
        if not len(self.chart):
            return
        if current_time > self.chart.index[-1]:
            price = self.chart.Close[-1]
            price_data = [price, price, price, price, 0]
            self.chart.loc[current_time] = price_data
            self.draw_chart.start()

    def display_chart(self):
        if not len(self.chart):
            return

        self.ax.clear()

        # Axis ticker formatting
        if len(self.chart) // 30 > len(self.chart_locator) - 1:
            for index in range(
                    len(self.chart_locator) * 30, len(self.chart), 30):
                time_format = self.chart.index[index].strftime('%H:%M')
                self.chart_locator.append(index)
                self.chart_formatter.append(time_format)
        self.ax.xaxis.set_major_locator(ticker.FixedLocator(
            self.chart_locator))
        self.ax.xaxis.set_major_formatter(
            ticker.FixedFormatter(self.chart_formatter))

        # Axis yticks & lines
        max_price = self.chart.High.max()
        min_price = self.chart.Low.min()
        if max_price > self.top_price or min_price < self.bottom_price:
            self.top_price = math.ceil(
                max_price / self.interval) * self.interval
            self.bottom_price = math.floor(
                min_price / self.interval) * self.interval
            self.interval_prices = np.arange(self.bottom_price,
                                             self.top_price + self.interval,
                                             self.interval)
            self.loss_cut_prices = np.arange(
                self.bottom_price + self.interval - self.loss_cut,
                self.top_price, self.interval)
        self.ax.grid(axis='x', alpha=0.5)
        self.ax.set_yticks(self.interval_prices)
        for price in self.interval_prices:
            self.ax.axhline(price, alpha=0.5, linewidth=0.2)
        for price in self.loss_cut_prices:
            self.ax.axhline(price, alpha=0.4, linewidth=0.2, color='Gray')

        # Current Price Annotation
        current_time = len(self.chart)
        current_price = self.chart.Close.iloc[-1]
        if self.chart_item_code[:3] == FUTURES_CODE:
            formatted_current_price = format(current_price, ',.2f')
        else:
            formatted_current_price = format(current_price, ',')
        # self.ax.text(current_time + 2, current_price, format(current_price, ','))
        self.ax.text(current_time + 2, current_price, formatted_current_price)

        # Draw Chart
        candlestick2_ohlc(self.ax,
                          self.chart.Open,
                          self.chart.High,
                          self.chart.Low,
                          self.chart.Close,
                          width=0.4,
                          colorup='red',
                          colordown='blue')
        self.fig.tight_layout()
        self.canvas.draw()

    def on_select_account(self, index):
        self.broker.account_number = self.cbb_account.currentText()

        if index == self.futures_account_index:
            self.broker.request_futures_deposit_info()
            self.broker.request_futures_portfolio_info()
            if not self.running_futures_code():
                self.cbb_item_code.setCurrentText('101')
        else:
            self.broker.request_deposit_info()
            self.broker.request_portfolio_info()
            if self.running_futures_code():
                self.cbb_item_code.setCurrentText('')

    def on_select_item_code(self, item_code):
        item_name = CODES.get(item_code)
        if item_name is None:
            item_name = self.broker.get_item_name(item_code)
            if item_name != '':
                CODES[item_code] = item_name
                self.cbb_item_code.addItem(item_code)
                self.cbb_item_name.addItem(item_name)

        if item_code[:3] == FUTURES_CODE:
            self.cbb_order_type.clear()
            self.cbb_order_type.addItems(FUTURES_ORDER_TYPE)
            self.cbb_account.setCurrentIndex(self.futures_account_index)
        else:
            self.cbb_order_type.clear()
            self.cbb_order_type.addItems(ORDER_TYPE)
            self.cbb_account.setCurrentIndex(self.general_account_index)

        self.cbb_item_name.setCurrentText(item_name)
        self.update_order_variables()

    def on_select_item_name(self, index):
        item_name = self.cbb_item_name.currentText()
        item_code = self.broker.get_item_code(item_name)
        self.cbb_item_code.setCurrentText(item_code)

    def on_add_item(self):
        item_code = self.cbb_item_code.currentText()
        item_name = self.cbb_item_name.currentText()
        if item_code in self.broker.monitoring_items:
            return

        item = Item()
        item.item_code = item_code
        item.item_name = item_name
        self.broker.demand_monitoring_items_info(item)
        self.display_monitoring_items()
        self.info(item.item_name, 'trading information begins to be monitored')

    def on_remove_item(self):
        item_code = self.cbb_item_code.currentText()
        item_name = self.cbb_item_name.currentText()
        if item_code not in self.broker.monitoring_items:
            return

        self.broker.init_screen(item_code)
        del self.broker.monitoring_items[item_code]
        self.table_monitoring_items.clearContents()
        self.display_monitoring_items()
        self.info(item_name, 'stock information monitoring is finished')

    def on_select_portfolio_table(self, row, column):
        column_count = self.table_portfolio.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_portfolio.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_portfolio.item(row, item_name_column)
        item_name = item_name_item.text()
        if item_name[:2] == 'F ':
            item_name = item_name[:8]
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        current_price_column = 1
        current_price_item = self.table_portfolio.item(row,
                                                       current_price_column)
        current_price = self.process_type(current_price_item.text())
        self.sb_price.setValue(current_price)

        holding_amount_column = 3
        holding_amount_item = self.table_portfolio.item(
            row, holding_amount_column)
        holding_amount = self.process_type(holding_amount_item.text())
        self.sb_amount.setValue(abs(holding_amount))

        self.cbb_order_type.setCurrentText('MARKET')
        if holding_amount > 0:
            self.cbb_order_position.setCurrentText('SELL')
        else:
            self.cbb_order_position.setCurrentText('BUY')

    def on_select_trading_items_table(self, row, column):
        column_count = self.table_monitoring_items.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_monitoring_items.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_monitoring_items.item(
            row, item_name_column)
        item_name = item_name_item.text()
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        current_price_column = 2
        current_price_item = self.table_monitoring_items.item(
            row, current_price_column)
        current_price = self.process_type(current_price_item.text())
        self.sb_price.setValue(current_price)

    def on_select_balance_table(self, row, column):
        column_count = self.table_balance.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_balance.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_balance.item(row, item_name_column)
        item_name = item_name_item.text()
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        current_price_column = 1
        current_price_item = self.table_balance.item(row, current_price_column)
        current_price = self.process_type(current_price_item.text())
        self.sb_price.setValue(current_price)

    def on_select_open_orders_table(self, row, column):
        column_count = self.table_order_history.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_open_orders.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_open_orders.item(row, item_name_column)
        item_name = item_name_item.text()
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        open_amount_column = 4
        open_amount_item = self.table_open_orders.item(row, open_amount_column)
        open_amount = self.process_type(open_amount_item.text())
        self.sb_amount.setValue(open_amount)

        order_number_column = 5
        order_number_item = self.table_open_orders.item(
            row, order_number_column)
        order_number = order_number_item.text()
        self.le_order_number.setText(order_number)

        order_price_column = 7
        order_price_item = self.table_open_orders.item(row, order_price_column)
        order_price = self.process_type(order_price_item.text())
        self.sb_price.setValue(order_price)

        order_position_column = 9
        order_position_item = self.table_open_orders.item(
            row, order_position_column)
        order_position = order_position_item.text()
        if order_position == PURCHASE[-2:]:
            self.cbb_order_position.setCurrentText('CANCEL BUY')
        elif order_position == SELL[-2:]:
            self.cbb_order_position.setCurrentText('CANCEL SELL')

    def on_select_order_history_table(self, row, column):
        column_count = self.table_order_history.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_order_history.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_order_history.item(row, item_name_column)
        item_name = item_name_item.text()
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        open_amount_column = 4
        open_amount_item = self.table_order_history.item(
            row, open_amount_column)
        open_amount = self.process_type(open_amount_item.text())
        self.sb_amount.setValue(open_amount)

        order_number_column = 5
        order_number_item = self.table_order_history.item(
            row, order_number_column)
        order_number = order_number_item.text()
        self.le_order_number.setText(order_number)

        order_price_column = 7
        order_price_item = self.table_order_history.item(
            row, order_price_column)
        order_price = self.process_type(order_price_item.text())
        self.sb_price.setValue(order_price)

    def on_select_algorithm_trading_table(self, row, column):
        column_count = self.table_algorithm_trading.columnCount() - 1
        selection_range = QTableWidgetSelectionRange(row, 0, row, column_count)
        self.table_algorithm_trading.setRangeSelected(selection_range, True)

        item_name_column = 0
        item_name_item = self.table_algorithm_trading.item(
            row, item_name_column)
        item_name = item_name_item.text()
        index = self.cbb_item_name.findText(item_name)
        self.cbb_item_name.setCurrentIndex(index)

        order_number_column = 3
        order_number_item = self.table_algorithm_trading.item(
            row, order_number_column)
        order_number = order_number_item.text()
        self.le_order_number.setText(order_number)

        order_price_column = 6
        order_price_item = self.table_algorithm_trading.item(
            row, order_price_column)
        order_price = self.process_type(order_price_item.text())
        if order_price:
            self.sb_price.setValue(order_price)

        open_amount_column = 10
        open_amount_item = self.table_algorithm_trading.item(
            row, open_amount_column)
        open_amount = self.process_type(open_amount_item.text())
        self.sb_amount.setValue(open_amount)

    def set_algorithm_parameters(self):
        self.interval = self.sb_interval.value()
        self.loss_cut = self.sb_loss_cut.value()
        self.algorithm.capital = self.sb_capital.value()
        self.algorithm.interval = self.interval
        self.algorithm.loss_cut = self.loss_cut
        self.algorithm.fee = self.sb_fee.value()
        self.algorithm.minimum_transaction_amount = self.sb_min_transaction.value(
        )

    def on_edit_save_file(self):
        file = self.le_save_file.text()
        self.broker.log_file = file

    def on_change_save_file(self):
        file = QFileDialog.getOpenFileName(self, 'Select File',
                                           self.setting['save_folder'])[0]
        if file != '':
            self.le_save_file.setText(file)
            self.broker.log_file = file

    def running_futures_code(self):
        if self.cbb_item_code.currentText()[:3] == FUTURES_CODE:
            return True
        else:
            return False

    def running_futures_account(self):
        if self.cbb_account.currentIndex() == self.futures_account_index:
            return True
        else:
            return False

    def edit_setting(self):
        self.debug('setting')

    def log(self, *args):
        message = str(args[0])
        for arg in args[1:]:
            message += ' ' + str(arg)
        current_time = datetime.now().strftime('%H:%M:%S') + ' '
        self.te_info.append(current_time + message)
        self.info(message)

    def log_info(self, *args):
        message = str(args[0])
        for arg in args[1:]:
            message += ' ' + str(arg)
        current_time = datetime.now().strftime('%H:%M:%S') + ' '
        self.te_info.append(current_time + message)

    def status(self, *args):
        message = str(args[0])
        for arg in args[1:]:
            message += ' ' + str(arg)
        self.status_bar.showMessage(message)

    def closeEvent(self, event):
        self.broker.log('Closing process initializing...')
        self.broker.close_process()
        self.broker.clear()
        self.broker.deleteLater()
        self.deleteLater()
Exemple #16
0
from PyQt5.QtWidgets import *
from PyQt5.QAxContainer import *
from PyQt5.QtCore import *
import time

from kiwoom import Kiwoom

TR_REQ_TIME_INTERVAL = 3.8
START = '20000101'

# starting app
app = QApplication(['kiwoom.py'])
kiwoom = Kiwoom()
kiwoom.comm_connect()

tasks = {}
# retrieving kospi, kosdaq codes
for market_type in ['0', '10']:
    code_list = kiwoom.get_code_list_by_market(market_type)
    name_list = [kiwoom.get_master_code_name(code) for code in code_list]
    market_dict = dict(zip(code_list, name_list))
    if market_type == '0':
        tasks['kospi'] = market_dict
    elif market_type == '10':
        tasks['kosdaq'] = market_dict

# requesting data from kiwoom server
# for market_type in [0, 10]:
#     if market_type == 0:
#         market = 'kospi'
#     elif market_type == 10:
Exemple #17
0
class AjouStock(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.kiwoom = Kiwoom()
        self.kiwoom.comm_connect()  # 키움 로그인

        self.trade_stocks_done = False  # 자동 주문

        self.timer = QTimer(self)
        self.timer.start(1000)  # 1초에 한 번씩 주기적으로 timeout 시그널 발생
        self.timer.timeout.connect(
            self.timeout)  # timeout 시그널이 발생할 때 이를 처리할 슬롯으로 self.timeout을 설정
        # StatusBar 위젯에 서버 연결 상태 및 현재 시간을 출력

        self.lineEdit.textChanged.connect(
            self.code_changed)  # lineEdit 객체가 변경될 때 호출되는 슬롯을 지정
        self.pushButton_2.clicked.connect(self.send_order)  # 현금주문

        accouns_num = int(self.kiwoom.get_login_info(
            "ACCOUNT_CNT"))  # 계좌 정보를 QComboBox 위젯에 출력하는 코드
        accounts = self.kiwoom.get_login_info("ACCNO")  # 로그인 정보 가져오기
        accounts_list = accounts.split(';')[
            0:accouns_num]  # 계좌가 여러 개인 경우 각 계좌는';'를 통해 구분

        self.comboBox.addItems(accounts_list)
        self.pushButton.clicked.connect(self.check_balance)  # 시그널과 슬롯을 연결

        self.load_buy_sell_list()  # 선정 종목 리스트 출력

    # 시간 체크 및 서버 연결 상태 확인
    def timeout(self):
        market_start_time = QTime(9, 0,
                                  0)  # 시간이 9시를 지났고 주문이 안됐을 때 trade_stocks 호출
        current_time = QTime.currentTime()

        if current_time > market_start_time and self.trade_stocks_done is False:  # 장이 시작할 때 주문을 넣으려면 timeout에서 시간 체크
            self.trade_stocks()
            self.trade_stocks_done = True

        text_time = current_time.toString("hh:mm:ss")
        time_msg = "Current Time : " + text_time

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

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

# 수동 주문 ----------------------------------------------------------------
# 종목 명 입력하면 종목코드 자동 생성

    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")

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

        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_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.timer2 = QTimer(self)  # 실시간 조회 체크박스
        self.timer2.start(1000 * 10)  # 10초에 한 번
        self.timer2.timeout.connect(self.timeout2)

    # 실시간 조회 체크박스 확인
    def timeout2(self):
        if self.checkBox.isChecked():
            self.check_balance()


# buy list, sell list 읽기 ----------------------------------------------------------------
# buy_list.txt와 sell_list.txt 읽는 함수

    def load_buy_sell_list(self):
        f = open(os.path.join(settings.BASE_DIR, "data/list/buy_list.txt"),
                 'rt')
        buy_list = f.readlines()
        f.close()

        f = open(os.path.join(settings.BASE_DIR, "data/list/sell_list.txt"),
                 'rt')
        sell_list = f.readlines()
        f.close()

        row_count = len(buy_list) + len(sell_list)
        self.tableWidget_3.setRowCount(row_count)

        # buy list
        for j in range(len(buy_list)):
            row_data = buy_list[j]
            split_row_data = row_data.split(';')
            split_row_data[1] = self.kiwoom.get_master_code_name(
                split_row_data[1].rsplit())

            for i in range(len(split_row_data)):
                item = QTableWidgetItem(split_row_data[i].rstrip())
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.tableWidget_3.setItem(j, i, item)

        # sell list
        for j in range(len(sell_list)):
            row_data = sell_list[j]
            split_row_data = row_data.split(';')
            split_row_data[1] = self.kiwoom.get_master_code_name(
                split_row_data[1].rstrip())

            for i in range(len(split_row_data)):
                item = QTableWidgetItem(split_row_data[i].rstrip())
                item.setTextAlignment(Qt.AlignVCenter | Qt.AlignCenter)
                self.tableWidget_3.setItem(len(buy_list) + j, i, item)

        self.tableWidget_3.resizeRowsToContents()

    # 장이 시작하면 매매 리스트에 선정된 종목에 대해 자동으로 주문하는 함수
    def trade_stocks(self):
        hoga_lookup = {'limits': "00", 'market': "03"}

        f = open(os.path.join(settings.BASE_DIR, "data/list/buy_list.txt"),
                 'rt')
        buy_list = f.readlines()
        f.close()

        f = open(os.path.join(settings.BASE_DIR, "data/list/sell_list.txt"),
                 'rt')
        sell_list = f.readlines()
        f.close()

        account = self.comboBox.currentText()  # 주문할 때 필요한 계좌 정보

        # 매수
        for row_data in buy_list:
            split_row_data = row_data.split(';')
            hoga = split_row_data[2]
            code = split_row_data[1]
            num = split_row_data[3]
            price = split_row_data[4]

            if split_row_data[-1].rstrip() == 'before':
                self.kiwoom.send_order("send_order_req", "0101", account, 1,
                                       code, num, price, hoga_lookup[hoga], "")

        # 매도
        for row_data in sell_list:
            split_row_data = row_data.split(';')
            hoga = split_row_data[2]
            code = split_row_data[1]
            num = split_row_data[3]
            price = split_row_data[4]

            if split_row_data[-1].rstrip() == 'before':
                self.kiwoom.send_order("send_order_req", "0101", account, 2,
                                       code, num, price, hoga_lookup[hoga], "")

        # 주문 여부 업데이트
        for i, row_data in enumerate(buy_list):
            buy_list[i] = buy_list[i].replace("before", "complete")

        f = open(os.path.join(settings.BASE_DIR, "data/list/buy_list.txt"),
                 'wt')
        for row_data in buy_list:
            f.write(row_data)
        f.close()

        for i, row_data in enumerate(sell_list):
            sell_list[i] = sell_list[i].replace("before", "complete")

        f = open(os.path.join(settings.BASE_DIR, "data/list/sell_list.txt"),
                 'wt')
        for row_data in sell_list:
            f.write(row_data)
        f.close()