티스토리 뷰

<한국투자증권의 OpenAPI를 활용한 주식 자동 주문 프로그램 만들기>

* 특정 종목에 대해 현재 시세를 확인함.

* 잔고 데이터와 비교하여 해당 종목을 매도할 지 여부를 판단하는 데에 활용

* 과거 데이터와 비교하여 해당 종목을 매수할 지 여부를 판단하는 데에 활용

 

 

[Source Code]

    def GetItemPrice_Now(self, paramIn): # 국내 주식 현재가 시세
        # paramIn = {'ItemCode', ...}

        itemCode = paramIn.get('ItemCode')

        url1 = "/uapi/domestic-stock/v1/quotations/inquire-price"
        acceccURL = self.URL0 + url1

        header1 = {
            'Content-Type': "application/json; charset=UTF-8",
            'authorization': f"Bearer {self.AccessToken}",
            #'authorization': self.AccessToken,
            'appkey': self.AppKey,
            'appsecret': self.AppSecret,
            'tr_id': "FHKST01010100", ## TTTC8001R(주식 일별 주문 체결 조회)
            'custtype': "P",
        }

        param1 = {
            'fid_cond_mrkt_div_code': "J", # J(주식, ETF, EFN)
            'fid_input_iscd': itemCode,
            #'fid_input_iscd': "005930"
        }

        res1 = requests.get(acceccURL, params=param1, headers=header1)
        #print(res1.text)
        try:
            inquiryResults = json.loads(res1.text)
        except:
            print("[?? in ] GetItemPrice_Now()", )
            return;
        success2 = inquiryResults.get('rt_cd') # if "0": SUCCESS
        if success2!="0": 
            print("Inquiry FAILED: ", success2.get('msg1'))
            return
        responseCode2 = inquiryResults.get('msg_cd')
        message2 = inquiryResults.get('msg1')
        outputs = inquiryResults.get('output')
        if not outputs: return;
        # iscd_stat_cls_code, marg_rate, marg_rate, new_hgpr_lwpr_cls_code, bstp_kor_isnm, temp_stop_yn, oprc_rang_cont_yn, clpr_rang_cont_yn
        # crdt_able_yn, grmn_rate_cls_code, elw_pblc_yn, 
        # stck_prpr, prdy_vrss, prdy_vrss_sign, prdy_ctrt, acml_tr_pbmn, acml_vol, prdy_vrss_vol_rate, 
        # stck_oprc, stck_hgpr, stck_lwpr, stck_mxpr, stck_llam, stck_sdpr,
        # wghn_avrg_stck_prc, hts_frgn_ehrt, frgn_ntby_qty, pgtr_ntby_qty, pvt_scnd_dmrs_prc, pvt_frst_dmrs_prc, 
        # pvt_pont_val, pvt_frst_dmsp_prc, pvt_frst_dmsp_prc, dmrs_val, dmrs_val, cpfn, rstc_wdth_prc, stck_fcam, stck_sspr, 
        # aspr_unit, hts_deal_qty_unit_val, lstn_stcn, hts_avls, per, pbr, stac_month, vol_tnrt, eps, bps,
        # d250_hgpr, d250_hgpr_date, d250_hgpr_vrss_prpr_rate, d250_lwpr, d250_lwpr, d250_lwpr, stck_dryy_hgpr, 
        # dryy_hgpr_vrss_prpr_rate, dryy_hgpr_vrss_prpr_rate, stck_dryy_lwpr, stck_dryy_lwpr, dryy_lwpr_date, 
        # w52_hgpr, w52_hgpr_vrss_prpr_ctrt, w52_hgpr_date, w52_lwpr, w52_lwpr_vrss_prpr_ctrt, w52_lwpr_date, whol_loan_rmnd_rate
        # ssts_yn, stck_shrn_iscd, fcam_cnnm, fcam_cnnm, apprch_rate, frgn_hldn_qty, vi_cls_code, ovtm_vi_cls_code, ovtm_vi_cls_code, 
        # invt_caful_yn, mrkt_warn_cls_code, short_over_yn
        itemCode = outputs.get('XXX') # 해당 없음
        highToday = int(outputs.get('stck_hgpr'))
        lowToday = int(outputs.get('stck_lwpr'))
        startPrice = int(outputs.get('stck_oprc'))
        currentPrice = int(outputs.get('stck_prpr'))
        signYesterday = int(outputs.get('prdy_vrss_sign')) # 1(상한), 2(상승), 3(보합), 4(하한), 5(하락)
        diffPreviousday = int(outputs.get('prdy_vrss'))
        high250 = int(outputs.get('d250_hgpr'))
        low250 = int(outputs.get('d250_lwpr'))

        rstParam = {
            'CurrentPrice': currentPrice,
            'HighToday': highToday,
            'LowToday': lowToday,
            'StartPrice': startPrice,
            'SignYesterday': signYesterday,
            'DiffPreviousDay': diffPreviousday,
            'High250': high250,
            'Low250': low250,
        }
        return rstParam
        ##return inquiryResults
반응형