Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public record StockPriceCache(
Long currentPrice,
Double changeRate
Double changeRate,
Long closePrice
) {
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.back.together02be.stock.client;

import com.back.together02be.stock.dto.KisDailyPriceRes;
import com.back.together02be.stock.dto.KisPriceRes;
import com.back.together02be.stock.dto.KisTokenRes;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
Expand Down Expand Up @@ -91,4 +91,30 @@ public KisPriceRes getCurrentPrice(String token, String stockCode) {

return response;
}

public KisDailyPriceRes getDailyPrice(String token, String stockCode) {

String url = restBaseUrl + "/uapi/domestic-stock/v1/quotations/inquire-daily-price"
+ "?fid_cond_mrkt_div_code=J"
+ "&fid_input_iscd=" + stockCode
+ "&fid_org_adj_prc=1"
+ "&fid_period_div_code=D";

KisDailyPriceRes response = restClient.get()
.uri(url)
.headers(headers -> {
headers.setBearerAuth(token);
headers.set("appkey", appKey);
headers.set("appsecret", appSecret);
headers.set("tr_id", "FHKST01010400");
})
.retrieve()
.body(KisDailyPriceRes.class);

if (response == null || response.output() == null) {
throw new IllegalStateException("종가 조회 실패: stockCode=" + stockCode);
}

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//주식 현재가 일자별 API
package com.back.together02be.stock.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public record KisDailyPriceRes(
Output output
) {
public record Output(

//종가
@JsonProperty("stck_clpr")
String closePrice
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.back.together02be.infra.kis.KisWebSocketClient;
import com.back.together02be.stock.cache.StockPriceCache;
import com.back.together02be.stock.client.KisPriceClient;
import com.back.together02be.stock.dto.KisDailyPriceRes;
import com.back.together02be.stock.dto.KisPriceRes;
import com.back.together02be.stock.dto.RealtimeStockPrice;
import com.back.together02be.stock.dto.StockListRes;
Expand Down Expand Up @@ -84,18 +85,26 @@ public void updatePriceCache() {
int index = currentIndex % stocks.size();
Stock stock = stocks.get(index);

try {
KisPriceRes price = kisPriceClient.getCurrentPrice(token, stock.getStockCode());
try {
KisPriceRes price = kisPriceClient.getCurrentPrice(token, stock.getStockCode());

Long currentPrice = Long.parseLong(price.output().currentPrice());
Double changeRate = Double.parseDouble(price.output().changeRate());
Long currentPrice = Long.parseLong(price.output().currentPrice());
Double changeRate = Double.parseDouble(price.output().changeRate());

priceCache.put(stock.getStockCode(),
new StockPriceCache(currentPrice, changeRate));
Long closePrice = null;

} catch (Exception e) {
System.out.println("가격 갱신 실패: " + stock.getStockCode() + " / " + e.getMessage());
}
if (currentPrice == 0) {
KisDailyPriceRes daily = kisPriceClient.getDailyPrice(token, stock.getStockCode());
closePrice = Long.parseLong(daily.output().closePrice());
currentPrice = closePrice;
}

priceCache.put(stock.getStockCode(),
new StockPriceCache(currentPrice, changeRate, closePrice));

} catch (Exception e) {
System.out.println("가격 갱신 실패: " + stock.getStockCode() + " / " + e.getMessage());
}

// 다음 위치로 이동
currentIndex = (currentIndex + 1) % stocks.size();
Expand Down