티스토리 뷰

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

* 주식시장에 상장된 전체 주식 중에서 관심 대상이 되는 주식에 대해,

* 특정 기간(100일, 200일 등) 동안의 종가 데이터를 수집함.

 

 

[Source Code]

    def CollectPrice_byPeriod(self, paramIn=None):
        # paramIn = {'ItemCode', 'Days', 'SourceFile'}
        if paramIn:
            days0 = paramIn.get('Days')
            itemCode0 = paramIn.get('ItemCode')
            sourceFile = paramIn.get('SourceFile') # "./ItemDatas.xlsx"
        else: 
            days0 = None
            sourceFile = None
            itemCode0 = None

        if not days0: days0 = 100
        print("[...] CollectDailyPrice()")
        if self.ItemDF.empty: return;
        outFile = "./ItemDatas.xlsx"
        outSheet = "ItemDataD"+str(days0)+"_"+bTime.GetDateString()
        itemDataDF = bExcel.ReadFile(outFile, outSheet)
        if not itemDataDF.empty: return;
        itemList = self.ItemDF.to_dict('records')
        preTime1 = bTime.CalDateTime()
        message1 = "[BST-KIS] Collecting ... Price by Period with " + str(paramIn)
        message1 += "\n@ " + bTime.GetTimeString()
        bDiscordManager.SendMessage2BST(message1)

        random.shuffle(itemList)
        itemNumber = len(itemList)
        itemCount = 0
        for itemInfo1 in itemList:
            itemCount += 1
            #if itemCount>10: break; #------------------------
            while True:
                timeDiff2 = bTime.GetTimeDifference(bTime.GetTimeString(preTime1))
                secDiff2 = timeDiff2['Second']
                if secDiff2>self.WAITINGSECOND_MINIMUM: 
                    preTime1 = bTime.CalDateTime()
                    break
                else: time.sleep(0.3)

            print("[ItemInfo] ", itemCount, "/", itemNumber)
            #print(itemInfo1)
            itemInfo1['Days'] = days0
            try:
                res1 = self.GetItemPrice_byPeriod(itemInfo1)
            except: continue;
            if not res1: continue;

            output1 = res1.get('output1')
            output2 = res1.get('output2')
            itemCode = itemInfo1.get('ItemCode')
            comName = itemInfo1.get('CompanyName')
            marketType = itemInfo1.get('MarketType')
            itemData2 = {
                'ItemCode': itemCode,
                'CompanyName': comName,
                'MarketType': marketType
            }
            if output2:
                res2 = self.TreatDailyData(output2)
                #itemData2['Abstract'] = res2[0]
                itemData2.update(res2[0])
                itemData2['DataStatus'] = bTime.GetDateString()
            else:
                itemData2['DataStatus'] = "None of price data"

            #print(itemData2)

            itemDataDF = pd.concat([itemDataDF, DataFrame([itemData2])], ignore_index=True, axis=0)

        #print(itemDataDF)
        if not itemDataDF.empty: 
            itemDataDF = itemDataDF.sort_values(by=['Score'], axis=0, ascending=False)
            bExcel.WriteFile(itemDataDF, outFile, outSheet)
            return outFile
반응형